Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix: use the new api schema and response format

MonsPropre3d1337aff4a3
diff --git a/apps/api/src/custom-field/index.ts b/apps/api/src/custom-field/index.ts
index 01b8d873f..4f663c34e 100644
--- a/apps/api/src/custom-field/index.ts
+++ b/apps/api/src/custom-field/index.ts
@@ -1,6 +1,9 @@
-import { Hono } from "hono";
-import { describeRoute, resolver, validator } from "hono-openapi";
-import * as v from "valibot";
+import {
+ apiRouter,
+ createRoute,
+ errorResponse,
+ jsonResponse,
+} from "../openapi";
import { requireWorkspacePermission } from "../utils/require-workspace-permission";
import { workspaceAccess } from "../utils/workspace-access-middleware";
import createCustomField from "./controllers/create-custom-field";
@@ -11,297 +14,264 @@ import getCustomFieldValuesByTask from "./controllers/get-custom-field-values-by
import getCustomFieldsByProject from "./controllers/get-custom-fields-by-project";
import reorderCustomFields from "./controllers/reorder-custom-field";
import setCustomFieldValue from "./controllers/set-custom-field-value";
+import {
+ customFieldDefinitionListSchema,
+ customFieldDefinitionSchema,
+ customFieldFilterValuesListSchema,
+ customFieldValueListSchema,
+ reorderCustomFieldsResponseSchema,
+ setCustomFieldValueResponseSchema,
+} from "./response";
+import {
+ createCustomFieldBody,
+ customFieldIdParam,
+ projectIdParam,
+ reorderCustomFieldsBody,
+ setCustomFieldValueBody,
+ taskIdParam,
+} from "./schema";
-const customFieldDefinitionSchema = v.object({
- id: v.string(),
- projectId: v.string(),
- name: v.string(),
- type: v.string(),
- required: v.boolean(),
- defaultValue: v.nullable(v.string()),
- options: v.nullable(v.any()),
- position: v.number(),
- createdAt: v.date(),
- updatedAt: v.date(),
+const getCustomFieldsRoute = createRoute({
+ method: "get",
+ operationId: "getCustomFieldsByProject",
+ path: "/project/{projectId}",
+ tags: ["Custom Fields"],
+ summary: "Get custom fields",
+ description: "Get all custom field definitions for a project.",
+ middleware: [workspaceAccess.fromProject("projectId")] as const,
+ request: { params: projectIdParam },
+ responses: {
+ 200: jsonResponse(
+ "List of custom field definitions",
+ customFieldDefinitionListSchema,
+ ),
+ 400: errorResponse(
+ "Unknown project, or its workspace could not be determined",
+ ),
+ 403: errorResponse("No access to the project's workspace"),
+ },
+});
+
+const getCustomFieldValuesByProjectRoute = createRoute({
+ method: "get",
+ operationId: "getCustomFieldValuesByProject",
+ path: "/project/{projectId}/values",
+ tags: ["Custom Fields"],
+ summary: "Get project custom field values",
+ description: "Get all custom field values for every task in a project.",
+ middleware: [workspaceAccess.fromProject("projectId")] as const,
+ request: { params: projectIdParam },
+ responses: {
+ 200: jsonResponse(
+ "List of custom field values for the project",
+ customFieldValueListSchema,
+ ),
+ 400: errorResponse(
+ "Unknown project, or its workspace could not be determined",
+ ),
+ 403: errorResponse("No access to the project's workspace"),
+ },
+});
+
+const getCustomFieldValuesByTaskRoute = createRoute({
+ method: "get",
+ operationId: "getCustomFieldValuesByTask",
+ path: "/task/{taskId}",
+ tags: ["Custom Fields"],
+ summary: "Get task custom field values",
+ description: "Get all custom field values for a task with their definitions.",
+ middleware: [workspaceAccess.fromTaskId("taskId")] as const,
+ request: { params: taskIdParam },
+ responses: {
+ 200: jsonResponse(
+ "List of custom field values for the task",
+ customFieldValueListSchema,
+ ),
+ 400: errorResponse(
+ "Unknown task, or its workspace could not be determined",
+ ),
+ 403: errorResponse("No access to the task's workspace"),
+ },
});
-const customFieldValueSchema = v.object({
- id: v.string(),
- taskId: v.string(),
- fieldId: v.string(),
- value: v.nullable(v.string()),
- fieldName: v.string(),
- fieldType: v.string(),
- fieldOptions: v.nullable(v.any()),
+const getCustomFieldFilterValuesRoute = createRoute({
+ method: "get",
+ operationId: "getCustomFieldFilterValues",
+ path: "/project/{projectId}/filter-values",
+ tags: ["Custom Fields"],
+ summary: "Get custom field filter values",
+ description:
+ "Get distinct values used by tasks for each custom field of a project.",
+ middleware: [workspaceAccess.fromProject("projectId")] as const,
+ request: { params: projectIdParam },
+ responses: {
+ 200: jsonResponse(
+ "Distinct values used for each custom field",
+ customFieldFilterValuesListSchema,
+ ),
+ 400: errorResponse(
+ "Unknown project, or its workspace could not be determined",
+ ),
+ 403: errorResponse("No access to the project's workspace"),
+ },
});
-const customField = new Hono<{
- Variables: {
- userId: string;
- workspaceId: string;
- };
-}>()
- .get(
- "/project/:projectId",
- describeRoute({
- operationId: "getCustomFieldsByProject",
- tags: ["Custom Fields"],
- description: "Get all custom field definitions for a project",
- responses: {
- 200: {
- description: "List of custom field definitions",
- content: {
- "application/json": {
- schema: resolver(v.array(customFieldDefinitionSchema)),
- },
- },
- },
- },
- }),
- validator("param", v.object({ projectId: v.string() })),
+const createCustomFieldRoute = createRoute({
+ method: "post",
+ operationId: "createCustomField",
+ path: "/",
+ tags: ["Custom Fields"],
+ summary: "Create custom field",
+ description: "Create a custom field definition for a project.",
+ middleware: [
workspaceAccess.fromProject("projectId"),
- async (c) => {
- const { projectId } = c.req.valid("param");
- const fields = await getCustomFieldsByProject(projectId);
- return c.json(fields);
+ requireWorkspacePermission({ project: ["update"] }),
+ ] as const,
+ request: {
+ body: {
+ required: true,
+ content: { "application/json": { schema: createCustomFieldBody } },
},
- )
- .get(
- "/project/:projectId/values",
- describeRoute({
- operationId: "getCustomFieldValuesByProject",
- tags: ["Custom Fields"],
- description: "Get all custom field values for a project",
- responses: {
- 200: {
- description:
- "List of custom field values for all tasks in the project",
- content: {
- "application/json": {
- schema: resolver(v.array(customFieldValueSchema)),
- },
- },
- },
- },
- }),
- validator("param", v.object({ projectId: v.string() })),
- workspaceAccess.fromProject("projectId"),
- async (c) => {
- const { projectId } = c.req.valid("param");
-
- const values = await getCustomFieldValuesByProject(projectId);
+ },
+ responses: {
+ 200: jsonResponse("The created custom field", customFieldDefinitionSchema),
+ 400: errorResponse("Invalid body, or unknown project"),
+ 403: errorResponse(
+ "No workspace access, or missing project:update permission",
+ ),
+ },
+});
- return c.json(values);
+const reorderCustomFieldsRoute = createRoute({
+ method: "put",
+ operationId: "reorderCustomFields",
+ path: "/reorder/{projectId}",
+ tags: ["Custom Fields"],
+ summary: "Reorder custom fields",
+ description: "Set new positions for a project's custom fields.",
+ middleware: [
+ workspaceAccess.fromProject("projectId"),
+ requireWorkspacePermission({ project: ["update"] }),
+ ] as const,
+ request: {
+ params: projectIdParam,
+ body: {
+ required: true,
+ content: { "application/json": { schema: reorderCustomFieldsBody } },
},
- )
- .get(
- "/task/:taskId",
- describeRoute({
- operationId: "getCustomFieldValuesByTask",
- tags: ["Custom Fields"],
- description: "Get all custom field values for a task",
- responses: {
- 200: {
- description: "List of custom field values with their definitions",
- content: {
- "application/json": {
- schema: resolver(v.array(customFieldValueSchema)),
- },
- },
- },
- },
- }),
- validator("param", v.object({ taskId: v.string() })),
+ },
+ responses: {
+ 200: jsonResponse(
+ "The reordered custom fields",
+ reorderCustomFieldsResponseSchema,
+ ),
+ 400: errorResponse("A custom field does not belong to this project"),
+ 403: errorResponse(
+ "No workspace access, or missing project:update permission",
+ ),
+ },
+});
+
+const setCustomFieldValueRoute = createRoute({
+ method: "put",
+ operationId: "setCustomFieldValue",
+ path: "/value",
+ tags: ["Custom Fields"],
+ summary: "Set custom field value",
+ description: "Create or update a custom field value for a task.",
+ middleware: [
workspaceAccess.fromTaskId("taskId"),
- async (c) => {
- const { taskId } = c.req.valid("param");
- const values = await getCustomFieldValuesByTask(taskId);
- return c.json(values);
+ requireWorkspacePermission({ task: ["update"] }),
+ ] as const,
+ request: {
+ body: {
+ required: true,
+ content: { "application/json": { schema: setCustomFieldValueBody } },
},
+ },
+ responses: {
+ 200: jsonResponse(
+ "The saved custom field value",
+ setCustomFieldValueResponseSchema,
+ ),
+ 400: errorResponse("Invalid body, unknown task, or unknown custom field"),
+ 403: errorResponse(
+ "No workspace access, or missing task:update permission",
+ ),
+ },
+});
+
+const deleteCustomFieldRoute = createRoute({
+ method: "delete",
+ operationId: "deleteCustomField",
+ path: "/{id}",
+ tags: ["Custom Fields"],
+ summary: "Delete custom field",
+ description: "Delete a custom field definition by ID.",
+ middleware: [
+ workspaceAccess.fromCustomField("id"),
+ requireWorkspacePermission({ project: ["update"] }),
+ ] as const,
+ request: { params: customFieldIdParam },
+ responses: {
+ 200: jsonResponse("The deleted custom field", customFieldDefinitionSchema),
+ 400: errorResponse(
+ "Unknown custom field, or its workspace could not be determined",
+ ),
+ 403: errorResponse(
+ "No workspace access, or missing project:update permission",
+ ),
+ },
+});
+
+const customField = apiRouter()
+ .openapi(getCustomFieldsRoute, async (c) =>
+ c.json(await getCustomFieldsByProject(c.req.valid("param").projectId), 200),
)
- .get(
- "/project/:projectId/filter-values",
- describeRoute({
- operationId: "getCustomFieldFilterValues",
- tags: ["Custom Fields"],
- description:
- "Get distinct values actually used by tasks for each custom field of a project (for filters)",
- responses: {
- 200: {
- description: "List of custom fields with their distinct used values",
- content: {
- "application/json": {
- schema: resolver(
- v.array(
- v.object({
- fieldId: v.string(),
- fieldName: v.string(),
- fieldType: v.string(),
- values: v.array(v.string()),
- }),
- ),
- ),
- },
- },
- },
- },
- }),
- validator("param", v.object({ projectId: v.string() })),
- workspaceAccess.fromProject("projectId"),
- async (c) => {
- const { projectId } = c.req.valid("param");
- const filterValues = await getCustomFieldFilterValues(projectId);
- return c.json(filterValues);
- },
+ .openapi(getCustomFieldValuesByProjectRoute, async (c) =>
+ c.json(
+ await getCustomFieldValuesByProject(c.req.valid("param").projectId),
+ 200,
+ ),
+ )
+ .openapi(getCustomFieldValuesByTaskRoute, async (c) =>
+ c.json(await getCustomFieldValuesByTask(c.req.valid("param").taskId), 200),
)
- .post(
- "/",
- describeRoute({
- operationId: "createCustomField",
- tags: ["Custom Fields"],
- description: "Create a new custom field definition for a project",
- responses: {
- 200: {
- description: "Custom field created successfully",
- content: {
- "application/json": {
- schema: resolver(customFieldDefinitionSchema),
- },
- },
- },
- },
- }),
- validator(
- "json",
- v.object({
- projectId: v.string(),
- name: v.string(),
- type: v.picklist(["text", "number", "date", "dropdown", "boolean"]),
- required: v.optional(v.boolean(), false),
- defaultValue: v.optional(v.string()),
- options: v.optional(v.array(v.string())),
- }),
+ .openapi(getCustomFieldFilterValuesRoute, async (c) =>
+ c.json(
+ await getCustomFieldFilterValues(c.req.valid("param").projectId),
+ 200,
),
- workspaceAccess.fromProject("projectId"),
- requireWorkspacePermission({ project: ["update"] }),
- async (c) => {
- const { projectId, name, type, required, defaultValue, options } =
- c.req.valid("json");
+ )
+ .openapi(createCustomFieldRoute, async (c) => {
+ const { projectId, name, type, required, defaultValue, options } =
+ c.req.valid("json");
- const field = await createCustomField(
+ return c.json(
+ await createCustomField(
projectId,
name,
type,
required,
defaultValue,
options,
- );
- return c.json(field);
- },
- )
- .put(
- "/reorder/:projectId",
- describeRoute({
- operationId: "reorderCustomFields",
- tags: ["Custom Fields"],
- description: "Reorder custom fields in a project",
- responses: {
- 200: {
- description: "Custom fields reordered successfully",
- content: {
- "application/json": { schema: resolver(v.any()) },
- },
- },
- },
- }),
- validator("param", v.object({ projectId: v.string() })),
- validator(
- "json",
- v.object({
- fields: v.array(
- v.object({
- id: v.string(),
- position: v.number(),
- }),
- ),
- }),
- ),
- workspaceAccess.fromProject("projectId"),
- requireWorkspacePermission({ project: ["update"] }),
- async (c) => {
- const { projectId } = c.req.valid("param");
- const { fields } = c.req.valid("json");
- const result = await reorderCustomFields(projectId, fields);
- return c.json(result);
- },
- )
- .put(
- "/value",
- describeRoute({
- operationId: "setCustomFieldValue",
- tags: ["Custom Fields"],
- description: "Set (create or update) a custom field value for a task",
- responses: {
- 200: {
- description: "Custom field value set successfully",
- content: {
- "application/json": {
- schema: resolver(
- v.object({
- id: v.string(),
- taskId: v.string(),
- fieldId: v.string(),
- value: v.nullable(v.string()),
- }),
- ),
- },
- },
- },
- },
- }),
- validator(
- "json",
- v.object({
- taskId: v.string(),
- fieldId: v.string(),
- value: v.string(),
- }),
- ),
- workspaceAccess.fromTaskId("taskId"),
- requireWorkspacePermission({ task: ["update"] }),
- async (c) => {
- const { taskId, fieldId, value } = c.req.valid("json");
+ ),
+ 200,
+ );
+ })
+ .openapi(reorderCustomFieldsRoute, async (c) => {
+ const { projectId } = c.req.valid("param");
+ const { fields } = c.req.valid("json");
- const result = await setCustomFieldValue(taskId, fieldId, value);
+ return c.json(await reorderCustomFields(projectId, fields), 200);
+ })
+ .openapi(setCustomFieldValueRoute, async (c) => {
+ const { taskId, fieldId, value } = c.req.valid("json");
- return c.json(result);
- },
- )
- .delete(
- "/:id",
- describeRoute({
- operationId: "deleteCustomField",
- tags: ["Custom Fields"],
- description: "Delete a custom field definition by ID",
- responses: {
- 200: {
- description: "Custom field deleted successfully",
- content: {
- "application/json": {
- schema: resolver(customFieldDefinitionSchema),
- },
- },
- },
- },
- }),
- validator("param", v.object({ id: v.string() })),
- workspaceAccess.fromCustomField(),
- requireWorkspacePermission({ project: ["update"] }),
- async (c) => {
- const { id } = c.req.valid("param");
- const field = await deleteCustomField(id);
- return c.json(field);
- },
+ return c.json(await setCustomFieldValue(taskId, fieldId, value), 200);
+ })
+ .openapi(deleteCustomFieldRoute, async (c) =>
+ c.json(await deleteCustomField(c.req.valid("param").id), 200),
);
export default customField;
diff --git a/apps/api/src/custom-field/response.ts b/apps/api/src/custom-field/response.ts
new file mode 100644
index 000000000..468078480
--- /dev/null
+++ b/apps/api/src/custom-field/response.ts
@@ -0,0 +1,61 @@
+import { responseTimestamp, z } from "../openapi";
+
+export const customFieldDefinitionSchema = z
+ .object({
+ id: z.string(),
+ projectId: z.string(),
+ name: z.string(),
+ type: z.string(),
+ required: z.boolean(),
+ defaultValue: z.string().nullable(),
+ options: z.unknown().nullable(),
+ position: z.number(),
+ createdAt: responseTimestamp,
+ updatedAt: responseTimestamp,
+ })
+ .openapi("CustomFieldDefinition");
+
+export const customFieldDefinitionListSchema = z.array(
+ customFieldDefinitionSchema,
+);
+
+export const customFieldValueSchema = z
+ .object({
+ id: z.string(),
+ taskId: z.string(),
+ fieldId: z.string(),
+ value: z.string().nullable(),
+ fieldName: z.string(),
+ fieldPosition: z.number(),
+ fieldType: z.string(),
+ fieldOptions: z.unknown().nullable(),
+ })
+ .openapi("CustomFieldValue");
+
+export const customFieldValueListSchema = z.array(customFieldValueSchema);
+
+export const customFieldFilterValuesSchema = z
+ .object({
+ fieldId: z.string(),
+ fieldName: z.string(),
+ fieldType: z.string(),
+ values: z.array(z.string()),
+ })
+ .openapi("CustomFieldFilterValues");
+
+export const customFieldFilterValuesListSchema = z.array(
+ customFieldFilterValuesSchema,
+);
+
+export const setCustomFieldValueResponseSchema = z
+ .object({
+ id: z.string(),
+ taskId: z.string(),
+ fieldId: z.string(),
+ value: z.string().nullable(),
+ })
+ .openapi("SetCustomFieldValueResponse");
+
+export const reorderCustomFieldsResponseSchema = z
+ .unknown()
+ .openapi("ReorderCustomFieldsResponse");
diff --git a/apps/api/src/custom-field/schema.ts b/apps/api/src/custom-field/schema.ts
new file mode 100644
index 000000000..8b9cfb878
--- /dev/null
+++ b/apps/api/src/custom-field/schema.ts
@@ -0,0 +1,37 @@
+import { z } from "../openapi";
+
+export const projectIdParam = z.object({
+ projectId: z.string(),
+});
+
+export const taskIdParam = z.object({
+ taskId: z.string(),
+});
+
+export const customFieldIdParam = z.object({
+ id: z.string(),
+});
+
+export const createCustomFieldBody = z.object({
+ projectId: z.string(),
+ name: z.string(),
+ type: z.enum(["text", "number", "date", "dropdown", "boolean"]),
+ required: z.boolean().optional().default(false),
+ defaultValue: z.string().optional(),
+ options: z.array(z.string()).optional(),
+});
+
+export const reorderCustomFieldsBody = z.object({
+ fields: z.array(
+ z.object({
+ id: z.string(),
+ position: z.number(),
+ }),
+ ),
+});
+
+export const setCustomFieldValueBody = z.object({
+ taskId: z.string(),
+ fieldId: z.string(),
+ value: z.string(),
+});
diff --git a/apps/api/src/task/index.ts b/apps/api/src/task/index.ts
index 430fa040f..919d49e35 100644
--- a/apps/api/src/task/index.ts
+++ b/apps/api/src/task/index.ts
@@ -581,8 +581,16 @@ const task = apiRouter<BaseVariables & { workspaceId: string }>()
})
.openapi(createTaskRoute, async (c) => {
const { projectId } = c.req.param();
- const { title, description, startDate, dueDate, priority, status, userId } =
- c.req.valid("json");
+ const {
+ title,
+ description,
+ startDate,
+ dueDate,
+ priority,
+ status,
+ userId,
+ customFields,
+ } = c.req.valid("json");
const parsedStartDate =
startDate !== undefined
@@ -605,6 +613,7 @@ const task = apiRouter<BaseVariables & { workspaceId: string }>()
dueDate: parsedDueDate,
priority,
status,
+ customFields,
});
return c.json(task, 200);
diff --git a/apps/api/src/task/response.ts b/apps/api/src/task/response.ts
index c0afb0037..dd5f7f56b 100644
--- a/apps/api/src/task/response.ts
+++ b/apps/api/src/task/response.ts
@@ -25,6 +25,9 @@ export const taskSchema = z
startDate: nullableResponseTimestamp,
dueDate: nullableResponseTimestamp,
createdAt: responseTimestamp,
+ customFields: z
+ .array(z.object({ fieldId: z.string(), value: z.string() }))
+ .optional(),
})
.openapi("Task");
diff --git a/apps/api/src/task/schema.ts b/apps/api/src/task/schema.ts
index a3ae0d563..88ffc9f5b 100644
--- a/apps/api/src/task/schema.ts
+++ b/apps/api/src/task/schema.ts
@@ -55,6 +55,9 @@ export const createTaskBody = z.object({
priority,
status: z.string().openapi({ description: "The target column's slug." }),
userId: z.string().optional().openapi({ description: "Assignee, if any." }),
+ customFields: z
+ .array(z.object({ fieldId: z.string(), value: z.string() }))
+ .optional(),
});
export const updateTaskBody = z.object({
diff --git a/apps/web/src/hooks/mutations/custom-field/use-create-task.tsx b/apps/web/src/hooks/mutations/custom-field/use-create-task.tsx
deleted file mode 100644
index 405ef1ff3..000000000
--- a/apps/web/src/hooks/mutations/custom-field/use-create-task.tsx
+++ /dev/null
@@ -1,40 +0,0 @@
-import { useMutation, useQueryClient } from "@tanstack/react-query";
-import createTask, {
- type CreateTaskRequest,
-} from "@/fetchers/task/create-task";
-
-function useCreateTask() {
- const queryClient = useQueryClient();
-
- return useMutation({
- mutationFn: ({
- title,
- description,
- userId,
- projectId,
- status,
- startDate,
- dueDate,
- priority,
- customFields,
- }: CreateTaskRequest) =>
- createTask(
- title,
- description,
- projectId,
- userId ?? "",
- status,
- startDate ? new Date(startDate) : undefined,
- dueDate ? new Date(dueDate) : undefined,
- priority,
- customFields,
- ),
- onSuccess: (_data, variables) => {
- void queryClient.invalidateQueries({
- queryKey: ["tasks", variables.projectId],
- });
- },
- });
-}
-
-export default useCreateTask;