Skip to content

Commit

Permalink
feat: added error hanlder and fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth9890 committed Nov 10, 2023
1 parent 7ff9d7f commit 87c3c49
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 134 deletions.
8 changes: 6 additions & 2 deletions .meshrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 0 additions & 16 deletions custom-fetch-backup.ts

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 ."
Expand All @@ -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"
Expand Down
29 changes: 25 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 106 additions & 39 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
}
}

/**
Expand All @@ -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));
}
}

/**
Expand All @@ -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));
}
}

/**
Expand All @@ -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));
}
}

/**
Expand All @@ -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));
}
}

/**
Expand All @@ -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));
}
}

/**
Expand All @@ -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));
}
}

/**
Expand All @@ -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));
}
}

/**
Expand All @@ -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));
}
}

/**
Expand All @@ -131,24 +168,40 @@ 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,
}: {
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));
}
}

/**
Expand All @@ -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));
}
}
}
7 changes: 7 additions & 0 deletions src/utils/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -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!';
}
};
Loading

0 comments on commit 87c3c49

Please sign in to comment.