Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: switch to named imports #205

Merged
merged 3 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,24 +208,24 @@ As input a [json file](https://github.com/contentful/contentful-cli/tree/master/

```typescript
import * as CFRichTextTypes from '@contentful/rich-text-types';
import * as Contentful from 'contentful';
import { Entry, EntryFields } from 'contentful';

export interface TypeArtistFields {
name: Contentful.EntryFields.Symbol;
profilePicture?: Contentful.Asset;
bio?: CFRichTextTypes.Block | CFRichTextTypes.Inline;
}

export type TypeArtist = Contentful.Entry<TypeArtistFields>;
export type TypeArtist = Entry<TypeArtistFields>;

export interface TypeArtworkFields {
name: Contentful.EntryFields.Symbol;
name: EntryFields.Symbol;
type?: 'print' | 'drawing' | 'painting';
preview?: Contentful.Asset[];
artist: Contentful.Entry<TypeArtistFields>;
preview?: Asset[];
artist: Entry<TypeArtistFields>;
}

export type TypeArtwork = Contentful.Entry<TypeArtworkFields>;
export type TypeArtwork = Entry<TypeArtworkFields>;
```

This all only works if you add the [`contentful`](https://www.npmjs.com/package/contentful) package to your target project to get all relevant type definitions.
Expand Down Expand Up @@ -404,13 +404,14 @@ const stringContent = new CFDefinitionsBuilder()

console.log(stringContent);

// import * as Contentful from "contentful";
// import { Entry, EntryFields } from "contentful";

//
// export interface TypeMyEntryFields {
// myField: Contentful.EntryFields.Symbol;
// myField: EntryFields.Symbol;
// }
//
// export type TypeMyEntry = Contentful.Entry<TypeMyEntryFields>;
// export type TypeMyEntry = Entry<TypeMyEntryFields>;
```

# Browser Usage
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
DefaultContentTypeRenderer,
JsDocRenderer,
LocalizedContentTypeRenderer,
} from './renderer/type';
} from './renderer';

// eslint-disable-next-line unicorn/prefer-module
const contentfulExport = require('contentful-export');
Expand Down
3 changes: 2 additions & 1 deletion src/property-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ export const propertyImports = (
): OptionalKind<ImportDeclarationStructure>[] => {
const filterIgnoredModule = (name: string) => ignoreModule !== context.moduleName(name);

const moduleImport = (module: string) => {
const moduleImport = (module: string): OptionalKind<ImportDeclarationStructure> => {
return {
moduleSpecifier: `./${context.moduleName(module)}`,
namedImports: [context.moduleFieldsName(module)],
isTypeOnly: true,
};
};

Expand Down
13 changes: 10 additions & 3 deletions src/renderer/field/render-prop-any.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { renderTypeLiteral, renderTypeUnion } from '../generic';
import { Field } from 'contentful';
import { renderTypeLiteral, renderTypeUnion } from '../generic';
import { RenderContext } from '../type';

export const renderPropAny = (field: Field): string => {
export const renderPropAny = (field: Field, context: RenderContext): string => {
if (field.validations?.length > 0) {
const includesValidation = field.validations.find((validation) => validation.in);
if (includesValidation && includesValidation.in) {
Expand All @@ -17,5 +18,11 @@ export const renderPropAny = (field: Field): string => {
}
}

return `Contentful.EntryFields.${field.type}`;
context.imports.add({
moduleSpecifier: 'contentful',
namedImports: ['EntryFields'],
isTypeOnly: true,
});

return `EntryFields.${field.type}`;
};
8 changes: 7 additions & 1 deletion src/renderer/field/render-prop-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ export const renderPropArray = (field: Field, context: RenderContext): string =>
return renderTypeArray(`${renderTypeUnion(validationsTypes)}`);
}

return renderTypeArray('Contentful.EntryFields.Symbol');
context.imports.add({
moduleSpecifier: 'contentful',
namedImports: ['EntryFields'],
isTypeOnly: true,
});

return renderTypeArray('EntryFields.Symbol');
}

throw new Error('unhandled array type "' + field.items.type + '"');
Expand Down
23 changes: 20 additions & 3 deletions src/renderer/field/render-prop-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,24 @@ export const renderPropLink = (
: 'Record<string, any>';
};

return field.linkType === 'Entry'
? renderTypeGeneric('Contentful.' + field.linkType, linkContentType(field, context))
: 'Contentful.' + field.linkType!;
switch (field.linkType) {
case 'Entry':
context.imports.add({
moduleSpecifier: 'contentful',
namedImports: ['Entry'],
isTypeOnly: true,
});
return renderTypeGeneric(field.linkType, linkContentType(field, context));

case 'Asset':
context.imports.add({
moduleSpecifier: 'contentful',
namedImports: ['Asset'],
isTypeOnly: true,
});
return 'Asset';

default:
throw new Error(`Unknown linkType "${field.linkType}"`);
}
};
1 change: 1 addition & 0 deletions src/renderer/field/render-prop-richtext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const renderRichText = (field: Field, context: RenderContext): string =>
context.imports.add({
moduleSpecifier: '@contentful/rich-text-types',
namespaceImport: 'CFRichTextTypes',
isTypeOnly: true,
});
return renderTypeUnion(['CFRichTextTypes.Block', 'CFRichTextTypes.Inline']);
};
15 changes: 8 additions & 7 deletions src/renderer/type/default-content-type-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@ export class DefaultContentTypeRenderer extends BaseContentTypeRenderer {
}
}

protected addDefaultImports(context: RenderContext): void {
context.imports.add({
moduleSpecifier: 'contentful',
namespaceImport: 'Contentful',
});
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
protected addDefaultImports(context: RenderContext): void {}

protected renderField(
field: Field,
Expand Down Expand Up @@ -78,6 +74,11 @@ export class DefaultContentTypeRenderer extends BaseContentTypeRenderer {
}

protected renderEntryType(contentType: CFContentType, context: RenderContext): string {
return renderTypeGeneric('Contentful.Entry', context.moduleFieldsName(contentType.sys.id));
context.imports.add({
moduleSpecifier: 'contentful',
namedImports: ['Entry'],
isTypeOnly: true,
});
return renderTypeGeneric('Entry', context.moduleFieldsName(contentType.sys.id));
}
}
1 change: 1 addition & 0 deletions src/renderer/type/localized-content-type-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class LocalizedContentTypeRenderer extends BaseContentTypeRenderer {
context.imports.add({
moduleSpecifier: `./${this.FILE_BASE_NAME}`,
namedImports: ['LocalizedFields', 'LocalizedEntry'],
isTypeOnly: true,
});

for (const structure of context.imports) {
Expand Down
52 changes: 26 additions & 26 deletions test/cases/fixtures/01-output.txt
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import * as Contentful from "contentful";
import type { Asset, Entry, EntryFields } from "contentful";

export interface TypeBrandFields {
companyName: Contentful.EntryFields.Text;
logo?: Contentful.Asset;
companyDescription?: Contentful.EntryFields.Text;
website?: Contentful.EntryFields.Symbol;
twitter?: Contentful.EntryFields.Symbol;
email?: Contentful.EntryFields.Symbol;
phone?: Contentful.EntryFields.Symbol[];
companyName: EntryFields.Text;
logo?: Asset;
companyDescription?: EntryFields.Text;
website?: EntryFields.Symbol;
twitter?: EntryFields.Symbol;
email?: EntryFields.Symbol;
phone?: EntryFields.Symbol[];
}

export type TypeBrand = Contentful.Entry<TypeBrandFields>;
export type TypeBrand = Entry<TypeBrandFields>;

export interface TypeCategoryFields {
title: Contentful.EntryFields.Text;
icon?: Contentful.Asset;
categoryDescription?: Contentful.EntryFields.Text;
title: EntryFields.Text;
icon?: Asset;
categoryDescription?: EntryFields.Text;
}

export type TypeCategory = Contentful.Entry<TypeCategoryFields>;
export type TypeCategory = Entry<TypeCategoryFields>;

export interface TypeProductFields {
productName: Contentful.EntryFields.Text;
slug?: Contentful.EntryFields.Symbol;
productDescription?: Contentful.EntryFields.Text;
sizetypecolor?: Contentful.EntryFields.Symbol;
image?: Contentful.Asset[];
tags?: Contentful.EntryFields.Symbol[];
categories?: Contentful.Entry<TypeCategoryFields>[];
price?: Contentful.EntryFields.Number;
brand?: Contentful.Entry<TypeBrandFields>;
quantity?: Contentful.EntryFields.Integer;
sku?: Contentful.EntryFields.Symbol;
website?: Contentful.EntryFields.Symbol;
productName: EntryFields.Text;
slug?: EntryFields.Symbol;
productDescription?: EntryFields.Text;
sizetypecolor?: EntryFields.Symbol;
image?: Asset[];
tags?: EntryFields.Symbol[];
categories?: Entry<TypeCategoryFields>[];
price?: EntryFields.Number;
brand?: Entry<TypeBrandFields>;
quantity?: EntryFields.Integer;
sku?: EntryFields.Symbol;
website?: EntryFields.Symbol;
}

export type TypeProduct = Contentful.Entry<TypeProductFields>;
export type TypeProduct = Entry<TypeProductFields>;
Loading