-
Notifications
You must be signed in to change notification settings - Fork 4
/
get-service-sample-datasets.ts
96 lines (79 loc) · 3.09 KB
/
get-service-sample-datasets.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
/*!
* Copyright (c) 2009-2024 World Data Exchange Holdings Pty Limited (WDXH). All rights reserved.
*/
import { handleServerResponse, net } from "./net";
import { sign } from "jsonwebtoken";
import { getRandomAlphaNumeric } from "./crypto";
import { SDKConfiguration } from "./types/sdk-configuration";
import { ContractDetails, ContractDetailsCodec } from "./types/common";
import * as t from "io-ts";
import { TypeValidationError } from "./errors";
import { CodecAssertion, codecAssertion } from "./utils/codec-assertion";
export interface GetServiceSampleDataSetsOptions {
contractDetails: ContractDetails;
sourceId: number;
}
export const ServiceSampleDataSetsOptionsOptionsCodec: t.Type<GetServiceSampleDataSetsOptions> = t.type({
contractDetails: ContractDetailsCodec,
sourceId: t.number,
});
type DataSet = {
description: string;
name: string;
};
const DataSetCodec = t.type({
description: t.string,
name: t.string,
});
export type GetServiceSampleDataSetsResponse = {
[key: string]: DataSet;
};
const GetServiceSampleDataSetsResponseCodec = t.record(t.string, DataSetCodec);
export const assertIsDataSetsResponse: CodecAssertion<GetServiceSampleDataSetsResponse> = codecAssertion(
GetServiceSampleDataSetsResponseCodec
);
const getServiceSampleDataSets = async (
options: GetServiceSampleDataSetsOptions,
sdkConfig: SDKConfiguration
): Promise<GetServiceSampleDataSetsResponse> => {
if (!ServiceSampleDataSetsOptionsOptionsCodec.is(options)) {
throw new TypeValidationError(
"Parameters failed validation. props should be a plain object that contains the properties contractDetails and sourceId"
);
}
const { contractDetails, sourceId } = options;
const { contractId, privateKey } = contractDetails;
try {
const url = `${sdkConfig.baseUrl}permission-access/sample/datasets/${sourceId}`;
const response = await net.get(url, {
responseType: "json",
retry: sdkConfig.retryOptions,
hooks: {
beforeRequest: [
(options) => {
const jwt: string = sign(
{
client_id: `${sdkConfig.applicationId}_${contractId}`,
nonce: getRandomAlphaNumeric(32),
timestamp: Date.now(),
},
privateKey.toString(),
{
algorithm: "PS512",
noTimestamp: true,
}
);
options.headers["Authorization"] = `Bearer ${jwt}`;
},
],
},
});
const datasets = response.body;
assertIsDataSetsResponse(datasets);
return datasets;
} catch (error) {
handleServerResponse(error);
throw error;
}
};
export { getServiceSampleDataSets };