11import { app , type HttpRequest , type InvocationContext } from '@azure/functions' ;
2+ import process from 'node:process' ;
23import fs from 'node:fs/promises' ;
34import path from 'node:path' ;
45import yaml from 'js-yaml' ;
6+ import dotenv from 'dotenv' ;
7+
8+ // Env file is located in the root of the repository
9+ dotenv . config ( { path : path . join ( process . cwd ( ) , "../../.env" ) } ) ;
510
611app . http ( 'openapi-get' , {
712 methods : [ 'GET' ] ,
@@ -11,17 +16,23 @@ app.http('openapi-get', {
1116 context . log ( 'Processing request to get OpenAPI specification...' ) ;
1217
1318 try {
14- const openapiPath = path . join ( __dirname , '../../../ openapi.yaml' ) ;
19+ const openapiPath = path . join ( process . cwd ( ) , 'openapi.yaml' ) ;
1520 const openapiContent = await fs . readFile ( openapiPath , 'utf8' ) ;
1621
22+ // Replace BURGER_API_HOST placeholder with actual host URL
23+ console . log ( 'BURGER_API_URL:' , process . env . BURGER_API_URL ) ;
24+ context . log ( 'Replacing <BURGER_API_HOST> in OpenAPI specification...' ) ;
25+ const burgerApiHost = process . env . BURGER_API_URL || 'http://localhost:7071' ;
26+ const processedContent = openapiContent . replace ( '<BURGER_API_HOST>' , burgerApiHost ) ;
27+
1728 const url = new URL ( request . url ) ;
1829 const wantsJson =
1930 url . searchParams . get ( 'format' ) ?. toLowerCase ( ) === 'json' ||
2031 ( request . headers . get ( 'accept' ) ?. toLowerCase ( ) . includes ( 'json' ) ?? false ) ;
2132
2233 if ( wantsJson ) {
2334 try {
24- const json = yaml . load ( openapiContent ) ;
35+ const json = yaml . load ( processedContent ) ;
2536 return {
2637 jsonBody : json ,
2738 headers : {
@@ -39,19 +50,19 @@ app.http('openapi-get', {
3950 }
4051
4152 return {
42- body : openapiContent ,
53+ body : processedContent ,
4354 headers : {
4455 'Content-Type' : 'text/yaml'
4556 } ,
4657 status : 200
4758 } ;
4859 } catch ( error ) {
4960 context . error ( 'Error reading OpenAPI specification file:' , error ) ;
50-
61+
5162 return {
5263 jsonBody : { error : 'Error reading OpenAPI specification' } ,
5364 status : 500
5465 } ;
5566 }
5667 }
57- } ) ;
68+ } ) ;
0 commit comments