Skip to content

Commit

Permalink
feat: add support for custom fetcher on Provider (FuelLabs#767)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhaiwat10 authored Feb 20, 2023
1 parent ab42ddd commit 361fa1e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-experts-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/providers": minor
---

`Provider` can now accept options for additional network-related configurations.
38 changes: 36 additions & 2 deletions packages/providers/src/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import { bn } from '@fuel-ts/math';
import type { Receipt } from '@fuel-ts/transactions';
import { ReceiptType, TransactionType } from '@fuel-ts/transactions';
import * as GraphQL from 'graphql-request';
// eslint-disable-next-line import/no-extraneous-dependencies
import fetch, { Response } from 'node-fetch';

import Provider from './provider';

afterEach(() => {
jest.restoreAllMocks();
});

describe('Provider', () => {
it('can getVersion()', async () => {
const provider = new Provider('http://127.0.0.1:4000/graphql');
Expand Down Expand Up @@ -154,7 +160,7 @@ describe('Provider', () => {
expect(minGasPrice).toBeDefined();
});

it('can change the provider url of the curernt instance', async () => {
it('can change the provider url of the current instance', async () => {
const providerUrl1 = 'http://127.0.0.1:4000/graphql';
const providerUrl2 = 'http://127.0.0.1:8080/graphql';
const provider = new Provider(providerUrl1);
Expand All @@ -163,6 +169,34 @@ describe('Provider', () => {
expect(provider.url).toBe(providerUrl1);
provider.connect(providerUrl2);
expect(provider.url).toBe(providerUrl2);
expect(spyGraphQLClient).toBeCalledWith(providerUrl2);
expect(spyGraphQLClient).toBeCalledWith(providerUrl2, undefined);
});

it('can accept a custom fetch function', async () => {
const providerUrl = 'http://127.0.0.1:4000/graphql';

const customFetch = async (
url: string,
options: {
body: string;
headers: { [key: string]: string };
[key: string]: unknown;
}
) => {
const graphqlRequest = JSON.parse(options.body);
const { operationName } = graphqlRequest;
if (operationName === 'getVersion') {
const responseText = JSON.stringify({
data: { nodeInfo: { nodeVersion: '0.30.0' } },
});
const response = new Response(responseText, options);

return response;
}
return fetch(url, options);
};

const provider = new Provider(providerUrl, { fetch: customFetch });
expect(await provider.getVersion()).toEqual('0.30.0');
});
});
22 changes: 18 additions & 4 deletions packages/providers/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@ export type BuildPredicateOptions = {
fundTransaction?: boolean;
} & Pick<TransactionRequestLike, 'gasLimit' | 'gasPrice' | 'maturity'>;

export type FetchRequestOptions = {
method: 'POST';
headers: { [key: string]: string };
body: string;
};

/*
* Provider initialization options
*/
export type ProviderOptions = {
fetch?: (url: string, options: FetchRequestOptions) => Promise<any>;
};

/**
* Provider Call transaction params
*/
Expand All @@ -204,17 +217,18 @@ export default class Provider {

constructor(
/** GraphQL endpoint of the Fuel node */
public url: string
public url: string,
public options: ProviderOptions = {}
) {
this.operations = this.createOperations(url);
this.operations = this.createOperations(url, options);
}

/**
* Create GraphQL client and set operations
*/
private createOperations(url: string) {
private createOperations(url: string, options: ProviderOptions = {}) {
this.url = url;
const gqlClient = new GraphQLClient(url);
const gqlClient = new GraphQLClient(url, options.fetch ? { fetch: options.fetch } : undefined);
return getOperationsSdk(gqlClient);
}

Expand Down

0 comments on commit 361fa1e

Please sign in to comment.