11import { app , type HttpRequest , type InvocationContext } from '@azure/functions' ;
22import { DbService } from '../db-service' ;
3- import { ToppingCategory } from '../topping' ;
3+ import { ToppingCategory , Topping } from '../topping' ;
4+
5+ // Helper function to transform topping imageUrl with full URL
6+ function transformToppingImageUrl ( topping : Topping , request : HttpRequest ) : Topping {
7+ // Get the base URL directly from the request URL
8+ const url = new URL ( request . url ) ;
9+ const baseUrl = `${ url . protocol } //${ url . host } ` ;
10+
11+ return {
12+ ...topping ,
13+ imageUrl : `${ baseUrl } /api/images/${ topping . imageUrl } `
14+ } ;
15+ }
416
517app . http ( 'toppings-get' , {
618 methods : [ 'GET' ] ,
@@ -12,20 +24,24 @@ app.http('toppings-get', {
1224
1325 const dataService = await DbService . getInstance ( ) ;
1426 const categoryParam = request . query . get ( 'category' ) ;
15-
27+
1628 // If a category is specified, filter toppings by category
1729 if ( categoryParam && Object . values ( ToppingCategory ) . includes ( categoryParam as ToppingCategory ) ) {
1830 const toppings = await dataService . getToppingsByCategory ( categoryParam as ToppingCategory ) ;
31+ // Transform imageUrls to include full URL
32+ const toppingsWithFullUrls = toppings . map ( topping => transformToppingImageUrl ( topping , request ) ) ;
1933 return {
20- jsonBody : toppings ,
34+ jsonBody : toppingsWithFullUrls ,
2135 status : 200
2236 } ;
2337 }
24-
38+
2539 // Otherwise return all toppings
2640 const toppings = await dataService . getToppings ( ) ;
41+ // Transform imageUrls to include full URL
42+ const toppingsWithFullUrls = toppings . map ( topping => transformToppingImageUrl ( topping , request ) ) ;
2743 return {
28- jsonBody : toppings ,
44+ jsonBody : toppingsWithFullUrls ,
2945 status : 200
3046 } ;
3147 }
@@ -47,8 +63,11 @@ app.http('topping-get-by-id', {
4763 } ;
4864 }
4965
66+ // Transform imageUrl to include full URL
67+ const toppingWithFullUrl = transformToppingImageUrl ( topping , request ) ;
68+
5069 return {
51- jsonBody : topping ,
70+ jsonBody : toppingWithFullUrl ,
5271 status : 200
5372 } ;
5473 }
0 commit comments