Skip to content

Commit

Permalink
Allow headers to be defined for all requests via FetchProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox committed Aug 13, 2024
1 parent 1fe1cbf commit d07e8df
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/api/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,25 @@ export interface FetchProviderOptions {
* You can use the node-fetch package in Node.js.
*/
fetch?: Fetch
/**
* Headers that will be applied to every request
* */
headers?: Record<string, string>
}

/** Default provider that uses the Fetch API to call a single node. */
export class FetchProvider implements APIProvider {
readonly url: string
readonly fetch: Fetch
readonly headers: Record<string, string> = {}

constructor(url: string, options: FetchProviderOptions = {}) {
url = url.trim()
if (url.endsWith('/')) url = url.slice(0, -1)
this.url = url
if (options.headers) {
this.headers = options.headers
}
if (!options.fetch) {
if (typeof window !== 'undefined' && window.fetch) {
this.fetch = window.fetch.bind(window)
Expand All @@ -59,6 +67,7 @@ export class FetchProvider implements APIProvider {
const url = this.url + args.path
const reqBody = args.params !== undefined ? JSON.stringify(args.params) : undefined
const reqHeaders = {
...this.headers,
...args.headers,
}
const response = await this.fetch(url, {
Expand Down
12 changes: 12 additions & 0 deletions test/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ suite('api v1', function () {
assert.equal(postresponse.status, 200)
})

test('FetchProvider headers', async function () {
const test = new APIClient({
provider: new MockProvider('https://wax.greymass.com', {
reqHeaders: {'X-Test': 'true'},
}),
})
const defaultresponse = await test.provider.call({
path: '/v1/chain/get_info',
})
assert.equal(defaultresponse.status, 200)
})

test('chain get_abi', async function () {
const response = await jungle4.v1.chain.get_abi('eosio.token')
assert.equal(response.account_name, 'eosio.token')
Expand Down
39 changes: 39 additions & 0 deletions test/data/1c78e4778f12a83f7d409ef3b4d046f599124094.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"api": "https://wax.greymass.com",
"headers": {
"access-control-allow-headers": "X-Requested-With,Accept,Content-Type,Origin",
"access-control-allow-methods": "GET, POST, OPTIONS",
"access-control-allow-origin": "*",
"connection": "close",
"content-length": "979",
"content-type": "application/json",
"date": "Tue, 13 Aug 2024 16:01:33 GMT",
"host": "wax.greymass.com",
"server": "nginx",
"x-cached": "EXPIRED"
},
"status": 200,
"json": {
"server_version": "7aa24ce9",
"chain_id": "1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4",
"head_block_num": 324182164,
"last_irreversible_block_num": 324181832,
"last_irreversible_block_id": "13529f480edc6cc69b01b72572bfb614e1e121a14ef536e712e2fa00f9c0c74c",
"head_block_id": "1352a094ee9232526f60b42f0d9819ca25e337a083b8f6d9752940e22afbfd23",
"head_block_time": "2024-08-13T16:01:33.500",
"head_block_producer": "guild.nefty",
"virtual_block_cpu_limit": 230457,
"virtual_block_net_limit": 1048576000,
"block_cpu_limit": 200000,
"block_net_limit": 1048576,
"server_version_string": "v5.0.0wax01",
"fork_db_head_block_num": 324182164,
"fork_db_head_block_id": "1352a094ee9232526f60b42f0d9819ca25e337a083b8f6d9752940e22afbfd23",
"server_full_version_string": "v5.0.0wax01-7aa24ce9d98c744564840e23833bdbf7ec00af33",
"total_cpu_weight": "46438068615642343",
"total_net_weight": "123948965555668578",
"earliest_available_block_num": 324008658,
"last_irreversible_block_time": "2024-08-13T15:58:47.500"
},
"text": "{\"server_version\":\"7aa24ce9\",\"chain_id\":\"1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4\",\"head_block_num\":324182164,\"last_irreversible_block_num\":324181832,\"last_irreversible_block_id\":\"13529f480edc6cc69b01b72572bfb614e1e121a14ef536e712e2fa00f9c0c74c\",\"head_block_id\":\"1352a094ee9232526f60b42f0d9819ca25e337a083b8f6d9752940e22afbfd23\",\"head_block_time\":\"2024-08-13T16:01:33.500\",\"head_block_producer\":\"guild.nefty\",\"virtual_block_cpu_limit\":230457,\"virtual_block_net_limit\":1048576000,\"block_cpu_limit\":200000,\"block_net_limit\":1048576,\"server_version_string\":\"v5.0.0wax01\",\"fork_db_head_block_num\":324182164,\"fork_db_head_block_id\":\"1352a094ee9232526f60b42f0d9819ca25e337a083b8f6d9752940e22afbfd23\",\"server_full_version_string\":\"v5.0.0wax01-7aa24ce9d98c744564840e23833bdbf7ec00af33\",\"total_cpu_weight\":\"46438068615642343\",\"total_net_weight\":\"123948965555668578\",\"earliest_available_block_num\":324008658,\"last_irreversible_block_time\":\"2024-08-13T15:58:47.500\"}"
}
4 changes: 2 additions & 2 deletions test/utils/mock-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const writeFile = promisify(_writeFile)
import {APIMethods, APIProvider, Bytes, Checksum160, FetchProvider} from '$lib'

export class MockProvider implements APIProvider {
recordProvider = new FetchProvider(this.api, {fetch})
recordProvider = new FetchProvider(this.api, {fetch, headers: this.reqHeaders})

constructor(private api: string = 'https://jungle4.greymass.com') {}
constructor(private api: string = 'https://jungle4.greymass.com', private reqHeaders = {}) {}

getFilename(path: string, params?: unknown) {
const digest = Checksum160.hash(
Expand Down

0 comments on commit d07e8df

Please sign in to comment.