Skip to content

Commit

Permalink
feat: Allow passing in fetch options to compass requests
Browse files Browse the repository at this point in the history
* Allows a client to disable cache in next.js requests for example
* Signal can still be given through these options
  • Loading branch information
reobin committed Oct 16, 2024
1 parent 916dae9 commit f7bff2e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/providers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/providers",
"version": "0.5.5",
"version": "0.5.6",
"description": "Providers module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
29 changes: 18 additions & 11 deletions packages/providers/src/core/PoapCompass/PoapCompass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,32 @@ export class PoapCompass implements CompassProvider {
* @name PoapCompass#fetchGraphQL
* @param {string} query - The GraphQL query to fetch.
* @param {{ readonly [variable: string]: unknown }} variables - The variables to include with the query.
* @param {AbortSignal} signal - When given, the request can be aborted with its controller.
* @param {RequestInit} options - Additional options to pass to the fetch call.
* @returns {Promise<R>} A Promise that resolves with the result of the query.
* @template R - The type of the result.
*/
private async fetchGraphQL<R>(
query: string,
variables: { readonly [variable: string]: unknown },
signal?: AbortSignal,
options?: RequestInit,
): Promise<R> {
let response: Response;

const headers: HeadersInit = {
...options?.headers,
'Content-Type': 'application/json',
'x-api-key': this.apiKey,
};

try {
response = await fetch(this.baseUrl, {
signal,
method: 'POST',
body: JSON.stringify({
query,
variables,
}),
headers: {
'Content-Type': 'application/json',
'x-api-key': this.apiKey,
},
...(options || {}),
headers,
});
} catch (error: unknown) {
throw new Error(`Network error, received error ${error}`);
Expand Down Expand Up @@ -174,22 +177,26 @@ export class PoapCompass implements CompassProvider {
async request<D, V = { readonly [variable: string]: unknown }>(
query: string,
variables?: null | undefined | V,
signal?: AbortSignal,
options?: RequestInit,
): Promise<{ data: D }> {
return await this.fetchGraphQL<{ data: D }>(query, variables ?? {}, signal);
return await this.fetchGraphQL<{ data: D }>(
query,
variables ?? {},
options,
);
}

async batch<D, V = { readonly [variable: string]: unknown }>(
query: string,
variables: V[],
signal?: AbortSignal,
options?: RequestInit,
): Promise<{ data: D }[]> {
const results: { data: D }[] = [];
const chunks: V[][] = chunk(variables, this.batchSize);

for (const chunk of chunks) {
const responses = await Promise.all(
chunk.map((variables) => this.request<D, V>(query, variables, signal)),
chunk.map((variables) => this.request<D, V>(query, variables, options)),
);
results.push(...responses);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export interface CompassProvider {
* @name CompassProvider#request
* @param {string} query - The query string to execute.
* @param {null | undefined | { readonly [variable: string]: unknown }} [variables] - The variables to pass with the query.
* @param {AbortSignal} signal - When given, the request can be aborted with its controller.
* @param {RequestInit} [options] - Options to pass to the fetch call.
* @returns {Promise<{ data: D }>} A Promise that resolves with the result of the query.
* @template D - The type of the result's data.
* @template V - The type of the query's variables.
*/
request<D, V = { readonly [variable: string]: unknown }>(
query: string,
variables?: null | undefined | V,
signal?: AbortSignal,
options?: RequestInit,
): Promise<{ data: D }>;

/**
Expand All @@ -31,14 +31,14 @@ export interface CompassProvider {
* @name CompassProvider#batch
* @param {string} query - The query string to execute.
* @param {{ readonly [variable: string]: unknown }[]} variables - The variables to pass with the each query.
* @param {AbortSignal} [signal] - When given, the requests can be aborted with its controller.
* @param {RequestInit} [options] - Options to pass to the fetch call.
* @returns {Promise<{ data: D }[]>} A Promise that resolves with the results of the queries.
* @template D - The type of the result's data.
* @template V - The type of the query's variables.
*/
batch<D, V = { readonly [variable: string]: unknown }>(
query: string,
variables: V[],
signal?: AbortSignal,
options?: RequestInit,
): Promise<{ data: D }[]>;
}

0 comments on commit f7bff2e

Please sign in to comment.