From 755b461e46f9b9e3935803faec77f7fd3580c960 Mon Sep 17 00:00:00 2001 From: suhaotian Date: Wed, 9 Oct 2024 23:03:13 +1100 Subject: [PATCH] feat: enhance script for `from` word and add `Expand` to API generate --- .../server/src/modules/todo/Todo.entity.ts | 20 ++++++++++++++++++- packages/tsdk/fe-sdk-template/src/gen-api.ts | 14 +++++++++++++ packages/tsdk/src/sync-api.ts | 6 +++--- packages/tsdk/src/transform-typeorm-entity.ts | 13 +++++++++++- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/examples/server/src/modules/todo/Todo.entity.ts b/examples/server/src/modules/todo/Todo.entity.ts index 192691e..9596e9f 100644 --- a/examples/server/src/modules/todo/Todo.entity.ts +++ b/examples/server/src/modules/todo/Todo.entity.ts @@ -1,4 +1,4 @@ -import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; +import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; // from import { CreatedUpdatedAt } from '@/src/db/entity/CreatedUpdated.entity'; @@ -34,4 +34,22 @@ export class Todo extends CreatedUpdatedAt { }) /** remark */ remark?: string; + + @Column({ + nullable: true, // from + }) + /** for test only */ + from?: string; + + @Column({ + nullable: true, + }) + /** for test only */ + _from?: string; + + @Column({ + nullable: true, + }) + /** for test only from */ + from_?: string; } diff --git a/packages/tsdk/fe-sdk-template/src/gen-api.ts b/packages/tsdk/fe-sdk-template/src/gen-api.ts index 87a2b23..d34e68d 100644 --- a/packages/tsdk/fe-sdk-template/src/gen-api.ts +++ b/packages/tsdk/fe-sdk-template/src/gen-api.ts @@ -68,3 +68,17 @@ export default function genAPICall( return APICall; } + +export type Expand = T extends (...args: infer A) => infer R + ? (...args: Expand) => Expand + : T extends infer O + ? { [K in keyof O]: O[K] } + : never; + +export type ExpandRecursively = T extends (...args: infer A) => infer R + ? (...args: ExpandRecursively) => ExpandRecursively + : T extends object + ? T extends infer O + ? { [K in keyof O]: ExpandRecursively } + : never + : T; diff --git a/packages/tsdk/src/sync-api.ts b/packages/tsdk/src/sync-api.ts index 22249ac..a6f5bb9 100644 --- a/packages/tsdk/src/sync-api.ts +++ b/packages/tsdk/src/sync-api.ts @@ -88,7 +88,7 @@ export async function syncAPI() { * **/ - import genApi from './gen-api'; + import genApi, { Expand } from './gen-api'; `; @@ -155,9 +155,9 @@ export async function syncAPI() { * * @category ${category} */ - export const ${name} = genApi<${name}Req${ + export const ${name} = genApi${ isGET ? '' : ' | FormData' - }, ${name}Res>(${name}Config); + }, Expand<${name}Res>>(${name}Config); `; dataHookImportStr += ` diff --git a/packages/tsdk/src/transform-typeorm-entity.ts b/packages/tsdk/src/transform-typeorm-entity.ts index 9724c6f..58c9ac9 100644 --- a/packages/tsdk/src/transform-typeorm-entity.ts +++ b/packages/tsdk/src/transform-typeorm-entity.ts @@ -11,7 +11,18 @@ export function transformTypeormEntity(_fileContent: string, entityLibName: stri result.forEach((i) => { const hasImport = i.indexOf('import ') > -1; - const hasFrom = i.indexOf(' from') > -1; + const fromIdx = i.indexOf(' from'); + + const commentIdx = i.indexOf('//'); + const commentIdx2 = i.indexOf('/*'); + const commentIdx3 = i.indexOf('*/'); + + const isComment = + (commentIdx > -1 && commentIdx < fromIdx) || + (commentIdx2 > -1 && commentIdx2 < fromIdx) || + (commentIdx3 > -1 && commentIdx3 > fromIdx); + + const hasFrom = fromIdx > -1 && !isComment && (i[fromIdx + 5] || '').trim() === ''; const hasTypeormFrom = hasFrom && (i.indexOf(` '${entityLibName}`) > -1 || i.indexOf(` "${entityLibName}`) > -1);