Skip to content

Commit

Permalink
feat: enable custom providers headers option
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Oct 27, 2022
1 parent e810c88 commit f2e9b1a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { Block, BlockIdentifier } from './utils';
export type RpcProviderOptions = {
nodeUrl: string;
retries?: number;
headers?: object;
};

export class RpcProvider implements ProviderInterface {
Expand All @@ -46,14 +47,17 @@ export class RpcProvider implements ProviderInterface {
// from interface
public chainId!: StarknetChainId;

public headers: object;

private responseParser = new RPCResponseParser();

private retries: number;

constructor(optionsOrProvider: RpcProviderOptions) {
const { nodeUrl, retries } = optionsOrProvider;
const { nodeUrl, retries, headers } = optionsOrProvider;
this.nodeUrl = nodeUrl;
this.retries = retries || 200;
this.headers = { 'Content-Type': 'application/json', ...headers };

this.getChainId().then((chainId) => {
this.chainId = chainId;
Expand All @@ -64,7 +68,7 @@ export class RpcProvider implements ProviderInterface {
return fetch(this.nodeUrl, {
method: 'POST',
body: stringify({ method, jsonrpc: '2.0', params, id: 0 }),
headers: { 'Content-Type': 'application/json' },
headers: this.headers,
});
}

Expand Down
10 changes: 8 additions & 2 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export type SequencerProviderOptions =
feederGatewayUrl?: string;
gatewayUrl?: string;
chainId?: StarknetChainId;
headers?: object;
};

export class SequencerProvider implements ProviderInterface {
Expand All @@ -73,6 +74,8 @@ export class SequencerProvider implements ProviderInterface {

public chainId: StarknetChainId;

public headers: object | undefined;

private responseParser = new SequencerAPIResponseParser();

constructor(optionsOrProvider: SequencerProviderOptions = { network: 'goerli-alpha' }) {
Expand All @@ -93,6 +96,8 @@ export class SequencerProvider implements ProviderInterface {
this.chainId =
optionsOrProvider.chainId ??
SequencerProvider.getChainIdFromBaseUrl(optionsOrProvider.baseUrl);

this.headers = optionsOrProvider?.headers;
}
}

Expand Down Expand Up @@ -153,13 +158,14 @@ export class SequencerProvider implements ProviderInterface {
return `?${queryString}`;
}

private getHeaders(method: 'POST' | 'GET'): Record<string, string> | undefined {
private getHeaders(method: 'POST' | 'GET'): object | undefined {
if (method === 'POST') {
return {
'Content-Type': 'application/json',
...this.headers,
};
}
return undefined;
return this.headers;
}

// typesafe fetch
Expand Down

0 comments on commit f2e9b1a

Please sign in to comment.