From 9b09b93e234d784ca1cf0605eef0760d38dab12d Mon Sep 17 00:00:00 2001 From: Julien Ripouteau Date: Wed, 1 May 2024 19:29:11 +0200 Subject: [PATCH] refactor: generate cleaner type --- packages/core/commands/generate.ts | 6 ++-- .../core/src/codegen/api_types_generator.ts | 8 ++--- .../core/tests/api_types_generator.spec.ts | 10 +++--- packages/utils/src/types.ts | 12 +++++++ playground/.adonisjs/types/api.d.ts | 32 +++++++++++++------ 5 files changed, 47 insertions(+), 21 deletions(-) diff --git a/packages/core/commands/generate.ts b/packages/core/commands/generate.ts index 9adf7e4..7f07ad6 100644 --- a/packages/core/commands/generate.ts +++ b/packages/core/commands/generate.ts @@ -1,5 +1,5 @@ import { Project, QuoteKind } from 'ts-morph' -import { BaseCommand } from '@adonisjs/core/ace' +import { BaseCommand, flags } from '@adonisjs/core/ace' import type { CommandOptions } from '@adonisjs/core/types/ace' import { ApiTypesGenerator } from '../src/codegen/api_types_generator.js' @@ -7,9 +7,11 @@ import { ApiTypesGenerator } from '../src/codegen/api_types_generator.js' export default class CodegenTypes extends BaseCommand { static override commandName = 'tuyau:generate' static override description = 'Tuyau generator command' - static override options: CommandOptions = { startApp: true } + @flags.boolean({ description: 'Verbose logs', default: false, alias: 'v' }) + declare verbose: boolean + /** * Get routes from the router instance */ diff --git a/packages/core/src/codegen/api_types_generator.ts b/packages/core/src/codegen/api_types_generator.ts index d01f402..6e4afcd 100644 --- a/packages/core/src/codegen/api_types_generator.ts +++ b/packages/core/src/codegen/api_types_generator.ts @@ -147,9 +147,7 @@ export class ApiTypesGenerator { file.insertText(0, (writer) => { writer - .writeLine( - `import type { MakeOptional, Serialize, Simplify, ConvertReturnTypeToRecordStatusResponse } from '@tuyau/utils/types'`, - ) + .writeLine(`import type { MakeTuyauRequest, MakeTuyauResponse } from '@tuyau/utils/types'`) .writeLine(`import type { InferInput } from '@vinejs/vine/types'`) .newLine() .writeLine(`export interface AdonisApi {`) @@ -242,8 +240,8 @@ export class ApiTypesGenerator { currentLevel['$url'] = {} for (const method of methods) { currentLevel[method] = { - request: schemaImport ? `MakeOptional<${schemaImport}>` : 'unknown', - response: `Simplify>>>>`, + request: schemaImport ? `MakeTuyauRequest<${schemaImport}>` : 'unknown', + response: `MakeTuyauResponse`, } } } diff --git a/packages/core/tests/api_types_generator.spec.ts b/packages/core/tests/api_types_generator.spec.ts index f607b8e..db8d7db 100644 --- a/packages/core/tests/api_types_generator.spec.ts +++ b/packages/core/tests/api_types_generator.spec.ts @@ -54,7 +54,7 @@ test.group('Api Types Generator', () => { const file = await fs.contents('./.adonisjs/types/api.d.ts') assert.snapshot(file).matchInline(` - "import type { MakeOptional, Serialize, Simplify, ConvertReturnTypeToRecordStatusResponse } from '@tuyau/utils/types' + "import type { MakeTuyauRequest, MakeTuyauResponse } from '@tuyau/utils/types' import type { InferInput } from '@vinejs/vine/types' export interface AdonisApi { @@ -63,7 +63,7 @@ test.group('Api Types Generator', () => { }; '$get': { 'request': unknown; - 'response': Simplify>>>>; + 'response': MakeTuyauResponse; }; }; } @@ -117,7 +117,7 @@ test.group('Api Types Generator', () => { const file = await fs.contents('./.adonisjs/types/api.d.ts') assert.snapshot(file).matchInline(` - "import type { MakeOptional, Serialize, Simplify, ConvertReturnTypeToRecordStatusResponse } from '@tuyau/utils/types' + "import type { MakeTuyauRequest, MakeTuyauResponse } from '@tuyau/utils/types' import type { InferInput } from '@vinejs/vine/types' export interface AdonisApi { @@ -125,8 +125,8 @@ test.group('Api Types Generator', () => { '$url': { }; '$get': { - 'request': MakeOptional>; - 'response': Simplify>>>>; + 'request': MakeTuyauRequest>; + 'response': MakeTuyauResponse; }; }; } diff --git a/packages/utils/src/types.ts b/packages/utils/src/types.ts index 009efd9..e0f603c 100644 --- a/packages/utils/src/types.ts +++ b/packages/utils/src/types.ts @@ -89,3 +89,15 @@ type UndefinedProps = { * type Bar = MakeOptional // { a: string, b?: number, c: boolean } */ export type MakeOptional = UndefinedProps & Omit> + +/** + * Shortcut for computing the Tuyau response type + */ +export type MakeTuyauResponse any> = Simplify< + Serialize>>> +> + +/** + * Shortcut for computing the Tuyau request type + */ +export type MakeTuyauRequest = MakeOptional diff --git a/playground/.adonisjs/types/api.d.ts b/playground/.adonisjs/types/api.d.ts index af63b7b..e38fa43 100644 --- a/playground/.adonisjs/types/api.d.ts +++ b/playground/.adonisjs/types/api.d.ts @@ -1,23 +1,37 @@ -import type { MakeOptional, Serialize, Simplify, ConvertReturnTypeToRecordStatusResponse } from '@tuyau/utils/types' +import type { MakeTuyauRequest, MakeTuyauResponse } from '@tuyau/utils/types' import type { InferInput } from '@vinejs/vine/types' export interface AdonisApi { 'users': { - 'get': { - 'request': MakeOptional>; - 'response': Simplify>>>>; + '$url': { + }; + '$get': { + 'request': MakeTuyauRequest>; + 'response': MakeTuyauResponse; + }; + '$head': { + 'request': MakeTuyauRequest>; + 'response': MakeTuyauResponse; }; }; 'simple-text': { - 'get': { + '$url': { + }; + '$get': { 'request': unknown; - 'response': Simplify>>>>; + 'response': MakeTuyauResponse; + }; + '$head': { + 'request': unknown; + 'response': MakeTuyauResponse; }; }; 'file-upload': { - 'post': { - 'request': MakeOptional>; - 'response': Simplify>>>>; + '$url': { + }; + '$post': { + 'request': MakeTuyauRequest>; + 'response': MakeTuyauResponse; }; }; }