Skip to content

Commit

Permalink
update types to handle possible wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Apr 17, 2024
1 parent e85e4e6 commit 1636901
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/no-unsafe-member-access": "off"
}
},
{
"files": ["__generated__/**/*"],
"rules": {
"@typescript-eslint/no-unused-vars": "off"
}
}
]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"build": "rm -rf ./dist && tsc",
"lint": "eslint .",
"format": "prettier --write .",
"checkTs": "tsc --noEmit"
"checkTs": "tsc --noEmit",
"codegen:dev": "pnpm build && ./baserow.js -d > out.txt"
},
"keywords": [],
"author": "",
Expand Down
4 changes: 2 additions & 2 deletions src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export default async function main({
.filter((t) => !!t);

const modelImports = isDev
? `import { BaserowSdk, Row } from '${__dirname}/index.js'`
: "import { BaserowSdk, Row } from 'baserow-sdk'";
? `import { BaserowSdk, Row, FieldValue } from '${__dirname}/index.js'`
: "import { BaserowSdk, Row, FieldValue } from 'baserow-sdk'";
const typeDef = `export type ${tableName}RowType = ${makeType(fields)}
${modelImports}
Expand Down
9 changes: 9 additions & 0 deletions src/codegen/getRawType.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, it, expect } from "vitest";
import { getRawType } from "./getRawType";
import f from "../test/fixtures/fieldDefinition";

describe("getRawType", () => {
it("runs", () => {
expect(getRawType(f({ type: "text" }))).toBe("string");
});
});
4 changes: 2 additions & 2 deletions src/codegen/makeFieldType.ts → src/codegen/getRawType.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { FieldDefinition } from "../index.js";
import { mapPrimitive } from "./mapPrimitive.js";

export function makeFieldType(field: FieldDefinition): string {
export function getRawType(field: FieldDefinition): string {
if (field.type === "link_row") {
return '{ "id": number, "value": string }[]';
}

if (["rollup", "formula", "lookup"].includes(field.type)) {
switch (field.formula_type) {
case "array":
return `(${mapPrimitive(field.array_formula_type)})[]`;
return `FieldValue<${mapPrimitive(field.array_formula_type)}>[]`;
default:
return mapPrimitive(field.formula_type);
}
Expand Down
6 changes: 3 additions & 3 deletions src/codegen/makeGetter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Table } from "../codegen.js";
import { FieldDefinition, ListFieldsResponse } from "../index.js";
import { makeFieldType } from "./makeFieldType.js";
import { getRawType } from "./getRawType.js";
import { toCamelCase } from "./toCamelCase.js";

function getForeignTable(
Expand Down Expand Up @@ -42,11 +42,11 @@ function getReturnType(field: FieldDefinition, tables: Table[]): string {
return "number";
}

return makeFieldType(field);
return getRawType(field);
}

function getBody(field: FieldDefinition, tables: Table[]): string {
const rawType = makeFieldType(field);
const rawType = getRawType(field);
const query = `this.getField<${rawType}>("${field.name}")`;

if (field.type === "number" || field.formula_type === "number") {
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/makeSetter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { FieldDefinition } from "../index.js";
import { makeFieldType } from "./makeFieldType.js";
import { getRawType } from "./getRawType.js";
import { toCamelCase } from "./toCamelCase.js";

function getInputType(field: FieldDefinition): string {
if (field.type === "link_row") {
return "number[]";
}

return makeFieldType(field);
return getRawType(field);
}

export function makeSetter(field: FieldDefinition): string {
Expand Down
12 changes: 6 additions & 6 deletions src/codegen/makeType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe("makeType", () => {
array_formula_type: "text",
}),
]),
).toContain("(string)[]");
).toContain("FieldValue<string>[]");
});

it("handles formula type array number", () => {
Expand All @@ -87,7 +87,7 @@ describe("makeType", () => {
array_formula_type: "number",
}),
]),
).toContain("(number | string)[]");
).toContain("FieldValue<number | string>[]");
});

it("handles formula type array boolean", () => {
Expand All @@ -99,7 +99,7 @@ describe("makeType", () => {
array_formula_type: "boolean",
}),
]),
).toContain("(boolean)[]");
).toContain("FieldValue<boolean>[]");
});

it("quotes field names", () => {
Expand Down Expand Up @@ -187,7 +187,7 @@ describe("makeType", () => {
array_formula_type: "text",
}),
]),
).toContain("(string)[]");
).toContain("FieldValue<string>[]");
});

it("handles rollup type array number", () => {
Expand All @@ -199,7 +199,7 @@ describe("makeType", () => {
array_formula_type: "number",
}),
]),
).toContain("(number | string)[]");
).toContain("FieldValue<number | string>[]");
});

it("handles link row", () => {
Expand All @@ -221,7 +221,7 @@ describe("makeType", () => {
array_formula_type: "number",
}),
]),
).toContain("(number | string)[]");
).toContain("FieldValue<number | string>[]");
});

it("handles rollup type date", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/makeType.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ListFieldsResponse } from "../index.js";
import { makeFieldType } from "./makeFieldType.js";
import { getRawType } from "./getRawType.js";

export default function makeType(fields: ListFieldsResponse): string {
let typeDef = `{\n`;
Expand All @@ -8,7 +8,7 @@ export default function makeType(fields: ListFieldsResponse): string {
typeDef += ' "order": string;\n';

fields.forEach((field) => {
typeDef += ` "${field.name}": ${makeFieldType(field)};\n`;
typeDef += ` "${field.name}": ${getRawType(field)};\n`;
});

typeDef += `}`;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import c from "./client.js";

export * from "./row.js";
export * from "./factory.js";
export * from "./types.js";

export type AddRowOptions = {
user_field_names?: boolean;
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type FieldValue<T> =
| T
| { id: number; value: T }
| { ids: Record<string, number>; value: T };

0 comments on commit 1636901

Please sign in to comment.