Skip to content

Commit 80366cd

Browse files
refactor: move FunctionsModuleConfig to types file and add missing tests
- Moved FunctionsModuleConfig interface from functions.ts to functions.types.ts - Added test for path normalization (with/without leading slash) - Added test for service role auth using asServiceRole.functions.fetch Co-authored-by: Netanel Gilad <netanelgilad@users.noreply.github.com>
1 parent a6b8503 commit 80366cd

3 files changed

Lines changed: 46 additions & 6 deletions

File tree

src/modules/functions.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { AxiosInstance } from "axios";
2-
import { FunctionsFetchInit, FunctionsModule } from "./functions.types";
3-
4-
export interface FunctionsModuleConfig {
5-
getAuthHeaders?: () => Record<string, string>;
6-
baseURL?: string;
7-
}
2+
import { FunctionsFetchInit, FunctionsModule, FunctionsModuleConfig } from "./functions.types";
83

94
/**
105
* Creates the functions module for the Base44 SDK.

src/modules/functions.types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ export type FunctionName = keyof FunctionNameRegistry extends never
2424
*/
2525
export type FunctionsFetchInit = RequestInit;
2626

27+
/**
28+
* Configuration for the functions module.
29+
* @internal
30+
*/
31+
export interface FunctionsModuleConfig {
32+
getAuthHeaders?: () => Record<string, string>;
33+
baseURL?: string;
34+
}
35+
2736
/**
2837
* Functions module for invoking custom backend functions.
2938
*

tests/unit/functions.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,4 +492,40 @@ describe("Functions Module", () => {
492492
const headers = new Headers(requestInit.headers);
493493
expect(headers.get("Authorization")).toBe(`Bearer ${userToken}`);
494494
});
495+
496+
test("should normalize path with and without leading slash", async () => {
497+
// Test with leading slash
498+
fetchMock.mockResolvedValueOnce(new Response("ok", { status: 200 }));
499+
await base44.functions.fetch("/my_function");
500+
expect(fetchMock).toHaveBeenCalledWith(
501+
`${serverUrl}/api/functions/my_function`,
502+
expect.any(Object)
503+
);
504+
505+
// Test without leading slash
506+
fetchMock.mockResolvedValueOnce(new Response("ok", { status: 200 }));
507+
await base44.functions.fetch("my_function");
508+
expect(fetchMock).toHaveBeenCalledWith(
509+
`${serverUrl}/api/functions/my_function`,
510+
expect.any(Object)
511+
);
512+
});
513+
514+
test("should include service role Authorization header when using asServiceRole.functions.fetch", async () => {
515+
const serviceToken = "service-role-token";
516+
const serviceRoleBase44 = createClient({
517+
serverUrl,
518+
appId,
519+
serviceToken,
520+
});
521+
fetchMock.mockResolvedValueOnce(new Response("ok", { status: 200 }));
522+
523+
await serviceRoleBase44.asServiceRole.functions.fetch("/service_function", {
524+
method: "GET",
525+
});
526+
527+
const requestInit = fetchMock.mock.calls[0][1];
528+
const headers = new Headers(requestInit.headers);
529+
expect(headers.get("Authorization")).toBe(`Bearer ${serviceToken}`);
530+
});
495531
});

0 commit comments

Comments
 (0)