-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from mindrunner/superagent
feat: Add Custom Fetcher Support to SimpleSDK Constructor
- Loading branch information
Showing
3 changed files
with
63 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { constructSimpleSDK } from '../sdk/simple'; | ||
import { FetcherFunction } from '../types'; | ||
|
||
// mock superagent module without adding a dependency | ||
interface SuperAgentStatic | ||
extends Promise<{ | ||
body: any; | ||
ok: boolean; | ||
status: number; | ||
statusCode: number; | ||
error: false | { status: number; textx: string }; | ||
}> { | ||
set(headers: Record<string, string>): this; | ||
get(url: string): this; | ||
post(url: string): this; | ||
send(body: any): this; | ||
} | ||
// pretend we do `import superagent from "superagent" | ||
declare const superagent: SuperAgentStatic; | ||
|
||
export const superagentFetcher = (): FetcherFunction => | ||
async function (options) { | ||
if (options.method.toLowerCase() === 'post' && 'data' in options) { | ||
const response = await superagent | ||
.post(options.url) | ||
.set(options?.headers || {}) | ||
.send(options.data); | ||
|
||
return response.body; | ||
} else { | ||
const response = await superagent | ||
.get(options.url) | ||
.set(options?.headers || {}); | ||
|
||
return response.body; | ||
} | ||
|
||
// Add error handling with FetcherError | ||
// handle options.signal | ||
}; | ||
|
||
const fetcher: FetcherFunction = superagentFetcher(); | ||
const sdk = constructSimpleSDK({ fetcher, chainId: 1 }); | ||
Check warning on line 43 in src/examples/customFetcher.ts GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters