Skip to content

Commit

Permalink
Merge pull request #161 from mindrunner/superagent
Browse files Browse the repository at this point in the history
feat: Add Custom Fetcher Support to SimpleSDK Constructor
  • Loading branch information
Velenir committed Oct 31, 2023
2 parents 1ad8cdb + 0b41bb9 commit e4ee7ac
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
43 changes: 43 additions & 0 deletions src/examples/customFetcher.ts
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

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

'sdk' is assigned a value but never used

Check warning on line 43 in src/examples/customFetcher.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

'sdk' is assigned a value but never used
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,17 @@ import {
constructAxiosFetcher,
constructFetchFetcher,
isFetcherError,
FetcherError,
FetcherErrorInterface,
EthersProviderDeps,
constructToken,
FetcherError,
} from './helpers';
import type {
ConstructFetchInput,
ConstructProviderFetchInput,
Address,
AddressOrSymbol,
FetcherFunction,
Token,
PriceString,
TxHash,
Expand Down Expand Up @@ -189,6 +190,9 @@ export {
isFetcherError,
isAllowance,
constructToken,
// custom fetcher
FetcherError,
FetcherFunction,
};

export type {
Expand Down Expand Up @@ -238,7 +242,6 @@ export type {
TxSendOverrides,
OptimalRate,
OptionalRate,
FetcherError,
FetcherErrorInterface,
};

Expand Down
20 changes: 15 additions & 5 deletions src/sdk/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type {
TransactionContractCallerFn,
TxHash,
Address,
FetcherFunction,
} from '../types';

import type { EthersProviderDeps } from '../helpers';
Expand Down Expand Up @@ -126,16 +127,28 @@ export type FetcherOptions =
| {
axios: AxiosRequirement;
}
| { fetch: typeof fetch };
| { fetch: typeof fetch }
| { fetcher: FetcherFunction };

type SimpleOptions = ConstructBaseInput & FetcherOptions;

export type ProviderOptions = (EthersProviderDeps | { web3: Web3 }) & {
account: Address;
};

const constructFetcher = (options: FetcherOptions): FetcherFunction => {
if ('axios' in options) {
return constructAxiosFetcher(options.axios);
}
if ('fetch' in options) {
return constructFetchFetcher(options.fetch);
}
return options.fetcher;
};

/** @description construct SDK with methods that fetch from API and optionally with blockchain provider calling methods */
export function constructSimpleSDK(options: SimpleOptions): SimpleFetchSDK;

export function constructSimpleSDK(
options: SimpleOptions,
providerOptions: ProviderOptions
Expand All @@ -144,10 +157,7 @@ export function constructSimpleSDK(
options: SimpleOptions,
providerOptions?: ProviderOptions
): SimpleFetchSDK | SimpleSDK {
const fetcher =
'axios' in options
? constructAxiosFetcher(options.axios)
: constructFetchFetcher(options.fetch);
const fetcher = constructFetcher(options);

if (!providerOptions) {
const config: ConstructFetchInput = {
Expand Down

0 comments on commit e4ee7ac

Please sign in to comment.