@@ -11,23 +11,38 @@ const SPEC_URL =
1111 process . env . ROXY_OPENAPI_URL ?? 'https://roxyapi.com/api/v2/openapi.json' ;
1212const SPEC_PATH = 'specs/openapi.json' ;
1313
14- console . log ( `Fetching OpenAPI spec from ${ SPEC_URL } ` ) ;
14+ /**
15+ * Path to read the spec from instead of fetching it.
16+ *
17+ * @remarks
18+ * Keeps generation offline and byte-reproducible, which is what the codegen drift check in CI
19+ * relies on. Distinct from `ROXY_OPENAPI_URL`, which only points the fetch at a different server.
20+ */
21+ const SPEC_FILE = process . env . ROXYAPI_SPEC_FILE ;
1522
1623let spec : unknown ;
17- try {
18- const res = await fetch ( SPEC_URL , {
19- headers : { 'Cache-Control' : 'no-cache' } ,
20- } ) ;
21- if ( ! res . ok ) throw new Error ( `HTTP ${ res . status } ${ res . statusText } ` ) ;
22- spec = await res . json ( ) ;
23- } catch ( err ) {
24- if ( existsSync ( SPEC_PATH ) ) {
25- console . warn (
26- `! Live spec fetch failed (${ err instanceof Error ? err . message : String ( err ) } ). Using cached ${ SPEC_PATH } .` ,
27- ) ;
28- spec = JSON . parse ( await Bun . file ( SPEC_PATH ) . text ( ) ) ;
29- } else {
30- throw err ;
24+ if ( SPEC_FILE ) {
25+ console . log (
26+ `Reading OpenAPI spec from ${ SPEC_FILE } (offline, ROXYAPI_SPEC_FILE)` ,
27+ ) ;
28+ spec = JSON . parse ( await Bun . file ( SPEC_FILE ) . text ( ) ) ;
29+ } else {
30+ console . log ( `Fetching OpenAPI spec from ${ SPEC_URL } ` ) ;
31+ try {
32+ const res = await fetch ( SPEC_URL , {
33+ headers : { 'Cache-Control' : 'no-cache' } ,
34+ } ) ;
35+ if ( ! res . ok ) throw new Error ( `HTTP ${ res . status } ${ res . statusText } ` ) ;
36+ spec = await res . json ( ) ;
37+ } catch ( err ) {
38+ if ( existsSync ( SPEC_PATH ) ) {
39+ console . warn (
40+ `! Live spec fetch failed (${ err instanceof Error ? err . message : String ( err ) } ). Using cached ${ SPEC_PATH } .` ,
41+ ) ;
42+ spec = JSON . parse ( await Bun . file ( SPEC_PATH ) . text ( ) ) ;
43+ } else {
44+ throw err ;
45+ }
3146 }
3247}
3348
0 commit comments