prismic-ts-codegen 0.1.30 → 0.1.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/configSchema.d.mts +2 -4
- package/dist/cli/configSchema.d.mts.map +1 -1
- package/dist/cli/configSchema.mjs +126 -2
- package/dist/cli/configSchema.mjs.map +1 -1
- package/dist/cli/constants.d.mts +1 -2
- package/dist/cli/constants.d.mts.map +1 -1
- package/dist/cli/loadConfig.mjs +7 -10
- package/dist/cli/loadConfig.mjs.map +1 -1
- package/dist/cli/loadLocaleIDs.mjs +0 -1
- package/dist/cli/loadLocaleIDs.mjs.map +1 -1
- package/dist/cli/loadModels.mjs +11 -10
- package/dist/cli/loadModels.mjs.map +1 -1
- package/dist/cli.d.mts +1 -1
- package/dist/cli.mjs +41 -16
- package/dist/cli.mjs.map +1 -1
- package/dist/constants.mjs.map +1 -1
- package/dist/detectTypesProvider.d.mts +2 -4
- package/dist/detectTypesProvider.d.mts.map +1 -1
- package/dist/detectTypesProvider.mjs.map +1 -1
- package/dist/generateTypes.d.mts +3 -5
- package/dist/generateTypes.d.mts.map +1 -1
- package/dist/generateTypes.mjs +15 -12
- package/dist/generateTypes.mjs.map +1 -1
- package/dist/lib/addBlankLine.mjs.map +1 -1
- package/dist/lib/addLine.mjs.map +1 -1
- package/dist/lib/addSection.mjs.map +1 -1
- package/dist/lib/addTrailingNewLine.mjs.map +1 -1
- package/dist/lib/buildCustomTypeDataType.mjs +2 -2
- package/dist/lib/buildCustomTypeDataType.mjs.map +1 -1
- package/dist/lib/buildCustomTypeType.mjs +2 -2
- package/dist/lib/buildCustomTypeType.mjs.map +1 -1
- package/dist/lib/buildFieldDocs.mjs +9 -4
- package/dist/lib/buildFieldDocs.mjs.map +1 -1
- package/dist/lib/buildFieldProperties.mjs +18 -14
- package/dist/lib/buildFieldProperties.mjs.map +1 -1
- package/dist/lib/buildSharedSliceType.mjs +10 -10
- package/dist/lib/buildSharedSliceType.mjs.map +1 -1
- package/dist/lib/buildTypeName.mjs +2 -2
- package/dist/lib/buildTypeName.mjs.map +1 -1
- package/dist/lib/buildUnion.mjs.map +1 -1
- package/dist/lib/checkHasUIDFIeld.mjs.map +1 -1
- package/dist/lib/dedent.mjs +11 -0
- package/dist/lib/dedent.mjs.map +1 -0
- package/dist/lib/getAPIIDPath.mjs.map +1 -1
- package/dist/lib/getCacheKey.mjs.map +1 -1
- package/dist/lib/getHumanReadableModelName.mjs.map +1 -1
- package/dist/lib/getHumanReadablePath.mjs.map +1 -1
- package/dist/package.mjs +7 -0
- package/dist/package.mjs.map +1 -0
- package/dist/types.d.mts +1 -3
- package/dist/types.d.mts.map +1 -1
- package/package.json +18 -25
- package/src/cli/index.ts +38 -22
- package/src/cli/loadConfig.ts +5 -11
- package/src/cli/loadLocaleIDs.ts +0 -1
- package/src/cli/loadModels.ts +27 -12
- package/src/generateTypes.ts +3 -3
- package/src/lib/buildCustomTypeDataType.ts +2 -2
- package/src/lib/buildCustomTypeType.ts +6 -6
- package/src/lib/buildFieldProperties.ts +7 -7
- package/src/lib/buildSharedSliceType.ts +22 -33
- package/src/lib/buildTypeName.ts +9 -4
- package/src/lib/dedent.ts +10 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildSharedSliceType.mjs","names":[],"sources":["../../src/lib/buildSharedSliceType.ts"],"sourcesContent":["import type { SharedSliceModel } from \"@prismicio/client\";\nimport
|
|
1
|
+
{"version":3,"file":"buildSharedSliceType.mjs","names":[],"sources":["../../src/lib/buildSharedSliceType.ts"],"sourcesContent":["import type { SharedSliceModel } from \"@prismicio/client\";\nimport type QuickLRU from \"quick-lru\";\n\nimport { SHARED_SLICES_DOCUMENTATION_URL } from \"../constants\";\nimport type { AuxiliaryType, FieldConfigs, FieldPath } from \"../types\";\nimport { addSection } from \"./addSection\";\nimport { buildFieldProperties } from \"./buildFieldProperties\";\nimport { buildTypeName } from \"./buildTypeName\";\nimport { buildUnion } from \"./buildUnion\";\nimport { dedent } from \"./dedent\";\nimport { getCacheKey } from \"./getCacheKey\";\nimport { getHumanReadableModelName } from \"./getHumanReadableModelName\";\nimport { getHumanReadablePath } from \"./getHumanReadablePath\";\n\ntype BuildSharedSliceTypeArgs = {\n\tmodel: SharedSliceModel;\n\tfieldConfigs: FieldConfigs;\n\tcache?: QuickLRU<string, unknown>;\n};\n\ntype BuildSharedSliceTypeReturnValue = {\n\tname: string;\n\tvariationNames: string[];\n\tcode: string;\n\tauxiliaryTypes: AuxiliaryType[];\n\tcontentTypeNames: string[];\n};\n\nexport function buildSharedSliceType(\n\targs: BuildSharedSliceTypeArgs,\n): BuildSharedSliceTypeReturnValue {\n\tif (args.cache) {\n\t\tconst key = getCacheKey([args.model, args.fieldConfigs]);\n\t\tconst cached = args.cache.get(key);\n\n\t\tif (cached) {\n\t\t\treturn cached as BuildSharedSliceTypeReturnValue;\n\t\t}\n\t}\n\n\tlet code = \"\";\n\n\tconst auxiliaryTypes: AuxiliaryType[] = [];\n\tconst contentTypeNames: string[] = [];\n\n\tconst name = buildTypeName(args.model.id, \"Slice\");\n\tconst humanReadableName = getHumanReadableModelName({\n\t\tname: args.model.id,\n\t\tmodel: args.model,\n\t});\n\n\tconst variationNames: string[] = [];\n\tfor (const variationModel of args.model.variations) {\n\t\tconst variationName = buildTypeName(name, variationModel.id);\n\n\t\tlet primaryInterfaceName: string | undefined;\n\t\tif (variationModel.primary && Object.keys(variationModel.primary).length > 0) {\n\t\t\tprimaryInterfaceName = buildTypeName(variationName, \"Primary\");\n\n\t\t\tconst path: FieldPath = [\n\t\t\t\t{\n\t\t\t\t\tname: args.model.id,\n\t\t\t\t\tmodel: args.model,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tname: variationModel.id,\n\t\t\t\t\tlabel: variationModel.name,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tname: \"primary\",\n\t\t\t\t\tlabel: \"Primary\",\n\t\t\t\t},\n\t\t\t];\n\t\t\tconst humanReadablePath = getHumanReadablePath({ path });\n\n\t\t\tconst primaryFieldProperties = buildFieldProperties({\n\t\t\t\tfields: variationModel.primary,\n\t\t\t\tfieldConfigs: args.fieldConfigs,\n\t\t\t\tpath,\n\t\t\t});\n\t\t\tauxiliaryTypes.push(...primaryFieldProperties.auxiliaryTypes);\n\t\t\tcontentTypeNames.push(...primaryFieldProperties.contentTypeNames);\n\n\t\t\tcontentTypeNames.push(primaryInterfaceName);\n\n\t\t\tconst docs = dedent`\n\t\t\t\t/**\n\t\t\t\t * Primary content in *${humanReadablePath}*\n\t\t\t\t */\n\t\t\t`;\n\n\t\t\tcode = addSection(\n\t\t\t\tprimaryFieldProperties.code\n\t\t\t\t\t? dedent`\n\t\t\t\t\t\t${docs}\n\t\t\t\t\t\texport interface ${primaryInterfaceName} {\n\t\t\t\t\t\t\t${primaryFieldProperties.code}\n\t\t\t\t\t\t}\n\t\t\t\t\t`\n\t\t\t\t\t: dedent`\n\t\t\t\t\t\t${docs}\n\t\t\t\t\t\texport interface ${primaryInterfaceName} {}\n\t\t\t\t\t`,\n\t\t\t\tcode,\n\t\t\t);\n\t\t}\n\n\t\tlet itemInterfaceName: string | undefined;\n\t\tif (variationModel.items && Object.keys(variationModel.items).length > 0) {\n\t\t\titemInterfaceName = buildTypeName(variationName, \"Item\");\n\n\t\t\tconst path: FieldPath = [\n\t\t\t\t{\n\t\t\t\t\tname: args.model.id,\n\t\t\t\t\tmodel: args.model,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tname: \"items\",\n\t\t\t\t\tlabel: \"Items\",\n\t\t\t\t},\n\t\t\t];\n\t\t\tconst humanReadablePath = getHumanReadablePath({ path });\n\n\t\t\tconst itemFieldProperties = buildFieldProperties({\n\t\t\t\tfields: variationModel.items,\n\t\t\t\tfieldConfigs: args.fieldConfigs,\n\t\t\t\tpath,\n\t\t\t});\n\t\t\tauxiliaryTypes.push(...itemFieldProperties.auxiliaryTypes);\n\t\t\tcontentTypeNames.push(...itemFieldProperties.contentTypeNames);\n\n\t\t\tcontentTypeNames.push(itemInterfaceName);\n\n\t\t\tconst docs = dedent`\n\t\t\t\t/**\n\t\t\t\t * Primary content in *${humanReadablePath}*\n\t\t\t\t */\n\t\t\t`;\n\n\t\t\tcode = addSection(\n\t\t\t\titemFieldProperties.code\n\t\t\t\t\t? dedent`\n\t\t\t\t\t\t${docs}\n\t\t\t\t\t\texport interface ${itemInterfaceName} {\n\t\t\t\t\t\t\t${itemFieldProperties.code}\n\t\t\t\t\t\t}\n\t\t\t\t\t`\n\t\t\t\t\t: dedent`\n\t\t\t\t\t\t${docs}\n\t\t\t\t\t\texport interface ${itemInterfaceName} {}\n\t\t\t\t\t`,\n\t\t\t\tcode,\n\t\t\t);\n\t\t}\n\n\t\tcode = addSection(\n\t\t\tdedent`\n\t\t\t\t/**\n\t\t\t\t * ${variationModel.name} variation for ${humanReadableName} Slice\n\t\t\t\t *\n\t\t\t\t * - **API ID**: \\`${variationModel.id}\\`\n\t\t\t\t * - **Description**: ${variationModel.description || \"*None*\"}\n\t\t\t\t * - **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}\n\t\t\t\t */\n\t\t\t\texport type ${variationName} = prismic.SharedSliceVariation<\"${variationModel.id}\", ${\n\t\t\t\t\tprimaryInterfaceName ? `Simplify<${primaryInterfaceName}>` : `Record<string, never>`\n\t\t\t\t}, ${itemInterfaceName ? `Simplify<${itemInterfaceName}>` : `never`}>;\n\t\t\t`,\n\t\t\tcode,\n\t\t);\n\n\t\tvariationNames.push(variationName);\n\t}\n\n\tconst variationUnionName = buildTypeName(name, \"Variation\");\n\tconst variationsUnion = buildUnion(variationNames);\n\n\tcontentTypeNames.push(variationUnionName);\n\tcontentTypeNames.push(...variationNames);\n\n\tcode = addSection(\n\t\tdedent`\n\t\t\t/**\n\t\t\t * Slice variation for *${humanReadableName}*\n\t\t\t */\n\t\t\ttype ${variationUnionName} = ${variationNames.length > 0 ? variationsUnion : \"never\"}\n\t\t`,\n\t\tcode,\n\t);\n\n\tcode = addSection(\n\t\tdedent`\n\t\t\t/**\n\t\t\t * ${humanReadableName} Shared Slice\n\t\t\t *\n\t\t\t * - **API ID**: \\`${args.model.id}\\`\n\t\t\t * - **Description**: ${args.model.description || \"*None*\"}\n\t\t\t * - **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}\n\t\t\t */\n\t\t\texport type ${name} = prismic.SharedSlice<\"${args.model.id}\", ${variationUnionName}>;\n\t\t`,\n\t\tcode,\n\t);\n\n\tconst result = {\n\t\tname,\n\t\tvariationNames,\n\t\tcode,\n\t\tauxiliaryTypes,\n\t\tcontentTypeNames,\n\t};\n\n\tif (args.cache) {\n\t\tconst key = getCacheKey([args.model, args.fieldConfigs]);\n\n\t\targs.cache.set(key, result);\n\t}\n\n\treturn result;\n}\n"],"mappings":"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"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { pascalCase
|
|
1
|
+
import { pascalCase } from "change-case";
|
|
2
2
|
//#region src/lib/buildTypeName.ts
|
|
3
3
|
const buildTypeName = (...parts) => {
|
|
4
|
-
let name = pascalCase(parts.filter(Boolean).join(" "), {
|
|
4
|
+
let name = pascalCase(parts.filter(Boolean).join(" ").replace(/[^A-Z0-9]+/gi, " "), { mergeAmbiguousCharacters: true });
|
|
5
5
|
if (/^[0-9]/.test(name) ? "_" : "") name = `_${name}`;
|
|
6
6
|
return name;
|
|
7
7
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildTypeName.mjs","names":[],"sources":["../../src/lib/buildTypeName.ts"],"sourcesContent":["import { pascalCase
|
|
1
|
+
{"version":3,"file":"buildTypeName.mjs","names":[],"sources":["../../src/lib/buildTypeName.ts"],"sourcesContent":["import { pascalCase } from \"change-case\";\n\nexport const buildTypeName = (...parts: string[]): string => {\n\t// Non-ASCII characters are separators, as they were with the previous `pascal-case` package.\n\tlet name = pascalCase(\n\t\tparts\n\t\t\t.filter(Boolean)\n\t\t\t.join(\" \")\n\t\t\t.replace(/[^A-Z0-9]+/gi, \" \"),\n\t\t{ mergeAmbiguousCharacters: true },\n\t);\n\n\tif (/^[0-9]/.test(name) ? \"_\" : \"\") {\n\t\tname = `_${name}`;\n\t}\n\n\treturn name;\n};\n"],"mappings":";;AAEA,MAAa,iBAAiB,GAAG,UAA4B;CAE5D,IAAI,OAAO,WACV,MACE,OAAO,OAAO,CAAC,CACf,KAAK,GAAG,CAAC,CACT,QAAQ,gBAAgB,GAAG,GAC7B,EAAE,0BAA0B,KAAK,CAClC;CAEA,IAAI,SAAS,KAAK,IAAI,IAAI,MAAM,IAC/B,OAAO,IAAI;CAGZ,OAAO;AACR"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildUnion.mjs","names":[],"sources":["../../src/lib/buildUnion.ts"],"sourcesContent":["export function buildUnion(types: string[]): string {\n\treturn types.filter(Boolean).join(\" | \") || \"never\";\n}\n"],"mappings":";AAAA,SAAgB,WAAW,OAAyB;
|
|
1
|
+
{"version":3,"file":"buildUnion.mjs","names":[],"sources":["../../src/lib/buildUnion.ts"],"sourcesContent":["export function buildUnion(types: string[]): string {\n\treturn types.filter(Boolean).join(\" | \") || \"never\";\n}\n"],"mappings":";AAAA,SAAgB,WAAW,OAAyB;CACnD,OAAO,MAAM,OAAO,OAAO,CAAC,CAAC,KAAK,KAAK,KAAK;AAC7C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"checkHasUIDFIeld.mjs","names":[],"sources":["../../src/lib/checkHasUIDFIeld.ts"],"sourcesContent":["import type { CustomTypeModel } from \"@prismicio/client\";\n\nexport function checkHasUIDField(model: CustomTypeModel): boolean {\n\treturn \"uid\" in Object.assign({}, ...Object.values(model.json));\n}\n"],"mappings":";AAEA,SAAgB,iBAAiB,OAAiC;
|
|
1
|
+
{"version":3,"file":"checkHasUIDFIeld.mjs","names":[],"sources":["../../src/lib/checkHasUIDFIeld.ts"],"sourcesContent":["import type { CustomTypeModel } from \"@prismicio/client\";\n\nexport function checkHasUIDField(model: CustomTypeModel): boolean {\n\treturn \"uid\" in Object.assign({}, ...Object.values(model.json));\n}\n"],"mappings":";AAEA,SAAgB,iBAAiB,OAAiC;CACjE,OAAO,SAAS,OAAO,OAAO,CAAC,GAAG,GAAG,OAAO,OAAO,MAAM,IAAI,CAAC;AAC/D"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import baseDedent from "dedent";
|
|
2
|
+
//#region src/lib/dedent.ts
|
|
3
|
+
const baseDedentWithOptions = baseDedent.withOptions({ alignValues: true });
|
|
4
|
+
const dedent = (strings, ...values) => {
|
|
5
|
+
const resolved = Array.from(strings);
|
|
6
|
+
return baseDedentWithOptions(Object.assign(resolved, { raw: resolved }), ...values);
|
|
7
|
+
};
|
|
8
|
+
//#endregion
|
|
9
|
+
export { dedent };
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=dedent.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dedent.mjs","names":[],"sources":["../../src/lib/dedent.ts"],"sourcesContent":["import baseDedent from \"dedent\";\n\nconst baseDedentWithOptions = baseDedent.withOptions({ alignValues: true });\n\nexport const dedent = (strings: TemplateStringsArray, ...values: unknown[]): string => {\n\t// dedent reads `strings.raw` by default, which keeps escapes in string literals. Point `raw` at\n\t// the regular string segments instead, where escapes are already resolved.\n\tconst resolved = Array.from(strings);\n\treturn baseDedentWithOptions(Object.assign(resolved, { raw: resolved }), ...values);\n};\n"],"mappings":";;AAEA,MAAM,wBAAwB,WAAW,YAAY,EAAE,aAAa,KAAK,CAAC;AAE1E,MAAa,UAAU,SAA+B,GAAG,WAA8B;CAGtF,MAAM,WAAW,MAAM,KAAK,OAAO;CACnC,OAAO,sBAAsB,OAAO,OAAO,UAAU,EAAE,KAAK,SAAS,CAAC,GAAG,GAAG,MAAM;AACnF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getAPIIDPath.mjs","names":[],"sources":["../../src/lib/getAPIIDPath.ts"],"sourcesContent":["import type { FieldPath } from \"../types\";\n\ntype GetAPIIDPathArgs = {\n\tpath: FieldPath;\n};\n\nexport function getAPIIDPath(args: GetAPIIDPathArgs): string {\n\tlet result = \"\";\n\n\tfor (let i = 0; i < args.path.length; i++) {\n\t\tif (i > 0) {\n\t\t\tresult += \".\";\n\t\t}\n\n\t\tconst element = args.path[i];\n\n\t\tif (\n\t\t\telement.model &&\n\t\t\t\"type\" in element.model &&\n\t\t\t(element.model.type === \"Group\" || element.model.type === \"Slices\")\n\t\t) {\n\t\t\tresult += `${element.name}[]`;\n\t\t} else {\n\t\t\tif (element.name === \"items\") {\n\t\t\t\tconst previousElement = args.path[i - 1];\n\n\t\t\t\tif (\n\t\t\t\t\t(previousElement.model && \"json\" in previousElement.model) ||\n\t\t\t\t\t(previousElement.model &&\n\t\t\t\t\t\t\"type\" in previousElement.model &&\n\t\t\t\t\t\tpreviousElement.model.type === \"SharedSlice\")\n\t\t\t\t) {\n\t\t\t\t\tresult += `${element.name}[]`;\n\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tresult += element.name;\n\t\t}\n\t}\n\n\treturn result;\n}\n"],"mappings":";AAMA,SAAgB,aAAa,MAAgC;CAC5D,IAAI,SAAS;
|
|
1
|
+
{"version":3,"file":"getAPIIDPath.mjs","names":[],"sources":["../../src/lib/getAPIIDPath.ts"],"sourcesContent":["import type { FieldPath } from \"../types\";\n\ntype GetAPIIDPathArgs = {\n\tpath: FieldPath;\n};\n\nexport function getAPIIDPath(args: GetAPIIDPathArgs): string {\n\tlet result = \"\";\n\n\tfor (let i = 0; i < args.path.length; i++) {\n\t\tif (i > 0) {\n\t\t\tresult += \".\";\n\t\t}\n\n\t\tconst element = args.path[i];\n\n\t\tif (\n\t\t\telement.model &&\n\t\t\t\"type\" in element.model &&\n\t\t\t(element.model.type === \"Group\" || element.model.type === \"Slices\")\n\t\t) {\n\t\t\tresult += `${element.name}[]`;\n\t\t} else {\n\t\t\tif (element.name === \"items\") {\n\t\t\t\tconst previousElement = args.path[i - 1];\n\n\t\t\t\tif (\n\t\t\t\t\t(previousElement.model && \"json\" in previousElement.model) ||\n\t\t\t\t\t(previousElement.model &&\n\t\t\t\t\t\t\"type\" in previousElement.model &&\n\t\t\t\t\t\tpreviousElement.model.type === \"SharedSlice\")\n\t\t\t\t) {\n\t\t\t\t\tresult += `${element.name}[]`;\n\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tresult += element.name;\n\t\t}\n\t}\n\n\treturn result;\n}\n"],"mappings":";AAMA,SAAgB,aAAa,MAAgC;CAC5D,IAAI,SAAS;CAEb,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,KAAK,QAAQ,KAAK;EAC1C,IAAI,IAAI,GACP,UAAU;EAGX,MAAM,UAAU,KAAK,KAAK;EAE1B,IACC,QAAQ,SACR,UAAU,QAAQ,UACjB,QAAQ,MAAM,SAAS,WAAW,QAAQ,MAAM,SAAS,WAE1D,UAAU,GAAG,QAAQ,KAAK;OACpB;GACN,IAAI,QAAQ,SAAS,SAAS;IAC7B,MAAM,kBAAkB,KAAK,KAAK,IAAI;IAEtC,IACE,gBAAgB,SAAS,UAAU,gBAAgB,SACnD,gBAAgB,SAChB,UAAU,gBAAgB,SAC1B,gBAAgB,MAAM,SAAS,eAC/B;KACD,UAAU,GAAG,QAAQ,KAAK;KAE1B;IACD;GACD;GAEA,UAAU,QAAQ;EACnB;CACD;CAEA,OAAO;AACR"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getCacheKey.mjs","names":[],"sources":["../../src/lib/getCacheKey.ts"],"sourcesContent":["import * as crypto from \"node:crypto\";\n\n/**\n * Returns a key based on the provided input that can be used as a cache identifier.\n *\n * @param input - The value used to create a key.\n * @returns The cache key.\n */\nexport const getCacheKey = (input: unknown): string => {\n\treturn crypto.createHash(\"sha1\").update(JSON.stringify(input)).digest(\"hex\");\n};\n"],"mappings":";;;;;;;;AAQA,MAAa,eAAe,UAA2B;
|
|
1
|
+
{"version":3,"file":"getCacheKey.mjs","names":[],"sources":["../../src/lib/getCacheKey.ts"],"sourcesContent":["import * as crypto from \"node:crypto\";\n\n/**\n * Returns a key based on the provided input that can be used as a cache identifier.\n *\n * @param input - The value used to create a key.\n * @returns The cache key.\n */\nexport const getCacheKey = (input: unknown): string => {\n\treturn crypto.createHash(\"sha1\").update(JSON.stringify(input)).digest(\"hex\");\n};\n"],"mappings":";;;;;;;;AAQA,MAAa,eAAe,UAA2B;CACtD,OAAO,OAAO,WAAW,MAAM,CAAC,CAAC,OAAO,KAAK,UAAU,KAAK,CAAC,CAAC,CAAC,OAAO,KAAK;AAC5E"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getHumanReadableModelName.mjs","names":[],"sources":["../../src/lib/getHumanReadableModelName.ts"],"sourcesContent":["import type {\n\tCustomTypeModel,\n\tCustomTypeModelField,\n\tCustomTypeModelSlice,\n\tSharedSliceModel,\n} from \"@prismicio/client\";\n\ntype GetModelHumanNameArgs = {\n\tname: string;\n\tmodel: CustomTypeModel | SharedSliceModel | CustomTypeModelField | CustomTypeModelSlice;\n};\n\nexport const getHumanReadableModelName = (args: GetModelHumanNameArgs): string => {\n\tif (\"json\" in args.model) {\n\t\t// Custom type model\n\n\t\treturn args.model.label || args.model.id;\n\t} else if (\"type\" in args.model && args.model.type === \"SharedSlice\") {\n\t\t// Shared Slice model\n\n\t\treturn args.model.name;\n\t} else if (\"type\" in args.model && args.model.type === \"Slice\") {\n\t\t// Legacy Slice model\n\n\t\treturn args.model.fieldset || args.name;\n\t} else if (\"type\" in args.model) {\n\t\t// Field model\n\n\t\tif (args.model.config && \"label\" in args.model.config) {\n\t\t\t// Non-Slice Zone fields\n\t\t\treturn args.model.config.label || args.name;\n\t\t} else if (args.model.config && \"fieldset\" in args.model) {\n\t\t\t// Slice Zone\n\t\t\treturn args.model.fieldset || args.name;\n\t\t}\n\t}\n\n\treturn `\\`${args.name}\\``;\n};\n"],"mappings":";AAYA,MAAa,6BAA6B,SAAwC;
|
|
1
|
+
{"version":3,"file":"getHumanReadableModelName.mjs","names":[],"sources":["../../src/lib/getHumanReadableModelName.ts"],"sourcesContent":["import type {\n\tCustomTypeModel,\n\tCustomTypeModelField,\n\tCustomTypeModelSlice,\n\tSharedSliceModel,\n} from \"@prismicio/client\";\n\ntype GetModelHumanNameArgs = {\n\tname: string;\n\tmodel: CustomTypeModel | SharedSliceModel | CustomTypeModelField | CustomTypeModelSlice;\n};\n\nexport const getHumanReadableModelName = (args: GetModelHumanNameArgs): string => {\n\tif (\"json\" in args.model) {\n\t\t// Custom type model\n\n\t\treturn args.model.label || args.model.id;\n\t} else if (\"type\" in args.model && args.model.type === \"SharedSlice\") {\n\t\t// Shared Slice model\n\n\t\treturn args.model.name;\n\t} else if (\"type\" in args.model && args.model.type === \"Slice\") {\n\t\t// Legacy Slice model\n\n\t\treturn args.model.fieldset || args.name;\n\t} else if (\"type\" in args.model) {\n\t\t// Field model\n\n\t\tif (args.model.config && \"label\" in args.model.config) {\n\t\t\t// Non-Slice Zone fields\n\t\t\treturn args.model.config.label || args.name;\n\t\t} else if (args.model.config && \"fieldset\" in args.model) {\n\t\t\t// Slice Zone\n\t\t\treturn args.model.fieldset || args.name;\n\t\t}\n\t}\n\n\treturn `\\`${args.name}\\``;\n};\n"],"mappings":";AAYA,MAAa,6BAA6B,SAAwC;CACjF,IAAI,UAAU,KAAK,OAGlB,OAAO,KAAK,MAAM,SAAS,KAAK,MAAM;MAChC,IAAI,UAAU,KAAK,SAAS,KAAK,MAAM,SAAS,eAGtD,OAAO,KAAK,MAAM;MACZ,IAAI,UAAU,KAAK,SAAS,KAAK,MAAM,SAAS,SAGtD,OAAO,KAAK,MAAM,YAAY,KAAK;MAC7B,IAAI,UAAU,KAAK,OAAO;EAGhC,IAAI,KAAK,MAAM,UAAU,WAAW,KAAK,MAAM,QAE9C,OAAO,KAAK,MAAM,OAAO,SAAS,KAAK;OACjC,IAAI,KAAK,MAAM,UAAU,cAAc,KAAK,OAElD,OAAO,KAAK,MAAM,YAAY,KAAK;CAErC;CAEA,OAAO,KAAK,KAAK,KAAK;AACvB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getHumanReadablePath.mjs","names":[],"sources":["../../src/lib/getHumanReadablePath.ts"],"sourcesContent":["import type { FieldPath } from \"../types\";\nimport { getHumanReadableModelName } from \"./getHumanReadableModelName\";\n\ntype GetHumanReadablePathArgs = {\n\tpath: FieldPath;\n};\n\nexport function getHumanReadablePath(args: GetHumanReadablePathArgs): string {\n\treturn args.path\n\t\t.map((element) => {\n\t\t\tif (element.label) {\n\t\t\t\treturn element.label;\n\t\t\t} else if (element.model) {\n\t\t\t\treturn getHumanReadableModelName({\n\t\t\t\t\tmodel: element.model,\n\t\t\t\t\tname: element.name,\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\treturn element.name;\n\t\t\t}\n\t\t})\n\t\t.join(\" → \");\n}\n"],"mappings":";;AAOA,SAAgB,qBAAqB,MAAwC;
|
|
1
|
+
{"version":3,"file":"getHumanReadablePath.mjs","names":[],"sources":["../../src/lib/getHumanReadablePath.ts"],"sourcesContent":["import type { FieldPath } from \"../types\";\nimport { getHumanReadableModelName } from \"./getHumanReadableModelName\";\n\ntype GetHumanReadablePathArgs = {\n\tpath: FieldPath;\n};\n\nexport function getHumanReadablePath(args: GetHumanReadablePathArgs): string {\n\treturn args.path\n\t\t.map((element) => {\n\t\t\tif (element.label) {\n\t\t\t\treturn element.label;\n\t\t\t} else if (element.model) {\n\t\t\t\treturn getHumanReadableModelName({\n\t\t\t\t\tmodel: element.model,\n\t\t\t\t\tname: element.name,\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\treturn element.name;\n\t\t\t}\n\t\t})\n\t\t.join(\" → \");\n}\n"],"mappings":";;AAOA,SAAgB,qBAAqB,MAAwC;CAC5E,OAAO,KAAK,KACV,KAAK,YAAY;EACjB,IAAI,QAAQ,OACX,OAAO,QAAQ;OACT,IAAI,QAAQ,OAClB,OAAO,0BAA0B;GAChC,OAAO,QAAQ;GACf,MAAM,QAAQ;EACf,CAAC;OAED,OAAO,QAAQ;CAEjB,CAAC,CAAC,CACD,KAAK,KAAK;AACb"}
|
package/dist/package.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package.mjs","names":[],"sources":["../package.json"],"sourcesContent":[""],"mappings":""}
|
package/dist/types.d.mts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { CustomTypeModel, SharedSliceModel } from "@prismicio/client";
|
|
2
|
-
|
|
3
2
|
//#region src/types.d.ts
|
|
4
|
-
type FieldConfigs = {
|
|
3
|
+
export type FieldConfigs = {
|
|
5
4
|
embed?: {
|
|
6
5
|
providerTypes?: Record<string, string>;
|
|
7
6
|
};
|
|
@@ -10,5 +9,4 @@ type FieldConfigs = {
|
|
|
10
9
|
};
|
|
11
10
|
};
|
|
12
11
|
//#endregion
|
|
13
|
-
export { FieldConfigs };
|
|
14
12
|
//# sourceMappingURL=types.d.mts.map
|
package/dist/types.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"mappings":";;YAyBY;EACX;IACC,gBAAgB;;EAEjB;IACC,eAAe"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prismic-ts-codegen",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.31",
|
|
4
4
|
"description": "An experimental Prismic model-to-TypeScript-type generator",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"prismic",
|
|
@@ -42,42 +42,35 @@
|
|
|
42
42
|
"bench:watch": "vitest bench"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"pascal-case": "^3.1.2",
|
|
52
|
-
"quick-lru": "^6.1.2",
|
|
53
|
-
"zod": "^4.3.6"
|
|
45
|
+
"change-case": "^5.4.4",
|
|
46
|
+
"dedent": "^1.7.2",
|
|
47
|
+
"jiti": "^2.7.0",
|
|
48
|
+
"quick-lru": "^7.3.0",
|
|
49
|
+
"tinyglobby": "^0.2.17",
|
|
50
|
+
"zod": "^4.6.5"
|
|
54
51
|
},
|
|
55
52
|
"devDependencies": {
|
|
56
|
-
"@prismicio/client": "7.
|
|
57
|
-
"@prismicio/mock": "0.7.
|
|
58
|
-
"@types/
|
|
59
|
-
"@
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"vitest": "^4.1.2"
|
|
53
|
+
"@prismicio/client": "7.22.3",
|
|
54
|
+
"@prismicio/mock": "0.7.2",
|
|
55
|
+
"@types/node": "^26.6.4",
|
|
56
|
+
"@vitest/coverage-v8": "^5.0.3",
|
|
57
|
+
"oxfmt": "^0.72.0",
|
|
58
|
+
"oxlint": "^1.87.0",
|
|
59
|
+
"ts-morph": "^28.0.0",
|
|
60
|
+
"tsdown": "^0.23.0",
|
|
61
|
+
"typescript": "^7.0.2",
|
|
62
|
+
"vitest": "^5.0.3"
|
|
67
63
|
},
|
|
68
64
|
"peerDependencies": {
|
|
69
65
|
"@prismicio/client": "^6.6 || ^7",
|
|
70
66
|
"@prismicio/types": "^0.2.8"
|
|
71
67
|
},
|
|
72
68
|
"peerDependenciesMeta": {
|
|
73
|
-
"@prismicio/client": {
|
|
74
|
-
"optional": true
|
|
75
|
-
},
|
|
76
69
|
"@prismicio/types": {
|
|
77
70
|
"optional": true
|
|
78
71
|
}
|
|
79
72
|
},
|
|
80
73
|
"engines": {
|
|
81
|
-
"node": ">=
|
|
74
|
+
"node": ">=22"
|
|
82
75
|
}
|
|
83
76
|
}
|
package/src/cli/index.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import { existsSync, writeFileSync } from "fs";
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
2
3
|
import { resolve as resolvePath } from "path";
|
|
3
4
|
|
|
4
|
-
import
|
|
5
|
-
import meow from "meow";
|
|
6
|
-
|
|
5
|
+
import packageJson from "../../package.json" with { type: "json" };
|
|
7
6
|
import { detectTypesProvider, generateTypes } from "../index";
|
|
7
|
+
import { dedent } from "../lib/dedent";
|
|
8
8
|
import { configSchema } from "./configSchema";
|
|
9
9
|
import { NON_EDITABLE_FILE_HEADER } from "./constants";
|
|
10
10
|
import { loadConfig } from "./loadConfig";
|
|
11
11
|
import { loadLocaleIDs } from "./loadLocaleIDs";
|
|
12
12
|
import { loadModels } from "./loadModels";
|
|
13
13
|
|
|
14
|
-
const
|
|
15
|
-
|
|
14
|
+
const HELP = dedent`
|
|
15
|
+
${packageJson.description}
|
|
16
|
+
|
|
16
17
|
Usage:
|
|
17
18
|
prismic-ts-codegen [options...]
|
|
18
19
|
prismic-ts-codegen init [options...]
|
|
@@ -22,22 +23,37 @@ const cli = meow(
|
|
|
22
23
|
|
|
23
24
|
Options:
|
|
24
25
|
-c, --config <path> Path to a prismic-ts-codegen configuration file.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
flags: {
|
|
29
|
-
config: {
|
|
30
|
-
type: "string",
|
|
31
|
-
shortFlag: "c",
|
|
32
|
-
isRequired: false,
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
);
|
|
26
|
+
-h, --help Show this help.
|
|
27
|
+
-v, --version Show the version.
|
|
28
|
+
`;
|
|
37
29
|
|
|
38
30
|
const main = async () => {
|
|
39
|
-
|
|
40
|
-
|
|
31
|
+
const {
|
|
32
|
+
positionals: [command],
|
|
33
|
+
values: { config: configFlag, help, version },
|
|
34
|
+
} = parseArgs({
|
|
35
|
+
options: {
|
|
36
|
+
config: { type: "string", short: "c" },
|
|
37
|
+
help: { type: "boolean", short: "h" },
|
|
38
|
+
version: { type: "boolean", short: "v" },
|
|
39
|
+
},
|
|
40
|
+
allowPositionals: true,
|
|
41
|
+
strict: false,
|
|
42
|
+
});
|
|
43
|
+
const configPathFlag = typeof configFlag === "string" ? configFlag : undefined;
|
|
44
|
+
|
|
45
|
+
if (help) {
|
|
46
|
+
console.info(`\n${HELP}\n`);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (version) {
|
|
51
|
+
console.info(packageJson.version);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (command === "init") {
|
|
56
|
+
const configPath = configPathFlag || "prismicCodegen.config.ts";
|
|
41
57
|
|
|
42
58
|
if (existsSync(configPath)) {
|
|
43
59
|
console.info(`\n${configPath} already exists.`);
|
|
@@ -45,7 +61,7 @@ const main = async () => {
|
|
|
45
61
|
let contents = "";
|
|
46
62
|
|
|
47
63
|
if (existsSync("slicemachine.config.json") || existsSync("sm.json")) {
|
|
48
|
-
contents =
|
|
64
|
+
contents = dedent`
|
|
49
65
|
import type { Config } from "prismic-ts-codegen";
|
|
50
66
|
|
|
51
67
|
const config: Config = {
|
|
@@ -56,7 +72,7 @@ const main = async () => {
|
|
|
56
72
|
export default config;
|
|
57
73
|
`;
|
|
58
74
|
} else {
|
|
59
|
-
contents =
|
|
75
|
+
contents = dedent`
|
|
60
76
|
import type { Config } from "prismic-ts-codegen";
|
|
61
77
|
|
|
62
78
|
const config: Config = {
|
|
@@ -72,7 +88,7 @@ const main = async () => {
|
|
|
72
88
|
console.info(`\nCreated prismic-ts-codegen config file: ${configPath}`);
|
|
73
89
|
}
|
|
74
90
|
} else {
|
|
75
|
-
const unvalidatedConfig = loadConfig({ path:
|
|
91
|
+
const unvalidatedConfig = await loadConfig({ path: configPathFlag });
|
|
76
92
|
|
|
77
93
|
const result = configSchema.safeParse(unvalidatedConfig);
|
|
78
94
|
|
package/src/cli/loadConfig.ts
CHANGED
|
@@ -1,17 +1,11 @@
|
|
|
1
1
|
import { existsSync } from "fs";
|
|
2
2
|
import { resolve as resolvePath } from "path";
|
|
3
3
|
|
|
4
|
-
import
|
|
4
|
+
import { createJiti } from "jiti";
|
|
5
5
|
|
|
6
6
|
import type { Config } from "./configSchema";
|
|
7
7
|
|
|
8
|
-
const jiti =
|
|
9
|
-
|
|
10
|
-
const loadModuleWithJiti = <TModule>(id: string): TModule => {
|
|
11
|
-
const mod = jiti(id) as TModule | { default: TModule };
|
|
12
|
-
|
|
13
|
-
return typeof mod === "object" && mod !== null && "default" in mod ? mod.default : mod;
|
|
14
|
-
};
|
|
8
|
+
const jiti = createJiti(process.cwd());
|
|
15
9
|
|
|
16
10
|
const DEFAULT_CONFIG_PATHS = ["prismicCodegen.config.ts", "prismicCodegen.config.js"];
|
|
17
11
|
|
|
@@ -19,17 +13,17 @@ type LoadConfigConfig = {
|
|
|
19
13
|
path?: string;
|
|
20
14
|
};
|
|
21
15
|
|
|
22
|
-
export const loadConfig = (config: LoadConfigConfig): Config => {
|
|
16
|
+
export const loadConfig = async (config: LoadConfigConfig): Promise<Config> => {
|
|
23
17
|
if (config.path) {
|
|
24
18
|
if (existsSync(config.path)) {
|
|
25
|
-
return
|
|
19
|
+
return await jiti.import(resolvePath(config.path), { default: true });
|
|
26
20
|
} else {
|
|
27
21
|
throw new Error(`Config file does not exist: ${config.path}`);
|
|
28
22
|
}
|
|
29
23
|
} else {
|
|
30
24
|
for (const configPath of DEFAULT_CONFIG_PATHS) {
|
|
31
25
|
if (existsSync(configPath)) {
|
|
32
|
-
return
|
|
26
|
+
return await jiti.import(resolvePath(configPath), { default: true });
|
|
33
27
|
}
|
|
34
28
|
}
|
|
35
29
|
}
|
package/src/cli/loadLocaleIDs.ts
CHANGED
package/src/cli/loadModels.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { readFileSync } from "fs";
|
|
2
2
|
|
|
3
3
|
import type { CustomTypeModel, SharedSliceModel } from "@prismicio/client";
|
|
4
|
-
import
|
|
5
|
-
import fg from "fast-glob";
|
|
6
|
-
import fetch from "node-fetch";
|
|
4
|
+
import { glob } from "tinyglobby";
|
|
7
5
|
|
|
8
6
|
const isCustomTypeModel = (input: unknown): input is CustomTypeModel => {
|
|
9
7
|
return typeof input === "object" && input !== null && "json" in input;
|
|
@@ -14,7 +12,10 @@ const isSharedSliceModel = (input: unknown): input is SharedSliceModel => {
|
|
|
14
12
|
};
|
|
15
13
|
|
|
16
14
|
const readJSONFromGlob = async <T>(globs: string): Promise<T[]> => {
|
|
17
|
-
const paths = await
|
|
15
|
+
const paths = await glob(
|
|
16
|
+
globs.split(",").map((path) => path.trim()),
|
|
17
|
+
{ expandDirectories: false },
|
|
18
|
+
);
|
|
18
19
|
|
|
19
20
|
return paths.map((path) => {
|
|
20
21
|
const raw = readFileSync(path, "utf8");
|
|
@@ -23,6 +24,26 @@ const readJSONFromGlob = async <T>(globs: string): Promise<T[]> => {
|
|
|
23
24
|
});
|
|
24
25
|
};
|
|
25
26
|
|
|
27
|
+
const fetchCustomTypesAPI = async <T>(
|
|
28
|
+
path: string,
|
|
29
|
+
config: { repositoryName: string; customTypesAPIToken: string },
|
|
30
|
+
): Promise<T[]> => {
|
|
31
|
+
const res = await fetch(new URL(path, "https://customtypes.prismic.io/"), {
|
|
32
|
+
headers: {
|
|
33
|
+
repository: config.repositoryName,
|
|
34
|
+
Authorization: `Bearer ${config.customTypesAPIToken}`,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
if (!res.ok) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`Failed to fetch models from the Custom Types API (${res.status}): ${await res.text()}`,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return await res.json();
|
|
45
|
+
};
|
|
46
|
+
|
|
26
47
|
type LoadModelsConfig =
|
|
27
48
|
| {
|
|
28
49
|
localPaths?: string[];
|
|
@@ -44,16 +65,10 @@ export const loadModels = async (config: LoadModelsConfig): Promise<LoadModelsRe
|
|
|
44
65
|
const sharedSliceModels: Record<string, SharedSliceModel> = {};
|
|
45
66
|
|
|
46
67
|
if ("customTypesAPIToken" in config) {
|
|
47
|
-
const customTypesClient = prismicCT.createClient({
|
|
48
|
-
repositoryName: config.repositoryName,
|
|
49
|
-
token: config.customTypesAPIToken,
|
|
50
|
-
fetch,
|
|
51
|
-
});
|
|
52
|
-
|
|
53
68
|
if (config.fetchFromRepository) {
|
|
54
69
|
const [remoteCustomTypeModels, remoteSharedSliceModels] = await Promise.all([
|
|
55
|
-
|
|
56
|
-
|
|
70
|
+
fetchCustomTypesAPI<CustomTypeModel>("customtypes", config),
|
|
71
|
+
fetchCustomTypesAPI<SharedSliceModel>("slices", config),
|
|
57
72
|
]);
|
|
58
73
|
|
|
59
74
|
for (const customTypeModel of remoteCustomTypeModels) {
|
package/src/generateTypes.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { CustomTypeModel, SharedSliceModel } from "@prismicio/client";
|
|
2
|
-
import { source } from "common-tags";
|
|
3
2
|
import QuickLRU from "quick-lru";
|
|
4
3
|
|
|
5
4
|
import { addLine } from "./lib/addLine";
|
|
@@ -7,6 +6,7 @@ import { addSection } from "./lib/addSection";
|
|
|
7
6
|
import { buildCustomTypeType } from "./lib/buildCustomTypeType";
|
|
8
7
|
import { buildSharedSliceType } from "./lib/buildSharedSliceType";
|
|
9
8
|
import { buildUnion } from "./lib/buildUnion";
|
|
9
|
+
import { dedent } from "./lib/dedent";
|
|
10
10
|
import type { FieldConfigs } from "./types";
|
|
11
11
|
|
|
12
12
|
export type TypesProvider = "@prismicio/client" | "@prismicio/types";
|
|
@@ -207,7 +207,7 @@ type ContentRelationshipFieldWithData<
|
|
|
207
207
|
|
|
208
208
|
if (config.clientIntegration.includeContentNamespace) {
|
|
209
209
|
clientModuleCode = addSection(
|
|
210
|
-
|
|
210
|
+
dedent`
|
|
211
211
|
namespace Content {
|
|
212
212
|
export type {
|
|
213
213
|
${contentTypeNames.join(",\n")}
|
|
@@ -219,7 +219,7 @@ type ContentRelationshipFieldWithData<
|
|
|
219
219
|
}
|
|
220
220
|
|
|
221
221
|
code = addSection(
|
|
222
|
-
|
|
222
|
+
dedent`
|
|
223
223
|
declare module "@prismicio/client" {
|
|
224
224
|
${clientModuleCode}
|
|
225
225
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { CustomTypeModel } from "@prismicio/client";
|
|
2
|
-
import { source } from "common-tags";
|
|
3
2
|
|
|
4
3
|
import { buildTypeName } from "../lib/buildTypeName";
|
|
5
4
|
import type { AuxiliaryType, FieldConfigs } from "../types";
|
|
6
5
|
import { buildFieldProperties } from "./buildFieldProperties";
|
|
6
|
+
import { dedent } from "./dedent";
|
|
7
7
|
import { getHumanReadableModelName } from "./getHumanReadableModelName";
|
|
8
8
|
|
|
9
9
|
type BuildCustomTypeDataTypeArgs = {
|
|
@@ -55,7 +55,7 @@ export function buildCustomTypeDataType(
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
if (fieldProperties) {
|
|
58
|
-
code =
|
|
58
|
+
code = dedent`
|
|
59
59
|
/**
|
|
60
60
|
* Content for ${humanReadableName} documents
|
|
61
61
|
*/
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { CustomTypeModel } from "@prismicio/client";
|
|
2
|
-
import { source } from "common-tags";
|
|
3
2
|
import type QuickLRU from "quick-lru";
|
|
4
3
|
|
|
5
4
|
import { CUSTOM_TYPES_DOCUMENTATION_URL } from "../constants";
|
|
@@ -9,6 +8,7 @@ import { buildCustomTypeDataType } from "./buildCustomTypeDataType";
|
|
|
9
8
|
import { buildTypeName } from "./buildTypeName";
|
|
10
9
|
import { buildUnion } from "./buildUnion";
|
|
11
10
|
import { checkHasUIDField } from "./checkHasUIDFIeld";
|
|
11
|
+
import { dedent } from "./dedent";
|
|
12
12
|
import { getCacheKey } from "./getCacheKey";
|
|
13
13
|
import { getHumanReadableModelName } from "./getHumanReadableModelName";
|
|
14
14
|
|
|
@@ -27,7 +27,7 @@ type BuildCustomTypeTypeReturnValue = {
|
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
export function buildCustomTypeType(
|
|
30
|
-
args: BuildCustomTypeTypesArgs
|
|
30
|
+
args: BuildCustomTypeTypesArgs,
|
|
31
31
|
): BuildCustomTypeTypeReturnValue {
|
|
32
32
|
if (args.cache) {
|
|
33
33
|
const key = getCacheKey(args.model);
|
|
@@ -68,7 +68,7 @@ export function buildCustomTypeType(
|
|
|
68
68
|
code = addSection(dataType.code, code);
|
|
69
69
|
|
|
70
70
|
code = addSection(
|
|
71
|
-
|
|
71
|
+
dedent`
|
|
72
72
|
/**
|
|
73
73
|
* ${humanReadableName} document from Prismic
|
|
74
74
|
*
|
|
@@ -79,10 +79,10 @@ export function buildCustomTypeType(
|
|
|
79
79
|
* @typeParam Lang - Language API ID of the document.
|
|
80
80
|
*/
|
|
81
81
|
export type ${name}<Lang extends string = ${langDefault}> = prismic.${baseDocumentType}<Simplify<${
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
dataType.name
|
|
83
|
+
}>, "${args.model.id}", Lang>;
|
|
84
84
|
`,
|
|
85
|
-
code
|
|
85
|
+
code,
|
|
86
86
|
);
|
|
87
87
|
|
|
88
88
|
const result = {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { type CustomTypeModelField, CustomTypeModelFieldType } from "@prismicio/client";
|
|
2
|
-
import { source, stripIndent } from "common-tags";
|
|
3
2
|
|
|
4
3
|
import type { AuxiliaryType, FieldConfigs, FieldPath } from "../types";
|
|
5
4
|
import { addLine } from "./addLine";
|
|
@@ -7,6 +6,7 @@ import { addSection } from "./addSection";
|
|
|
7
6
|
import { buildFieldDocs } from "./buildFieldDocs";
|
|
8
7
|
import { buildTypeName } from "./buildTypeName";
|
|
9
8
|
import { buildUnion } from "./buildUnion";
|
|
9
|
+
import { dedent } from "./dedent";
|
|
10
10
|
import { getHumanReadablePath } from "./getHumanReadablePath";
|
|
11
11
|
|
|
12
12
|
type BuildFieldPropertyArgs = Pick<
|
|
@@ -284,7 +284,7 @@ function buildFieldProperty(args: BuildFieldPropertyArgs): BuildFieldPropertyRet
|
|
|
284
284
|
|
|
285
285
|
auxiliaryTypes.push({
|
|
286
286
|
name: itemName,
|
|
287
|
-
code:
|
|
287
|
+
code: dedent`
|
|
288
288
|
/**
|
|
289
289
|
* Item in *${humanReadablePath}*
|
|
290
290
|
*/
|
|
@@ -362,14 +362,14 @@ function buildFieldProperty(args: BuildFieldPropertyArgs): BuildFieldPropertyRet
|
|
|
362
362
|
auxiliaryTypes.push(...primaryFieldProperties.auxiliaryTypes);
|
|
363
363
|
contentTypeNames.push(...primaryFieldProperties.contentTypeNames);
|
|
364
364
|
|
|
365
|
-
let primaryCode =
|
|
365
|
+
let primaryCode = dedent`
|
|
366
366
|
/**
|
|
367
367
|
* Primary content in *${humanReadablePath}*
|
|
368
368
|
*/
|
|
369
369
|
`;
|
|
370
370
|
primaryCode = primaryFieldProperties.code
|
|
371
371
|
? addLine(
|
|
372
|
-
|
|
372
|
+
dedent`
|
|
373
373
|
export interface ${primaryInterfaceName} {
|
|
374
374
|
${primaryFieldProperties.code}
|
|
375
375
|
}
|
|
@@ -414,14 +414,14 @@ function buildFieldProperty(args: BuildFieldPropertyArgs): BuildFieldPropertyRet
|
|
|
414
414
|
auxiliaryTypes.push(...itemFieldProperties.auxiliaryTypes);
|
|
415
415
|
contentTypeNames.push(...itemFieldProperties.contentTypeNames);
|
|
416
416
|
|
|
417
|
-
let itemCode =
|
|
417
|
+
let itemCode = dedent`
|
|
418
418
|
/**
|
|
419
419
|
* Item content in *${humanReadablePath}*
|
|
420
420
|
*/
|
|
421
421
|
`;
|
|
422
422
|
itemCode = itemFieldProperties.code
|
|
423
423
|
? addLine(
|
|
424
|
-
|
|
424
|
+
dedent`
|
|
425
425
|
export interface ${itemInterfaceName} {
|
|
426
426
|
${itemFieldProperties.code}
|
|
427
427
|
}
|
|
@@ -439,7 +439,7 @@ function buildFieldProperty(args: BuildFieldPropertyArgs): BuildFieldPropertyRet
|
|
|
439
439
|
|
|
440
440
|
auxiliaryTypes.push({
|
|
441
441
|
name: sliceName,
|
|
442
|
-
code:
|
|
442
|
+
code: dedent`
|
|
443
443
|
/**
|
|
444
444
|
* Slice for *${getHumanReadablePath({
|
|
445
445
|
path: [
|