@@ -2,61 +2,64 @@ import { readFileSync } from 'node:fs'
22
33/**
44 * Read and upload the provided OpenAPI specification to Hey API.
5- * @param pathToOpenApi Path to the OpenAPI specification file.
6- * @param heyApiToken Hey API token.
7- * @returns {Promise<void> } Resolves after the file is uploaded.
85 */
9- export async function upload (
10- pathToOpenApi : string ,
11- heyApiToken : string ,
12- dryRun ?: boolean
13- ) : Promise < void > {
14- return new Promise ( async resolve => {
15- if ( ! pathToOpenApi ) {
16- throw new Error ( 'invalid OpenAPI path' )
17- }
6+ export async function upload ( {
7+ dryRun,
8+ heyApiToken,
9+ pathToOpenApi,
10+ } : {
11+ dryRun ?: boolean ;
12+ /**
13+ * Hey API token.
14+ */
15+ heyApiToken : string ;
16+ /**
17+ * Path to the OpenAPI specification file.
18+ */
19+ pathToOpenApi : string ;
20+ } ) : Promise < void > {
21+ if ( ! pathToOpenApi ) {
22+ throw new Error ( 'invalid OpenAPI path' )
23+ }
1824
19- let data : Buffer
20- try {
21- data = readFileSync ( pathToOpenApi )
22- } catch ( error ) {
23- throw new Error ( 'invalid OpenAPI path' )
24- }
25+ let data : Buffer
26+ try {
27+ data = readFileSync ( pathToOpenApi )
28+ } catch {
29+ throw new Error ( 'invalid OpenAPI path' )
30+ }
2531
26- const formData : Record < string , string | number | boolean > = {
27- github_repo : process . env . GITHUB_REPOSITORY ! ,
28- github_repo_id : process . env . GITHUB_REPOSITORY_ID ! ,
29- openapi : data . toString ( )
30- }
31-
32- if ( dryRun ) {
33- formData [ 'dry-run' ] = dryRun
34- }
32+ const formData : Record < string , string | number | boolean > = {
33+ github_repo : process . env . GITHUB_REPOSITORY ! ,
34+ github_repo_id : process . env . GITHUB_REPOSITORY_ID ! ,
35+ openapi : data . toString ( )
36+ }
3537
36- const body = Object . entries ( formData )
37- . flatMap (
38- ( [ key , value ] ) =>
39- `${ encodeURIComponent ( key ) } =${ encodeURIComponent ( value ) } `
40- )
41- . join ( '&' )
38+ if ( dryRun ) {
39+ formData [ 'dry-run' ] = dryRun
40+ }
4241
43- const response = await fetch (
44- 'https://platform-production-25fb.up.railway.app/api/openapi' ,
45- {
46- body,
47- headers : {
48- Authorization : `Bearer ${ heyApiToken } ` ,
49- 'Content-Type' : 'application/x-www-form-urlencoded'
50- } ,
51- method : 'POST'
52- }
42+ const body = Object . entries ( formData )
43+ . flatMap (
44+ ( [ key , value ] ) =>
45+ `${ encodeURIComponent ( key ) } =${ encodeURIComponent ( value ) } `
5346 )
47+ . join ( '&' )
5448
55- if ( response . status >= 300 ) {
56- const error = await response . json ( )
57- throw new Error ( JSON . stringify ( error ) )
49+ const response = await fetch (
50+ 'https://platform-production-25fb.up.railway.app/api/openapi' ,
51+ {
52+ body,
53+ headers : {
54+ Authorization : `Bearer ${ heyApiToken } ` ,
55+ 'Content-Type' : 'application/x-www-form-urlencoded'
56+ } ,
57+ method : 'POST'
5858 }
59+ )
5960
60- resolve ( )
61- } )
61+ if ( response . status >= 300 ) {
62+ const error = await response . json ( )
63+ throw new Error ( JSON . stringify ( error ) )
64+ }
6265}
0 commit comments