Skip to content

Commit

Permalink
feat: enhance script for from word and add Expand<type> to API ge…
Browse files Browse the repository at this point in the history
…nerate
  • Loading branch information
suhaotian committed Oct 9, 2024
1 parent 198b3f9 commit 755b461
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
20 changes: 19 additions & 1 deletion examples/server/src/modules/todo/Todo.entity.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
}
14 changes: 14 additions & 0 deletions packages/tsdk/fe-sdk-template/src/gen-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,17 @@ export default function genAPICall<ReqPayload, ResData>(

return APICall;
}

export type Expand<T> = T extends (...args: infer A) => infer R
? (...args: Expand<A>) => Expand<R>
: T extends infer O
? { [K in keyof O]: O[K] }
: never;

export type ExpandRecursively<T> = T extends (...args: infer A) => infer R
? (...args: ExpandRecursively<A>) => ExpandRecursively<R>
: T extends object
? T extends infer O
? { [K in keyof O]: ExpandRecursively<O[K]> }
: never
: T;
6 changes: 3 additions & 3 deletions packages/tsdk/src/sync-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export async function syncAPI() {
*
**/
import genApi from './gen-api';
import genApi, { Expand } from './gen-api';
`;

Expand Down Expand Up @@ -155,9 +155,9 @@ export async function syncAPI() {
*
* @category ${category}
*/
export const ${name} = genApi<${name}Req${
export const ${name} = genApi<Expand<${name}Req>${
isGET ? '' : ' | FormData'
}, ${name}Res>(${name}Config);
}, Expand<${name}Res>>(${name}Config);
`;

dataHookImportStr += `
Expand Down
13 changes: 12 additions & 1 deletion packages/tsdk/src/transform-typeorm-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 755b461

Please sign in to comment.