@rivascva/dt-idl 1.1.8 → 1.1.10

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/dist/index.esm.js CHANGED
@@ -5,9 +5,7 @@ const DEFAULT_HEADERS = {
5
5
 
6
6
  const PATH_PARAM_RE = /\{[^{}]+\}/g;
7
7
 
8
- /**
9
- * Add custom parameters to Request object
10
- */
8
+ /** Add custom parameters to Request object */
11
9
  class CustomRequest extends Request {
12
10
  constructor(input, init) {
13
11
  super(input, init);
@@ -21,6 +19,14 @@ class CustomRequest extends Request {
21
19
  }
22
20
  }
23
21
 
22
+ /**
23
+ * Returns a cheap, non-cryptographically-secure random ID
24
+ * Courtesy of @imranbarbhuiya (https://github.com/imranbarbhuiya)
25
+ */
26
+ function randomID() {
27
+ return Math.random().toString(36).slice(2, 11);
28
+ }
29
+
24
30
  /**
25
31
  * Create an openapi-fetch client.
26
32
  * @type {import("./index.js").default}
@@ -45,7 +51,7 @@ function createClient(clientOptions) {
45
51
  * @param {T} url
46
52
  * @param {import('./index.js').FetchOptions<T>} fetchOptions
47
53
  */
48
- async function coreFetch(url, fetchOptions) {
54
+ async function coreFetch(schemaPath, fetchOptions) {
49
55
  const {
50
56
  fetch = baseFetch,
51
57
  headers,
@@ -78,30 +84,42 @@ function createClient(clientOptions) {
78
84
  };
79
85
  if (requestInit.body) {
80
86
  requestInit.body = bodySerializer(requestInit.body);
87
+ // remove `Content-Type` if serialized body is FormData; browser will correctly set Content-Type & boundary expression
88
+ if (requestInit.body instanceof FormData) {
89
+ requestInit.headers.delete("Content-Type");
90
+ }
81
91
  }
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");
92
+
93
+ let id;
94
+ let options;
95
+ let request = new CustomRequest(createFinalURL(schemaPath, { baseUrl, params, querySerializer }), requestInit);
96
+
97
+ if (middlewares.length) {
98
+ id = randomID();
99
+
100
+ // middleware (request)
101
+ options = Object.freeze({
102
+ baseUrl,
103
+ fetch,
104
+ parseAs,
105
+ querySerializer,
106
+ bodySerializer,
107
+ });
108
+ for (const m of middlewares) {
109
+ if (m && typeof m === "object" && typeof m.onRequest === "function") {
110
+ const result = await m.onRequest({
111
+ request,
112
+ schemaPath,
113
+ params,
114
+ options,
115
+ id,
116
+ });
117
+ if (result) {
118
+ if (!(result instanceof Request)) {
119
+ throw new Error("onRequest: must return new Request() when modifying the request");
120
+ }
121
+ request = result;
103
122
  }
104
- request = result;
105
123
  }
106
124
  }
107
125
  }
@@ -111,15 +129,24 @@ function createClient(clientOptions) {
111
129
 
112
130
  // middleware (response)
113
131
  // 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");
132
+ if (middlewares.length) {
133
+ for (let i = middlewares.length - 1; i >= 0; i--) {
134
+ const m = middlewares[i];
135
+ if (m && typeof m === "object" && typeof m.onResponse === "function") {
136
+ const result = await m.onResponse({
137
+ request,
138
+ response,
139
+ schemaPath,
140
+ params,
141
+ options,
142
+ id,
143
+ });
144
+ if (result) {
145
+ if (!(result instanceof Response)) {
146
+ throw new Error("onResponse: must return new Response() when modifying the response");
147
+ }
148
+ response = result;
121
149
  }
122
- response = result;
123
150
  }
124
151
  }
125
152
  }
@@ -397,7 +424,7 @@ function defaultPathSerializer(pathname, pathParams) {
397
424
  nextURL = nextURL.replace(match, `;${serializePrimitiveParam(name, value)}`);
398
425
  continue;
399
426
  }
400
- nextURL = nextURL.replace(match, style === "label" ? `.${value}` : value);
427
+ nextURL = nextURL.replace(match, style === "label" ? `.${encodeURIComponent(value)}` : encodeURIComponent(value));
401
428
  }
402
429
  return nextURL;
403
430
  }
@@ -407,6 +434,9 @@ function defaultPathSerializer(pathname, pathParams) {
407
434
  * @type {import("./index.js").defaultBodySerializer}
408
435
  */
409
436
  function defaultBodySerializer(body) {
437
+ if (body instanceof FormData) {
438
+ return body;
439
+ }
410
440
  return JSON.stringify(body);
411
441
  }
412
442
 
@@ -458,4 +488,7 @@ function mergeHeaders(...allHeaders) {
458
488
  const createMarketServiceClient = (options) => createClient(options);
459
489
  const createTradeServiceClient = (options) => createClient(options);
460
490
 
461
- export { createMarketServiceClient, createTradeServiceClient };
491
+ const isMarketServiceError = (error) => typeof error === 'object';
492
+ const isTradeServiceError = (error) => typeof error === 'object';
493
+
494
+ export { createMarketServiceClient, createTradeServiceClient, isMarketServiceError, isTradeServiceError };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rivascva/dt-idl",
3
- "version": "1.1.8",
3
+ "version": "1.1.10",
4
4
  "description": "Dream Trade - Interface Definition Language",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
@@ -9,7 +9,7 @@
9
9
  "check": "tsc --noEmit",
10
10
  "lint": "eslint .",
11
11
  "lint:fix": "eslint . --fix",
12
- "gen": "rimraf ts/services && npx openapi-typescript services/**/*.yaml -o ts/",
12
+ "gen": "rimraf ts/services && openapi-typescript --redocly redocly.yaml",
13
13
  "build": "rimraf dist && rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript"
14
14
  },
15
15
  "homepage": "https://github.com/RivasCVA/dt-idl",
@@ -27,7 +27,7 @@
27
27
  "eslint-config-prettier": "^9.1.0",
28
28
  "eslint-plugin-import": "^2.29.1",
29
29
  "eslint-plugin-prettier": "^5.1.3",
30
- "openapi-typescript": "^6.7.6",
30
+ "openapi-typescript": "^7.2.0",
31
31
  "rimraf": "^5.0.7",
32
32
  "rollup": "^4.17.2",
33
33
  "rollup-plugin-dts": "^6.1.1",
@@ -35,6 +35,6 @@
35
35
  "typescript": "^5.4.5"
36
36
  },
37
37
  "dependencies": {
38
- "openapi-fetch": "^0.9.7"
38
+ "openapi-fetch": "^0.10.5"
39
39
  }
40
40
  }
package/redocly.yaml ADDED
@@ -0,0 +1,9 @@
1
+ apis:
2
+ dt-market-service@v1:
3
+ root: ./services/dt-market-service.yaml
4
+ x-openapi-ts:
5
+ output: ./ts/services/dt-market-service.ts
6
+ dt-trade-service@v1:
7
+ root: ./services/dt-trade-service.yaml
8
+ x-openapi-ts:
9
+ output: ./ts/services/dt-trade-service.ts
@@ -0,0 +1,7 @@
1
+ import { MarketServiceError, TradeServiceError } from '@ts/types';
2
+
3
+ export const isMarketServiceError = (error: unknown): error is MarketServiceError =>
4
+ typeof error === 'object';
5
+
6
+ export const isTradeServiceError = (error: unknown): error is TradeServiceError =>
7
+ typeof error === 'object';
@@ -1 +1,2 @@
1
1
  export * from './clients';
2
+ export * from './errors';