fets 0.6.7 → 0.6.8
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.
|
@@ -34,7 +34,7 @@ function useValidationErrors() {
|
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
36
|
const EMPTY_OBJECT = {};
|
|
37
|
-
function createClient({ endpoint, fetchFn = fetch_1.fetch, plugins = [] }) {
|
|
37
|
+
function createClient({ endpoint, fetchFn = fetch_1.fetch, plugins = [], globalParams, }) {
|
|
38
38
|
plugins.unshift(useValidationErrors());
|
|
39
39
|
const onRequestInitHooks = [];
|
|
40
40
|
const onFetchHooks = [];
|
|
@@ -55,6 +55,43 @@ function createClient({ endpoint, fetchFn = fetch_1.fetch, plugins = [] }) {
|
|
|
55
55
|
return new Proxy(EMPTY_OBJECT, {
|
|
56
56
|
get(_target, method) {
|
|
57
57
|
async function clientMethod(requestParams = {}) {
|
|
58
|
+
// Merge globalParams with the current requestParams
|
|
59
|
+
if (globalParams?.headers) {
|
|
60
|
+
requestParams.headers = {
|
|
61
|
+
...globalParams.headers,
|
|
62
|
+
...requestParams.headers,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
if (globalParams?.query) {
|
|
66
|
+
requestParams.query = {
|
|
67
|
+
...globalParams.query,
|
|
68
|
+
...requestParams.query,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
if (globalParams?.params) {
|
|
72
|
+
requestParams.params = {
|
|
73
|
+
...globalParams.params,
|
|
74
|
+
...requestParams.params,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (globalParams?.json) {
|
|
78
|
+
requestParams.json = {
|
|
79
|
+
...globalParams.json,
|
|
80
|
+
...requestParams.json,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
if (globalParams?.formData) {
|
|
84
|
+
requestParams.formData = {
|
|
85
|
+
...globalParams.formData,
|
|
86
|
+
...requestParams.formData,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
if (globalParams?.formUrlEncoded) {
|
|
90
|
+
requestParams.formUrlEncoded = {
|
|
91
|
+
...globalParams.formUrlEncoded,
|
|
92
|
+
...requestParams.formUrlEncoded,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
58
95
|
const { headers = {}, params: paramsBody, json: jsonBody, formData: formDataBody, formUrlEncoded: formUrlEncodedBody, query: queryBody, ...requestInitByUser } = requestParams;
|
|
59
96
|
if (paramsBody) {
|
|
60
97
|
for (const pathParamKey in paramsBody) {
|
|
@@ -30,7 +30,7 @@ function useValidationErrors() {
|
|
|
30
30
|
};
|
|
31
31
|
}
|
|
32
32
|
const EMPTY_OBJECT = {};
|
|
33
|
-
export function createClient({ endpoint, fetchFn = fetch, plugins = [] }) {
|
|
33
|
+
export function createClient({ endpoint, fetchFn = fetch, plugins = [], globalParams, }) {
|
|
34
34
|
plugins.unshift(useValidationErrors());
|
|
35
35
|
const onRequestInitHooks = [];
|
|
36
36
|
const onFetchHooks = [];
|
|
@@ -51,6 +51,43 @@ export function createClient({ endpoint, fetchFn = fetch, plugins = [] }) {
|
|
|
51
51
|
return new Proxy(EMPTY_OBJECT, {
|
|
52
52
|
get(_target, method) {
|
|
53
53
|
async function clientMethod(requestParams = {}) {
|
|
54
|
+
// Merge globalParams with the current requestParams
|
|
55
|
+
if (globalParams?.headers) {
|
|
56
|
+
requestParams.headers = {
|
|
57
|
+
...globalParams.headers,
|
|
58
|
+
...requestParams.headers,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
if (globalParams?.query) {
|
|
62
|
+
requestParams.query = {
|
|
63
|
+
...globalParams.query,
|
|
64
|
+
...requestParams.query,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if (globalParams?.params) {
|
|
68
|
+
requestParams.params = {
|
|
69
|
+
...globalParams.params,
|
|
70
|
+
...requestParams.params,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
if (globalParams?.json) {
|
|
74
|
+
requestParams.json = {
|
|
75
|
+
...globalParams.json,
|
|
76
|
+
...requestParams.json,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
if (globalParams?.formData) {
|
|
80
|
+
requestParams.formData = {
|
|
81
|
+
...globalParams.formData,
|
|
82
|
+
...requestParams.formData,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
if (globalParams?.formUrlEncoded) {
|
|
86
|
+
requestParams.formUrlEncoded = {
|
|
87
|
+
...globalParams.formUrlEncoded,
|
|
88
|
+
...requestParams.formUrlEncoded,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
54
91
|
const { headers = {}, params: paramsBody, json: jsonBody, formData: formDataBody, formUrlEncoded: formUrlEncodedBody, query: queryBody, ...requestInitByUser } = requestParams;
|
|
55
92
|
if (paramsBody) {
|
|
56
93
|
for (const pathParamKey in paramsBody) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { HTTPMethod } from '../typed-fetch.cjs';
|
|
2
|
-
import { OpenAPIDocument, Router } from '../types.cjs';
|
|
3
|
-
import { ClientOptions, ClientOptionsWithStrictEndpoint, OASClient } from './types.cjs';
|
|
2
|
+
import { OpenAPIDocument, Router, SecurityScheme } from '../types.cjs';
|
|
3
|
+
import { ClientOptions, ClientOptionsWithStrictEndpoint, OASClient, OASSecurityParams } from './types.cjs';
|
|
4
4
|
export declare class ClientValidationError extends Error implements AggregateError {
|
|
5
5
|
readonly path: string;
|
|
6
6
|
readonly method: HTTPMethod;
|
|
@@ -9,6 +9,29 @@ export declare class ClientValidationError extends Error implements AggregateErr
|
|
|
9
9
|
constructor(path: string, method: HTTPMethod, errors: any[], response: Response);
|
|
10
10
|
[Symbol.iterator](): IterableIterator<any>;
|
|
11
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Create a client for an OpenAPI document
|
|
14
|
+
* You need to pass the imported OpenAPI document as a generic
|
|
15
|
+
*
|
|
16
|
+
* We recommend using the `NormalizeOAS` type to normalize the OpenAPI document
|
|
17
|
+
*
|
|
18
|
+
* @see https://the-guild.dev/openapi/fets/client/quick-start#usage-with-existing-rest-api
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { createClient, type NormalizeOAS } from 'fets';
|
|
23
|
+
* import type oas from './oas.ts';
|
|
24
|
+
*
|
|
25
|
+
* const client = createClient<NormalizeOAS<typeof oas>>({});
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function createClient<const TOAS extends OpenAPIDocument & {
|
|
29
|
+
components: {
|
|
30
|
+
securitySchemes: Record<string, SecurityScheme>;
|
|
31
|
+
};
|
|
32
|
+
}>(options: Omit<ClientOptionsWithStrictEndpoint<TOAS>, 'globalParams'> & {
|
|
33
|
+
globalParams: OASSecurityParams<TOAS['components']['securitySchemes'][keyof TOAS['components']['securitySchemes']]>;
|
|
34
|
+
}): OASClient<TOAS, false>;
|
|
12
35
|
/**
|
|
13
36
|
* Create a client for an OpenAPI document
|
|
14
37
|
* You need to pass the imported OpenAPI document as a generic
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { HTTPMethod } from '../typed-fetch.js';
|
|
2
|
-
import { OpenAPIDocument, Router } from '../types.js';
|
|
3
|
-
import { ClientOptions, ClientOptionsWithStrictEndpoint, OASClient } from './types.js';
|
|
2
|
+
import { OpenAPIDocument, Router, SecurityScheme } from '../types.js';
|
|
3
|
+
import { ClientOptions, ClientOptionsWithStrictEndpoint, OASClient, OASSecurityParams } from './types.js';
|
|
4
4
|
export declare class ClientValidationError extends Error implements AggregateError {
|
|
5
5
|
readonly path: string;
|
|
6
6
|
readonly method: HTTPMethod;
|
|
@@ -9,6 +9,29 @@ export declare class ClientValidationError extends Error implements AggregateErr
|
|
|
9
9
|
constructor(path: string, method: HTTPMethod, errors: any[], response: Response);
|
|
10
10
|
[Symbol.iterator](): IterableIterator<any>;
|
|
11
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Create a client for an OpenAPI document
|
|
14
|
+
* You need to pass the imported OpenAPI document as a generic
|
|
15
|
+
*
|
|
16
|
+
* We recommend using the `NormalizeOAS` type to normalize the OpenAPI document
|
|
17
|
+
*
|
|
18
|
+
* @see https://the-guild.dev/openapi/fets/client/quick-start#usage-with-existing-rest-api
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { createClient, type NormalizeOAS } from 'fets';
|
|
23
|
+
* import type oas from './oas.ts';
|
|
24
|
+
*
|
|
25
|
+
* const client = createClient<NormalizeOAS<typeof oas>>({});
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function createClient<const TOAS extends OpenAPIDocument & {
|
|
29
|
+
components: {
|
|
30
|
+
securitySchemes: Record<string, SecurityScheme>;
|
|
31
|
+
};
|
|
32
|
+
}>(options: Omit<ClientOptionsWithStrictEndpoint<TOAS>, 'globalParams'> & {
|
|
33
|
+
globalParams: OASSecurityParams<TOAS['components']['securitySchemes'][keyof TOAS['components']['securitySchemes']]>;
|
|
34
|
+
}): OASClient<TOAS, false>;
|
|
12
35
|
/**
|
|
13
36
|
* Create a client for an OpenAPI document
|
|
14
37
|
* You need to pass the imported OpenAPI document as a generic
|
|
@@ -88,9 +88,9 @@ export type OASParamMap<TParameters extends {
|
|
|
88
88
|
Tuples.Map<OASParamToRequestParam<TParameters>>,
|
|
89
89
|
Tuples.ToIntersection
|
|
90
90
|
]>;
|
|
91
|
-
export type OASClient<TOAS extends OpenAPIDocument> = {
|
|
91
|
+
export type OASClient<TOAS extends OpenAPIDocument, TAuthParamsRequired extends boolean = true> = {
|
|
92
92
|
[TPath in keyof OASPathMap<TOAS>]: {
|
|
93
|
-
[TMethod in keyof OASMethodMap<TOAS, TPath>]: OASRequestParams<TOAS, TPath, TMethod> extends {
|
|
93
|
+
[TMethod in keyof OASMethodMap<TOAS, TPath>]: OASRequestParams<TOAS, TPath, TMethod, TAuthParamsRequired> extends {
|
|
94
94
|
json: {};
|
|
95
95
|
} | {
|
|
96
96
|
params: {};
|
|
@@ -98,7 +98,7 @@ export type OASClient<TOAS extends OpenAPIDocument> = {
|
|
|
98
98
|
headers: {};
|
|
99
99
|
} | {
|
|
100
100
|
query: {};
|
|
101
|
-
} ? (requestParams: Simplify<OASRequestParams<TOAS, TPath, TMethod>> & ClientRequestInit) => ClientTypedResponsePromise<OASResponse<TOAS, TPath, TMethod>> : (requestParams?: Simplify<OASRequestParams<TOAS, TPath, TMethod>> & ClientRequestInit) => ClientTypedResponsePromise<OASResponse<TOAS, TPath, TMethod>>;
|
|
101
|
+
} ? (requestParams: Simplify<OASRequestParams<TOAS, TPath, TMethod, TAuthParamsRequired>> & ClientRequestInit) => ClientTypedResponsePromise<OASResponse<TOAS, TPath, TMethod>> : (requestParams?: Simplify<OASRequestParams<TOAS, TPath, TMethod, TAuthParamsRequired>> & ClientRequestInit) => ClientTypedResponsePromise<OASResponse<TOAS, TPath, TMethod>>;
|
|
102
102
|
};
|
|
103
103
|
} & OASOAuthPath<TOAS>;
|
|
104
104
|
export type OASModel<TOAS extends OpenAPIDocument, TName extends TOAS extends {
|
|
@@ -141,7 +141,7 @@ type FixExtraRequiredFields<T> = T extends {
|
|
|
141
141
|
} ? Omit<T, 'required'> & {
|
|
142
142
|
required: Call<Tuples.Filter<B.Extends<keyof T['properties']>>, T['required']>;
|
|
143
143
|
} : T;
|
|
144
|
-
export type OASRequestParams<TOAS extends OpenAPIDocument, TPath extends keyof OASPathMap<TOAS>, TMethod extends keyof OASMethodMap<TOAS, TPath
|
|
144
|
+
export type OASRequestParams<TOAS extends OpenAPIDocument, TPath extends keyof OASPathMap<TOAS>, TMethod extends keyof OASMethodMap<TOAS, TPath>, TAuthParamsRequired extends boolean = true> = (OASMethodMap<TOAS, TPath>[TMethod] extends {
|
|
145
145
|
requestBody: {
|
|
146
146
|
content: {
|
|
147
147
|
'application/json': {
|
|
@@ -236,7 +236,10 @@ export type OASRequestParams<TOAS extends OpenAPIDocument, TPath extends keyof O
|
|
|
236
236
|
* For example if path is `/todos/:id` and `params` is `{ id: '1' }`, the path will be `/todos/1`.
|
|
237
237
|
*/
|
|
238
238
|
params: Record<ExtractPathParamsWithPattern<TPath>, string | number | bigint | boolean>;
|
|
239
|
-
} : {}) & OASSecurityParamsBySecurityRef<TOAS, OASMethodMap<TOAS, TPath>[TMethod]> & OASSecurityParamsBySecurityRef<TOAS, TOAS
|
|
239
|
+
} : {}) & (TAuthParamsRequired extends true ? OASSecurityParamsBySecurityRef<TOAS, OASMethodMap<TOAS, TPath>[TMethod]> : DeepPartial<OASSecurityParamsBySecurityRef<TOAS, OASMethodMap<TOAS, TPath>[TMethod]>>) & (TAuthParamsRequired extends true ? OASSecurityParamsBySecurityRef<TOAS, TOAS> : DeepPartial<OASSecurityParamsBySecurityRef<TOAS, TOAS>>);
|
|
240
|
+
type DeepPartial<T> = T extends Record<string, any> ? {
|
|
241
|
+
[K in keyof T]?: DeepPartial<T[K]>;
|
|
242
|
+
} : T;
|
|
240
243
|
export type OASInput<TOAS extends OpenAPIDocument, TPath extends keyof OASPathMap<TOAS>, TMethod extends keyof OASMethodMap<TOAS, TPath>, TRequestType extends keyof OASRequestParams<TOAS, TPath, TMethod>> = OASRequestParams<TOAS, TPath, TMethod>[TRequestType];
|
|
241
244
|
export type OASOutput<TOAS extends OpenAPIDocument, TPath extends keyof OASPathMap<TOAS>, TMethod extends keyof OASMethodMap<TOAS, TPath>, TStatusCode extends keyof OASStatusMap<TOAS, TPath, TMethod> = 200> = FromSchema<OASJSONResponseSchema<TOAS, TPath, TMethod, TStatusCode>>;
|
|
242
245
|
export type OASComponentSchema<TOAS extends OpenAPIDocument, TName extends string> = TOAS extends {
|
|
@@ -263,6 +266,10 @@ export interface ClientOptions {
|
|
|
263
266
|
* @see https://the-guild.dev/openapi/fets/client/plugins
|
|
264
267
|
*/
|
|
265
268
|
plugins?: ClientPlugin[];
|
|
269
|
+
/**
|
|
270
|
+
* Global parameters
|
|
271
|
+
*/
|
|
272
|
+
globalParams?: ClientRequestParams;
|
|
266
273
|
}
|
|
267
274
|
export type ClientOptionsWithStrictEndpoint<TOAS extends OpenAPIDocument> = Omit<ClientOptions, 'endpoint'> & (TOAS extends {
|
|
268
275
|
servers: (infer TEndpoint extends string)[];
|
|
@@ -88,9 +88,9 @@ export type OASParamMap<TParameters extends {
|
|
|
88
88
|
Tuples.Map<OASParamToRequestParam<TParameters>>,
|
|
89
89
|
Tuples.ToIntersection
|
|
90
90
|
]>;
|
|
91
|
-
export type OASClient<TOAS extends OpenAPIDocument> = {
|
|
91
|
+
export type OASClient<TOAS extends OpenAPIDocument, TAuthParamsRequired extends boolean = true> = {
|
|
92
92
|
[TPath in keyof OASPathMap<TOAS>]: {
|
|
93
|
-
[TMethod in keyof OASMethodMap<TOAS, TPath>]: OASRequestParams<TOAS, TPath, TMethod> extends {
|
|
93
|
+
[TMethod in keyof OASMethodMap<TOAS, TPath>]: OASRequestParams<TOAS, TPath, TMethod, TAuthParamsRequired> extends {
|
|
94
94
|
json: {};
|
|
95
95
|
} | {
|
|
96
96
|
params: {};
|
|
@@ -98,7 +98,7 @@ export type OASClient<TOAS extends OpenAPIDocument> = {
|
|
|
98
98
|
headers: {};
|
|
99
99
|
} | {
|
|
100
100
|
query: {};
|
|
101
|
-
} ? (requestParams: Simplify<OASRequestParams<TOAS, TPath, TMethod>> & ClientRequestInit) => ClientTypedResponsePromise<OASResponse<TOAS, TPath, TMethod>> : (requestParams?: Simplify<OASRequestParams<TOAS, TPath, TMethod>> & ClientRequestInit) => ClientTypedResponsePromise<OASResponse<TOAS, TPath, TMethod>>;
|
|
101
|
+
} ? (requestParams: Simplify<OASRequestParams<TOAS, TPath, TMethod, TAuthParamsRequired>> & ClientRequestInit) => ClientTypedResponsePromise<OASResponse<TOAS, TPath, TMethod>> : (requestParams?: Simplify<OASRequestParams<TOAS, TPath, TMethod, TAuthParamsRequired>> & ClientRequestInit) => ClientTypedResponsePromise<OASResponse<TOAS, TPath, TMethod>>;
|
|
102
102
|
};
|
|
103
103
|
} & OASOAuthPath<TOAS>;
|
|
104
104
|
export type OASModel<TOAS extends OpenAPIDocument, TName extends TOAS extends {
|
|
@@ -141,7 +141,7 @@ type FixExtraRequiredFields<T> = T extends {
|
|
|
141
141
|
} ? Omit<T, 'required'> & {
|
|
142
142
|
required: Call<Tuples.Filter<B.Extends<keyof T['properties']>>, T['required']>;
|
|
143
143
|
} : T;
|
|
144
|
-
export type OASRequestParams<TOAS extends OpenAPIDocument, TPath extends keyof OASPathMap<TOAS>, TMethod extends keyof OASMethodMap<TOAS, TPath
|
|
144
|
+
export type OASRequestParams<TOAS extends OpenAPIDocument, TPath extends keyof OASPathMap<TOAS>, TMethod extends keyof OASMethodMap<TOAS, TPath>, TAuthParamsRequired extends boolean = true> = (OASMethodMap<TOAS, TPath>[TMethod] extends {
|
|
145
145
|
requestBody: {
|
|
146
146
|
content: {
|
|
147
147
|
'application/json': {
|
|
@@ -236,7 +236,10 @@ export type OASRequestParams<TOAS extends OpenAPIDocument, TPath extends keyof O
|
|
|
236
236
|
* For example if path is `/todos/:id` and `params` is `{ id: '1' }`, the path will be `/todos/1`.
|
|
237
237
|
*/
|
|
238
238
|
params: Record<ExtractPathParamsWithPattern<TPath>, string | number | bigint | boolean>;
|
|
239
|
-
} : {}) & OASSecurityParamsBySecurityRef<TOAS, OASMethodMap<TOAS, TPath>[TMethod]> & OASSecurityParamsBySecurityRef<TOAS, TOAS
|
|
239
|
+
} : {}) & (TAuthParamsRequired extends true ? OASSecurityParamsBySecurityRef<TOAS, OASMethodMap<TOAS, TPath>[TMethod]> : DeepPartial<OASSecurityParamsBySecurityRef<TOAS, OASMethodMap<TOAS, TPath>[TMethod]>>) & (TAuthParamsRequired extends true ? OASSecurityParamsBySecurityRef<TOAS, TOAS> : DeepPartial<OASSecurityParamsBySecurityRef<TOAS, TOAS>>);
|
|
240
|
+
type DeepPartial<T> = T extends Record<string, any> ? {
|
|
241
|
+
[K in keyof T]?: DeepPartial<T[K]>;
|
|
242
|
+
} : T;
|
|
240
243
|
export type OASInput<TOAS extends OpenAPIDocument, TPath extends keyof OASPathMap<TOAS>, TMethod extends keyof OASMethodMap<TOAS, TPath>, TRequestType extends keyof OASRequestParams<TOAS, TPath, TMethod>> = OASRequestParams<TOAS, TPath, TMethod>[TRequestType];
|
|
241
244
|
export type OASOutput<TOAS extends OpenAPIDocument, TPath extends keyof OASPathMap<TOAS>, TMethod extends keyof OASMethodMap<TOAS, TPath>, TStatusCode extends keyof OASStatusMap<TOAS, TPath, TMethod> = 200> = FromSchema<OASJSONResponseSchema<TOAS, TPath, TMethod, TStatusCode>>;
|
|
242
245
|
export type OASComponentSchema<TOAS extends OpenAPIDocument, TName extends string> = TOAS extends {
|
|
@@ -263,6 +266,10 @@ export interface ClientOptions {
|
|
|
263
266
|
* @see https://the-guild.dev/openapi/fets/client/plugins
|
|
264
267
|
*/
|
|
265
268
|
plugins?: ClientPlugin[];
|
|
269
|
+
/**
|
|
270
|
+
* Global parameters
|
|
271
|
+
*/
|
|
272
|
+
globalParams?: ClientRequestParams;
|
|
266
273
|
}
|
|
267
274
|
export type ClientOptionsWithStrictEndpoint<TOAS extends OpenAPIDocument> = Omit<ClientOptions, 'endpoint'> & (TOAS extends {
|
|
268
275
|
servers: (infer TEndpoint extends string)[];
|