@@ -7,6 +7,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
77const packageRoot = path . resolve ( __dirname , '..' ) ;
88const repoRoot = path . resolve ( packageRoot , '..' ) ;
99const defaultApiReferenceDir = path . join ( repoRoot , 'api-reference' ) ;
10+ const docsBaseUrl = 'https://docs.flashcat.cloud' ;
11+ const httpMethods = [ 'get' , 'post' , 'put' , 'patch' , 'delete' , 'head' , 'options' , 'trace' ] ;
1012
1113export const requiredEnv = [
1214 'CDN_ACCESS_KEY' ,
@@ -46,6 +48,36 @@ export function validateJsonFile(filePath) {
4648 JSON . parse ( fs . readFileSync ( filePath , 'utf8' ) ) ;
4749}
4850
51+ export function buildOpenapiReferenceIndex ( openapi ) {
52+ const result = { } ;
53+
54+ for ( const [ apiPath , pathItem ] of Object . entries ( openapi . paths ?? { } ) ) {
55+ const operation = httpMethods
56+ . map ( ( method ) => pathItem ?. [ method ] )
57+ . find ( Boolean ) ;
58+ const href = operation ?. [ 'x-mint' ] ?. href ;
59+ const label = operation ?. summary || operation ?. [ 'x-mint' ] ?. metadata ?. sidebarTitle || operation ?. operationId ;
60+
61+ if ( href && label ) {
62+ result [ apiPath ] = {
63+ label,
64+ url : new URL ( href , docsBaseUrl ) . toString ( )
65+ } ;
66+ }
67+ }
68+
69+ return result ;
70+ }
71+
72+ function buildJsonUploadOptions ( ) {
73+ return {
74+ headers : {
75+ 'Content-Type' : 'application/json; charset=utf-8' ,
76+ 'Cache-Control' : 'public, max-age=300'
77+ }
78+ } ;
79+ }
80+
4981async function createOssClient ( env = process . env ) {
5082 const { default : OSS } = await import ( 'ali-oss' ) ;
5183 return new OSS ( {
@@ -77,6 +109,20 @@ async function refreshCdnCache(cdnRuntime, url) {
77109 console . log ( `Refreshed CDN cache: ${ url } ` ) ;
78110}
79111
112+ async function uploadJsonAsset ( {
113+ env,
114+ ossClient,
115+ cdnRuntime,
116+ ossFilePath,
117+ payload,
118+ label
119+ } ) {
120+ const result = await ossClient . put ( ossFilePath , payload , buildJsonUploadOptions ( ) ) ;
121+ const cdnUrl = buildCdnUrl ( result . url , env . CDN_ENDPOINT , env . CDN_URL ) ;
122+ console . log ( `Uploaded ${ label } -> ${ cdnUrl } ` ) ;
123+ await refreshCdnCache ( cdnRuntime , cdnUrl ) ;
124+ }
125+
80126export async function uploadOpenapiJsonFiles ( {
81127 apiReferenceDir = defaultApiReferenceDir ,
82128 env = process . env ,
@@ -103,18 +149,32 @@ export async function uploadOpenapiJsonFiles({
103149 for ( const file of files ) {
104150 const localFilePath = path . join ( apiReferenceDir , file ) ;
105151 const ossFilePath = buildOssFilePath ( env . CDN_DIR , file ) ;
106- const result = await resolvedOssClient . put ( ossFilePath , localFilePath , {
107- headers : {
108- 'Content-Type' : 'application/json; charset=utf-8' ,
109- 'Cache-Control' : 'public, max-age=300'
110- }
152+ const uploadPayload = Buffer . from ( `${ JSON . stringify (
153+ buildOpenapiReferenceIndex ( JSON . parse ( fs . readFileSync ( localFilePath , 'utf8' ) ) ) ,
154+ null ,
155+ 2
156+ ) } \n`) ;
157+ await uploadJsonAsset ( {
158+ env,
159+ ossClient : resolvedOssClient ,
160+ cdnRuntime : resolvedCdnRuntime ,
161+ ossFilePath,
162+ payload : uploadPayload ,
163+ label : file
111164 } ) ;
112- const cdnUrl = buildCdnUrl ( result . url , env . CDN_ENDPOINT , env . CDN_URL ) ;
113- console . log ( `Uploaded ${ file } -> ${ cdnUrl } ` ) ;
114- await refreshCdnCache ( resolvedCdnRuntime , cdnUrl ) ;
115165 }
116166
117- console . log ( `Uploaded ${ files . length } OpenAPI JSON files from ${ apiReferenceDir } ` ) ;
167+ const manifestPayload = Buffer . from ( `${ JSON . stringify ( { files } , null , 2 ) } \n` ) ;
168+ await uploadJsonAsset ( {
169+ env,
170+ ossClient : resolvedOssClient ,
171+ cdnRuntime : resolvedCdnRuntime ,
172+ ossFilePath : buildOssFilePath ( env . CDN_DIR , 'manifest.json' ) ,
173+ payload : manifestPayload ,
174+ label : 'manifest.json'
175+ } ) ;
176+
177+ console . log ( `Uploaded ${ files . length } OpenAPI JSON files and manifest from ${ apiReferenceDir } ` ) ;
118178}
119179
120180async function loadDotenvIfAvailable ( ) {
0 commit comments