contentful-typescript-codegen
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -74,3 +74,7 @@ #!/usr/bin/env node | ||
function renderContentfulImports() { | ||
function renderContentfulImports(localization) { | ||
if (localization === void 0) { localization = false; } | ||
if (localization) { | ||
return "\n // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.\n\n import { Entry } from 'contentful'\n import { Document } from '@contentful/rich-text-types'\n "; | ||
} | ||
return "\n // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.\n\n import { Asset, Entry } from 'contentful'\n import { Document } from '@contentful/rich-text-types'\n "; | ||
@@ -84,3 +88,3 @@ } | ||
function renderInterfaceProperty(name, type, required, description) { | ||
function renderInterfaceProperty(name, type, required, localization, description) { | ||
return [ | ||
@@ -91,3 +95,3 @@ descriptionComment(description), | ||
": ", | ||
type, | ||
localization ? "LocalizedField<" + type + ">" : type, | ||
required ? "" : " | undefined", | ||
@@ -106,4 +110,5 @@ ";", | ||
function renderField(field, type) { | ||
return renderInterfaceProperty(field.id, type, field.required, field.name); | ||
function renderField(field, type, localization) { | ||
if (localization === void 0) { localization = false; } | ||
return renderInterfaceProperty(field.id, type, field.required, localization, field.name); | ||
} | ||
@@ -193,5 +198,5 @@ | ||
function renderContentType(contentType) { | ||
function renderContentType(contentType, localization) { | ||
var name = renderContentTypeId(contentType.sys.id); | ||
var fields = renderContentTypeFields(contentType.fields); | ||
var fields = renderContentTypeFields(contentType.fields, localization); | ||
var sys = renderSys(contentType.sys); | ||
@@ -206,3 +211,3 @@ return "\n " + renderInterface({ name: name + "Fields", fields: fields }) + "\n\n " + descriptionComment$1(contentType.description) + "\n " + renderInterface({ name: name, extension: "Entry<" + name + "Fields>", fields: sys }) + "\n "; | ||
} | ||
function renderContentTypeFields(fields) { | ||
function renderContentTypeFields(fields, localization) { | ||
return fields | ||
@@ -224,3 +229,3 @@ .filter(function (field) { return !field.omitted; }) | ||
}; | ||
return renderField(field, functionMap[field.type](field)); | ||
return renderField(field, functionMap[field.type](field), localization); | ||
}) | ||
@@ -245,7 +250,15 @@ .join("\n\n"); | ||
function render(contentTypes, locales) { | ||
/** renders helper types for --localization flag */ | ||
function renderLocalizedTypes(localization) { | ||
if (!localization) | ||
return null; | ||
return "\n export type LocalizedField<T> = Partial<Record<LOCALE_CODE, T>>\n \n // We have to use our own localized version of Asset because of a bug in contentful https://github.com/contentful/contentful.js/issues/208\n export interface Asset {\n sys: Sys\n fields: {\n title: LocalizedField<string>\n description: LocalizedField<string>\n file: LocalizedField<{\n url: string\n details: {\n size: number\n image?: {\n width: number\n height: number\n }\n }\n fileName: string\n contentType: string\n }>\n }\n toPlainObject(): object\n }\n "; | ||
} | ||
function render(contentTypes, locales, _a) { | ||
var _b = (_a === void 0 ? {} : _a).localization, localization = _b === void 0 ? false : _b; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var sortedContentTypes, sortedLocales, source, prettierConfig; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
@@ -255,11 +268,12 @@ sortedContentTypes = contentTypes.sort(function (a, b) { return a.sys.id.localeCompare(b.sys.id); }); | ||
source = [ | ||
renderContentfulImports(), | ||
renderAllContentTypes(sortedContentTypes), | ||
renderContentfulImports(localization), | ||
renderAllContentTypes(sortedContentTypes, localization), | ||
renderAllContentTypeIds(sortedContentTypes), | ||
renderAllLocales(sortedLocales), | ||
renderDefaultLocale(sortedLocales), | ||
renderLocalizedTypes(localization), | ||
].join("\n\n"); | ||
return [4 /*yield*/, prettier.resolveConfig(process.cwd())]; | ||
case 1: | ||
prettierConfig = _a.sent(); | ||
prettierConfig = _c.sent(); | ||
return [2 /*return*/, prettier.format(source, __assign({}, prettierConfig, { parser: "typescript" }))]; | ||
@@ -270,4 +284,4 @@ } | ||
} | ||
function renderAllContentTypes(contentTypes) { | ||
return contentTypes.map(function (contentType) { return renderContentType(contentType); }).join("\n\n"); | ||
function renderAllContentTypes(contentTypes, localization) { | ||
return contentTypes.map(function (contentType) { return renderContentType(contentType, localization); }).join("\n\n"); | ||
} | ||
@@ -359,3 +373,3 @@ function renderAllContentTypeIds(contentTypes) { | ||
var meow = require("meow"); | ||
var cli = meow("\n Usage\n $ contentful-typescript-codegen --output <file> <options>\n\n Options\n --output, -o Where to write to\n --poll, -p Continuously refresh types\n --interval N, -i The interval in seconds at which to poll (defaults to 15)\n --fields-only Output a tree that _only_ ensures fields are valid\n and present, and does not provide types for Sys,\n Assets, or Rich Text. This is useful for ensuring raw\n Contentful responses will be compatible with your code.\n\n Examples\n $ contentful-typescript-codegen -o src/@types/generated/contentful.d.ts\n", { | ||
var cli = meow("\n Usage\n $ contentful-typescript-codegen --output <file> <options>\n\n Options\n --output, -o Where to write to\n --poll, -p Continuously refresh types\n --interval N, -i The interval in seconds at which to poll (defaults to 15)\n --fields-only Output a tree that _only_ ensures fields are valid\n and present, and does not provide types for Sys,\n Assets, or Rich Text. This is useful for ensuring raw\n Contentful responses will be compatible with your code.\n --localization -l Output fields with localized values\n\n Examples\n $ contentful-typescript-codegen -o src/@types/generated/contentful.d.ts\n", { | ||
flags: { | ||
@@ -381,2 +395,7 @@ output: { | ||
}, | ||
localization: { | ||
type: "boolean", | ||
alias: "l", | ||
required: false, | ||
}, | ||
}, | ||
@@ -407,3 +426,5 @@ }); | ||
return [3 /*break*/, 7]; | ||
case 5: return [4 /*yield*/, render(contentTypes.items, locales.items)]; | ||
case 5: return [4 /*yield*/, render(contentTypes.items, locales.items, { | ||
localization: cli.flags.localization, | ||
})]; | ||
case 6: | ||
@@ -410,0 +431,0 @@ output = _a.sent(); |
@@ -43,3 +43,3 @@ "use strict"; | ||
var meow = require("meow"); | ||
var cli = meow("\n Usage\n $ contentful-typescript-codegen --output <file> <options>\n\n Options\n --output, -o Where to write to\n --poll, -p Continuously refresh types\n --interval N, -i The interval in seconds at which to poll (defaults to 15)\n --fields-only Output a tree that _only_ ensures fields are valid\n and present, and does not provide types for Sys,\n Assets, or Rich Text. This is useful for ensuring raw\n Contentful responses will be compatible with your code.\n\n Examples\n $ contentful-typescript-codegen -o src/@types/generated/contentful.d.ts\n", { | ||
var cli = meow("\n Usage\n $ contentful-typescript-codegen --output <file> <options>\n\n Options\n --output, -o Where to write to\n --poll, -p Continuously refresh types\n --interval N, -i The interval in seconds at which to poll (defaults to 15)\n --fields-only Output a tree that _only_ ensures fields are valid\n and present, and does not provide types for Sys,\n Assets, or Rich Text. This is useful for ensuring raw\n Contentful responses will be compatible with your code.\n --localization -l Output fields with localized values\n\n Examples\n $ contentful-typescript-codegen -o src/@types/generated/contentful.d.ts\n", { | ||
flags: { | ||
@@ -65,2 +65,7 @@ output: { | ||
}, | ||
localization: { | ||
type: "boolean", | ||
alias: "l", | ||
required: false, | ||
}, | ||
}, | ||
@@ -91,3 +96,5 @@ }); | ||
return [3 /*break*/, 7]; | ||
case 5: return [4 /*yield*/, render_1.default(contentTypes.items, locales.items)]; | ||
case 5: return [4 /*yield*/, render_1.default(contentTypes.items, locales.items, { | ||
localization: cli.flags.localization, | ||
})]; | ||
case 6: | ||
@@ -94,0 +101,0 @@ output = _a.sent(); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function renderContentfulImports() { | ||
function renderContentfulImports(localization) { | ||
if (localization === void 0) { localization = false; } | ||
if (localization) { | ||
return "\n // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.\n\n import { Entry } from 'contentful'\n import { Document } from '@contentful/rich-text-types'\n "; | ||
} | ||
return "\n // THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.\n\n import { Asset, Entry } from 'contentful'\n import { Document } from '@contentful/rich-text-types'\n "; | ||
@@ -5,0 +9,0 @@ } |
@@ -14,5 +14,5 @@ "use strict"; | ||
var renderSymbol_1 = require("./fields/renderSymbol"); | ||
function renderContentType(contentType) { | ||
function renderContentType(contentType, localization) { | ||
var name = renderContentTypeId_1.default(contentType.sys.id); | ||
var fields = renderContentTypeFields(contentType.fields); | ||
var fields = renderContentTypeFields(contentType.fields, localization); | ||
var sys = renderSys(contentType.sys); | ||
@@ -28,3 +28,3 @@ return "\n " + renderInterface_1.default({ name: name + "Fields", fields: fields }) + "\n\n " + descriptionComment(contentType.description) + "\n " + renderInterface_1.default({ name: name, extension: "Entry<" + name + "Fields>", fields: sys }) + "\n "; | ||
} | ||
function renderContentTypeFields(fields) { | ||
function renderContentTypeFields(fields, localization) { | ||
return fields | ||
@@ -46,3 +46,3 @@ .filter(function (field) { return !field.omitted; }) | ||
}; | ||
return renderField_1.default(field, functionMap[field.type](field)); | ||
return renderField_1.default(field, functionMap[field.type](field), localization); | ||
}) | ||
@@ -49,0 +49,0 @@ .join("\n\n"); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var renderInterfaceProperty_1 = require("../typescript/renderInterfaceProperty"); | ||
function renderField(field, type) { | ||
return renderInterfaceProperty_1.default(field.id, type, field.required, field.name); | ||
function renderField(field, type, localization) { | ||
if (localization === void 0) { localization = false; } | ||
return renderInterfaceProperty_1.default(field.id, type, field.required, localization, field.name); | ||
} | ||
exports.default = renderField; | ||
//# sourceMappingURL=renderField.js.map |
@@ -55,7 +55,9 @@ "use strict"; | ||
var renderDefaultLocale_1 = require("./contentful/renderDefaultLocale"); | ||
function render(contentTypes, locales) { | ||
var renderLocalizedTypes_1 = require("./contentful/renderLocalizedTypes"); | ||
function render(contentTypes, locales, _a) { | ||
var _b = (_a === void 0 ? {} : _a).localization, localization = _b === void 0 ? false : _b; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var sortedContentTypes, sortedLocales, source, prettierConfig; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
@@ -65,11 +67,12 @@ sortedContentTypes = contentTypes.sort(function (a, b) { return a.sys.id.localeCompare(b.sys.id); }); | ||
source = [ | ||
renderContentfulImports_1.default(), | ||
renderAllContentTypes(sortedContentTypes), | ||
renderContentfulImports_1.default(localization), | ||
renderAllContentTypes(sortedContentTypes, localization), | ||
renderAllContentTypeIds(sortedContentTypes), | ||
renderAllLocales_1.default(sortedLocales), | ||
renderDefaultLocale_1.default(sortedLocales), | ||
renderLocalizedTypes_1.default(localization), | ||
].join("\n\n"); | ||
return [4 /*yield*/, prettier_1.resolveConfig(process.cwd())]; | ||
case 1: | ||
prettierConfig = _a.sent(); | ||
prettierConfig = _c.sent(); | ||
return [2 /*return*/, prettier_1.format(source, __assign({}, prettierConfig, { parser: "typescript" }))]; | ||
@@ -81,4 +84,4 @@ } | ||
exports.default = render; | ||
function renderAllContentTypes(contentTypes) { | ||
return contentTypes.map(function (contentType) { return renderContentType_1.default(contentType); }).join("\n\n"); | ||
function renderAllContentTypes(contentTypes, localization) { | ||
return contentTypes.map(function (contentType) { return renderContentType_1.default(contentType, localization); }).join("\n\n"); | ||
} | ||
@@ -85,0 +88,0 @@ function renderAllContentTypeIds(contentTypes) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function renderInterfaceProperty(name, type, required, description) { | ||
function renderInterfaceProperty(name, type, required, localization, description) { | ||
return [ | ||
@@ -9,3 +9,3 @@ descriptionComment(description), | ||
": ", | ||
type, | ||
localization ? "LocalizedField<" + type + ">" : type, | ||
required ? "" : " | undefined", | ||
@@ -12,0 +12,0 @@ ";", |
@@ -1,1 +0,1 @@ | ||
export default function renderContentfulImports(): string; | ||
export default function renderContentfulImports(localization?: boolean): string; |
import { ContentType } from "contentful"; | ||
export default function renderContentType(contentType: ContentType): string; | ||
export default function renderContentType(contentType: ContentType, localization: boolean): string; |
import { Field } from "contentful"; | ||
export default function renderField(field: Field, type: string): string; | ||
export default function renderField(field: Field, type: string, localization?: boolean): string; |
import { ContentType, Locale } from "contentful"; | ||
export default function render(contentTypes: ContentType[], locales: Locale[]): Promise<string>; | ||
interface Options { | ||
localization?: boolean; | ||
} | ||
export default function render(contentTypes: ContentType[], locales: Locale[], { localization }?: Options): Promise<string>; | ||
export {}; |
@@ -1,1 +0,1 @@ | ||
export default function renderInterfaceProperty(name: string, type: string, required: boolean, description?: string): string; | ||
export default function renderInterfaceProperty(name: string, type: string, required: boolean, localization: boolean, description?: string): string; |
@@ -10,3 +10,3 @@ { | ||
}, | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"main": "dist/contentful-typescript-codegen.js", | ||
@@ -13,0 +13,0 @@ "bin": "./dist/contentful-typescript-codegen.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
103228
83
1075