Skip to content

Commit

Permalink
feat: Alternative default import configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Apr 16, 2021
1 parent 2c470f8 commit 4ae1b82
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```
Expand All @@ -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`

Expand Down
4 changes: 3 additions & 1 deletion src/helpers/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ export function createConfig(data: Record<string, string | undefined>) {
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];
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/field-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type FieldSetting = {
input: boolean;
output: boolean;
from: string;
defaultImport?: string;
defaultImport?: string | true;
namespaceImport?: string;
};

Expand Down
4 changes: 2 additions & 2 deletions src/helpers/import-declaration-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: `
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 4ae1b82

Please sign in to comment.