fets 0.4.12 → 0.4.13

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.
@@ -1,7 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createClient = exports.ClientValidationError = void 0;
4
+ const qs_1 = require("qs");
4
5
  const fetch_1 = require("@whatwg-node/fetch");
6
+ const qsOptions = {
7
+ indices: false,
8
+ arrayFormat: 'repeat',
9
+ };
5
10
  class ClientValidationError extends Error {
6
11
  constructor(path, method, errors, response) {
7
12
  super(`Validation failed for ${method} ${path}`);
@@ -57,23 +62,6 @@ function createClient({ endpoint, fetchFn = fetch_1.fetch, plugins = [] }) {
57
62
  if (!path.startsWith('/') && !path.startsWith('http')) {
58
63
  path = `/${path}`;
59
64
  }
60
- let searchParams;
61
- if (requestParams?.query) {
62
- searchParams = new fetch_1.URLSearchParams();
63
- for (const queryParamKey in requestParams?.query || {}) {
64
- const value = requestParams?.query?.[queryParamKey];
65
- if (value) {
66
- if (Array.isArray(value)) {
67
- for (const v of value) {
68
- searchParams.append(queryParamKey, v);
69
- }
70
- }
71
- else {
72
- searchParams.append(queryParamKey, value);
73
- }
74
- }
75
- }
76
- }
77
65
  const requestInit = {
78
66
  method,
79
67
  headers: requestParams?.headers || {},
@@ -86,21 +74,7 @@ function createClient({ endpoint, fetchFn = fetch_1.fetch, plugins = [] }) {
86
74
  requestInit.body = requestParams.formData;
87
75
  }
88
76
  if (requestParams?.formUrlEncoded) {
89
- const urlSearchParams = new fetch_1.URLSearchParams();
90
- for (const key in requestParams.formUrlEncoded) {
91
- const value = requestParams.formUrlEncoded[key];
92
- if (value) {
93
- if (Array.isArray(value)) {
94
- for (const v of value) {
95
- urlSearchParams.append(key, v);
96
- }
97
- }
98
- else {
99
- urlSearchParams.append(key, value);
100
- }
101
- }
102
- }
103
- requestInit.body = urlSearchParams;
77
+ requestInit.body = (0, qs_1.stringify)(requestParams.formUrlEncoded, qsOptions);
104
78
  requestInit.headers['Content-Type'] = 'application/x-www-form-urlencoded';
105
79
  }
106
80
  let response;
@@ -119,12 +93,13 @@ function createClient({ endpoint, fetchFn = fetch_1.fetch, plugins = [] }) {
119
93
  if (endpoint && !path.startsWith('http')) {
120
94
  finalUrl = `${endpoint}${path}`;
121
95
  }
122
- if (searchParams) {
96
+ if (requestParams?.query) {
97
+ const searchParams = (0, qs_1.stringify)(requestParams.query, qsOptions);
123
98
  if (finalUrl.includes('?')) {
124
- finalUrl += '&' + searchParams.toString();
99
+ finalUrl += '&' + searchParams;
125
100
  }
126
101
  else {
127
- finalUrl += '?' + searchParams.toString();
102
+ finalUrl += '?' + searchParams;
128
103
  }
129
104
  }
130
105
  let currentFetchFn = fetchFn;
@@ -1,4 +1,9 @@
1
- import { fetch, URLSearchParams } from '@whatwg-node/fetch';
1
+ import { stringify as qsStringify } from 'qs';
2
+ import { fetch } from '@whatwg-node/fetch';
3
+ const qsOptions = {
4
+ indices: false,
5
+ arrayFormat: 'repeat',
6
+ };
2
7
  export class ClientValidationError extends Error {
3
8
  constructor(path, method, errors, response) {
4
9
  super(`Validation failed for ${method} ${path}`);
@@ -53,23 +58,6 @@ export function createClient({ endpoint, fetchFn = fetch, plugins = [] }) {
53
58
  if (!path.startsWith('/') && !path.startsWith('http')) {
54
59
  path = `/${path}`;
55
60
  }
56
- let searchParams;
57
- if (requestParams?.query) {
58
- searchParams = new URLSearchParams();
59
- for (const queryParamKey in requestParams?.query || {}) {
60
- const value = requestParams?.query?.[queryParamKey];
61
- if (value) {
62
- if (Array.isArray(value)) {
63
- for (const v of value) {
64
- searchParams.append(queryParamKey, v);
65
- }
66
- }
67
- else {
68
- searchParams.append(queryParamKey, value);
69
- }
70
- }
71
- }
72
- }
73
61
  const requestInit = {
74
62
  method,
75
63
  headers: requestParams?.headers || {},
@@ -82,21 +70,7 @@ export function createClient({ endpoint, fetchFn = fetch, plugins = [] }) {
82
70
  requestInit.body = requestParams.formData;
83
71
  }
84
72
  if (requestParams?.formUrlEncoded) {
85
- const urlSearchParams = new URLSearchParams();
86
- for (const key in requestParams.formUrlEncoded) {
87
- const value = requestParams.formUrlEncoded[key];
88
- if (value) {
89
- if (Array.isArray(value)) {
90
- for (const v of value) {
91
- urlSearchParams.append(key, v);
92
- }
93
- }
94
- else {
95
- urlSearchParams.append(key, value);
96
- }
97
- }
98
- }
99
- requestInit.body = urlSearchParams;
73
+ requestInit.body = qsStringify(requestParams.formUrlEncoded, qsOptions);
100
74
  requestInit.headers['Content-Type'] = 'application/x-www-form-urlencoded';
101
75
  }
102
76
  let response;
@@ -115,12 +89,13 @@ export function createClient({ endpoint, fetchFn = fetch, plugins = [] }) {
115
89
  if (endpoint && !path.startsWith('http')) {
116
90
  finalUrl = `${endpoint}${path}`;
117
91
  }
118
- if (searchParams) {
92
+ if (requestParams?.query) {
93
+ const searchParams = qsStringify(requestParams.query, qsOptions);
119
94
  if (finalUrl.includes('?')) {
120
- finalUrl += '&' + searchParams.toString();
95
+ finalUrl += '&' + searchParams;
121
96
  }
122
97
  else {
123
- finalUrl += '?' + searchParams.toString();
98
+ finalUrl += '?' + searchParams;
124
99
  }
125
100
  }
126
101
  let currentFetchFn = fetchFn;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fets",
3
- "version": "0.4.12",
3
+ "version": "0.4.13",
4
4
  "description": "TypeScript HTTP Framework focusing on e2e type-safety, easy setup, performance & great developer experience",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -12,6 +12,7 @@
12
12
  "ajv-formats": "^2.1.1",
13
13
  "hotscript": "^1.0.11",
14
14
  "json-schema-to-ts": "^2.9.1",
15
+ "qs": "^6.11.2",
15
16
  "ts-toolbelt": "^9.6.0",
16
17
  "tslib": "^2.3.1",
17
18
  "zod": "^3.21.4",
@@ -1,6 +1,6 @@
1
1
  import { HTTPMethod } from '../typed-fetch.cjs';
2
2
  import { OpenAPIDocument, Router } from '../types.cjs';
3
- import { ClientOptions, OASClient } from './types.cjs';
3
+ import { ClientOptions, ClientOptionsWithStrictEndpoint, OASClient } from './types.cjs';
4
4
  export declare class ClientValidationError extends Error implements AggregateError {
5
5
  readonly path: string;
6
6
  readonly method: HTTPMethod;
@@ -25,7 +25,7 @@ export declare class ClientValidationError extends Error implements AggregateErr
25
25
  * const client = createClient<NormalizeOAS<typeof oas>>({});
26
26
  * ```
27
27
  */
28
- export declare function createClient<TOAS extends OpenAPIDocument>(options: ClientOptions): OASClient<TOAS>;
28
+ export declare function createClient<TOAS extends OpenAPIDocument>(options: ClientOptionsWithStrictEndpoint<TOAS>): OASClient<TOAS>;
29
29
  /**
30
30
  * Create a client from a typed `Router`
31
31
  *
@@ -1,6 +1,6 @@
1
1
  import { HTTPMethod } from '../typed-fetch.js';
2
2
  import { OpenAPIDocument, Router } from '../types.js';
3
- import { ClientOptions, OASClient } from './types.js';
3
+ import { ClientOptions, ClientOptionsWithStrictEndpoint, OASClient } from './types.js';
4
4
  export declare class ClientValidationError extends Error implements AggregateError {
5
5
  readonly path: string;
6
6
  readonly method: HTTPMethod;
@@ -25,7 +25,7 @@ export declare class ClientValidationError extends Error implements AggregateErr
25
25
  * const client = createClient<NormalizeOAS<typeof oas>>({});
26
26
  * ```
27
27
  */
28
- export declare function createClient<TOAS extends OpenAPIDocument>(options: ClientOptions): OASClient<TOAS>;
28
+ export declare function createClient<TOAS extends OpenAPIDocument>(options: ClientOptionsWithStrictEndpoint<TOAS>): OASClient<TOAS>;
29
29
  /**
30
30
  * Create a client from a typed `Router`
31
31
  *
@@ -264,6 +264,15 @@ export interface ClientOptions {
264
264
  plugins?: ClientPlugin[];
265
265
  }
266
266
  export type ClientOptionsWithStrictEndpoint<TOAS extends OpenAPIDocument> = Omit<ClientOptions, 'endpoint'> & (TOAS extends {
267
+ servers: (infer TEndpoint extends string)[];
268
+ } ? {
269
+ /**
270
+ * The base URL of the API defined in the OAS document.
271
+ *
272
+ * @see https://swagger.io/docs/specification/api-host-and-base-path/
273
+ */
274
+ endpoint: TEndpoint;
275
+ } : TOAS extends {
267
276
  servers: {
268
277
  url: infer TEndpoint extends string;
269
278
  }[];
@@ -264,6 +264,15 @@ export interface ClientOptions {
264
264
  plugins?: ClientPlugin[];
265
265
  }
266
266
  export type ClientOptionsWithStrictEndpoint<TOAS extends OpenAPIDocument> = Omit<ClientOptions, 'endpoint'> & (TOAS extends {
267
+ servers: (infer TEndpoint extends string)[];
268
+ } ? {
269
+ /**
270
+ * The base URL of the API defined in the OAS document.
271
+ *
272
+ * @see https://swagger.io/docs/specification/api-host-and-base-path/
273
+ */
274
+ endpoint: TEndpoint;
275
+ } : TOAS extends {
267
276
  servers: {
268
277
  url: infer TEndpoint extends string;
269
278
  }[];
@@ -52,7 +52,7 @@ export type OpenAPIDocument = {
52
52
  info?: OpenAPIInfo;
53
53
  servers?: {
54
54
  url: string;
55
- }[];
55
+ }[] | string[];
56
56
  paths?: Record<string, OpenAPIPathObject>;
57
57
  components?: unknown;
58
58
  };
@@ -52,7 +52,7 @@ export type OpenAPIDocument = {
52
52
  info?: OpenAPIInfo;
53
53
  servers?: {
54
54
  url: string;
55
- }[];
55
+ }[] | string[];
56
56
  paths?: Record<string, OpenAPIPathObject>;
57
57
  components?: unknown;
58
58
  };