Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/core/resources/function/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ async function readFunctionConfig(configPath: string): Promise<FunctionConfig> {
return result.data;
}

async function readFunction(configPath: string): Promise<BackendFunction> {
async function readFunction(
configPath: string,
functionsDir: string,
): Promise<BackendFunction> {
const config = await readFunctionConfig(configPath);
const functionDir = dirname(configPath);
const name =
config.name ??
relative(functionsDir, functionDir).split(/[/\\]/).join("/");
const entryPath = join(functionDir, config.entry);

if (!(await pathExists(entryPath))) {
Expand All @@ -47,7 +53,7 @@ async function readFunction(configPath: string): Promise<BackendFunction> {
absolute: true,
});

const functionData: BackendFunction = { ...config, entryPath, filePaths };
const functionData: BackendFunction = { ...config, name, entryPath, filePaths };
return functionData;
}

Expand Down Expand Up @@ -76,7 +82,7 @@ export async function readAllFunctions(
);

const functionsFromConfig = await Promise.all(
configFiles.map((configPath) => readFunction(configPath)),
configFiles.map((configPath) => readFunction(configPath, functionsDir)),
);

const functionsWithoutConfig = await Promise.all(
Expand Down
3 changes: 2 additions & 1 deletion src/core/resources/function/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ const AutomationSchema = z.union([
]);

export const FunctionConfigSchema = z.object({
name: FunctionNameSchema,
name: FunctionNameSchema.optional(),
entry: z.string().min(1, "Entry point cannot be empty"),
automations: z.array(AutomationSchema).optional(),
});

const BackendFunctionSchema = FunctionConfigSchema.extend({
name: FunctionNameSchema,
entryPath: z.string().min(1, "Entry path cannot be empty"),
filePaths: z.array(z.string()).min(1, "Function must have at least one file"),
});
Expand Down
13 changes: 12 additions & 1 deletion tests/core/function-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ describe("readAllFunctions", () => {
const functionsDir = resolve(FIXTURES_DIR, "function-discovery");
const result = await readAllFunctions(functionsDir);

expect(result).toHaveLength(4); // foo/bar, foo/kfir/hello, stam, with-config (config-based)
expect(result).toHaveLength(5); // foo/bar, foo/kfir/hello, stam, with-config (config-based), no-name-in-config (config-based, no name)

const names = result.map((f) => f.name).sort();
expect(names).toContain("foo/bar");
expect(names).toContain("foo/kfir/hello");
expect(names).toContain("stam");
expect(names).toContain("custom-name");
expect(names).toContain("no-name-in-config");

const fooBar = result.find((f) => f.name === "foo/bar");
expect(fooBar).toBeDefined();
Expand All @@ -46,6 +47,16 @@ describe("readAllFunctions", () => {
expect(withConfig?.name).toBe("custom-name");
});

it("uses path-based name when function.jsonc omits the name field", async () => {
const functionsDir = resolve(FIXTURES_DIR, "function-discovery");
const result = await readAllFunctions(functionsDir);

const noName = result.find((f) => f.name === "no-name-in-config");
expect(noName).toBeDefined();
expect(noName?.entry).toBe("entry.ts");
expect(noName?.entryPath).toContain("no-name-in-config/entry.ts");
});

it("includes files recursively in filePaths for zero-config functions", async () => {
const functionsDir = resolve(FIXTURES_DIR, "function-discovery");
const result = await readAllFunctions(functionsDir);
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/function-discovery/no-name-in-config/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function handler() {
return { ok: true };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"entry": "entry.ts"
}
Loading