Skip to content

Commit

Permalink
feat: switch to named imports
Browse files Browse the repository at this point in the history
  • Loading branch information
marcolink committed Feb 2, 2023
1 parent fa7b3bb commit 221a981
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 123 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,24 +206,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 All @@ -249,8 +249,8 @@ Relevant methods to override:
Set content type renderers:

```typescript
import CFDefinitionsBuilder from 'cf-content-types-generator';
import {
CFDefinitionsBuilder,
DefaultContentTypeRenderer,
LocalizedContentTypeRenderer,
} from 'cf-content-types-generator';
Expand Down Expand Up @@ -329,21 +329,21 @@ const stringContent = new CFDefinitionsBuilder()
validations: [],
disabled: false,
localized: false,
omitted: false
omitted: false,
},
],
})
.toString();

console.log(stringContent)
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 @@ -7,7 +7,7 @@ import {
ContentTypeRenderer,
DefaultContentTypeRenderer,
LocalizedContentTypeRenderer,
} from './renderer/type';
} from './renderer';

// eslint-disable-next-line unicorn/prefer-module
const contentfulExport = require('contentful-export');
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import CFDefinitionsBuilder from './cf-definitions-builder';

export * from './extract-validation';
export * from './module-name';
export * from './property-imports';
export * from './renderer';
export * from './types';
export default CFDefinitionsBuilder;
export { CFDefinitionsBuilder };
12 changes: 9 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,10 @@ export const renderPropAny = (field: Field): string => {
}
}

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

return `EntryFields.${field.type}`;
};
7 changes: 6 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,12 @@ export const renderPropArray = (field: Field, context: RenderContext): string =>
return renderTypeArray(`${renderTypeUnion(validationsTypes)}`);
}

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

return renderTypeArray('EntryFields.Symbol');
}

throw new Error('unhandled array type "' + field.items.type + '"');
Expand Down
21 changes: 18 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,22 @@ 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'],
});
return renderTypeGeneric(field.linkType, linkContentType(field, context));

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

default:
throw new Error(`Unknown linkType "${field.linkType}"`);
}
};
14 changes: 7 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,10 @@ 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'],
});
return renderTypeGeneric('Entry', context.moduleFieldsName(contentType.sys.id));
}
}
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 { 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

0 comments on commit 221a981

Please sign in to comment.