From 82805e46cb0b2e2c9b93e74c21db62dccdbde595 Mon Sep 17 00:00:00 2001 From: Kelly Andrews Date: Mon, 7 Mar 2022 14:50:22 -0500 Subject: [PATCH] feat(numbers): add numbers module) --- packages/numbers/lib/index.ts | 20 ++++++++++++++++- packages/numbers/lib/numbers.ts | 40 +++++++++++++++------------------ packages/numbers/lib/types.ts | 40 ++++++++++----------------------- 3 files changed, 49 insertions(+), 51 deletions(-) diff --git a/packages/numbers/lib/index.ts b/packages/numbers/lib/index.ts index 4f141595..6c334f64 100644 --- a/packages/numbers/lib/index.ts +++ b/packages/numbers/lib/index.ts @@ -1 +1,19 @@ -export { Numbers } from './numbers'; \ No newline at end of file +import { Numbers } from './numbers'; + +export { + NumbersClassParameters, + NumbersResponse, + NumbersAvailableList, + NumbersOwnedList, + NumbersEmptyResponse, + NumbersParams, + NumbersAvailableNumber, + Country, + NumbersUpdateParams, + VoiceCallbackTypeEnum, + MessagesCallbackTypeEnum, + NumbersOwnedNumber, + NumbersOwnedFilter +} from './types' + +export { Numbers }; \ No newline at end of file diff --git a/packages/numbers/lib/numbers.ts b/packages/numbers/lib/numbers.ts index b3a222cf..dca189e7 100644 --- a/packages/numbers/lib/numbers.ts +++ b/packages/numbers/lib/numbers.ts @@ -1,28 +1,23 @@ import { Auth, AuthInterface } from '@vonage/auth'; -import { request, VetchResponse } from "@vonage/vetch"; +import { request, ResponseTypes } from "@vonage/vetch"; import { - NumbersError, - NumbersAvailableListResponse, + NumbersResponse, + NumbersAvailableList, NumbersOwnedFilter, - NumbersOwnedListResponse, - NumbersOwnedNumberResponse, + NumbersOwnedList, + NumbersOwnedNumber, NumbersEmptyResponse, NumbersUpdateParams, NumbersSearchFilter, NumbersClassParameters, - NumbersParams, - NumbersPromise + NumbersParams } from './types'; -const runRequest = async (options: NumbersClassParameters): NumbersPromise => { - try { - let result = await request(options); - return { type: 'success', ...result } as unknown as T; - } catch (error) { - return { type: 'error', ...error } as NumbersError; - } +const runRequest = async (options: NumbersClassParameters): Promise> => { + let result = await request(options); + return result; } const BASE_URL = "https://rest.nexmo.com".replace(/\/+$/, ""); @@ -33,7 +28,7 @@ export const NumbersParamCreator = function (options?: NumbersClassParameters) { const localVetchOptions = {}; localVetchOptions['url'] = `${options.baseUrl}/number/buy`; localVetchOptions['headers'] = Object.assign({}, options.headers); - localVetchOptions['data'] = Object.assign({}, options.auth, params); + localVetchOptions['data'] = options.auth.getQueryParams(params); localVetchOptions['method'] = 'POST'; return localVetchOptions; }, @@ -41,7 +36,7 @@ export const NumbersParamCreator = function (options?: NumbersClassParameters) { const localVetchOptions = {}; localVetchOptions['url'] = `${options.baseUrl}/number/cancel`; localVetchOptions['headers'] = Object.assign({}, options.headers); - localVetchOptions['data'] = Object.assign({}, options.auth, params); + localVetchOptions['data'] = options.auth.getQueryParams(params); localVetchOptions['method'] = 'POST'; return localVetchOptions; }, @@ -49,21 +44,21 @@ export const NumbersParamCreator = function (options?: NumbersClassParameters) { const localVetchOptions = {}; localVetchOptions['url'] = `${options.baseUrl}/number/search`; localVetchOptions['headers'] = Object.assign({}, options.headers); - localVetchOptions['params'] = Object.assign({}, options.auth.getQueryParams(), filter); + localVetchOptions['params'] = options.auth.getQueryParams(filter); return localVetchOptions; }, getOwnedNumbers(filter?: NumbersOwnedFilter) { const localVetchOptions = {}; localVetchOptions['url'] = `${options.baseUrl}/account/numbers`; localVetchOptions['headers'] = Object.assign({}, options.headers); - localVetchOptions['params'] = Object.assign({}, options.auth.getQueryParams(), filter); + localVetchOptions['params'] = options.auth.getQueryParams(filter); return localVetchOptions; }, updateNumber(params?: NumbersUpdateParams) { const localVetchOptions = {}; localVetchOptions['url'] = `${options.baseUrl}/number/update`; localVetchOptions['headers'] = Object.assign({}, options.headers); - localVetchOptions['data'] = Object.assign({}, options.auth, params); + localVetchOptions['data'] = options.auth.getQueryParams(params); localVetchOptions['method'] = 'POST'; return localVetchOptions; }, @@ -79,6 +74,7 @@ export class BaseAPI { if (opts) { opts['auth'] = new Auth({ apiKey: opts.apiKey, apiSecret: opts.apiSecret, file: opts.file }); opts['baseUrl'] = opts.baseUrl || BASE_URL; + opts['responseType'] = opts.responseType || ResponseTypes.json; this.config = opts; } } @@ -96,15 +92,15 @@ export class Numbers extends BaseAPI { } public getAvailableNumbers(filter?: NumbersSearchFilter) { const localVetchOptions = NumbersParamCreator(this.config).getAvailableNumbers(filter); - return runRequest(localVetchOptions); + return runRequest(localVetchOptions); } public getOwnedNumbers(filter?: NumbersOwnedFilter) { const localVetchOptions = NumbersParamCreator(this.config).getOwnedNumbers(filter); - return runRequest(localVetchOptions); + return runRequest(localVetchOptions); } public updateNumber(params?: NumbersUpdateParams) { const localVetchOptions = NumbersParamCreator(this.config).updateNumber(params); - return runRequest(localVetchOptions); + return runRequest(localVetchOptions); } } \ No newline at end of file diff --git a/packages/numbers/lib/types.ts b/packages/numbers/lib/types.ts index 902f21e5..db4a24a4 100644 --- a/packages/numbers/lib/types.ts +++ b/packages/numbers/lib/types.ts @@ -1,46 +1,30 @@ import { AuthOpts, AuthInterface } from '@vonage/auth' -import { VetchError, VetchResponse, VetchOptions } from '@vonage/vetch'; +import { VetchResponse, VetchOptions } from '@vonage/vetch'; export type NumbersClassParameters = AuthOpts & VetchOptions & { auth?: AuthInterface, } -export type NumbersPromise = Promise -export const NumbersPromise = Promise; +export interface NumbersResponse extends VetchResponse { } -export interface NumbersError extends VetchError { - type: 'error' +export interface NumbersAvailableList { + count?: number; + numbers?: Array; } -export interface NumbersAvailableListResponse extends VetchResponse { - type: 'success' - data: { - count?: number; - numbers?: Array; - } -} -export interface NumbersOwnedListResponse extends VetchResponse { - type: 'success' - data: { - count?: number; - numbers?: Array; - } +export interface NumbersOwnedList { + count?: number; + numbers?: Array; } -export interface NumbersOwnedNumberResponse extends VetchResponse { - type: 'success' - data: NumbersOwnedNumber -} -export interface NumbersEmptyResponse extends VetchResponse { - type: 'success' - data: { - errorCode?: string; - errorCodeLabel?: string; - } +export interface NumbersEmptyResponse { + errorCode?: string; + errorCodeLabel?: string; } + export interface NumbersParams { country: Country; msisdn: string;