Skip to content

Commit

Permalink
feat(number-insight-v2): added support for number insight v2
Browse files Browse the repository at this point in the history
  • Loading branch information
manchuck committed Sep 27, 2023
1 parent 65d4bcf commit faac742
Show file tree
Hide file tree
Showing 27 changed files with 320 additions and 0 deletions.
7 changes: 7 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ const config: Config.InitialOptions = {
testMatch: ['<rootDir>/packages/meetings/__tests__/**/*.test.ts'],
coveragePathIgnorePatterns: ['node_modules', '__tests__'],
},
{
displayName: 'NUMBER INSIGHT V2',
testMatch: [
'<rootDir>/packages/number-insight-v2/__tests__/**/*.test.ts',
],
coveragePathIgnorePatterns: ['node_modules', '__tests__'],
},
{
displayName: 'NUMBER INSIGHTS',
testMatch: ['<rootDir>/packages/number-insights/__tests__/**/*.test.ts'],
Expand Down
11 changes: 11 additions & 0 deletions packages/number-insight-v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `number-insight-v2`

> TODO: description
## Usage

```
const numberInsightV2 = require('number-insight-v2');
// TODO: DEMONSTRATE API
```
8 changes: 8 additions & 0 deletions packages/number-insight-v2/__tests__/__dataSets__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import postTests from './post';

export default [
{
label: 'Check For Fraud',
tests: postTests,
},
];
69 changes: 69 additions & 0 deletions packages/number-insight-v2/__tests__/__dataSets__/post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {
Status,
FraudCheckResponse,
FraudCheckParameters,
Insight,
FraudCheck,
Label,
RiskRecommendation,
FraudScoreResponse,
} from '../../lib';

const params = {
type: 'phone',
phone: '16127779311',
insights: [Insight.SIM_SWAP, Insight.FRAUD_SCORE],
} as FraudCheckParameters;

const fraudCheck = {
requestId: '00000000-0000-0000-0000-000000000000',
type: 'phone',
phone: {
phone: '16127779311',
carrier: 'Google (Grand Central) - SVR',
type: 'VOIP',
},
fraudScore: {
riskScore: '20',
riskRecommendation: RiskRecommendation.ALLOW,
label: Label.LOW,
status: Status.COMPLETED,
},
simSwap: {
status: Status.COMPLETED,
swapped: false,
},
} as FraudCheck;

const response = {
request_id: '00000000-0000-0000-0000-000000000000',
type: 'phone',
phone: {
phone: '16127779311',
carrier: 'Google (Grand Central) - SVR',
type: 'VOIP',
},
fraud_score: {
risk_score: '20',
risk_recommendation: 'allow',
label: 'low',
status: 'completed',
} as FraudScoreResponse,
sim_swap: {
status: 'completed',
swapped: false,
},
} as FraudCheckResponse;

export default [
{
label: 'check for fraud',
requests: [[`/v2/ni`, 'POST', params]],
responses: [[200, response]],
clientMethod: 'checkForFraud',
parameters: [params],
generator: false,
error: false,
expected: fraudCheck,
},
];
1 change: 1 addition & 0 deletions packages/number-insight-v2/__tests__/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const BASE_URL = 'https://api.nexmo.com/';
48 changes: 48 additions & 0 deletions packages/number-insight-v2/__tests__/numberInsightV2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { NumberInsightV2 } from '../lib/index';
import nock from 'nock';
import { Auth } from '@vonage/auth';
import { BASE_URL } from './common';
import testDataSets from './__dataSets__/index';
import { readFileSync } from 'fs';

const key = readFileSync(`${__dirname}/private.test.key`).toString();

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

beforeEach(function () {
client = new NumberInsightV2(
new Auth({
privateKey: key,
applicationId: 'my-application',
}),
);

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

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

test.each(tests)(
'Can $label using: $clientMethod',
async ({ requests, responses, clientMethod, parameters, expected }) => {
requests.forEach((request, index) => {
scope.intercept(...request).reply(...responses[index]);
});

const result = await client[clientMethod](...parameters);

expect(result).toEqual(expected);
expect(nock.isDone()).toBeTruthy();
},
);
});
28 changes: 28 additions & 0 deletions packages/number-insight-v2/__tests__/private.test.key
Original file line number Diff line number Di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rgU
k5q4t9vcu7cGiOr7L7UkySNYZqRjwvKEJ610COexTThSwl0v3GNLP8r4AMdBaqdK
uO6fVfxxqQKBgFc5ne2Atai9gJe3ltum0382FoRPy+/VYyb/xZA780eVcSXz0N9b
T+0sWKFFLvJKM/1fcil0FLYqfSpjHXcgqoHgbdpcWo5KqArHd+qWctwl0Bqy1IHy
q7vZ7jCNE9O7cTBy2OTSBbW8apm+a4Qlowm9zQXYN624zmueYb5YamHnAoGAZvJA
KHnv/o7AkF/NhpjVARR7SYOSkLY0kl48/zBIVoAK0TvdmrqBhyOcR8E+vIsn9XCw
uuzvzzdjHlDJYDruxcB2bXVDPoGY/sGrf3iSlXreVkNrY2st/o7euwFtvW0K9ElJ
34K5nbgHJClI+QajbKN6RSJnQ2hnhvjWfkBrPXECgYEA4MCEm9EyrguEO51am8va
OjIAiQMkj/iyjsMDL8eH0VM+OMdieHwbkKyajyrB9dFikvWfxiuo3dU1N5vJTzty
LmzkB8M/rKlAYKD8iKA8cRun4tKzRepHT3JPMu0GYTfcP9ovs5F3aEjX+UuWOO7n
doWDENAr/VU1RNCDwFdxYFg=
-----END PRIVATE KEY-----
4 changes: 4 additions & 0 deletions packages/number-insight-v2/lib/enums/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './insight';
export * from './label';
export * from './riskRecommendation';
export * from './status';
4 changes: 4 additions & 0 deletions packages/number-insight-v2/lib/enums/insight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Insight {
FRAUD_SCORE = 'fraud_score',
SIM_SWAP = 'sim_swap',
}
5 changes: 5 additions & 0 deletions packages/number-insight-v2/lib/enums/label.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum Label {
LOW = 'low',
MEDIUM = 'medium',
HIGH = 'high',
}
5 changes: 5 additions & 0 deletions packages/number-insight-v2/lib/enums/riskRecommendation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum RiskRecommendation {
ALLOW = 'allow',
FLAG = 'flag',
BLOCK = 'block',
}
4 changes: 4 additions & 0 deletions packages/number-insight-v2/lib/enums/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Status {
COMPLETED = 'completed',
FAILED = 'failed',
}
3 changes: 3 additions & 0 deletions packages/number-insight-v2/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './enums';
export * from './numberInsightV2';
export * from './types';
15 changes: 15 additions & 0 deletions packages/number-insight-v2/lib/numberInsightV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Client, AuthenticationType } from '@vonage/server-client';
import { FraudCheckParameters, FraudScore, FraudScoreResponse } from './types';

export class NumberInsightV2 extends Client {
protected authType = AuthenticationType.JWT;

async checkForFraud(params: FraudCheckParameters): Promise<FraudScore> {
const resp = await this.sendPostRequest<FraudScoreResponse>(
`${this.config.apiHost}/v2/ni`,
params,
);

return Client.transformers.camelCaseObjectKeys(resp?.data, true);
}
}
11 changes: 11 additions & 0 deletions packages/number-insight-v2/lib/types/fraudCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PhoneInfo } from './phoneInfo';
import { FraudScore } from './fraudScore';
import { SimSwap } from './simSwap';

export type FraudCheck = {
requestId: string;
type: 'phone';
phone: PhoneInfo;
fraudScore?: FraudScore;
simSwap?: SimSwap;
};
8 changes: 8 additions & 0 deletions packages/number-insight-v2/lib/types/fraudScore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RiskRecommendation, Label, Status } from '../enums';

export type FraudScore = {
riskScore: string;
riskRecommendation: RiskRecommendation;
label: Label;
status: Status;
};
7 changes: 7 additions & 0 deletions packages/number-insight-v2/lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './fraudCheck';
export * from './fraudScore';
export * from './parameters';
export * from './phoneInfo';
export * from './requests';
export * from './responses';
export * from './simSwap';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Insight } from '../../enums/';

export type FraudCheckParameters = {
type: 'phone';
phone: string;
insights: Insight[];
};
1 change: 1 addition & 0 deletions packages/number-insight-v2/lib/types/parameters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './fraudParameters';
5 changes: 5 additions & 0 deletions packages/number-insight-v2/lib/types/phoneInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type PhoneInfo = {
phone: string;
carrier?: string;
type?: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Insight } from '../../enums';

export type FraudCheckRequest = {
type: 'phone';
phone: string;
insights: Insight[];
};
1 change: 1 addition & 0 deletions packages/number-insight-v2/lib/types/requests/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './fraudCheckRequest';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { FraudCheck } from '../fraudCheck';
import { FraudScoreResponse } from './fraudScoreResponse';
import { SimSwap } from '../simSwap';

export type FraudCheckResponse = {
request_id: string;
fraud_score: FraudScoreResponse;
sim_swap: SimSwap;
} & Omit<FraudCheck, 'requestId' | 'fraudScore' | 'simSwap'>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { RiskRecommendation } from '../../enums';
import { FraudScore } from '../fraudScore';

export type FraudScoreResponse = {
risk_score: string;
risk_recommendation: RiskRecommendation;
} & Omit<FraudScore, 'riskRecommendation' | 'riskScore'>;
2 changes: 2 additions & 0 deletions packages/number-insight-v2/lib/types/responses/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './fraudCheckResponse';
export * from './fraudScoreResponse';
7 changes: 7 additions & 0 deletions packages/number-insight-v2/lib/types/simSwap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Status } from '../enums/';

export type SimSwap = {
status: Status;
swapped?: boolean;
reason?: string;
};
36 changes: 36 additions & 0 deletions packages/number-insight-v2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@vonage/number-insight-v2",
"version": "1.0.0",
"description": "Number Insight v2 is designed to give fraud scores for Application Integrations.",
"homepage": "https://github.com/vonage/vonage-node-sdk/tree/main/packages/number-insight-v2#readme",
"bugs": {
"url": "https://github.com/Vonage/vonage-node-sdk/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Vonage/vonage-node-sdk.git"
},
"license": "Apache-2.0",
"author": "Chuck MANCHUCK Reeves <[email protected]>",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"/dist"
],
"scripts": {
"build": "npm run clean && npm run compile",
"clean": "npx shx rm -rf dist tsconfig.tsbuildinfo",
"compile": "npx tsc --build --verbose"
},
"dependencies": {
"@vonage/server-client": "1.8.2"
},
"devDependencies": {
"@vonage/auth": "1.6.0",
"nock": "13.3.3"
}
}

0 comments on commit faac742

Please sign in to comment.