@rivascva/dt-idl 1.1.8 → 1.1.9

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.cjs.js CHANGED
@@ -7,9 +7,7 @@ const DEFAULT_HEADERS = {
7
7
 
8
8
  const PATH_PARAM_RE = /\{[^{}]+\}/g;
9
9
 
10
- /**
11
- * Add custom parameters to Request object
12
- */
10
+ /** Add custom parameters to Request object */
13
11
  class CustomRequest extends Request {
14
12
  constructor(input, init) {
15
13
  super(input, init);
@@ -23,6 +21,14 @@ class CustomRequest extends Request {
23
21
  }
24
22
  }
25
23
 
24
+ /**
25
+ * Returns a cheap, non-cryptographically-secure random ID
26
+ * Courtesy of @imranbarbhuiya (https://github.com/imranbarbhuiya)
27
+ */
28
+ function randomID() {
29
+ return Math.random().toString(36).slice(2, 11);
30
+ }
31
+
26
32
  /**
27
33
  * Create an openapi-fetch client.
28
34
  * @type {import("./index.js").default}
@@ -47,7 +53,7 @@ function createClient(clientOptions) {
47
53
  * @param {T} url
48
54
  * @param {import('./index.js').FetchOptions<T>} fetchOptions
49
55
  */
50
- async function coreFetch(url, fetchOptions) {
56
+ async function coreFetch(schemaPath, fetchOptions) {
51
57
  const {
52
58
  fetch = baseFetch,
53
59
  headers,
@@ -80,30 +86,42 @@ function createClient(clientOptions) {
80
86
  };
81
87
  if (requestInit.body) {
82
88
  requestInit.body = bodySerializer(requestInit.body);
89
+ // remove `Content-Type` if serialized body is FormData; browser will correctly set Content-Type & boundary expression
90
+ if (requestInit.body instanceof FormData) {
91
+ requestInit.headers.delete("Content-Type");
92
+ }
83
93
  }
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");
94
+
95
+ let id;
96
+ let options;
97
+ let request = new CustomRequest(createFinalURL(schemaPath, { baseUrl, params, querySerializer }), requestInit);
98
+
99
+ if (middlewares.length) {
100
+ id = randomID();
101
+
102
+ // middleware (request)
103
+ options = Object.freeze({
104
+ baseUrl,
105
+ fetch,
106
+ parseAs,
107
+ querySerializer,
108
+ bodySerializer,
109
+ });
110
+ for (const m of middlewares) {
111
+ if (m && typeof m === "object" && typeof m.onRequest === "function") {
112
+ const result = await m.onRequest({
113
+ request,
114
+ schemaPath,
115
+ params,
116
+ options,
117
+ id,
118
+ });
119
+ if (result) {
120
+ if (!(result instanceof Request)) {
121
+ throw new Error("onRequest: must return new Request() when modifying the request");
122
+ }
123
+ request = result;
105
124
  }
106
- request = result;
107
125
  }
108
126
  }
109
127
  }
@@ -113,15 +131,24 @@ function createClient(clientOptions) {
113
131
 
114
132
  // middleware (response)
115
133
  // 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");
134
+ if (middlewares.length) {
135
+ for (let i = middlewares.length - 1; i >= 0; i--) {
136
+ const m = middlewares[i];
137
+ if (m && typeof m === "object" && typeof m.onResponse === "function") {
138
+ const result = await m.onResponse({
139
+ request,
140
+ response,
141
+ schemaPath,
142
+ params,
143
+ options,
144
+ id,
145
+ });
146
+ if (result) {
147
+ if (!(result instanceof Response)) {
148
+ throw new Error("onResponse: must return new Response() when modifying the response");
149
+ }
150
+ response = result;
123
151
  }
124
- response = result;
125
152
  }
126
153
  }
127
154
  }
@@ -399,7 +426,7 @@ function defaultPathSerializer(pathname, pathParams) {
399
426
  nextURL = nextURL.replace(match, `;${serializePrimitiveParam(name, value)}`);
400
427
  continue;
401
428
  }
402
- nextURL = nextURL.replace(match, style === "label" ? `.${value}` : value);
429
+ nextURL = nextURL.replace(match, style === "label" ? `.${encodeURIComponent(value)}` : encodeURIComponent(value));
403
430
  }
404
431
  return nextURL;
405
432
  }
@@ -409,6 +436,9 @@ function defaultPathSerializer(pathname, pathParams) {
409
436
  * @type {import("./index.js").defaultBodySerializer}
410
437
  */
411
438
  function defaultBodySerializer(body) {
439
+ if (body instanceof FormData) {
440
+ return body;
441
+ }
412
442
  return JSON.stringify(body);
413
443
  }
414
444