Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sombochea committed Nov 22, 2023
1 parent 2cd6797 commit 43577f4
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 53 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ A simple way to send and verify verification code with SMS, Email, Telegram and
- [x] Send verify code
- [x] Verify a code

## Supported Providers

- [x] Telegram / VerifyBot (`telegram` or `verifybot`)
- [x] SMS (Twilio, `sms`)
- [x] Email (`email`)

## Usages

- Create a verify message and send it to the target
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cubetiq/verifyme",
"version": "0.0.3",
"version": "0.0.4",
"description": "A simple way to send and verify verification code with SMS, Email, Telegram and etc.",
"main": "dist/index.js",
"private": false,
Expand Down
37 changes: 19 additions & 18 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ class CreateVerifyRequestBuilder {
private _timeout?: number;
private _template?: string;

constructor() { }
constructor() {}

provider(provider: Provider | ProviderType | string | undefined): CreateVerifyRequestBuilder {
provider(
provider: Provider | ProviderType | string | undefined
): CreateVerifyRequestBuilder {
this._provider = provider;
return this;
}
Expand Down Expand Up @@ -74,32 +76,30 @@ class CreateVerifyRequestBuilder {
}

class CreateVerifyResponse {
token: string;
exp: number;
token?: string;
exp?: number;
error?: string;

constructor({
token,
exp,
error,
}: {
token: string;
exp: number;
token?: string;
exp?: number;
error?: string;
}) {
this.token = token;
this.exp = exp;
this.error = error;
}
}

class VerifyMessageRequest {
token: string;
code: string;

constructor({
token,
code,
}: {
token: string;
code: string;
}) {
constructor({ token, code }: { token: string; code: string }) {
this.token = token;
this.code = code;
}
Expand All @@ -113,7 +113,7 @@ class VerifyMessageRequestBuilder {
private _token?: string;
private _code?: string;

constructor() { }
constructor() {}

token(token: string | undefined): VerifyMessageRequestBuilder {
this._token = token;
Expand Down Expand Up @@ -182,7 +182,7 @@ class VerifymeOptionsBuilder {
private _apiKey?: string;
private _connectionTimeout?: number;

constructor() { }
constructor() {}

url(url: string | undefined): VerifymeOptionsBuilder {
this._url = url;
Expand All @@ -194,7 +194,9 @@ class VerifymeOptionsBuilder {
return this;
}

connectionTimeout(connectionTimeout: number | undefined): VerifymeOptionsBuilder {
connectionTimeout(
connectionTimeout: number | undefined
): VerifymeOptionsBuilder {
this._connectionTimeout = connectionTimeout;
return this;
}
Expand All @@ -208,7 +210,6 @@ class VerifymeOptionsBuilder {
}
}


export {
Provider,
ProviderType,
Expand All @@ -220,4 +221,4 @@ export {
VerifyMessageRequest,
VerifyMessageRequestBuilder,
VerifyMessageResponse,
}
};
89 changes: 59 additions & 30 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ class VerifymeService {
this.url = `${baseUrl}/api/v1/verify`;
}

async send(body: any, headers?: any, timeout?: number): Promise<CreateVerifyResponse> {
async send(
body: any,
headers?: any,
timeout?: number
): Promise<CreateVerifyResponse> {
const config: AxiosRequestConfig = {
method: 'POST',
url: this.url,
Expand All @@ -17,22 +21,36 @@ class VerifymeService {
timeout: timeout ? timeout * 1000 : undefined,
};

const response = await axios(config);

if (
response.status === 200 ||
response.status === 201 ||
response.status === 202
) {
return await response.data;
} else {
throw new Error(
`Failed to post data to verifyme server with status code: ${response.status} and message: ${response.statusText}`
);
}
return axios(config)
.then((response) => response.data)
.catch((error) => {
if (error.response) {
console.debug(
`Send Error: ${JSON.stringify(error.response.data)}`
);

return {
...error.response.data,
};
} else if (error.request) {
console.debug(`Send Error: ${JSON.stringify(error.request)}`);

return {
error: error.request,
};
}

return {
error: error?.message ?? 'Unknown error',
};
});
}

async verify(body: any, headers?: any, timeout?: number): Promise<VerifyMessageResponse> {
async verify(
body: any,
headers?: any,
timeout?: number
): Promise<VerifyMessageResponse> {
const config: AxiosRequestConfig = {
method: 'POST',
url: `${this.url}/check`,
Expand All @@ -41,21 +59,32 @@ class VerifymeService {
timeout: timeout ? timeout * 1000 : undefined,
};

const response = await axios(config);

if (
response.status === 200 ||
response.status === 201 ||
response.status === 202
) {
return await response.data;
} else {
throw new Error(
`Failed to post data to verifyme server with status code: ${response.status} and message: ${response.statusText}`
);
}
return axios(config)
.then((response) => response.data)
.catch((error) => {
if (error.response) {
console.debug(
`Verify Error: ${JSON.stringify(error.response.data)}`
);

return {
...error.response.data,
};
} else if (error.request) {
console.debug(
`Verify Error: ${JSON.stringify(error.request)}`
);

return {
error: error.request,
};
}

return {
error: error?.message ?? 'Unknown error',
};
});
}
}


export { VerifymeService };
export { VerifymeService };
4 changes: 2 additions & 2 deletions src/verifyme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { getSystemHostname, getSystemUsername } from "./util";
export class Verifyme {
private static readonly _logger = console;
private static readonly NAME = 'verifyme';
private static readonly VERSION = '0.0.1';
private static readonly VERSION_CODE = '1';
private static readonly VERSION = '0.0.4';
private static readonly VERSION_CODE = '2';
private static readonly DEFAULT_URL = 'https://verifyme-api.cubetiq.app';
private static readonly API_KEY_HEADER_PREFIX = 'x-api-key';
private static readonly DEFAULT_CONNECT_TIMEOUT = 60; // seconds
Expand Down
22 changes: 22 additions & 0 deletions tests/verifyme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ test('Verifyme sdk should be able to create a verify code and send to the target
expect(response.token).not.toBe('');
})

test('Verifyme sdk should be able to create a verify code and send to the target with error', async () => {
const request = CreateVerifyRequest.builder()
.provider("verifybot")
.target('+85510101010')
.template('Your verification code is {{code}}')
.build();

const response = await sdk.send(request);
console.log("Request: ", request);
console.log("Response: ", response);

expect(request.provider).toBeDefined();
expect(request.provider).not.toBeNull();
expect(request.target).toBeDefined();
expect(request.target).not.toBeNull();

expect(response).toBeDefined();
expect(response.error).toBeDefined();
expect(response.error).not.toBeNull();
expect(response.error).not.toBe('');
})

test('Verifyme sdk should be able to verify with token and code', async () => {
const request = VerifyMessageRequest.builder()
.token('5914b15eca02f53d5c0ae8f04a221c98c0813271346c2092021df56de9aa9fd4')
Expand Down

0 comments on commit 43577f4

Please sign in to comment.