Skip to content

Commit

Permalink
Improve Compass types and errors (#92)
Browse files Browse the repository at this point in the history
* Use unknown instead of any

* Improve compass error detection and thrown

* Variables make optional as in interface

* Increase version to 0.1.6

* Fix test

* Errors available on CompassRequestError

* yarn install

* Add status 200 to fake compass request
  • Loading branch information
jm42 authored Mar 18, 2024
1 parent 8d78b56 commit 75158e2
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 69 deletions.
6 changes: 3 additions & 3 deletions packages/drops/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/drops",
"version": "0.1.5",
"version": "0.1.6",
"description": "Drops module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down Expand Up @@ -29,7 +29,7 @@
"node": ">=18"
},
"dependencies": {
"@poap-xyz/providers": "0.1.0",
"@poap-xyz/utils": "0.1.0"
"@poap-xyz/providers": "0.1.6",
"@poap-xyz/utils": "0.1.6"
}
}
6 changes: 3 additions & 3 deletions packages/moments/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/moments",
"version": "0.1.2",
"version": "0.1.6",
"description": "Moments module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand All @@ -26,8 +26,8 @@
"build": "rollup -c --bundleConfigAsCjs"
},
"dependencies": {
"@poap-xyz/providers": "0.1.0",
"@poap-xyz/utils": "0.1.0",
"@poap-xyz/providers": "0.1.6",
"@poap-xyz/utils": "0.1.6",
"uuid": "^9.0.0"
},
"engines": {
Expand Down
6 changes: 3 additions & 3 deletions packages/poaps/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/poaps",
"version": "0.1.2",
"version": "0.1.6",
"description": "Poaps module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand All @@ -26,8 +26,8 @@
"build": "rollup -c --bundleConfigAsCjs"
},
"dependencies": {
"@poap-xyz/providers": "0.1.0",
"@poap-xyz/utils": "0.1.0"
"@poap-xyz/providers": "0.1.6",
"@poap-xyz/utils": "0.1.6"
},
"engines": {
"node": ">=18"
Expand Down
4 changes: 2 additions & 2 deletions packages/providers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/providers",
"version": "0.1.2",
"version": "0.1.6",
"description": "Providers module for the poap.js library",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand All @@ -25,7 +25,7 @@
"build": "rollup -c --bundleConfigAsCjs"
},
"dependencies": {
"@poap-xyz/utils": "0.1.0",
"@poap-xyz/utils": "0.1.6",
"axios": "^1.3.5"
},
"devDependencies": {
Expand Down
76 changes: 52 additions & 24 deletions packages/providers/src/core/PoapCompass/PoapCompass.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { CompassProvider } from '../../ports/CompassProvider/CompassProvider';
import { CompassErrors } from '../../ports/CompassProvider/types/CompassErrors';
import { CompassError } from '../../ports/CompassProvider/types/CompassError';
import { CompassRequestError } from '../../ports/CompassProvider/errors/CompassRequestError';

const DEFAULT_COMPASS_BASE_URL = 'https://public.compass.poap.tech/v1/graphql';

Expand Down Expand Up @@ -31,18 +32,18 @@ export class PoapCompass implements CompassProvider {
* @function
* @name PoapCompass#fetchGraphQL
* @param {string} query - The GraphQL query to fetch.
* @param {Record<string, any>} variables - The variables to include with the query.
* @param {Record<string, unknown>} variables - The variables to include with the query.
* @returns {Promise<R>} A Promise that resolves with the result of the query.
* @template R - The type of the result.
*/
private async fetchGraphQL<R = any>(
private async fetchGraphQL<R>(
query: string,
variables: Record<string, any>,
variables: Record<string, unknown>,
): Promise<R> {
const endpoint = this.baseUrl;
let response: Response;

try {
const response = await fetch(endpoint, {
response = await fetch(this.baseUrl, {
method: 'POST',
body: JSON.stringify({
query,
Expand All @@ -53,19 +54,48 @@ export class PoapCompass implements CompassProvider {
'x-api-key': this.apiKey,
},
});
} catch (error: unknown) {
throw new Error(`Network error, received error ${error}`);
}

const json = await response.json();
if (response.status !== 200) {
throw new Error(
`Response error, received status code ${response.status}`,
);
}

if (json.errors) {
throw new Error(
`Error fetching GraphQL data: ${JSON.stringify(json.errors)}`,
);
}
const body = await response.json();

return json;
} catch (error) {
throw new Error(`Network error, received status code ${error}`);
if (this.isError(body)) {
throw new CompassRequestError(body);
}

return body;
}

/**
* Returns true when the given response is a GraphQL error response.
*
* @private
* @function
* @name PoapCompass#isError
* @param {unknown} response - Some response from the GraphQL server.
* @returns {boolean}
*/
private isError(response: unknown): response is CompassErrors {
return (
response != null &&
typeof response === 'object' &&
'errors' in response &&
Array.isArray(response.errors) &&
response.errors.every(
(error: unknown): error is CompassError =>
error != null &&
typeof error === 'object' &&
'message' in error &&
typeof error.message === 'string',
)
);
}

/**
Expand All @@ -75,17 +105,15 @@ export class PoapCompass implements CompassProvider {
* @function
* @name PoapCompass#request
* @param {string} query - The GraphQL query to execute.
* @param {any} variables - The variables to include with the query.
* @param {Record<string, unknown>} [variables] - The variables to include with the query.
* @returns {Promise<T>} A Promise that resolves with the result of the query.
* @template T - The type of the result.
*/
async request<T>(query: string, variables: any): Promise<T> {
try {
const data = await this.fetchGraphQL<T>(query, variables);
return data;
} catch (error) {
throw error;
}
async request<T>(
query: string,
variables?: Record<string, unknown>,
): Promise<T> {
return await this.fetchGraphQL<T>(query, variables ?? {});
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// TODO: Change variable type any to a more specific type
/**
* Provides a request method for executing GraphQL queries.
*
Expand All @@ -12,9 +10,9 @@ export interface CompassProvider {
* @function
* @name CompassProvider#request
* @param {string} query - The query string to execute.
* @param {Record<string, any>} variables - The variables to pass with the query.
* @param {Record<string, unknown>} [variables] - The variables to pass with the query.
* @returns {Promise<T>} A Promise that resolves with the result of the query.
* @template T - The type of the result.
*/
request<T>(query: string, variables: Record<string, any>): Promise<T>;
request<T>(query: string, variables?: Record<string, unknown>): Promise<T>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CompassErrors } from '../types/CompassErrors';
import { CompassError } from '../types/CompassError';

export class CompassRequestError extends Error {
public errors: CompassError[];

constructor(compassErrors: CompassErrors) {
super(
`Error fetching Compass data: ${compassErrors.errors.map((error) => error.message).join(', ')}`,
);
this.errors = compassErrors.errors;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface CompassError {
message: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { CompassError } from './CompassError';

export interface CompassErrors {
errors: CompassError[];
}
7 changes: 4 additions & 3 deletions packages/providers/test/PoapCompass.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PoapCompass } from '../src/core/PoapCompass/PoapCompass';
import { CompassRequestError } from '../src/ports/CompassProvider/errors/CompassRequestError';
import { mock } from 'node:test';

describe('PoapCompass', () => {
Expand All @@ -16,6 +17,7 @@ describe('PoapCompass', () => {
mock.method(global, 'fetch', () => {
return Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve(responseData),
});
});
Expand All @@ -34,15 +36,14 @@ describe('PoapCompass', () => {
mock.method(global, 'fetch', () => {
return Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve(responseData),
});
});

const poapCompass = new PoapCompass(apiKey);

await expect(poapCompass.request(query, variables)).rejects.toThrowError(
/Error fetching GraphQL data/,
);
await expect(poapCompass.request(query, variables)).rejects.toThrow(CompassRequestError);
});

it('should throw a network error when the request fails', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/utils",
"version": "0.1.2",
"version": "0.1.6",
"description": "Utils module for the poap.js library",
"type": "module",
"main": "dist/cjs/index.cjs",
Expand Down
35 changes: 9 additions & 26 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -884,17 +884,17 @@ __metadata:
version: 0.0.0-use.local
resolution: "@poap-xyz/drops@workspace:packages/drops"
dependencies:
"@poap-xyz/providers": 0.1.0
"@poap-xyz/utils": 0.1.0
"@poap-xyz/providers": 0.1.6
"@poap-xyz/utils": 0.1.6
languageName: unknown
linkType: soft

"@poap-xyz/moments@*, @poap-xyz/moments@workspace:packages/moments":
version: 0.0.0-use.local
resolution: "@poap-xyz/moments@workspace:packages/moments"
dependencies:
"@poap-xyz/providers": 0.1.0
"@poap-xyz/utils": 0.1.0
"@poap-xyz/providers": 0.1.6
"@poap-xyz/utils": 0.1.6
"@types/uuid": ^9.0.2
uuid: ^9.0.0
languageName: unknown
Expand All @@ -904,44 +904,27 @@ __metadata:
version: 0.0.0-use.local
resolution: "@poap-xyz/poaps@workspace:packages/poaps"
dependencies:
"@poap-xyz/providers": 0.1.0
"@poap-xyz/utils": 0.1.0
"@poap-xyz/providers": 0.1.6
"@poap-xyz/utils": 0.1.6
languageName: unknown
linkType: soft

"@poap-xyz/providers@*, @poap-xyz/providers@workspace:packages/providers":
"@poap-xyz/providers@*, @poap-xyz/providers@0.1.6, @poap-xyz/providers@workspace:packages/providers":
version: 0.0.0-use.local
resolution: "@poap-xyz/providers@workspace:packages/providers"
dependencies:
"@poap-xyz/utils": 0.1.0
"@poap-xyz/utils": 0.1.6
axios: ^1.3.5
axios-mock-adapter: ^1.21.4
languageName: unknown
linkType: soft

"@poap-xyz/providers@npm:0.1.0":
version: 0.1.0
resolution: "@poap-xyz/providers@npm:0.1.0"
dependencies:
"@poap-xyz/utils": 0.1.0
axios: ^1.3.5
checksum: fe7f03cac15f160f0426d3084e020f2892f14439885ad8fd61dd96faec88dc45b02cedfdb861f742c8bc8f90ca4392facb11ac16ad73e6c2fa669592878071d3
languageName: node
linkType: hard

"@poap-xyz/utils@*, @poap-xyz/utils@workspace:packages/utils":
"@poap-xyz/utils@*, @poap-xyz/[email protected], @poap-xyz/utils@workspace:packages/utils":
version: 0.0.0-use.local
resolution: "@poap-xyz/utils@workspace:packages/utils"
languageName: unknown
linkType: soft

"@poap-xyz/utils@npm:0.1.0":
version: 0.1.0
resolution: "@poap-xyz/utils@npm:0.1.0"
checksum: 3571311ad244f2a358e34752a2fc4d13f5ce2bb5b1facbd6824dbe6bd1b73e9cb94791989e8f22b89dee10877a3d5bca1ee093bbc0a3bb25b76bd5a5e966318a
languageName: node
linkType: hard

"@rollup/plugin-commonjs@npm:^25.0.7":
version: 25.0.7
resolution: "@rollup/plugin-commonjs@npm:25.0.7"
Expand Down

0 comments on commit 75158e2

Please sign in to comment.