Skip to content

Commit

Permalink
feat: Allow passing in fetch options to compass requests (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
reobin authored Oct 16, 2024
1 parent 916dae9 commit a219dc4
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 19 deletions.
5 changes: 5 additions & 0 deletions docs/pages/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@

- Packages do not export input types anymore.
- Drop properties are not exported anymore.

## 0.6.0

- `createMoment` does not upload media beforehand anymore. Use `createMomentAndUploadMedia` instead.
- For compass requests, the fetch signal is not a parameter of the `PoapCompass.request` method anymore, it must be given through the new `options` parameter: `compass.request(url, { variables }, { signal })`.
2 changes: 1 addition & 1 deletion packages/drops/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"node": ">=18"
},
"dependencies": {
"@poap-xyz/providers": "0.5.5",
"@poap-xyz/providers": "0.6.0",
"@poap-xyz/utils": "0.5.5"
}
}
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.6.0",
"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 }[]>;
}
15 changes: 13 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@poap-xyz/drops@workspace:packages/drops"
dependencies:
"@poap-xyz/providers": 0.5.5
"@poap-xyz/providers": 0.6.0
"@poap-xyz/utils": 0.5.5
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -917,7 +917,7 @@ __metadata:
languageName: unknown
linkType: soft

"@poap-xyz/providers@0.5.5, @poap-xyz/providers@workspace:packages/providers":
"@poap-xyz/providers@0.6.0, @poap-xyz/providers@workspace:packages/providers":
version: 0.0.0-use.local
resolution: "@poap-xyz/providers@workspace:packages/providers"
dependencies:
Expand All @@ -929,6 +929,17 @@ __metadata:
languageName: unknown
linkType: soft

"@poap-xyz/providers@npm:0.5.5":
version: 0.5.5
resolution: "@poap-xyz/providers@npm:0.5.5"
dependencies:
"@poap-xyz/utils": 0.5.5
axios: ^1.6.8
lodash.chunk: ^4.2.0
checksum: 7648e7cbcd68610591a9a5a2123271a80791224ba9fb5d1c4fc86318514c863e9888450183cfc35aa2a45f6fc09a9f9e87299771f78fda807e9e0062a1bb4fd1
languageName: node
linkType: hard

"@poap-xyz/[email protected], @poap-xyz/utils@workspace:packages/utils":
version: 0.0.0-use.local
resolution: "@poap-xyz/utils@workspace:packages/utils"
Expand Down

0 comments on commit a219dc4

Please sign in to comment.