Skip to content

Commit

Permalink
feat(numbers): add numbers module)
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjandrews committed Mar 7, 2022
1 parent 7abbc4a commit 82805e4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 51 deletions.
20 changes: 19 additions & 1 deletion packages/numbers/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
export { Numbers } from './numbers';
import { Numbers } from './numbers';

export {
NumbersClassParameters,
NumbersResponse,
NumbersAvailableList,
NumbersOwnedList,
NumbersEmptyResponse,
NumbersParams,
NumbersAvailableNumber,
Country,
NumbersUpdateParams,
VoiceCallbackTypeEnum,
MessagesCallbackTypeEnum,
NumbersOwnedNumber,
NumbersOwnedFilter
} from './types'

export { Numbers };
40 changes: 18 additions & 22 deletions packages/numbers/lib/numbers.ts
Original file line number Diff line number Diff line change
@@ -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 <T extends VetchResponse>(options: NumbersClassParameters): NumbersPromise<T, NumbersError> => {
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 <T>(options: NumbersClassParameters): Promise<NumbersResponse<T>> => {
let result = await request<T>(options);
return result;
}

const BASE_URL = "https://rest.nexmo.com".replace(/\/+$/, "");
Expand All @@ -33,37 +28,37 @@ 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;
},
cancelNumber(params?: NumbersParams) {
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;
},
getAvailableNumbers(filter?: NumbersSearchFilter) {
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;
},
Expand All @@ -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;
}
}
Expand All @@ -96,15 +92,15 @@ export class Numbers extends BaseAPI {
}
public getAvailableNumbers(filter?: NumbersSearchFilter) {
const localVetchOptions = NumbersParamCreator(this.config).getAvailableNumbers(filter);
return runRequest<NumbersAvailableListResponse>(localVetchOptions);
return runRequest<NumbersAvailableList>(localVetchOptions);
}
public getOwnedNumbers(filter?: NumbersOwnedFilter) {
const localVetchOptions = NumbersParamCreator(this.config).getOwnedNumbers(filter);
return runRequest<NumbersOwnedListResponse>(localVetchOptions);
return runRequest<NumbersOwnedList>(localVetchOptions);

}
public updateNumber(params?: NumbersUpdateParams) {
const localVetchOptions = NumbersParamCreator(this.config).updateNumber(params);
return runRequest<NumbersOwnedNumberResponse>(localVetchOptions);
return runRequest<NumbersOwnedNumber>(localVetchOptions);
}
}
40 changes: 12 additions & 28 deletions packages/numbers/lib/types.ts
Original file line number Diff line number Diff line change
@@ -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<T, E> = Promise<T | E>
export const NumbersPromise = Promise;
export interface NumbersResponse<T> extends VetchResponse<T> { }

export interface NumbersError extends VetchError {
type: 'error'
export interface NumbersAvailableList {
count?: number;
numbers?: Array<NumbersAvailableNumber>;
}

export interface NumbersAvailableListResponse extends VetchResponse {
type: 'success'
data: {
count?: number;
numbers?: Array<NumbersAvailableNumber>;
}
}

export interface NumbersOwnedListResponse extends VetchResponse {
type: 'success'
data: {
count?: number;
numbers?: Array<NumbersOwnedNumber>;
}
export interface NumbersOwnedList {
count?: number;
numbers?: Array<NumbersOwnedNumber>;
}

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;
Expand Down

0 comments on commit 82805e4

Please sign in to comment.