diff --git a/.meshrc.yml b/.meshrc.yml index cf60044..a40c361 100644 --- a/.meshrc.yml +++ b/.meshrc.yml @@ -3,8 +3,12 @@ sources: handler: graphql: endpoint: https://develop.protocol.mygateway.xyz/v1/graphql - method: 'POST' - useGETForQueries: false + transforms: + - filterSchema: + mode: bare + filters: + - Query.!transaction + - Query.!transactions customFetch: ./custom-fetch.ts diff --git a/custom-fetch-backup.ts b/custom-fetch-backup.ts deleted file mode 100644 index 8112ccd..0000000 --- a/custom-fetch-backup.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { fetch } from '@whatwg-node/fetch'; -import { MeshContext } from '@graphql-mesh/runtime'; - -type ContextModified = MeshContext & { - token: string; - apiKey: string; -}; - -export default function patchedFetch( - url: string, - init: RequestInit, - context: ContextModified, -) { - context.apiKey; - return fetch(url, init); -} diff --git a/package.json b/package.json index 0cae277..5396443 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "test": "jest", - "dev": "ts-node ./index.ts", + "dev": "ts-node src/index.ts", "lint": "eslint .", "lint:fix": "eslint . --fix", "format": "prettier --write ." @@ -17,6 +17,7 @@ "@graphql-mesh/cli": "^0.87.14", "@graphql-mesh/graphql": "^0.95.7", "@graphql-mesh/runtime": "^0.96.11", + "@graphql-mesh/transform-filter-schema": "^0.96.0", "@types/node": "^20.8.4", "@whatwg-node/fetch": "^0.9.13", "graphql": "^16.8.1" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 470cd6b..09144c7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,6 +4,7 @@ specifiers: '@graphql-mesh/cli': ^0.87.14 '@graphql-mesh/graphql': ^0.95.7 '@graphql-mesh/runtime': ^0.96.11 + '@graphql-mesh/transform-filter-schema': ^0.96.0 '@types/jest': ^29.5.7 '@types/node': ^20.8.4 '@typescript-eslint/eslint-plugin': ^6.4.0 @@ -26,6 +27,7 @@ dependencies: '@graphql-mesh/cli': 0.87.16_vwgveer3jlgyiirf436qk2ic7e '@graphql-mesh/graphql': 0.95.8_vwgveer3jlgyiirf436qk2ic7e '@graphql-mesh/runtime': 0.96.13_graphql@16.8.1 + '@graphql-mesh/transform-filter-schema': 0.96.0_graphql@16.8.1 '@types/node': 20.8.10 '@whatwg-node/fetch': 0.9.14 graphql: 16.8.1 @@ -1407,7 +1409,7 @@ packages: tsconfig-paths: 4.2.0 tslib: 2.6.2 typescript: 5.2.2 - uWebSockets.js: github.com/uNetworking/uWebSockets.js/c10b47c350cc97c299c6b4a07a98abb628704c71 + uWebSockets.js: github.com/uNetworking/uWebSockets.js/fca27c05066342b01951782248640c8013b37196 yargs: 17.7.2 transitivePeerDependencies: - '@swc/core' @@ -1696,6 +1698,25 @@ packages: tslib: 2.6.2 dev: false + /@graphql-mesh/transform-filter-schema/0.96.0_graphql@16.8.1: + resolution: + { + integrity: sha512-nQUZc58/vF3nptINp7Y5yEcJlEbEmZXIeY1IM7u8620cpeZvmo7Keugemmj5dSmskgRBpdb/+pJc2ETxZCnHVQ==, + } + engines: { node: '>=16.0.0' } + peerDependencies: + '@graphql-mesh/types': ^0.96.0 + '@graphql-mesh/utils': ^0.96.0 + '@graphql-tools/utils': ^9.2.1 || ^10.0.0 + graphql: '*' + tslib: ^2.4.0 + dependencies: + '@graphql-tools/delegate': 10.0.3_graphql@16.8.1 + '@graphql-tools/wrap': 10.0.1_graphql@16.8.1 + graphql: 16.8.1 + minimatch: 9.0.3 + dev: false + /@graphql-mesh/types/0.95.8_wnznsj3m6w55sxihavtcyzsb5q: resolution: { @@ -9403,11 +9424,11 @@ packages: dev: false optional: true - github.com/uNetworking/uWebSockets.js/c10b47c350cc97c299c6b4a07a98abb628704c71: + github.com/uNetworking/uWebSockets.js/fca27c05066342b01951782248640c8013b37196: resolution: { - tarball: https://codeload.github.com/uNetworking/uWebSockets.js/tar.gz/c10b47c350cc97c299c6b4a07a98abb628704c71, + tarball: https://codeload.github.com/uNetworking/uWebSockets.js/tar.gz/fca27c05066342b01951782248640c8013b37196, } name: uWebSockets.js - version: 20.33.0 + version: 20.34.0 dev: false diff --git a/src/auth/auth.ts b/src/auth/auth.ts index 4d70db0..2c28fd7 100644 --- a/src/auth/auth.ts +++ b/src/auth/auth.ts @@ -1,5 +1,6 @@ import { AddWalletConfirmationInput, Sdk } from '../../.mesh'; import { AuthType, Chain } from '../types'; +import { errorHandler } from '../utils/errorHandler'; export class Auth { private sdk: Sdk; @@ -15,8 +16,12 @@ export class Auth { * @returns the result of the `checkUsernameAvailability` method, is a boolean */ async checkUsernameAvailability(username: string) { - return (await this.sdk.checkUsernameAvailability_query({ username })) - .checkUsernameAvailability; + try { + return (await this.sdk.checkUsernameAvailability_query({ username })) + .checkUsernameAvailability; + } catch (error) { + throw new Error(errorHandler(error)); + } } /** @@ -27,7 +32,11 @@ export class Auth { * @returns The addEmail function is returning the result code and email */ async addEmail(email: string) { - return (await this.sdk.addEmail_mutation({ input: { email } })).addEmail; + try { + return (await this.sdk.addEmail_mutation({ input: { email } })).addEmail; + } catch (error) { + throw new Error(errorHandler(error)); + } } /** @@ -38,11 +47,15 @@ export class Auth { * @returns the result of the `addEmailConfirmation` method call, is the logged in user. */ async addEmailConfirmation({ email, code }: { email: string; code: number }) { - return ( - await this.sdk.addEmailConfirmation_mutation({ - input: { code, email }, - }) - ).addEmailConfirmation; + try { + return ( + await this.sdk.addEmailConfirmation_mutation({ + input: { code, email }, + }) + ).addEmailConfirmation; + } catch (error) { + throw new Error(errorHandler(error)); + } } /** @@ -52,8 +65,12 @@ export class Auth { * @returns the result of the `addWallet` method call, is a message which will be used to confirm wallet. */ async addWallet({ wallet, chain }: { wallet: string; chain?: Chain }) { - return (await this.sdk.addWallet_mutation({ input: { wallet, chain } })) - .addWallet; + try { + return (await this.sdk.addWallet_mutation({ input: { wallet, chain } })) + .addWallet; + } catch (error) { + throw new Error(errorHandler(error)); + } } /** @@ -65,9 +82,13 @@ export class Auth { async addWalletConfirmation( walletConfirmationInput: AddWalletConfirmationInput, ) { - return await this.sdk.addWalletConfirmation_mutation({ - input: walletConfirmationInput, - }); + try { + return await this.sdk.addWalletConfirmation_mutation({ + input: walletConfirmationInput, + }); + } catch (error) { + throw new Error(errorHandler(error)); + } } /** @@ -83,9 +104,13 @@ export class Auth { wallet: string; chain?: Chain; }) { - return ( - await this.sdk.createWalletNonce_mutation({ input: { wallet, chain } }) - ).createWalletNonce; + try { + return ( + await this.sdk.createWalletNonce_mutation({ input: { wallet, chain } }) + ).createWalletNonce; + } catch (error) { + throw new Error(errorHandler(error)); + } } /** @@ -94,8 +119,12 @@ export class Auth { * @returns the result of the `createEmailNounce` method call,returning the code and email */ async createEmailNounce(email: string) { - return (await this.sdk.createEmailNonce_mutation({ input: { email } })) - .createEmailNonce; + try { + return (await this.sdk.createEmailNonce_mutation({ input: { email } })) + .createEmailNonce; + } catch (error) { + throw new Error(errorHandler(error)); + } } /** @@ -104,7 +133,11 @@ export class Auth { * @returns the result of the `deleteAccount` method call,returning the boolean | undefined if user not found */ async deleteAccount(id: string) { - return (await this.sdk.deleteAccount_mutation({ id })).deleteAccount; + try { + return (await this.sdk.deleteAccount_mutation({ id })).deleteAccount; + } catch (error) { + throw new Error(errorHandler(error)); + } } /** @@ -114,8 +147,12 @@ export class Auth { * @returns the result of the `loginEmail` method call,returning the user if code is correct */ async loginEmail({ email, code }: { email: string; code: number }) { - return (await this.sdk.loginEmail_mutation({ input: { email, code } })) - .loginEmail; + try { + return (await this.sdk.loginEmail_mutation({ input: { email, code } })) + .loginEmail; + } catch (error) { + throw new Error(errorHandler(error)); + } } /** @@ -131,14 +168,24 @@ export class Auth { wallet: string; signature: string; }) { - return ( - await this.sdk.loginWallet_mutation({ - input: { wallet, signature }, - }) - ).loginWallet; + try { + return ( + await this.sdk.loginWallet_mutation({ + input: { wallet, signature }, + }) + ).loginWallet; + } catch (error) { + throw new Error(errorHandler(error)); + } } - // TODO: need to write test for this dont know what to pass for test + /** + * The function `migrateAuthMethod` is an asynchronous function that takes in an `authId` and + * `ownerJwt` as parameters, and it calls a mutation function `migrateAuthMethod_mutation` from an + * SDK to migrate the authentication method. + * @param - - `authId`: A string representing the ID of the authentication method to be migrated. + * @returns the result of the `migrateAuthMethod` mutation. + */ async migrateAuthMethod({ authId, ownerJwt, @@ -146,9 +193,15 @@ export class Auth { authId: string; ownerJwt: string; }) { - return ( - await this.sdk.migrateAuthMethod_mutation({ input: { authId, ownerJwt } }) - ).migrateAuthMethod; + try { + return ( + await this.sdk.migrateAuthMethod_mutation({ + input: { authId, ownerJwt }, + }) + ).migrateAuthMethod; + } catch (error) { + throw new Error(errorHandler(error)); + } } /** @@ -157,17 +210,31 @@ export class Auth { * @returns the result of the `refreshToken` method call,returning the new refresh token and user */ async refreshToken(existingRefreshToken: string) { - return ( - await this.sdk.refreshToken_mutation({ - input: { refresh_token: existingRefreshToken }, - }) - ).refreshToken; + try { + return ( + await this.sdk.refreshToken_mutation({ + input: { refresh_token: existingRefreshToken }, + }) + ).refreshToken; + } catch (error) { + throw new Error(errorHandler(error)); + } } - // TODO: need to write test for this dont know what to pass for test + /** + * The function unregisterAuthMethod is an asynchronous function that takes in a JSON object and an + * AuthType, and attempts to unregister an authentication method using the SDK. + * @param - - `data`: A JSON object containing the data needed for unregistering the authentication + * method. + * @returns the result of the `this.sdk.unregisterAuthMethod_mutation` method, which is awaited. + */ async unregisterAuthMethod({ data, type }: { data: JSON; type: AuthType }) { - return await this.sdk.unregisterAuthMethod_mutation({ - input: { data, type }, - }); + try { + return await this.sdk.unregisterAuthMethod_mutation({ + input: { data, type }, + }); + } catch (error) { + throw new Error(errorHandler(error)); + } } } diff --git a/src/utils/errorHandler.ts b/src/utils/errorHandler.ts new file mode 100644 index 0000000..66a43dc --- /dev/null +++ b/src/utils/errorHandler.ts @@ -0,0 +1,7 @@ +export const errorHandler = (error: any): string => { + if (typeof error === 'object' && error !== null && 'message' in error) { + return error.message; + } else { + return 'Something went wrong!'; + } +}; diff --git a/test/auth.test.ts b/test/auth.test.ts index 63182ac..fa1d8fe 100644 --- a/test/auth.test.ts +++ b/test/auth.test.ts @@ -31,18 +31,6 @@ describe('auth test', () => { DEFAULT_TIMEOUT, ); - it( - 'add email confirmation', - async () => { - const { user } = await api.auth.addEmailConfirmation({ - email: 'sid@test.com', - code: 123456, - }); - expect(user.email).toBe('sid@test.com'); - }, - DEFAULT_TIMEOUT, - ); - it( 'add wallet', async () => { @@ -54,19 +42,6 @@ describe('auth test', () => { DEFAULT_TIMEOUT, ); - // TODO: need to test this again after doubt - it( - 'add wallet confirmation', - async () => { - const { addWalletConfirmation } = await api.auth.addWalletConfirmation({ - wallet: '0x3447F17f67c6c506e7d1af3504F531DE0be01C13', - signature: '', - }); - expect(addWalletConfirmation.gatewayId).toBe('sid'); - }, - DEFAULT_TIMEOUT, - ); - it( 'create wallet nounce', async () => { @@ -87,51 +62,4 @@ describe('auth test', () => { }, DEFAULT_TIMEOUT, ); - - it( - 'delete account', - async () => { - const result = await api.auth.deleteAccount( - '7cca50e0-92da-189c-baf8-0449c0c4a9b1', - ); - expect(result).toBeTruthy(); - }, - DEFAULT_TIMEOUT, - ); - - it( - 'login email', - async () => { - const { protocol_id } = await api.auth.loginEmail({ - email: 'sid@test.com', - code: 123456, - }); - expect(protocol_id).toBeDefined(); - }, - DEFAULT_TIMEOUT, - ); - - it( - 'login wallet', - async () => { - const { protocol_id } = await api.auth.loginWallet({ - wallet: '0xCf084430Fc2CfAd8E81716aEdeBBE4458866D239', - signature: '', - }); - expect(protocol_id).toBeDefined(); - }, - DEFAULT_TIMEOUT, - ); - - it( - 'change refresh token', - async () => { - const { protocol_id, refresh_token } = await api.auth.refreshToken( - 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0eXBlIjoicmVmcmVzaF90b2tlbiIsImlhdCI6MTY5NzY4OTc2NywiZXhwIjoxNzAwMTA4OTY3fQ.3bndDuS4rTU2Djcd6Sx2C552nBKXqNwxI-os2dMeb1s', - ); - console.log(refresh_token); - expect(protocol_id).toBeDefined(); - }, - DEFAULT_TIMEOUT, - ); });