diff --git a/README.md b/README.md index 7d622283..dfc57ac7 100644 --- a/README.md +++ b/README.md @@ -191,7 +191,7 @@ generator nestgraphql { fields_{Namespace}_from = "module specifier" fields_{Namespace}_input = true | false fields_{Namespace}_output = true | false - fields_{Namespace}_defaultImport = "default import name" + fields_{Namespace}_defaultImport = "default import name" | true fields_{Namespace}_namespaceImport = "namespace import name" } ``` @@ -213,7 +213,10 @@ Optional, default: `false`. Means that it will be applied on output types (class ##### `fields_{Namespace}_defaultImport` -Default import name, if module have no namespace +Default import name, if module have no namespace. +Type: `undefined | string | true` +Default: `undefined` +If defined as `true` then import name will be same as `{Namespace}` ##### `fields_{Namespace}_namespaceImport` diff --git a/src/helpers/create-config.ts b/src/helpers/create-config.ts index 2b1450f1..95f03224 100644 --- a/src/helpers/create-config.ts +++ b/src/helpers/create-config.ts @@ -61,7 +61,9 @@ export function createConfig(data: Record) { output: toBoolean(value.output), input: toBoolean(value.input), from: value.from, - defaultImport: value.defaultImport, + defaultImport: toBoolean(value.defaultImport) + ? true + : value.defaultImport, namespaceImport: value.namespaceImport, }; return [name, fieldSetting]; diff --git a/src/helpers/field-settings.ts b/src/helpers/field-settings.ts index 25f5a990..152a87f0 100644 --- a/src/helpers/field-settings.ts +++ b/src/helpers/field-settings.ts @@ -13,7 +13,7 @@ export type FieldSetting = { input: boolean; output: boolean; from: string; - defaultImport?: string; + defaultImport?: string | true; namespaceImport?: string; }; diff --git a/src/helpers/import-declaration-map.ts b/src/helpers/import-declaration-map.ts index c63bfb64..bb5d2398 100644 --- a/src/helpers/import-declaration-map.ts +++ b/src/helpers/import-declaration-map.ts @@ -25,7 +25,7 @@ export class ImportDeclarationMap extends Map< create(args: { name: string; from: string; - defaultImport?: string; + defaultImport?: string | true; namespaceImport?: string; }) { const { name, from, defaultImport, namespaceImport } = args; @@ -36,7 +36,7 @@ export class ImportDeclarationMap extends Map< namespaceImport: undefined as string | undefined, }; if (defaultImport) { - value.defaultImport = defaultImport; + value.defaultImport = defaultImport === true ? name : defaultImport; } else if (namespaceImport) { value.namespaceImport = namespaceImport; } else { diff --git a/src/test/custom-decorators.ts b/src/test/custom-decorators.spec.ts similarity index 86% rename from src/test/custom-decorators.ts rename to src/test/custom-decorators.spec.ts index 0c45cda2..6fe3ee62 100644 --- a/src/test/custom-decorators.ts +++ b/src/test/custom-decorators.spec.ts @@ -114,6 +114,7 @@ describe('custom decorators namespace both input and output', () => { describe('custom decorators default import', () => { let importDeclarations: any[]; + before(async () => { ({ project, sourceFiles } = await testGenerate({ schema: ` @@ -154,6 +155,41 @@ describe('custom decorators default import', () => { // it('^', () => console.log(sourceFile.getText())); }); + + describe('default import alternative syntax', () => { + before(async () => { + ({ project, sourceFiles } = await testGenerate({ + schema: ` + model User { + id Int @id + /// @IsEmail() + name String + }`, + options: [ + `outputFilePattern = "{name}.{type}.ts"`, + `fields_IsEmail_from = "isvalidemail"`, + `fields_IsEmail_input = true`, + `fields_IsEmail_defaultImport = true`, + ], + })); + setSourceFile('user-create.input.ts'); + }); + + it('test', () => { + importDeclarations = sourceFile + .getImportDeclarations() + .map(d => d.getStructure()) + .filter(d => d.moduleSpecifier === 'isvalidemail'); + expect(importDeclarations).toHaveLength(1); + expect(importDeclarations[0]).toEqual( + expect.objectContaining({ + defaultImport: 'IsEmail', + namedImports: [], + namespaceImport: undefined, + }), + ); + }); + }); }); describe('custom decorators field custom type namespace', () => {