From a74eac2e1f79cd3faab2ff1f08523ed090015661 Mon Sep 17 00:00:00 2001 From: Patrick Moody <4031292+patmood@users.noreply.github.com> Date: Wed, 21 Jun 2023 11:21:57 -0700 Subject: [PATCH] always include generic argument for expand (#67) * always include generic argument for expand * update snapshots --- dist/index.js | 55 +++++++++++------------- package.json | 2 +- src/generics.ts | 7 +-- src/lib.ts | 11 +++-- test/__snapshots__/fromJSON.test.ts.snap | 8 ++-- test/__snapshots__/lib.test.ts.snap | 4 +- test/generics.test.ts | 10 ----- test/pocketbase-types-example.ts | 8 ++-- 8 files changed, 43 insertions(+), 62 deletions(-) diff --git a/dist/index.js b/dist/index.js index 3741c45..bfbf678 100755 --- a/dist/index.js +++ b/dist/index.js @@ -83,33 +83,6 @@ var AUTH_SYSTEM_FIELDS_DEFINITION = `export type AuthSystemFields = { verified: boolean } & BaseSystemFields`; -// src/generics.ts -function fieldNameToGeneric(name) { - return `T${name}`; -} -function getGenericArgList(schema) { - const jsonFields = schema.filter((field) => field.type === "json").map((field) => fieldNameToGeneric(field.name)).sort(); - return jsonFields; -} -function getGenericArgStringForRecord(schema) { - const argList = getGenericArgList(schema); - if (argList.length === 0) - return ""; - return `<${argList.map((name) => `${name}`).join(", ")}>`; -} -function getGenericArgStringWithDefault(schema, opts) { - const argList = getGenericArgList(schema); - if (opts.includeExpand && canExpand(schema)) { - argList.push(fieldNameToGeneric(EXPAND_GENERIC_NAME)); - } - if (argList.length === 0) - return ""; - return `<${argList.map((name) => `${name} = unknown`).join(", ")}>`; -} -function canExpand(schema) { - return !!schema.find((field) => field.type === "relation"); -} - // src/utils.ts import { promises as fs2 } from "fs"; function toPascalCase(str) { @@ -167,6 +140,30 @@ ${nameRecordMap} }`; } +// src/generics.ts +function fieldNameToGeneric(name) { + return `T${name}`; +} +function getGenericArgList(schema) { + const jsonFields = schema.filter((field) => field.type === "json").map((field) => fieldNameToGeneric(field.name)).sort(); + return jsonFields; +} +function getGenericArgStringForRecord(schema) { + const argList = getGenericArgList(schema); + if (argList.length === 0) + return ""; + return `<${argList.map((name) => `${name}`).join(", ")}>`; +} +function getGenericArgStringWithDefault(schema, opts) { + const argList = getGenericArgList(schema); + if (opts.includeExpand) { + argList.push(fieldNameToGeneric(EXPAND_GENERIC_NAME)); + } + if (argList.length === 0) + return ""; + return `<${argList.map((name) => `${name} = unknown`).join(", ")}>`; +} + // src/fields.ts var pbSchemaTypescriptMap = { bool: "boolean", @@ -257,7 +254,7 @@ function createResponseType(collectionSchemaEntry) { }); const genericArgsForRecord = getGenericArgStringForRecord(schema); const systemFields = getSystemFields(type); - const expandArgString = canExpand(schema) ? `` : ""; + const expandArgString = ``; return `export type ${pascaleName}Response${genericArgsWithDefaults} = Required<${pascaleName}Record${genericArgsForRecord}> & ${systemFields}${expandArgString}`; } @@ -284,7 +281,7 @@ async function main(options2) { import { program } from "commander"; // package.json -var version = "1.1.9"; +var version = "1.1.10"; // src/index.ts program.name("Pocketbase Typegen").version(version).description( diff --git a/package.json b/package.json index 7e2e8a3..b1526db 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pocketbase-typegen", - "version": "1.1.9", + "version": "1.1.10", "description": "Generate pocketbase record types from your database", "main": "dist/index.js", "bin": { diff --git a/src/generics.ts b/src/generics.ts index 7e71870..10cd8b1 100644 --- a/src/generics.ts +++ b/src/generics.ts @@ -25,15 +25,10 @@ export function getGenericArgStringWithDefault( ): string { const argList = getGenericArgList(schema) - if (opts.includeExpand && canExpand(schema)) { + if (opts.includeExpand) { argList.push(fieldNameToGeneric(EXPAND_GENERIC_NAME)) } if (argList.length === 0) return "" return `<${argList.map((name) => `${name} = unknown`).join(", ")}>` } - -// Does the collection have relation fields that can be expanded -export function canExpand(schema: FieldSchema[]) { - return !!schema.find((field) => field.type === "relation") -} diff --git a/src/lib.ts b/src/lib.ts index b194d6f..5ff9394 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -9,17 +9,16 @@ import { RESPONSE_TYPE_COMMENT, } from "./constants" import { CollectionRecord, FieldSchema } from "./types" -import { - canExpand, - getGenericArgStringForRecord, - getGenericArgStringWithDefault, -} from "./generics" import { createCollectionEnum, createCollectionRecords, createCollectionResponses, } from "./collections" import { createSelectOptions, createTypeField } from "./fields" +import { + getGenericArgStringForRecord, + getGenericArgStringWithDefault, +} from "./generics" import { getSystemFields, toPascalCase } from "./utils" export function generate(results: Array): string { @@ -87,7 +86,7 @@ export function createResponseType( }) const genericArgsForRecord = getGenericArgStringForRecord(schema) const systemFields = getSystemFields(type) - const expandArgString = canExpand(schema) ? `` : "" + const expandArgString = `` return `export type ${pascaleName}Response${genericArgsWithDefaults} = Required<${pascaleName}Record${genericArgsForRecord}> & ${systemFields}${expandArgString}` } diff --git a/test/__snapshots__/fromJSON.test.ts.snap b/test/__snapshots__/fromJSON.test.ts.snap index d4672a3..e4f7d3d 100644 --- a/test/__snapshots__/fromJSON.test.ts.snap +++ b/test/__snapshots__/fromJSON.test.ts.snap @@ -92,12 +92,12 @@ export type UsersRecord = { } // Response types include system fields and match responses from the PocketBase API -export type BaseResponse = Required & BaseSystemFields -export type CustomAuthResponse = Required & AuthSystemFields +export type BaseResponse = Required & BaseSystemFields +export type CustomAuthResponse = Required & AuthSystemFields export type EverythingResponse = Required> & BaseSystemFields export type MyViewResponse = Required> & BaseSystemFields -export type PostsResponse = Required & BaseSystemFields -export type UsersResponse = Required & AuthSystemFields +export type PostsResponse = Required & BaseSystemFields +export type UsersResponse = Required & AuthSystemFields // Types containing all Records and Responses, useful for creating typing helper functions diff --git a/test/__snapshots__/lib.test.ts.snap b/test/__snapshots__/lib.test.ts.snap index 2894df9..5c33e64 100644 --- a/test/__snapshots__/lib.test.ts.snap +++ b/test/__snapshots__/lib.test.ts.snap @@ -12,7 +12,7 @@ exports[`createRecordType handles file fields with multiple files 1`] = ` }" `; -exports[`createResponseType creates type definition for a response 1`] = `"export type BooksResponse = Required & BaseSystemFields"`; +exports[`createResponseType creates type definition for a response 1`] = `"export type BooksResponse = Required & BaseSystemFields"`; exports[`createResponseType handles file fields with multiple files 1`] = ` "export type BooksRecord = { @@ -58,7 +58,7 @@ export type BooksRecord = { } // Response types include system fields and match responses from the PocketBase API -export type BooksResponse = Required & BaseSystemFields +export type BooksResponse = Required & BaseSystemFields // Types containing all Records and Responses, useful for creating typing helper functions diff --git a/test/generics.test.ts b/test/generics.test.ts index 9cfb3d4..57770cf 100644 --- a/test/generics.test.ts +++ b/test/generics.test.ts @@ -1,5 +1,4 @@ import { - canExpand, getGenericArgList, getGenericArgStringForRecord, getGenericArgStringWithDefault, @@ -127,12 +126,3 @@ describe("getGenericArgStringForRecord", () => { ).toEqual("") }) }) - -describe("canExpand", () => { - it("detects collections that can be expanded", () => { - expect(canExpand([textField, jsonField1, expandField])).toEqual(true) - }) - it("detects collections that cannot be expanded", () => { - expect(canExpand([textField, jsonField1])).toEqual(false) - }) -}) diff --git a/test/pocketbase-types-example.ts b/test/pocketbase-types-example.ts index 9cfdfa8..5b952b7 100644 --- a/test/pocketbase-types-example.ts +++ b/test/pocketbase-types-example.ts @@ -89,12 +89,12 @@ export type UsersRecord = { } // Response types include system fields and match responses from the PocketBase API -export type BaseResponse = Required & BaseSystemFields -export type CustomAuthResponse = Required & AuthSystemFields +export type BaseResponse = Required & BaseSystemFields +export type CustomAuthResponse = Required & AuthSystemFields export type EverythingResponse = Required> & BaseSystemFields export type MyViewResponse = Required> & BaseSystemFields -export type PostsResponse = Required & BaseSystemFields -export type UsersResponse = Required & AuthSystemFields +export type PostsResponse = Required & BaseSystemFields +export type UsersResponse = Required & AuthSystemFields // Types containing all Records and Responses, useful for creating typing helper functions