@@ -10,9 +10,12 @@ export type ReturnErr = { ok: true } | { ok: false; msg: string }
1010
1111const tryFetch =
1212 < T > ( transform : ( res : Response ) => Promise < T > ) =>
13- async ( url : string ) : Promise < ReturnValue < T > > => {
13+ async (
14+ f : typeof globalThis . fetch ,
15+ url : string
16+ ) : Promise < ReturnValue < T > > => {
1417 try {
15- const res = await fetch ( url )
18+ const res = await f ( url )
1619 if ( ! res . ok ) return { ok : false }
1720 return { ok : true , value : await transform ( res ) }
1821 } catch {
@@ -23,9 +26,10 @@ const tryFetch =
2326const tryFetchValue = tryFetch ( async res => + ( await res . text ( ) ) )
2427const tryFetchJson = < T > ( ) => tryFetch ( async res => ( await res . json ( ) ) as T ) // type assertion much?¿
2528
26- export const getStipend = ( ) => tryFetchValue ( `${ economyUrl } /currentStipend` )
27- export const getBalance = ( user : string ) =>
28- tryFetchValue ( `${ economyUrl } /balance/${ user } ` )
29+ export const getStipend = ( f : typeof globalThis . fetch ) =>
30+ tryFetchValue ( f , `${ economyUrl } /currentStipend` )
31+ export const getBalance = ( f : typeof globalThis . fetch , user : string ) =>
32+ tryFetchValue ( f , `${ economyUrl } /balance/${ user } ` )
2933
3034type BaseTx = {
3135 Note : string
@@ -58,21 +62,20 @@ type TxTypes =
5862
5963type ReceivedTx = BaseTx & TxTypes
6064
61- export const getTransactions = ( user : string ) =>
62- tryFetchJson < ReceivedTx [ ] > ( ) ( `${ economyUrl } /transactions/${ user } ` )
63- export const getAdminTransactions = ( ) =>
64- tryFetchJson < ReceivedTx [ ] > ( ) ( `${ economyUrl } /transactions` )
65+ export const getTransactions = ( f : typeof globalThis . fetch , user : string ) =>
66+ tryFetchJson < ReceivedTx [ ] > ( ) ( f , `${ economyUrl } /transactions/${ user } ` )
67+ export const getAdminTransactions = ( f : typeof globalThis . fetch ) =>
68+ tryFetchJson < ReceivedTx [ ] > ( ) ( f , `${ economyUrl } /transactions` )
6569
6670// doin nothing with returns rn
67- export async function stipend ( To : string ) {
71+ export async function stipend ( f : typeof globalThis . fetch , To : string ) {
6872 try {
69- await fetch ( `${ economyUrl } /stipend/${ To } ` , {
70- method : "post" ,
71- } )
73+ await f ( `${ economyUrl } /stipend/${ To } ` , { method : "post" } )
7274 } catch { }
7375}
7476
7577export async function transact (
78+ f : typeof globalThis . fetch ,
7679 From : string ,
7780 To : string ,
7881 Amount : number ,
@@ -81,7 +84,7 @@ export async function transact(
8184 Returns : { [ _ : number ] : number }
8285) : Promise < ReturnErr > {
8386 try {
84- const res = await fetch ( `${ economyUrl } /transact` , {
87+ const res = await f ( `${ economyUrl } /transact` , {
8588 method : "post" ,
8689 body : JSON . stringify ( { From, To, Amount, Note, Link, Returns } ) ,
8790 } )
@@ -94,6 +97,7 @@ export async function transact(
9497}
9598
9699async function burn (
100+ f : typeof globalThis . fetch ,
97101 From : string ,
98102 Amount : number ,
99103 Note : string ,
@@ -106,7 +110,7 @@ async function burn(
106110 // When my campaign turned to a cam-pain
107111 // We got $2.5 trillion stored on the blockchain
108112 // Uh, ₿urn ₿aby ₿urn
109- const res = await fetch ( `${ economyUrl } /burn` , {
113+ const res = await f ( `${ economyUrl } /burn` , {
110114 method : "post" ,
111115 body : JSON . stringify ( { From, Amount, Note, Link, Returns } ) ,
112116 } )
@@ -127,13 +131,15 @@ export const getGroupPrice = () => getFeeBasedPrice(50)
127131// export const getPlacePrice = () => getFeeBasedPrice(50)
128132
129133export async function createAsset (
134+ f : typeof globalThis . fetch ,
130135 To : string ,
131136 id : number ,
132137 name : string ,
133138 slug : string
134139) : Promise < ReturnErr > {
135140 const price = getAssetPrice ( )
136141 return await burn (
142+ f ,
137143 To ,
138144 price ,
139145 `Created asset ${ name } ` ,
@@ -143,13 +149,15 @@ export async function createAsset(
143149}
144150
145151// export async function createPlace(
152+ // f: typeof globalThis.fetch,
146153// To: string,
147154// id: number,
148155// name: string,
149156// slug: string
150157// ): Promise<ReturnErr> {
151158// const price = getPlacePrice()
152159// return await burn(
160+ // fee,
153161// To,
154162// price,
155163// `Created place ${name}`,
@@ -159,11 +167,19 @@ export async function createAsset(
159167// }
160168
161169export async function createGroup (
170+ f : typeof globalThis . fetch ,
162171 To : string ,
163172 name : string
164173) : Promise < ReturnErr > {
165174 const price = getGroupPrice ( )
166- return await burn ( To , price , `Created group ${ name } ` , `/groups/${ name } ` , { } )
175+ return await burn (
176+ f ,
177+ To ,
178+ price ,
179+ `Created group ${ name } ` ,
180+ `/groups/${ name } ` ,
181+ { }
182+ )
167183}
168184
169185export async function transformTransactions ( list : ReceivedTx [ ] ) {
0 commit comments