-
Notifications
You must be signed in to change notification settings - Fork 4
/
query-categories.ts
177 lines (156 loc) · 5.12 KB
/
query-categories.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*!
* Copyright (c) 2009-2024 World Data Exchange Holdings Pty Limited (WDXH). All rights reserved.
*/
import { net } from "./net";
import { SDKConfiguration } from "./types/sdk-configuration";
import { TypeValidationError } from "./errors";
import * as t from "io-ts";
export type { DiscoveryService } from "./types/api/get-discovery-api-services";
import { ContractDetails, ContractDetailsCodec } from "./types/common";
import { sign } from "jsonwebtoken";
import { getRandomAlphaNumeric } from "./crypto";
import { CodecAssertion, codecAssertion } from "./utils/codec-assertion";
export interface CategoryResource {
mimetype?: string;
resize?: string;
type?: number;
url?: string;
}
const ResourceCodec: t.Type<CategoryResource> = t.partial({
mimetype: t.string,
resize: t.string,
type: t.number,
url: t.string,
});
export interface Category {
id: number;
categoryTypeId?: number;
name?: string;
reference?: string;
resource?: CategoryResource;
subTitle?: string;
title?: string;
expandedTitle?: string;
expandedSubTitle?: string;
}
const PlatformCodec: t.Type<Category> = t.intersection([
t.type({
id: t.number,
}),
t.partial({
categoryTypeId: t.number,
name: t.string,
reference: t.string,
resource: ResourceCodec,
subTitle: t.string,
title: t.string,
expandedTitle: t.string,
expandedSubTitle: t.string,
}),
]);
export interface QueryCategoriesResponse {
/**
* List of categories
*/
data: Category[];
}
export type CategoriesIncludeFieldList = "id" | "name" | "reference" | "json" | "resource.mimetype" | "resource.url";
const CategoriesIncludeFieldListCodec: t.Type<CategoriesIncludeFieldList> = t.union([
t.literal("id"),
t.literal("name"),
t.literal("reference"),
t.literal("json"),
t.literal("resource.mimetype"),
t.literal("resource.url"),
]);
export interface CategoriesBodyParams {
query?: {
/**
* Posible fields to include are defined in type CategoriesIncludeFieldList .
*/
include?: CategoriesIncludeFieldList[];
filter?: {
id?: number[];
};
};
}
const CategoriesBodyParamsCodec: t.Type<CategoriesBodyParams> = t.partial({
query: t.partial({
include: t.array(CategoriesIncludeFieldListCodec),
}),
});
export interface QueryCategoriesOptions {
/**
* Contract details here.
*/
contractDetails: ContractDetails;
/**
* Additional query options.
*/
categoriesBodyParams?: CategoriesBodyParams;
}
const QueryCategoriesOptionsCodec: t.Type<QueryCategoriesOptions> = t.intersection([
t.type({
contractDetails: ContractDetailsCodec,
}),
t.partial({
categoriesBodyParams: CategoriesBodyParamsCodec,
}),
]);
const QueryCategoriesResponseCodec: t.Type<QueryCategoriesResponse> = t.type({
data: t.array(PlatformCodec),
});
const assertIsCategoriesApiData: CodecAssertion<QueryCategoriesResponse> = codecAssertion(QueryCategoriesResponseCodec);
const queryCategories = async (
options: QueryCategoriesOptions,
sdkConfig: SDKConfiguration
): Promise<QueryCategoriesResponse> => {
if (!QueryCategoriesOptionsCodec.is(options)) {
throw new TypeValidationError(
"Parameters failed validation. Params should be defined as outlined in QueryCategoriesOptions type"
);
}
const { contractDetails, categoriesBodyParams } = options;
const { contractId, privateKey } = contractDetails;
// set body params
const bodyParams: CategoriesBodyParams = {
query: {
include: categoriesBodyParams?.query?.include || ["id", "name", "reference"],
filter: {
...(categoriesBodyParams?.query?.filter?.id && { id: categoriesBodyParams?.query?.filter?.id }),
},
},
};
const response = await net.post(`${sdkConfig.baseUrl}discovery/categories`, {
headers: {
"Content-Type": "application/json",
},
json: bodyParams,
responseType: "json",
retry: { ...sdkConfig.retryOptions, methods: ["POST"] },
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}`;
},
],
},
});
assertIsCategoriesApiData(response.body);
return {
...response.body,
};
};
export { queryCategories };