diff --git a/.changeset/great-ends-dream.md b/.changeset/great-ends-dream.md new file mode 100644 index 00000000..b5ad0545 --- /dev/null +++ b/.changeset/great-ends-dream.md @@ -0,0 +1,5 @@ +--- +"@stephansama/typed-nocodb-api": minor +--- + +updated references implementation diff --git a/core/typed-nocodb-api/src/index.ts b/core/typed-nocodb-api/src/index.ts index 57540eef..b2991c2c 100644 --- a/core/typed-nocodb-api/src/index.ts +++ b/core/typed-nocodb-api/src/index.ts @@ -10,21 +10,31 @@ export const ACTIONS = [ ] as const; export type ACTION = (typeof ACTIONS)[number]; -export function createApi({ +export function createApi< + Schema extends z.ZodObject, + References extends Array, +>({ baseId, origin, + references, schema, tableId, token, }: { baseId: string; origin: string; + references?: References; schema: Schema; tableId: string; token?: string; }) { let _token: string | undefined = token; + const referenceSchema = + references && references.length !== 0 + ? z.record(z.enum(references), z.number()) + : z.object({}); + const api = { COUNT: { method: "get", @@ -37,7 +47,12 @@ export function createApi({ inputSchema: z.object({ fields: schema }), method: "post", responseSchema: z.object({ - records: z.array(z.object({ fields: schema, id: z.string() })), + records: z.array( + z.object({ + fields: schema.and(referenceSchema), + id: z.number(), + }), + ), }), url: `/api/v3/data/${baseId}/${tableId}/records`, }, @@ -63,7 +78,12 @@ export function createApi({ nestedPrev: z.string().optional().nullable(), next: z.string().optional().nullable(), prev: z.string().optional().nullable(), - records: z.array(z.object({ fields: schema, id: z.number() })), + records: z.array( + z.object({ + fields: schema.and(referenceSchema), + id: z.number(), + }), + ), }), url: `/api/v3/data/${baseId}/${tableId}/records`, }, @@ -73,9 +93,16 @@ export function createApi({ url: `/api/v3/data/${baseId}/${tableId}/records/{recordId}`, }, UPDATE: { - inputSchema: z.object({ fields: schema, id: z.string() }), + inputSchema: z.object({ fields: schema, id: z.number() }), method: "patch", - responseSchema: z.object(), + responseSchema: z.object({ + records: z.array( + z.object({ + fields: schema.and(referenceSchema), + id: z.number(), + }), + ), + }), url: `/api/v3/data/${baseId}/${tableId}/records`, }, } satisfies Record< diff --git a/core/typed-nocodb-api/test/index.test.ts b/core/typed-nocodb-api/test/index.test.ts index d33cd5ae..abaedb4d 100644 --- a/core/typed-nocodb-api/test/index.test.ts +++ b/core/typed-nocodb-api/test/index.test.ts @@ -117,7 +117,7 @@ describe("typed-nocodb-api", () => { it("should perform CREATE action", async () => { const newRecord = { completed: false, title: "New Task" }; const mockResponse = { - records: [{ fields: newRecord, id: "123" }], + records: [{ fields: newRecord, id: 123 }], }; mockFetch.mockResolvedValue({ @@ -169,10 +169,15 @@ describe("typed-nocodb-api", () => { it("should perform UPDATE action", async () => { const updateData = { fields: { completed: true, title: "Updated" }, - id: "123", + id: 123, + }; + + const newRecord = { completed: false, title: "New Task" }; + const mockResponse = { + records: [{ fields: newRecord, id: 123 }], }; mockFetch.mockResolvedValue({ - json: async () => ({}), + json: async () => mockResponse, ok: true, statusText: "OK", });