openapi-typesafe-fetch 1.0.0-rc.1 → 1.0.0-rc.2

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/src/index.js CHANGED
@@ -1,10 +1,18 @@
1
1
  // settings & const
2
2
  const PATH_PARAM_RE = /\{[^{}]+\}/g;
3
- /** A `.` or `..` path segment; URLs resolve it, so a parameter value could leave its path (#2483). */
3
+ /**
4
+ * A `.` or `..` path segment; URLs resolve it, so a parameter value could leave its path (#2483).
5
+ */
4
6
  const DOT_SEGMENT_RE = /(^|\/)\.\.?(\/|$)/;
5
- /** Any JSON media type: `application/json`, `application/problem+json`, with or without parameters. */
7
+ /** Where the path of a URL ends: its query or fragment, which have no segments. */
8
+ const QUERY_OR_FRAGMENT_RE = /[?#]/;
9
+ /**
10
+ * Any JSON media type: `application/json`, `application/problem+json`, with or without
11
+ * parameters.
12
+ */
6
13
  const JSON_MEDIA_TYPE_RE = /json/i;
7
- const FORM_MEDIA_TYPE = "application/x-www-form-urlencoded";
14
+ /** `application/x-www-form-urlencoded`, with or without parameters such as `charset`. */
15
+ const FORM_MEDIA_TYPE_RE = /^application\/x-www-form-urlencoded(;|$)/i;
8
16
  /** `multipart/form-data`, with or without parameters. */
9
17
  const MULTIPART_MEDIA_TYPE_RE = /^multipart\/form-data(;|$)/i;
10
18
  const NESTED_FORM_VALUE_MESSAGE = "Nested form values need your own `bodySerializer()`.";
@@ -39,6 +47,7 @@ export function randomID() {
39
47
  * Create an openapi-typesafe-fetch client.
40
48
  * @type {import("./index.js").default}
41
49
  */
50
+ // biome-ignore lint/complexity/noExcessiveLinesPerFunction: inherited, kept until 1.1 (ADR-036)
42
51
  export default function createClient(clientOptions) {
43
52
  let {
44
53
  baseUrl = "",
@@ -60,6 +69,7 @@ export default function createClient(clientOptions) {
60
69
  * @param {T} url
61
70
  * @param {import('./index.js').FetchOptions<T>} fetchOptions
62
71
  */
72
+ // biome-ignore lint/complexity/noExcessiveLinesPerFunction: inherited, kept until 1.1 (ADR-036)
63
73
  async function coreFetch(schemaPath, fetchOptions) {
64
74
  const {
65
75
  baseUrl: localBaseUrl,
@@ -101,11 +111,12 @@ export default function createClient(clientOptions) {
101
111
  ? undefined
102
112
  : bodySerializer(
103
113
  body,
104
- // Note: we declare mergeHeaders() both here and below because it’s a bit of a chicken-or-egg situation:
105
- // bodySerializer() needs all headers so we aren’t dropping ones set by the user, however,
106
- // the result of this ALSO sets the lowest-priority content-type header. So we re-merge below,
107
- // setting the content-type at the very beginning to be overwritten.
108
- // Lastly, based on the way headers work, it’s not a simple “present-or-not” check becauase null intentionally un-sets headers.
114
+ // Note: we declare mergeHeaders() both here and below because it’s a bit of a
115
+ // chicken-or-egg situation: bodySerializer() needs all headers so we aren’t dropping
116
+ // ones set by the user, however, the result of this ALSO sets the lowest-priority
117
+ // content-type header. So we re-merge below, setting the content-type at the very
118
+ // beginning to be overwritten. Lastly, based on the way headers work, it’s not a simple
119
+ // “present-or-not” check because null intentionally un-sets headers.
109
120
  mergeHeaders(baseHeaders, headers, params.header),
110
121
  );
111
122
  const finalHeaders = mergeHeaders(
@@ -124,7 +135,10 @@ export default function createClient(clientOptions) {
124
135
  // `fetch` sends a FormData as multipart with a boundary only it knows. A `Content-Type` without
125
136
  // that boundary, set by hand, replaced the platform's, and the server could not split the parts
126
137
  // (upstream #1214), so it goes and the platform sets its own (ADR-013, decision 10).
127
- if (serializedBody instanceof FormData && !BOUNDARY_PARAMETER_RE.test(finalHeaders.get("Content-Type"))) {
138
+ if (
139
+ serializedBody instanceof FormData &&
140
+ !BOUNDARY_PARAMETER_RE.test(finalHeaders.get("Content-Type"))
141
+ ) {
128
142
  finalHeaders.delete("Content-Type");
129
143
  }
130
144
 
@@ -142,7 +156,12 @@ export default function createClient(clientOptions) {
142
156
  let id;
143
157
  let options;
144
158
  let request = new Request(
145
- createFinalURL(schemaPath, { baseUrl: finalBaseUrl, params, querySerializer, pathSerializer }),
159
+ createFinalURL(schemaPath, {
160
+ baseUrl: finalBaseUrl,
161
+ params,
162
+ querySerializer,
163
+ pathSerializer,
164
+ }),
146
165
  requestInit,
147
166
  );
148
167
  let response;
@@ -182,7 +201,9 @@ export default function createClient(clientOptions) {
182
201
  response = result;
183
202
  break;
184
203
  } else {
185
- throw new Error("onRequest: must return new Request() or Response() when modifying the request");
204
+ throw new Error(
205
+ "onRequest: must return new Request() or Response() when modifying the request",
206
+ );
186
207
  }
187
208
  }
188
209
  }
@@ -250,7 +271,9 @@ export default function createClient(clientOptions) {
250
271
  });
251
272
  if (result) {
252
273
  if (!isResponse(result)) {
253
- throw new Error("onResponse: must return new Response() when modifying the response");
274
+ throw new Error(
275
+ "onResponse: must return new Response() when modifying the response",
276
+ );
254
277
  }
255
278
  response = result;
256
279
  }
@@ -278,7 +301,8 @@ export default function createClient(clientOptions) {
278
301
  }
279
302
 
280
303
  if (parseAs === "json" && !contentLength) {
281
- // use text() when no content-length is provided to avoid errors parsing empty bodies (200 with no content)
304
+ // use text() when no content-length is provided to avoid errors parsing empty bodies
305
+ // (200 with no content)
282
306
  const raw = await response.text();
283
307
  return raw ? JSON.parse(raw) : undefined;
284
308
  }
@@ -350,7 +374,9 @@ export default function createClient(clientOptions) {
350
374
  continue;
351
375
  }
352
376
  if (typeof m !== "object" || !("onRequest" in m || "onResponse" in m || "onError" in m)) {
353
- throw new Error("Middleware must be an object with one of `onRequest()`, `onResponse()` or `onError()`");
377
+ throw new Error(
378
+ "Middleware must be an object with one of `onRequest()`, `onResponse()` or `onError()`",
379
+ );
354
380
  }
355
381
  globalMiddlewares.push(m);
356
382
  }
@@ -461,7 +487,9 @@ function isHeaders(value) {
461
487
  }
462
488
 
463
489
  function isRequest(value) {
464
- return typeof value?.method === "string" && typeof value.url === "string" && isHeaders(value.headers);
490
+ return (
491
+ typeof value?.method === "string" && typeof value.url === "string" && isHeaders(value.headers)
492
+ );
465
493
  }
466
494
 
467
495
  function isResponse(value) {
@@ -496,6 +524,7 @@ export function serializePrimitiveParam(name, value, options) {
496
524
  * Serialize object param (shallow only)
497
525
  * @type {import("./index.js").serializeObjectParam}
498
526
  */
527
+ // biome-ignore lint/complexity/noExcessiveLinesPerFunction: inherited, kept until 1.1 (ADR-036)
499
528
  export function serializeObjectParam(name, value, options) {
500
529
  if (!value || typeof value !== "object") {
501
530
  return "";
@@ -511,9 +540,13 @@ export function serializeObjectParam(name, value, options) {
511
540
  // explode: false
512
541
  if (options.style !== "deepObject" && options.explode === false) {
513
542
  for (const k in value) {
514
- values.push(encodeName(k), options.allowReserved === true ? value[k] : encodeURIComponent(value[k]));
543
+ values.push(
544
+ encodeName(k),
545
+ options.allowReserved === true ? value[k] : encodeURIComponent(value[k]),
546
+ );
515
547
  }
516
- const final = values.join(","); // note: values are always joined by comma in explode: false (but joiner can prefix)
548
+ // note: values are always joined by comma in explode: false (but joiner can prefix)
549
+ const final = values.join(",");
517
550
  switch (options.style) {
518
551
  case "form": {
519
552
  return `${name}=${final}`;
@@ -543,6 +576,7 @@ export function serializeObjectParam(name, value, options) {
543
576
  * Serialize array param (shallow only)
544
577
  * @type {import("./index.js").serializeArrayParam}
545
578
  */
579
+ // biome-ignore lint/complexity/noExcessiveLinesPerFunction: inherited, kept until 1.1 (ADR-036)
546
580
  export function serializeArrayParam(name, value, options) {
547
581
  if (!Array.isArray(value)) {
548
582
  return "";
@@ -550,8 +584,11 @@ export function serializeArrayParam(name, value, options) {
550
584
 
551
585
  // explode: false
552
586
  if (options.explode === false) {
553
- const joiner = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[options.style] || ","; // note: for arrays, joiners vary wildly based on style + explode behavior
554
- const final = (options.allowReserved === true ? value : value.map((v) => encodeURIComponent(v))).join(joiner);
587
+ // note: for arrays, joiners vary wildly based on style + explode behavior
588
+ const joiner = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[options.style] || ",";
589
+ const final = (
590
+ options.allowReserved === true ? value : value.map((v) => encodeURIComponent(v))
591
+ ).join(joiner);
555
592
  switch (options.style) {
556
593
  case "simple": {
557
594
  return final;
@@ -636,6 +673,7 @@ export function createQuerySerializer(options) {
636
673
  * @type {import("./index.js").defaultPathSerializer}
637
674
  * @see https://swagger.io/docs/specification/serialization/#path
638
675
  */
676
+ // biome-ignore lint/complexity/noExcessiveLinesPerFunction: inherited, kept until 1.1 (ADR-036)
639
677
  export function defaultPathSerializer(pathname, pathParams) {
640
678
  let nextURL = pathname;
641
679
  for (const match of pathname.match(PATH_PARAM_RE) ?? []) {
@@ -657,24 +695,31 @@ export function defaultPathSerializer(pathname, pathParams) {
657
695
  continue;
658
696
  }
659
697
  const value = pathParams[name];
698
+ let serialized;
660
699
  if (Array.isArray(value)) {
661
- nextURL = nextURL.replace(match, serializeArrayParam(name, value, { style, explode }));
662
- continue;
663
- }
664
- if (typeof value === "object") {
665
- nextURL = nextURL.replace(match, serializeObjectParam(name, value, { style, explode }));
666
- continue;
667
- }
668
- if (style === "matrix") {
669
- nextURL = nextURL.replace(match, `;${serializePrimitiveParam(name, value)}`);
670
- continue;
700
+ serialized = serializeArrayParam(name, value, { style, explode });
701
+ } else if (typeof value === "object") {
702
+ serialized = serializeObjectParam(name, value, { style, explode });
703
+ } else if (style === "matrix") {
704
+ serialized = `;${serializePrimitiveParam(name, value)}`;
705
+ } else {
706
+ serialized = style === "label" ? `.${encodeURIComponent(value)}` : encodeURIComponent(value);
671
707
  }
672
- nextURL = nextURL.replace(match, style === "label" ? `.${encodeURIComponent(value)}` : encodeURIComponent(value));
708
+ // A function: a string replacement reads `$'`, `$&` and the like, which object keys keep.
709
+ nextURL = nextURL.replace(match, () => serialized);
673
710
  }
674
- // `encodeURIComponent` keeps dots, so a value `..` would move the request up a path level (ADR-013).
675
- if (DOT_SEGMENT_RE.test(nextURL)) {
711
+ const path = nextURL.split(QUERY_OR_FRAGMENT_RE, 1)[0];
712
+ // `encodeURIComponent` keeps dots, so a value `..` would move the request up a path level
713
+ // (ADR-013).
714
+ // Only the path is checked: before a query (`/a/{id}?v=2`), `$` did not match after `..`.
715
+ if (DOT_SEGMENT_RE.test(path)) {
676
716
  throw new Error(`Dot path segment in ${nextURL}`);
677
717
  }
718
+ // `//host/…` is a protocol-relative URL: with a relative baseUrl a browser sends it to that
719
+ // host.
720
+ if (path.startsWith("//")) {
721
+ throw new Error(`Empty first path segment in ${nextURL}`);
722
+ }
678
723
  return nextURL;
679
724
  }
680
725
 
@@ -694,15 +739,24 @@ function isRawBody(body) {
694
739
 
695
740
  /**
696
741
  * A form body. A nested object has no standard form encoding (servers read `a[b]`, `a.b` or JSON),
697
- * and `URLSearchParams` wrote it as `[object Object]` (ADR-013, decision 7).
742
+ * and `URLSearchParams` wrote it as `[object Object]` (ADR-013, decision 7). `null` and `undefined`
743
+ * fields are left out, as in a multipart body; `URLSearchParams` sent them as the text "null" and
744
+ * "undefined" (ADR-030). A string or a list of pairs is form data already.
698
745
  */
699
746
  function formBody(body) {
700
- for (const value of Object.values(body)) {
747
+ if (!body || typeof body !== "object" || Array.isArray(body)) {
748
+ return new URLSearchParams(body).toString();
749
+ }
750
+ const form = new URLSearchParams();
751
+ for (const [name, value] of Object.entries(body)) {
701
752
  if (value && typeof value === "object" && !Array.isArray(value)) {
702
753
  throw new Error(NESTED_FORM_VALUE_MESSAGE);
703
754
  }
755
+ if (value != null) {
756
+ form.append(name, value);
757
+ }
704
758
  }
705
- return new URLSearchParams(body).toString();
759
+ return form.toString();
706
760
  }
707
761
 
708
762
  /**
@@ -735,14 +789,12 @@ export function defaultBodySerializer(body, headers) {
735
789
  if (isRawBody(body)) {
736
790
  return body;
737
791
  }
738
- const contentType = headers
739
- ? // `typeof`, not `instanceof Function`: headers from another realm (iframe, `node:vm`) have a
740
- // `get` that is not an instance of this realm's Function (upstream #2637).
741
- typeof headers.get === "function"
742
- ? (headers.get("Content-Type") ?? headers.get("content-type"))
743
- : (headers["Content-Type"] ?? headers["content-type"])
744
- : undefined;
745
- if (contentType === FORM_MEDIA_TYPE) {
792
+ // Headers with a `get` are read directly: headers from another realm (iframe, `node:vm`) may have
793
+ // nothing else (upstream #2637). A record or a list of pairs is read through `Headers`, which
794
+ // matches the name in any letter case.
795
+ const contentType =
796
+ headers && (isHeaders(headers) ? headers : mergeHeaders(headers)).get("Content-Type");
797
+ if (FORM_MEDIA_TYPE_RE.test(contentType)) {
746
798
  return formBody(body);
747
799
  }
748
800
  if (body && typeof body === "object" && MULTIPART_MEDIA_TYPE_RE.test(contentType)) {
@@ -786,7 +838,12 @@ export function mergeHeaders(...allHeaders) {
786
838
  if (!h || typeof h !== "object") {
787
839
  continue;
788
840
  }
789
- const iterator = isHeaders(h) ? h.entries() : Object.entries(h);
841
+ // A list of pairs (`[["x-t", "1"]]`) is a `HeadersInit` too; `Headers` joins a repeated name.
842
+ const iterator = isHeaders(h)
843
+ ? h.entries()
844
+ : Symbol.iterator in h
845
+ ? new Headers(h).entries()
846
+ : Object.entries(h);
790
847
  for (const [k, v] of iterator) {
791
848
  if (v === null) {
792
849
  finalHeaders.delete(k);