@rivascva/dt-idl 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "parser": "@typescript-eslint/parser",
3
+ "plugins": [
4
+ "@typescript-eslint",
5
+ "prettier"
6
+ ],
7
+ "extends": [
8
+ "plugin:@typescript-eslint/eslint-recommended",
9
+ "plugin:@typescript-eslint/recommended",
10
+ "plugin:prettier/recommended",
11
+ "airbnb-base",
12
+ "airbnb-typescript/base",
13
+ "prettier"
14
+ ],
15
+ "parserOptions": {
16
+ "parser": "@typescript-eslint/parser",
17
+ "project": "tsconfig.json",
18
+ "sourceType": "module"
19
+ },
20
+ "ignorePatterns": [
21
+ "node_modules",
22
+ "dist",
23
+ "ts/services/**/*"
24
+ ],
25
+ "rules": {
26
+ "no-restricted-imports": [
27
+ "warn",
28
+ {
29
+ "patterns": [
30
+ "..*"
31
+ ]
32
+ }
33
+ ]
34
+ }
35
+ }
@@ -0,0 +1,8 @@
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.5.0
4
+ hooks:
5
+ - id: end-of-file-fixer
6
+ name: check newlines
7
+ - id: check-yaml
8
+ name: check yaml
@@ -0,0 +1,6 @@
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.5.0
4
+ hooks:
5
+ - id: end-of-file-fixer
6
+ name: check newlines
@@ -0,0 +1,40 @@
1
+ name: Publish
2
+
3
+ on:
4
+ push:
5
+ paths: ['ts/**/*']
6
+ branches: [main]
7
+ workflow_dispatch:
8
+ # allows manual runs
9
+
10
+ jobs:
11
+ publish:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v4
15
+ - uses: actions/setup-node@4
16
+ with:
17
+ node-version: 16.x
18
+ registry-url: 'https://registry.npmjs.org'
19
+ # install modules
20
+ - name: Install
21
+ run: npm install
22
+ # build files
23
+ - name: Build
24
+ run: npm run build
25
+ # bump version
26
+ - name: Version
27
+ run: |
28
+ git config --global user.name "Carlos Rivas"
29
+ git config --global user.email "RivasCVA@users.noreply.github.com"
30
+ tag=$(npm version patch --no-git-tag-version)
31
+ git tag $tag
32
+ git push origin tag $tag --no-verify
33
+ git commit -m "bump version $tag"
34
+ git add --all
35
+ git push origin main --no-verify
36
+ # publish package
37
+ - name: Publish
38
+ run: npm publish --access public
39
+ env:
40
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -0,0 +1,5 @@
1
+ {
2
+ "printWidth": 100,
3
+ "singleQuote": true,
4
+ "tabWidth": 2
5
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "editor.defaultFormatter": "esbenp.prettier-vscode",
3
+ "editor.formatOnSave": true,
4
+ "editor.codeActionsOnSave": {
5
+ "source.fixAll": "explicit"
6
+ },
7
+ "editor.detectIndentation": false,
8
+ "editor.tabSize": 2,
9
+ "files.insertFinalNewline": true,
10
+ }
package/Makefile ADDED
@@ -0,0 +1,4 @@
1
+ install:
2
+ brew install pre-commit
3
+ pre-commit install --hook-type pre-commit --config .githooks/pre-commit-config.yaml
4
+ pre-commit install --hook-type pre-push --config .githooks/pre-push-config.yaml
@@ -0,0 +1,464 @@
1
+ 'use strict';
2
+
3
+ // settings & const
4
+ const DEFAULT_HEADERS = {
5
+ "Content-Type": "application/json",
6
+ };
7
+
8
+ const PATH_PARAM_RE = /\{[^{}]+\}/g;
9
+
10
+ /**
11
+ * Add custom parameters to Request object
12
+ */
13
+ class CustomRequest extends Request {
14
+ constructor(input, init) {
15
+ super(input, init);
16
+
17
+ // add custom parameters
18
+ for (const key in init) {
19
+ if (!(key in this)) {
20
+ this[key] = init[key];
21
+ }
22
+ }
23
+ }
24
+ }
25
+
26
+ /**
27
+ * Create an openapi-fetch client.
28
+ * @type {import("./index.js").default}
29
+ */
30
+ function createClient(clientOptions) {
31
+ let {
32
+ baseUrl = "",
33
+ fetch: baseFetch = globalThis.fetch,
34
+ querySerializer: globalQuerySerializer,
35
+ bodySerializer: globalBodySerializer,
36
+ headers: baseHeaders,
37
+ ...baseOptions
38
+ } = { ...clientOptions };
39
+ if (baseUrl.endsWith("/")) {
40
+ baseUrl = baseUrl.substring(0, baseUrl.length - 1);
41
+ }
42
+ baseHeaders = mergeHeaders(DEFAULT_HEADERS, baseHeaders);
43
+ const middlewares = [];
44
+
45
+ /**
46
+ * Per-request fetch (keeps settings created in createClient()
47
+ * @param {T} url
48
+ * @param {import('./index.js').FetchOptions<T>} fetchOptions
49
+ */
50
+ async function coreFetch(url, fetchOptions) {
51
+ const {
52
+ fetch = baseFetch,
53
+ headers,
54
+ params = {},
55
+ parseAs = "json",
56
+ querySerializer: requestQuerySerializer,
57
+ bodySerializer = globalBodySerializer ?? defaultBodySerializer,
58
+ ...init
59
+ } = fetchOptions || {};
60
+
61
+ let querySerializer =
62
+ typeof globalQuerySerializer === "function"
63
+ ? globalQuerySerializer
64
+ : createQuerySerializer(globalQuerySerializer);
65
+ if (requestQuerySerializer) {
66
+ querySerializer =
67
+ typeof requestQuerySerializer === "function"
68
+ ? requestQuerySerializer
69
+ : createQuerySerializer({
70
+ ...(typeof globalQuerySerializer === "object" ? globalQuerySerializer : {}),
71
+ ...requestQuerySerializer,
72
+ });
73
+ }
74
+
75
+ const requestInit = {
76
+ redirect: "follow",
77
+ ...baseOptions,
78
+ ...init,
79
+ headers: mergeHeaders(baseHeaders, headers, params.header),
80
+ };
81
+ if (requestInit.body) {
82
+ requestInit.body = bodySerializer(requestInit.body);
83
+ }
84
+ // remove `Content-Type` if serialized body is FormData; browser will correctly set Content-Type & boundary expression
85
+ if (requestInit.body instanceof FormData) {
86
+ requestInit.headers.delete("Content-Type");
87
+ }
88
+ let request = new CustomRequest(createFinalURL(url, { baseUrl, params, querySerializer }), requestInit);
89
+ // middleware (request)
90
+ const mergedOptions = {
91
+ baseUrl,
92
+ fetch,
93
+ parseAs,
94
+ querySerializer,
95
+ bodySerializer,
96
+ };
97
+ for (const m of middlewares) {
98
+ if (m && typeof m === "object" && typeof m.onRequest === "function") {
99
+ request.schemaPath = url; // (re)attach original URL
100
+ request.params = params; // (re)attach params
101
+ const result = await m.onRequest(request, mergedOptions);
102
+ if (result) {
103
+ if (!(result instanceof Request)) {
104
+ throw new Error("Middleware must return new Request() when modifying the request");
105
+ }
106
+ request = result;
107
+ }
108
+ }
109
+ }
110
+
111
+ // fetch!
112
+ let response = await fetch(request);
113
+
114
+ // middleware (response)
115
+ // execute in reverse-array order (first priority gets last transform)
116
+ for (let i = middlewares.length - 1; i >= 0; i--) {
117
+ const m = middlewares[i];
118
+ if (m && typeof m === "object" && typeof m.onResponse === "function") {
119
+ const result = await m.onResponse(response, mergedOptions);
120
+ if (result) {
121
+ if (!(result instanceof Response)) {
122
+ throw new Error("Middleware must return new Response() when modifying the response");
123
+ }
124
+ response = result;
125
+ }
126
+ }
127
+ }
128
+
129
+ // handle empty content
130
+ // note: we return `{}` because we want user truthy checks for `.data` or `.error` to succeed
131
+ if (response.status === 204 || response.headers.get("Content-Length") === "0") {
132
+ return response.ok ? { data: {}, response } : { error: {}, response };
133
+ }
134
+
135
+ // parse response (falling back to .text() when necessary)
136
+ if (response.ok) {
137
+ // if "stream", skip parsing entirely
138
+ if (parseAs === "stream") {
139
+ return { data: response.body, response };
140
+ }
141
+ return { data: await response[parseAs](), response };
142
+ }
143
+
144
+ // handle errors
145
+ let error = await response.text();
146
+ try {
147
+ error = JSON.parse(error); // attempt to parse as JSON
148
+ } catch {
149
+ // noop
150
+ }
151
+ return { error, response };
152
+ }
153
+
154
+ return {
155
+ /** Call a GET endpoint */
156
+ async GET(url, init) {
157
+ return coreFetch(url, { ...init, method: "GET" });
158
+ },
159
+ /** Call a PUT endpoint */
160
+ async PUT(url, init) {
161
+ return coreFetch(url, { ...init, method: "PUT" });
162
+ },
163
+ /** Call a POST endpoint */
164
+ async POST(url, init) {
165
+ return coreFetch(url, { ...init, method: "POST" });
166
+ },
167
+ /** Call a DELETE endpoint */
168
+ async DELETE(url, init) {
169
+ return coreFetch(url, { ...init, method: "DELETE" });
170
+ },
171
+ /** Call a OPTIONS endpoint */
172
+ async OPTIONS(url, init) {
173
+ return coreFetch(url, { ...init, method: "OPTIONS" });
174
+ },
175
+ /** Call a HEAD endpoint */
176
+ async HEAD(url, init) {
177
+ return coreFetch(url, { ...init, method: "HEAD" });
178
+ },
179
+ /** Call a PATCH endpoint */
180
+ async PATCH(url, init) {
181
+ return coreFetch(url, { ...init, method: "PATCH" });
182
+ },
183
+ /** Call a TRACE endpoint */
184
+ async TRACE(url, init) {
185
+ return coreFetch(url, { ...init, method: "TRACE" });
186
+ },
187
+ /** Register middleware */
188
+ use(...middleware) {
189
+ for (const m of middleware) {
190
+ if (!m) {
191
+ continue;
192
+ }
193
+ if (typeof m !== "object" || !("onRequest" in m || "onResponse" in m)) {
194
+ throw new Error("Middleware must be an object with one of `onRequest()` or `onResponse()`");
195
+ }
196
+ middlewares.push(m);
197
+ }
198
+ },
199
+ /** Unregister middleware */
200
+ eject(...middleware) {
201
+ for (const m of middleware) {
202
+ const i = middlewares.indexOf(m);
203
+ if (i !== -1) {
204
+ middlewares.splice(i, 1);
205
+ }
206
+ }
207
+ },
208
+ };
209
+ }
210
+
211
+ // utils
212
+
213
+ /**
214
+ * Serialize primitive param values
215
+ * @type {import("./index.js").serializePrimitiveParam}
216
+ */
217
+ function serializePrimitiveParam(name, value, options) {
218
+ if (value === undefined || value === null) {
219
+ return "";
220
+ }
221
+ if (typeof value === "object") {
222
+ throw new Error(
223
+ "Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.",
224
+ );
225
+ }
226
+ return `${name}=${options?.allowReserved === true ? value : encodeURIComponent(value)}`;
227
+ }
228
+
229
+ /**
230
+ * Serialize object param (shallow only)
231
+ * @type {import("./index.js").serializeObjectParam}
232
+ */
233
+ function serializeObjectParam(name, value, options) {
234
+ if (!value || typeof value !== "object") {
235
+ return "";
236
+ }
237
+ const values = [];
238
+ const joiner =
239
+ {
240
+ simple: ",",
241
+ label: ".",
242
+ matrix: ";",
243
+ }[options.style] || "&";
244
+
245
+ // explode: false
246
+ if (options.style !== "deepObject" && options.explode === false) {
247
+ for (const k in value) {
248
+ values.push(k, options.allowReserved === true ? value[k] : encodeURIComponent(value[k]));
249
+ }
250
+ const final = values.join(","); // note: values are always joined by comma in explode: false (but joiner can prefix)
251
+ switch (options.style) {
252
+ case "form": {
253
+ return `${name}=${final}`;
254
+ }
255
+ case "label": {
256
+ return `.${final}`;
257
+ }
258
+ case "matrix": {
259
+ return `;${name}=${final}`;
260
+ }
261
+ default: {
262
+ return final;
263
+ }
264
+ }
265
+ }
266
+
267
+ // explode: true
268
+ for (const k in value) {
269
+ const finalName = options.style === "deepObject" ? `${name}[${k}]` : k;
270
+ values.push(serializePrimitiveParam(finalName, value[k], options));
271
+ }
272
+ const final = values.join(joiner);
273
+ return options.style === "label" || options.style === "matrix" ? `${joiner}${final}` : final;
274
+ }
275
+
276
+ /**
277
+ * Serialize array param (shallow only)
278
+ * @type {import("./index.js").serializeArrayParam}
279
+ */
280
+ function serializeArrayParam(name, value, options) {
281
+ if (!Array.isArray(value)) {
282
+ return "";
283
+ }
284
+
285
+ // explode: false
286
+ if (options.explode === false) {
287
+ const joiner = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[options.style] || ","; // note: for arrays, joiners vary wildly based on style + explode behavior
288
+ const final = (options.allowReserved === true ? value : value.map((v) => encodeURIComponent(v))).join(joiner);
289
+ switch (options.style) {
290
+ case "simple": {
291
+ return final;
292
+ }
293
+ case "label": {
294
+ return `.${final}`;
295
+ }
296
+ case "matrix": {
297
+ return `;${name}=${final}`;
298
+ }
299
+ // case "spaceDelimited":
300
+ // case "pipeDelimited":
301
+ default: {
302
+ return `${name}=${final}`;
303
+ }
304
+ }
305
+ }
306
+
307
+ // explode: true
308
+ const joiner = { simple: ",", label: ".", matrix: ";" }[options.style] || "&";
309
+ const values = [];
310
+ for (const v of value) {
311
+ if (options.style === "simple" || options.style === "label") {
312
+ values.push(options.allowReserved === true ? v : encodeURIComponent(v));
313
+ } else {
314
+ values.push(serializePrimitiveParam(name, v, options));
315
+ }
316
+ }
317
+ return options.style === "label" || options.style === "matrix"
318
+ ? `${joiner}${values.join(joiner)}`
319
+ : values.join(joiner);
320
+ }
321
+
322
+ /**
323
+ * Serialize query params to string
324
+ * @type {import("./index.js").createQuerySerializer}
325
+ */
326
+ function createQuerySerializer(options) {
327
+ return function querySerializer(queryParams) {
328
+ const search = [];
329
+ if (queryParams && typeof queryParams === "object") {
330
+ for (const name in queryParams) {
331
+ const value = queryParams[name];
332
+ if (value === undefined || value === null) {
333
+ continue;
334
+ }
335
+ if (Array.isArray(value)) {
336
+ search.push(
337
+ serializeArrayParam(name, value, {
338
+ style: "form",
339
+ explode: true,
340
+ ...options?.array,
341
+ allowReserved: options?.allowReserved || false,
342
+ }),
343
+ );
344
+ continue;
345
+ }
346
+ if (typeof value === "object") {
347
+ search.push(
348
+ serializeObjectParam(name, value, {
349
+ style: "deepObject",
350
+ explode: true,
351
+ ...options?.object,
352
+ allowReserved: options?.allowReserved || false,
353
+ }),
354
+ );
355
+ continue;
356
+ }
357
+ search.push(serializePrimitiveParam(name, value, options));
358
+ }
359
+ }
360
+ return search.join("&");
361
+ };
362
+ }
363
+
364
+ /**
365
+ * Handle different OpenAPI 3.x serialization styles
366
+ * @type {import("./index.js").defaultPathSerializer}
367
+ * @see https://swagger.io/docs/specification/serialization/#path
368
+ */
369
+ function defaultPathSerializer(pathname, pathParams) {
370
+ let nextURL = pathname;
371
+ for (const match of pathname.match(PATH_PARAM_RE) ?? []) {
372
+ let name = match.substring(1, match.length - 1);
373
+ let explode = false;
374
+ let style = "simple";
375
+ if (name.endsWith("*")) {
376
+ explode = true;
377
+ name = name.substring(0, name.length - 1);
378
+ }
379
+ if (name.startsWith(".")) {
380
+ style = "label";
381
+ name = name.substring(1);
382
+ } else if (name.startsWith(";")) {
383
+ style = "matrix";
384
+ name = name.substring(1);
385
+ }
386
+ if (!pathParams || pathParams[name] === undefined || pathParams[name] === null) {
387
+ continue;
388
+ }
389
+ const value = pathParams[name];
390
+ if (Array.isArray(value)) {
391
+ nextURL = nextURL.replace(match, serializeArrayParam(name, value, { style, explode }));
392
+ continue;
393
+ }
394
+ if (typeof value === "object") {
395
+ nextURL = nextURL.replace(match, serializeObjectParam(name, value, { style, explode }));
396
+ continue;
397
+ }
398
+ if (style === "matrix") {
399
+ nextURL = nextURL.replace(match, `;${serializePrimitiveParam(name, value)}`);
400
+ continue;
401
+ }
402
+ nextURL = nextURL.replace(match, style === "label" ? `.${value}` : value);
403
+ }
404
+ return nextURL;
405
+ }
406
+
407
+ /**
408
+ * Serialize body object to string
409
+ * @type {import("./index.js").defaultBodySerializer}
410
+ */
411
+ function defaultBodySerializer(body) {
412
+ return JSON.stringify(body);
413
+ }
414
+
415
+ /**
416
+ * Construct URL string from baseUrl and handle path and query params
417
+ * @type {import("./index.js").createFinalURL}
418
+ */
419
+ function createFinalURL(pathname, options) {
420
+ let finalURL = `${options.baseUrl}${pathname}`;
421
+ if (options.params?.path) {
422
+ finalURL = defaultPathSerializer(finalURL, options.params.path);
423
+ }
424
+ let search = options.querySerializer(options.params.query ?? {});
425
+ if (search.startsWith("?")) {
426
+ search = search.substring(1);
427
+ }
428
+ if (search) {
429
+ finalURL += `?${search}`;
430
+ }
431
+ return finalURL;
432
+ }
433
+
434
+ /**
435
+ * Merge headers a and b, with b taking priority
436
+ * @type {import("./index.js").mergeHeaders}
437
+ */
438
+ function mergeHeaders(...allHeaders) {
439
+ const finalHeaders = new Headers();
440
+ for (const h of allHeaders) {
441
+ if (!h || typeof h !== "object") {
442
+ continue;
443
+ }
444
+ const iterator = h instanceof Headers ? h.entries() : Object.entries(h);
445
+ for (const [k, v] of iterator) {
446
+ if (v === null) {
447
+ finalHeaders.delete(k);
448
+ } else if (Array.isArray(v)) {
449
+ for (const v2 of v) {
450
+ finalHeaders.append(k, v2);
451
+ }
452
+ } else if (v !== undefined) {
453
+ finalHeaders.set(k, v);
454
+ }
455
+ }
456
+ }
457
+ return finalHeaders;
458
+ }
459
+
460
+ const createMarketServiceClient = (options) => createClient(options);
461
+ const createTradeServiceClient = (options) => createClient(options);
462
+
463
+ exports.createMarketServiceClient = createMarketServiceClient;
464
+ exports.createTradeServiceClient = createTradeServiceClient;