Skip to content
Merged
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
20 changes: 4 additions & 16 deletions src/core/resources/function/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import type {
BackendFunction,
FunctionConfig,
} from "@/core/resources/function/schema.js";
import {
FunctionConfigSchema,
FunctionSchema,
} from "@/core/resources/function/schema.js";
import { FunctionConfigSchema } from "@/core/resources/function/schema.js";
import { pathExists, readJsonFile } from "@/core/utils/fs.js";

export async function readFunctionConfig(
Expand Down Expand Up @@ -42,22 +39,13 @@ export async function readFunction(
);
}

const files = await globby("*.{js,ts,json}", {
const filePaths = await globby("*.{js,ts,json}", {
cwd: functionDir,
absolute: true,
});

const functionData = { ...config, entryPath, files };
const result = FunctionSchema.safeParse(functionData);
if (!result.success) {
throw new SchemaValidationError(
"Invalid function",
result.error,
configPath
);
}

return result.data;
const functionData: BackendFunction = { ...config, entryPath, filePaths };
return functionData;
Comment on lines +47 to +48
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's enough to validate it based on paths - this is not user providing us with arbitrary data. It's our code

}

export async function readAllFunctions(
Expand Down
2 changes: 1 addition & 1 deletion src/core/resources/function/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function loadFunctionCode(
fn: BackendFunction
): Promise<FunctionWithCode> {
const loadedFiles: FunctionFile[] = await Promise.all(
fn.files.map(async (filePath) => {
fn.filePaths.map(async (filePath) => {
const content = await readTextFile(filePath);
return { path: basename(filePath), content };
})
Expand Down
6 changes: 3 additions & 3 deletions src/core/resources/function/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export const FunctionConfigSchema = z.object({
entry: z.string().min(1, "Entry point cannot be empty"),
});

export const FunctionSchema = FunctionConfigSchema.extend({
export const BackendFunctionSchema = FunctionConfigSchema.extend({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call in BackendFunctionSchema since type then called BackendFunction

entryPath: z.string().min(1, "Entry path cannot be empty"),
files: z.array(z.string()).min(1, "Function must have at least one file"),
filePaths: z.array(z.string()).min(1, "Function must have at least one file"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

files used twice in schemas as "file path" and as actual "file content" I want to make names clear

});

export const FunctionDeploySchema = z.object({
Expand All @@ -38,7 +38,7 @@ export const DeployFunctionsResponseSchema = z.object({
});

export type FunctionConfig = z.infer<typeof FunctionConfigSchema>;
export type BackendFunction = z.infer<typeof FunctionSchema>;
export type BackendFunction = z.infer<typeof BackendFunctionSchema>;
export type FunctionFile = z.infer<typeof FunctionFileSchema>;
export type FunctionDeploy = z.infer<typeof FunctionDeploySchema>;
export type DeployFunctionsResponse = z.infer<
Expand Down
Loading