Skip to content

Commit 0ed8851

Browse files
committed
feat: return full image urls
1 parent f6b16db commit 0ed8851

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

packages/burger-api/src/functions/burgers-get.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
import { app, type HttpRequest, type InvocationContext } from '@azure/functions';
22
import { DbService } from '../db-service';
3+
import { Burger } from '../burger';
4+
5+
function transformBurgerImageUrl(burger: Burger, request: HttpRequest): Burger {
6+
const url = new URL(request.url);
7+
const baseUrl = `${url.protocol}//${url.host}`;
8+
9+
return {
10+
...burger,
11+
imageUrl: `${baseUrl}/api/images/${burger.imageUrl}`
12+
};
13+
}
314

415
app.http('burgers-get', {
516
methods: ['GET'],
617
authLevel: 'anonymous',
718
route: 'burgers',
8-
handler: async (_request: HttpRequest, context: InvocationContext) => {
19+
handler: async (request: HttpRequest, context: InvocationContext) => {
920
context.log('Processing request to get all burgers...');
1021

1122
const dataService = await DbService.getInstance();
1223
const burgers = await dataService.getBurgers();
1324

25+
// Transform imageUrl to include full URL
26+
const burgersWithFullUrls = burgers.map(burger => transformBurgerImageUrl(burger, request));
27+
1428
return {
15-
jsonBody: burgers,
29+
jsonBody: burgersWithFullUrls,
1630
status: 200
1731
};
1832
}
@@ -34,8 +48,11 @@ app.http('burger-get-by-id', {
3448
};
3549
}
3650

51+
// Transform imageUrl to include full URL
52+
const burgerWithFullUrl = transformBurgerImageUrl(burger, request);
53+
3754
return {
38-
jsonBody: burger,
55+
jsonBody: burgerWithFullUrl,
3956
status: 200
4057
};
4158
}

packages/burger-api/src/functions/toppings-get.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import { app, type HttpRequest, type InvocationContext } from '@azure/functions';
22
import { 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

517
app.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

Comments
 (0)