Skip to content

Commit ab9d10d

Browse files
refactor(functions): simplify fetch API per review comments
- Remove fallback path logic - only use /functions endpoint - Remove X-App-Id header - not needed - Remove automatic content-type handling - let fetch handle it natively - Update tests to reflect the simplified behavior Co-authored-by: Netanel Gilad <netanelgilad@users.noreply.github.com>
1 parent 8c1ac2a commit ab9d10d

2 files changed

Lines changed: 6 additions & 26 deletions

File tree

src/modules/functions.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,9 @@ export function createFunctionsModule(
9696
async fetch(path: string, init: FunctionsFetchInit = {}) {
9797
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
9898
const primaryPath = `/functions${normalizedPath}`;
99-
const fallbackPath = `/apps/${appId}/functions${normalizedPath}`;
10099
const { data, ...fetchInit } = init;
101100

102101
const headers = toHeaders(fetchInit.headers);
103-
if (!headers.has("X-App-Id")) {
104-
headers.set("X-App-Id", appId);
105-
}
106102
let body: BodyInit | null | undefined = fetchInit.body;
107103

108104
if (body === undefined && data !== undefined) {
@@ -115,9 +111,6 @@ export function createFunctionsModule(
115111
body = data as BodyInit;
116112
} else {
117113
body = JSON.stringify(data);
118-
if (!headers.has("Content-Type")) {
119-
headers.set("Content-Type", "application/json");
120-
}
121114
}
122115
}
123116

@@ -127,18 +120,11 @@ export function createFunctionsModule(
127120
body,
128121
};
129122

130-
let response = await fetch(
123+
const response = await fetch(
131124
joinBaseUrl(axios.defaults.baseURL, primaryPath),
132125
requestInit
133126
);
134127

135-
if (response.status === 404) {
136-
response = await fetch(
137-
joinBaseUrl(axios.defaults.baseURL, fallbackPath),
138-
requestInit
139-
);
140-
}
141-
142128
return response;
143129
},
144130
};

tests/unit/functions.test.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -474,29 +474,23 @@ describe("Functions Module", () => {
474474
);
475475

476476
const requestInit = fetchMock.mock.calls[0][1];
477-
const headers = new Headers(requestInit.headers);
478-
expect(headers.get("Content-Type")).toBe("application/json");
479-
expect(headers.get("X-App-Id")).toBe(appId);
480477
expect(requestInit.body).toBe(JSON.stringify({ mode: "sse" }));
481478
});
482479

483-
test("should fallback to app-scoped functions endpoint on 404", async () => {
480+
test("should not fallback to app-scoped functions endpoint on 404", async () => {
484481
fetchMock
485-
.mockResolvedValueOnce(new Response("Not found", { status: 404 }))
486-
.mockResolvedValueOnce(new Response("ok", { status: 200 }));
482+
.mockResolvedValueOnce(new Response("Not found", { status: 404 }));
487483

488-
await base44.functions.fetch("/streaming_demo/deep/path", {
484+
const response = await base44.functions.fetch("/streaming_demo/deep/path", {
489485
method: "POST",
490486
data: { mode: "ndjson" },
491487
});
492488

493-
expect(fetchMock).toHaveBeenCalledTimes(2);
489+
expect(fetchMock).toHaveBeenCalledTimes(1);
494490
expect(fetchMock.mock.calls[0][0]).toBe(
495491
`${serverUrl}/api/functions/streaming_demo/deep/path`
496492
);
497-
expect(fetchMock.mock.calls[1][0]).toBe(
498-
`${serverUrl}/api/apps/${appId}/functions/streaming_demo/deep/path`
499-
);
493+
expect(response.status).toBe(404);
500494
});
501495

502496
test("should include Authorization header when using functions.fetch", async () => {

0 commit comments

Comments
 (0)