-
Notifications
You must be signed in to change notification settings - Fork 4
/
get-portability-report.ts
110 lines (98 loc) · 3.27 KB
/
get-portability-report.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*!
* Copyright (c) 2009-2024 World Data Exchange Holdings Pty Limited (WDXH). All rights reserved.
*/
import * as t from "io-ts";
import { getRandomAlphaNumeric } from "./crypto";
import { net } from "./net";
import { UserAccessToken, UserAccessTokenCodec } from "./types/user-access-token";
import { sign } from "jsonwebtoken";
import { SDKConfiguration } from "./types/sdk-configuration";
import { ContractDetails, ContractDetailsCodec } from "./types/common";
export type Format = "xml";
const FormatCodec: t.Type<Format> = t.keyof({
xml: null,
});
export type ServiceType = "medmij";
const ServiceTypeCodec: t.Type<ServiceType> = t.keyof({
medmij: null,
});
export interface GetPortabilityReportOptions {
/**
* File format to be returned. Currently only XML is supported.
*/
format: Format;
/**
* Service type medmij is only supported for now.
*/
serviceType: ServiceType;
/**
* Any contract related details here.
*/
contractDetails: ContractDetails;
/**
* User access token you may already have for this user from this or from another contract.
*/
userAccessToken: UserAccessToken;
/**
* From timestamp in seconds
*/
from?: number;
/**
* To timestamp in seconds
*/
to?: number;
}
export const GetPortabilityReportOptionsCodec: t.Type<GetPortabilityReportOptions> = t.intersection([
t.type({
contractDetails: ContractDetailsCodec,
userAccessToken: UserAccessTokenCodec,
format: FormatCodec,
serviceType: ServiceTypeCodec,
}),
t.partial({
from: t.number,
to: t.number,
}),
]);
export interface GetPortabilityReportResponse {
file: string;
}
const getPortabilityReport = async (
props: GetPortabilityReportOptions,
sdkConfig: SDKConfiguration
): Promise<GetPortabilityReportResponse> => {
const { to, from, contractDetails, userAccessToken, serviceType, format } = props;
const { contractId, privateKey } = contractDetails;
const response = await net.get(
`${sdkConfig.baseUrl}export/${serviceType}/report?format=${format}&from=${from}&to=${to}`,
{
headers: {
accept: "application/octet-stream",
},
hooks: {
beforeRequest: [
(options) => {
const jwt: string = sign(
{
access_token: userAccessToken.accessToken.value,
client_id: `${sdkConfig.applicationId}_${contractId}`,
nonce: getRandomAlphaNumeric(32),
timestamp: Date.now(),
},
privateKey.toString(),
{
algorithm: "PS512",
noTimestamp: true,
}
);
options.headers["Authorization"] = `Bearer ${jwt}`;
},
],
},
}
);
return {
file: response.body,
};
};
export { getPortabilityReport };