@@ -13,6 +13,150 @@ import type { Templates } from './utils/handlebars';
13
13
import { Performance } from './utils/performance' ;
14
14
import { postProcessClient } from './utils/postprocess' ;
15
15
16
+ const isPlatformPath = ( path : string ) =>
17
+ path . startsWith ( 'https://get.heyapi.dev' ) ;
18
+
19
+ export const compileInputPath = ( input : Config [ 'input' ] ) => {
20
+ const result : Pick <
21
+ Partial < Config [ 'input' ] > ,
22
+ | 'api_key'
23
+ | 'branch'
24
+ | 'commit_sha'
25
+ | 'organization'
26
+ | 'project'
27
+ | 'tags'
28
+ | 'version'
29
+ > &
30
+ Pick < Required < Config [ 'input' ] > , 'path' > = {
31
+ path : '' ,
32
+ } ;
33
+
34
+ if (
35
+ input . path &&
36
+ ( typeof input . path !== 'string' || ! isPlatformPath ( input . path ) )
37
+ ) {
38
+ result . path = input . path ;
39
+ return result ;
40
+ }
41
+
42
+ const [ basePath , baseQuery ] = input . path . split ( '?' ) ;
43
+ const queryParts = ( baseQuery || '' ) . split ( '&' ) ;
44
+ const queryPath = queryParts . map ( ( part ) => part . split ( '=' ) ) ;
45
+
46
+ let path = basePath || '' ;
47
+ if ( path . endsWith ( '/' ) ) {
48
+ path = path . slice ( 0 , path . length - 1 ) ;
49
+ }
50
+
51
+ const [ , pathUrl ] = path . split ( '://' ) ;
52
+ const [ baseUrl , organization , project ] = ( pathUrl || '' ) . split ( '/' ) ;
53
+ result . organization = organization || input . organization ;
54
+ result . project = project || input . project ;
55
+
56
+ const queryParams : Array < string > = [ ] ;
57
+
58
+ const kApiKey = 'api_key' ;
59
+ result . api_key =
60
+ queryPath . find ( ( [ key ] ) => key === kApiKey ) ?. [ 1 ] ||
61
+ input . api_key ||
62
+ process . env . HEY_API_TOKEN ;
63
+ if ( result . api_key ) {
64
+ queryParams . push ( `${ kApiKey } =${ result . api_key } ` ) ;
65
+ }
66
+
67
+ const kBranch = 'branch' ;
68
+ result . branch =
69
+ queryPath . find ( ( [ key ] ) => key === kBranch ) ?. [ 1 ] || input . branch ;
70
+ if ( result . branch ) {
71
+ queryParams . push ( `${ kBranch } =${ result . branch } ` ) ;
72
+ }
73
+
74
+ const kCommitSha = 'commit_sha' ;
75
+ result . commit_sha =
76
+ queryPath . find ( ( [ key ] ) => key === kCommitSha ) ?. [ 1 ] || input . commit_sha ;
77
+ if ( result . commit_sha ) {
78
+ queryParams . push ( `${ kCommitSha } =${ result . commit_sha } ` ) ;
79
+ }
80
+
81
+ const kTags = 'tags' ;
82
+ result . tags =
83
+ queryPath . find ( ( [ key ] ) => key === kTags ) ?. [ 1 ] ?. split ( ',' ) || input . tags ;
84
+ if ( result . tags ?. length ) {
85
+ queryParams . push ( `${ kTags } =${ result . tags . join ( ',' ) } ` ) ;
86
+ }
87
+
88
+ const kVersion = 'version' ;
89
+ result . version =
90
+ queryPath . find ( ( [ key ] ) => key === kVersion ) ?. [ 1 ] || input . version ;
91
+ if ( result . version ) {
92
+ queryParams . push ( `${ kVersion } =${ result . version } ` ) ;
93
+ }
94
+
95
+ if ( ! result . organization ) {
96
+ throw new Error (
97
+ '🚫 missing organization - from which Hey API platform organization do you want to generate your output?' ,
98
+ ) ;
99
+ }
100
+
101
+ if ( ! result . project ) {
102
+ throw new Error (
103
+ '🚫 missing project - from which Hey API platform project do you want to generate your output?' ,
104
+ ) ;
105
+ }
106
+
107
+ const query = queryParams . join ( '&' ) ;
108
+ const platformUrl = baseUrl || 'get.heyapi.dev' ;
109
+ const compiledPath = `https://${ [ platformUrl , result . organization , result . project ] . join ( '/' ) } ` ;
110
+ result . path = query ? `${ compiledPath } ?${ query } ` : compiledPath ;
111
+
112
+ return result ;
113
+ } ;
114
+
115
+ const logInputPath = ( {
116
+ config,
117
+ inputPath,
118
+ watch,
119
+ } : {
120
+ config : Config ;
121
+ inputPath : ReturnType < typeof compileInputPath > ;
122
+ watch ?: boolean ;
123
+ } ) => {
124
+ if ( config . logs . level === 'silent' ) {
125
+ return ;
126
+ }
127
+
128
+ if ( watch ) {
129
+ console . clear ( ) ;
130
+ }
131
+
132
+ const baseString = watch
133
+ ? 'Input changed, generating from'
134
+ : 'Generating from' ;
135
+
136
+ if ( typeof inputPath . path === 'string' ) {
137
+ const baseInput = isPlatformPath ( inputPath . path )
138
+ ? `${ inputPath . organization } /${ inputPath . project } `
139
+ : inputPath . path ;
140
+ console . log ( `⏳ ${ baseString } ${ baseInput } ` ) ;
141
+ if ( isPlatformPath ( inputPath . path ) ) {
142
+ if ( inputPath . branch ) {
143
+ console . log ( `branch: ${ inputPath . branch } ` ) ;
144
+ }
145
+ if ( inputPath . commit_sha ) {
146
+ console . log ( `commit: ${ inputPath . commit_sha } ` ) ;
147
+ }
148
+ if ( inputPath . tags ?. length ) {
149
+ console . log ( `tags: ${ inputPath . tags . join ( ', ' ) } ` ) ;
150
+ }
151
+ if ( inputPath . version ) {
152
+ console . log ( `version: ${ inputPath . version } ` ) ;
153
+ }
154
+ }
155
+ } else {
156
+ console . log ( `⏳ ${ baseString } raw OpenAPI specification` ) ;
157
+ }
158
+ } ;
159
+
16
160
export const createClient = async ( {
17
161
config,
18
162
templates,
@@ -22,14 +166,20 @@ export const createClient = async ({
22
166
templates : Templates ;
23
167
watch ?: WatchValues ;
24
168
} ) => {
25
- const inputPath = config . input . path ;
169
+ const inputPath = compileInputPath ( config . input ) ;
26
170
const timeout = config . watch . timeout ;
27
171
28
172
const watch : WatchValues = _watch || { headers : new Headers ( ) } ;
29
173
174
+ logInputPath ( {
175
+ config,
176
+ inputPath,
177
+ watch : Boolean ( _watch ) ,
178
+ } ) ;
179
+
30
180
Performance . start ( 'spec' ) ;
31
181
const { data, error, response } = await getSpec ( {
32
- inputPath,
182
+ inputPath : inputPath . path ,
33
183
timeout,
34
184
watch,
35
185
} ) ;
@@ -48,15 +198,6 @@ export const createClient = async ({
48
198
let context : IR . Context | undefined ;
49
199
50
200
if ( data ) {
51
- if ( config . logs . level !== 'silent' ) {
52
- if ( _watch ) {
53
- console . clear ( ) ;
54
- console . log ( `⏳ Input changed, generating from ${ inputPath } ` ) ;
55
- } else {
56
- console . log ( `⏳ Generating from ${ inputPath } ` ) ;
57
- }
58
- }
59
-
60
201
Performance . start ( 'parser' ) ;
61
202
if (
62
203
config . experimentalParser &&
@@ -95,7 +236,7 @@ export const createClient = async ({
95
236
Performance . end ( 'postprocess' ) ;
96
237
}
97
238
98
- if ( config . watch . enabled && typeof inputPath === 'string' ) {
239
+ if ( config . watch . enabled && typeof inputPath . path === 'string' ) {
99
240
setTimeout ( ( ) => {
100
241
createClient ( { config, templates, watch } ) ;
101
242
} , config . watch . interval ) ;
0 commit comments