Skip to content

Commit

Permalink
feat: adding network apis
Browse files Browse the repository at this point in the history
  • Loading branch information
manchuck committed Jun 17, 2024
1 parent 159112c commit 47b8d27
Show file tree
Hide file tree
Showing 51 changed files with 1,132 additions and 24 deletions.
19 changes: 17 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,29 @@ const config: Config.InitialOptions = {
testMatch: ['<rootDir>/packages/media/__tests__/**/*.test.ts'],
coveragePathIgnorePatterns: ['node_modules', '__tests__'],
},
{
displayName: 'MEETINGS',
testMatch: ['<rootDir>/packages/meetings/__tests__/**/*.test.ts'],
coveragePathIgnorePatterns: ['node_modules', '__tests__'],
},
{
displayName: 'MESSAGES',
testMatch: ['<rootDir>/packages/messages/__tests__/**/*.test.ts'],
coveragePathIgnorePatterns: ['node_modules', '__tests__'],
},
{
displayName: 'MEETINGS',
testMatch: ['<rootDir>/packages/meetings/__tests__/**/*.test.ts'],
displayName: 'NETWORK CLIENT',
testMatch: ['<rootDir>/packages/network-client/__tests__/**/*.test.ts'],
coveragePathIgnorePatterns: ['node_modules', '__tests__'],
},
{
displayName: 'NETWORK SIM SWAP',
testMatch: ['<rootDir>/packages/network-sim-swap/__tests__/**/*.test.ts'],
coveragePathIgnorePatterns: ['node_modules', '__tests__'],
},
{
displayName: 'NETWORK NUMBER VERIFICATION',
testMatch: ['<rootDir>/packages/network-sim-swap/__tests__/**/*.test.ts'],
coveragePathIgnorePatterns: ['node_modules', '__tests__'],
},
{
Expand Down
11 changes: 11 additions & 0 deletions packages/network-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `network-auth`

> TODO: description
## Usage

```
const networkAuth = require('network-auth');
// TODO: DEMONSTRATE API
```
30 changes: 30 additions & 0 deletions packages/network-client/__tests__/__dataSets__/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { NetworkClient } from '../../lib';
import { SDKTestCase, testPrivateKey } from '../../../../testHelpers';
import { SIMSwap } from '../../../network-sim-swap/lib';

export default [
{
label: 'retrieve all conversations',
client: new SIMSwap(
{
privateKey: testPrivateKey,
applicationId: 'my-application',
msisdn: '447700900000',
},
) as unknown as NetworkClient,
requests: [
[`/v1/conversations`, 'GET'],
],
responses: [
[
200,
{},
],
],
clientMethod: 'foo',
parameters: [],
generator: false,
error: false,
expected: [],
} as SDKTestCase,
]
9 changes: 9 additions & 0 deletions packages/network-client/__tests__/__dataSets__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import getTests from './get';

export default [
{
name: 'GET tests',
tests: getTests,
},
];

10 changes: 10 additions & 0 deletions packages/network-client/__tests__/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NetworkClient } from '../lib';
import { VetchOptions, VetchResponse } from '@vonage/vetch';

export const BASE_URL = 'https://api.nexmo.com/';

export class TestNetworkClient extends NetworkClient {
async testRequest<T = void>(request: VetchOptions): Promise<VetchResponse<T>> {
return await this.sendRequest<T>(request);
}
}
55 changes: 55 additions & 0 deletions packages/network-client/__tests__/networkClient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { NetworkAuthParameters, NetworkClient } from '../lib';
import nock from 'nock';
import { BASE_URL } from './common';
import testDataSets from './__dataSets__';
import { SDKTestCase, TestTuple, getResults } from '../../../testHelpers';

describe.each<TestTuple>(testDataSets)('$label', ({tests}) => {
let client;
let scope;

beforeEach(function () {
scope = nock(BASE_URL, {
reqheaders: {
authorization: (value) => value.startsWith('Bearer '),
},
}).persist();
});

afterEach(function () {
client = null;
scope = null;
nock.cleanAll();
});

test.each<SDKTestCase>(tests)(
'Can $label using: $clientMethod',
async ({
generator,
requests,
responses,
client,
clientMethod,
parameters,
expected,
}) => {

requests.forEach((request, index) => {
scope.intercept(...request).reply(...responses[index]);
});

const results = await getResults(
generator,
client,
clientMethod,
parameters,
);

expect(results).toEqual(expected);
expect(nock.isDone()).toBeTruthy();

expect(results).toEqual(expected);
expect(nock.isDone()).toBeTruthy();
},
);
});
2 changes: 2 additions & 0 deletions packages/network-client/lib/enums/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './purpose';
export * from './scope';
9 changes: 9 additions & 0 deletions packages/network-client/lib/enums/purpose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Netowrk API purposes for generating the scope
*/
export enum Purpose {
/**
* Purpose for Fraud Prevention and Detection
*/
FRAUD_PREVENTION_AND_DETECTION = 'FraudPreventionAndDetection',
}
14 changes: 14 additions & 0 deletions packages/network-client/lib/enums/scope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Scopes for the API
*/
export enum Scope {
/**
* Check for SIM Swap
*/
CHECK_SIM_SWAP = 'check-sim-swap',

/**
* Number Verification
*/
NUMBER_VERIFICATION_VERIFY_READ = 'number-verification-verify-read',
}
4 changes: 4 additions & 0 deletions packages/network-client/lib/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './invalidScopeError';
export * from './missingScopeError';
export * from './missingPurposeError';
export * from './invalidPurposeError';
8 changes: 8 additions & 0 deletions packages/network-client/lib/errors/invalidPurposeError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Error thrown when purpose is invalid
*/
export class InvalidPurposeError extends Error {
constructor() {
super(`You did not set a purpose for this request. Please set a proper purpose for this request.`);
}
}
8 changes: 8 additions & 0 deletions packages/network-client/lib/errors/invalidScopeError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Error thrown when scope is invalid
*/
export class InvalidScopeError extends Error {
constructor() {
super(`You did not set a scope for this request. Please set a proper scope for this request.`);
}
}
8 changes: 8 additions & 0 deletions packages/network-client/lib/errors/missingPurposeError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Error thrown when purpose is invalid
*/
export class MissingPurposeError extends Error {
constructor() {
super(`You did not set a purpose for this request.`);
}
}
8 changes: 8 additions & 0 deletions packages/network-client/lib/errors/missingScopeError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Error thrown when no scope is set for a request.
*/
export class MissingScopeError extends Error {
constructor() {
super(`You did not set a scope for this request.`);
}
}
4 changes: 4 additions & 0 deletions packages/network-client/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './errors';
export * from './networkClient';
export * from './types';
export * from './enums';
Loading

0 comments on commit 47b8d27

Please sign in to comment.