11import { stringify as qsStringify , type IStringifyOptions } from 'qs' ;
22import { fetch , FormData } from '@whatwg-node/fetch' ;
33import { HTTPMethod } from '../typed-fetch.js' ;
4- import { OpenAPIDocument , Router } from '../types.js' ;
4+ import { OpenAPIDocument , Router , SecurityScheme } from '../types.js' ;
55import { createClientTypedResponsePromise } from './clientResponse.js' ;
66import {
77 ClientMethod ,
@@ -10,6 +10,7 @@ import {
1010 ClientPlugin ,
1111 ClientRequestParams ,
1212 OASClient ,
13+ OASSecurityParams ,
1314 OnFetchHook ,
1415 OnRequestInitHook ,
1516 OnResponseHook ,
@@ -50,6 +51,33 @@ function useValidationErrors(): ClientPlugin {
5051
5152const EMPTY_OBJECT = { } ;
5253
54+ /**
55+ * Create a client for an OpenAPI document
56+ * You need to pass the imported OpenAPI document as a generic
57+ *
58+ * We recommend using the `NormalizeOAS` type to normalize the OpenAPI document
59+ *
60+ * @see https://the-guild.dev/openapi/fets/client/quick-start#usage-with-existing-rest-api
61+ *
62+ * @example
63+ * ```ts
64+ * import { createClient, type NormalizeOAS } from 'fets';
65+ * import type oas from './oas.ts';
66+ *
67+ * const client = createClient<NormalizeOAS<typeof oas>>({});
68+ * ```
69+ */
70+ export function createClient <
71+ const TOAS extends OpenAPIDocument & {
72+ components : { securitySchemes : Record < string , SecurityScheme > } ;
73+ } ,
74+ > (
75+ options : Omit < ClientOptionsWithStrictEndpoint < TOAS > , 'globalParams' > & {
76+ globalParams : OASSecurityParams <
77+ TOAS [ 'components' ] [ 'securitySchemes' ] [ keyof TOAS [ 'components' ] [ 'securitySchemes' ] ]
78+ > ;
79+ } ,
80+ ) : OASClient < TOAS , false > ;
5381/**
5482 * Create a client for an OpenAPI document
5583 * You need to pass the imported OpenAPI document as a generic
@@ -77,7 +105,12 @@ export function createClient<const TOAS extends OpenAPIDocument>(
77105export function createClient < const TRouter extends Router < any , any , any > > (
78106 options : ClientOptions ,
79107) : TRouter [ '__client' ] ;
80- export function createClient ( { endpoint, fetchFn = fetch , plugins = [ ] } : ClientOptions ) {
108+ export function createClient ( {
109+ endpoint,
110+ fetchFn = fetch ,
111+ plugins = [ ] ,
112+ globalParams,
113+ } : ClientOptions ) {
81114 plugins . unshift ( useValidationErrors ( ) ) ;
82115 const onRequestInitHooks : OnRequestInitHook [ ] = [ ] ;
83116 const onFetchHooks : OnFetchHook [ ] = [ ] ;
@@ -98,6 +131,44 @@ export function createClient({ endpoint, fetchFn = fetch, plugins = [] }: Client
98131 return new Proxy ( EMPTY_OBJECT , {
99132 get ( _target , method : HTTPMethod ) : ClientMethod {
100133 async function clientMethod ( requestParams : ClientRequestParams = { } ) {
134+ // Merge globalParams with the current requestParams
135+ if ( globalParams ?. headers ) {
136+ requestParams . headers = {
137+ ...globalParams . headers ,
138+ ...requestParams . headers ,
139+ } ;
140+ }
141+ if ( globalParams ?. query ) {
142+ requestParams . query = {
143+ ...globalParams . query ,
144+ ...requestParams . query ,
145+ } ;
146+ }
147+ if ( globalParams ?. params ) {
148+ requestParams . params = {
149+ ...globalParams . params ,
150+ ...requestParams . params ,
151+ } ;
152+ }
153+ if ( globalParams ?. json ) {
154+ requestParams . json = {
155+ ...globalParams . json ,
156+ ...requestParams . json ,
157+ } ;
158+ }
159+ if ( globalParams ?. formData ) {
160+ requestParams . formData = {
161+ ...globalParams . formData ,
162+ ...requestParams . formData ,
163+ } ;
164+ }
165+ if ( globalParams ?. formUrlEncoded ) {
166+ requestParams . formUrlEncoded = {
167+ ...globalParams . formUrlEncoded ,
168+ ...requestParams . formUrlEncoded ,
169+ } ;
170+ }
171+
101172 const {
102173 headers = { } ,
103174 params : paramsBody ,
0 commit comments