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
5 changes: 5 additions & 0 deletions .changeset/great-ends-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stephansama/typed-nocodb-api": minor
---

updated references implementation
37 changes: 32 additions & 5 deletions core/typed-nocodb-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,31 @@ export const ACTIONS = [
] as const;
export type ACTION = (typeof ACTIONS)[number];

export function createApi<Schema extends z.ZodObject>({
export function createApi<
Schema extends z.ZodObject,
References extends Array<string>,
>({
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({});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

const api = {
COUNT: {
method: "get",
Expand All @@ -37,7 +47,12 @@ export function createApi<Schema extends z.ZodObject>({
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(),
}),
),
}),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
url: `/api/v3/data/${baseId}/${tableId}/records`,
},
Expand All @@ -63,7 +78,12 @@ export function createApi<Schema extends z.ZodObject>({
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`,
},
Expand All @@ -73,9 +93,16 @@ export function createApi<Schema extends z.ZodObject>({
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<
Expand Down
11 changes: 8 additions & 3 deletions core/typed-nocodb-api/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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",
});
Expand Down
Loading