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