File tree Expand file tree Collapse file tree 8 files changed +99
-46
lines changed Expand file tree Collapse file tree 8 files changed +99
-46
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @hey-api/openapi-ts ' : patch
3
+ ---
4
+
5
+ fix: allow passing fetch options to the request resolving a specification
Original file line number Diff line number Diff line change @@ -17,6 +17,11 @@ export default defineConfig(() => {
17
17
input : {
18
18
branch : 'main' ,
19
19
// exclude: '^#/components/schemas/ModelWithCircularReference$',
20
+ // fetch: {
21
+ // headers: {
22
+ // 'x-foo': 'bar',
23
+ // },
24
+ // },
20
25
// include:
21
26
// '^(#/components/schemas/import|#/paths/api/v{api-version}/simple/options)$',
22
27
organization : 'hey-api' ,
Original file line number Diff line number Diff line change 78
78
"node" : " ^18.18.0 || ^20.9.0 || >=22.10.0"
79
79
},
80
80
"dependencies" : {
81
- "@hey-api/json-schema-ref-parser" : " 1.0.3 " ,
81
+ "@hey-api/json-schema-ref-parser" : " 1.0.4 " ,
82
82
"c12" : " 2.0.1" ,
83
83
"commander" : " 13.0.0" ,
84
84
"handlebars" : " 4.7.8"
Original file line number Diff line number Diff line change @@ -193,6 +193,7 @@ export const createClient = async ({
193
193
194
194
Performance . start ( 'spec' ) ;
195
195
const { data, error, response } = await getSpec ( {
196
+ fetchOptions : config . input . fetch ,
196
197
inputPath : inputPath . path ,
197
198
timeout,
198
199
watch,
Original file line number Diff line number Diff line change 5
5
sendRequest ,
6
6
} from '@hey-api/json-schema-ref-parser' ;
7
7
8
+ import { mergeHeaders } from './mergeHeaders' ;
8
9
import type { Config } from './types/config' ;
9
10
import type { WatchValues } from './types/types' ;
10
11
@@ -21,10 +22,12 @@ interface SpecError {
21
22
}
22
23
23
24
export const getSpec = async ( {
25
+ fetchOptions,
24
26
inputPath,
25
27
timeout,
26
28
watch,
27
29
} : {
30
+ fetchOptions ?: RequestInit ;
28
31
inputPath : Config [ 'input' ] [ 'path' ] ;
29
32
timeout : number ;
30
33
watch : WatchValues ;
@@ -42,9 +45,10 @@ export const getSpec = async ({
42
45
if ( watch . lastValue && watch . isHeadMethodSupported !== false ) {
43
46
try {
44
47
const request = await sendRequest ( {
45
- init : {
46
- headers : watch . headers ,
48
+ fetchOptions : {
47
49
method : 'HEAD' ,
50
+ ...fetchOptions ,
51
+ headers : mergeHeaders ( fetchOptions ?. headers , watch . headers ) ,
48
52
} ,
49
53
timeout,
50
54
url : resolvedInput . path ,
@@ -118,8 +122,9 @@ export const getSpec = async ({
118
122
119
123
try {
120
124
const request = await sendRequest ( {
121
- init : {
125
+ fetchOptions : {
122
126
method : 'GET' ,
127
+ ...fetchOptions ,
123
128
} ,
124
129
timeout,
125
130
url : resolvedInput . path ,
Original file line number Diff line number Diff line change
1
+ // copy-pasted from @hey-api/client-fetch
2
+ export const mergeHeaders = (
3
+ ...headers : Array < RequestInit [ 'headers' ] | undefined >
4
+ ) : Headers => {
5
+ const mergedHeaders = new Headers ( ) ;
6
+ for ( const header of headers ) {
7
+ if ( ! header || typeof header !== 'object' ) {
8
+ continue ;
9
+ }
10
+
11
+ const iterator =
12
+ header instanceof Headers ? header . entries ( ) : Object . entries ( header ) ;
13
+
14
+ for ( const [ key , value ] of iterator ) {
15
+ if ( value === null ) {
16
+ mergedHeaders . delete ( key ) ;
17
+ } else if ( Array . isArray ( value ) ) {
18
+ for ( const v of value ) {
19
+ mergedHeaders . append ( key , v as string ) ;
20
+ }
21
+ } else if ( value !== undefined ) {
22
+ // assume object headers are meant to be JSON stringified, i.e. their
23
+ // content value in OpenAPI specification is 'application/json'
24
+ mergedHeaders . set (
25
+ key ,
26
+ typeof value === 'object' ? JSON . stringify ( value ) : ( value as string ) ,
27
+ ) ;
28
+ }
29
+ }
30
+ }
31
+ return mergedHeaders ;
32
+ } ;
Original file line number Diff line number Diff line change @@ -49,6 +49,11 @@ interface Input {
49
49
* schema: '^#/components/schemas/Foo$'
50
50
*/
51
51
exclude ?: string ;
52
+ /**
53
+ * You pass any valid Fetch API options to the request for fetching your
54
+ * specification. This is useful if your file is behind auth for example.
55
+ */
56
+ fetch ?: RequestInit ;
52
57
/**
53
58
* Process only parts matching the regular expression. You can select both
54
59
* operations and components by reference within the bundled input. In
@@ -133,7 +138,7 @@ export interface UserConfig {
133
138
input :
134
139
| 'https://get.heyapi.dev/<organization>/<project>'
135
140
| ( string & { } )
136
- | Record < string , unknown >
141
+ | ( Record < string , unknown > & { path ?: never } )
137
142
| Input ;
138
143
/**
139
144
* The relative location of the logs folder
You can’t perform that action at this time.
0 commit comments