prismic-ts-codegen 0.1.30-pr.82.96148f3 → 0.1.30
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 +4 -2
- package/dist/cli/configSchema.d.mts.map +1 -1
- package/dist/cli/configSchema.mjs +2 -126
- package/dist/cli/configSchema.mjs.map +1 -1
- package/dist/cli/constants.d.mts +2 -1
- package/dist/cli/constants.d.mts.map +1 -1
- package/dist/cli/loadConfig.mjs +9 -9
- package/dist/cli/loadConfig.mjs.map +1 -1
- package/dist/cli/loadLocaleIDs.mjs +1 -0
- package/dist/cli/loadLocaleIDs.mjs.map +1 -1
- package/dist/cli/loadModels.mjs +10 -13
- package/dist/cli/loadModels.mjs.map +1 -1
- package/dist/cli.d.mts +1 -1
- package/dist/cli.mjs +16 -41
- package/dist/cli.mjs.map +1 -1
- package/dist/constants.mjs.map +1 -1
- package/dist/detectTypesProvider.d.mts +4 -2
- package/dist/detectTypesProvider.d.mts.map +1 -1
- package/dist/detectTypesProvider.mjs.map +1 -1
- package/dist/generateTypes.d.mts +5 -3
- package/dist/generateTypes.d.mts.map +1 -1
- package/dist/generateTypes.mjs +12 -15
- 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 +4 -9
- package/dist/lib/buildFieldDocs.mjs.map +1 -1
- package/dist/lib/buildFieldProperties.mjs +14 -18
- 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/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/types.d.mts +3 -1
- package/dist/types.d.mts.map +1 -1
- package/package.json +22 -18
- package/src/cli/index.ts +22 -38
- package/src/cli/loadConfig.ts +9 -7
- package/src/cli/loadLocaleIDs.ts +1 -0
- package/src/cli/loadModels.ts +12 -30
- 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 +33 -22
- package/src/lib/buildTypeName.ts +4 -9
- package/dist/lib/dedent.mjs +0 -12
- package/dist/lib/dedent.mjs.map +0 -1
- package/dist/package.mjs +0 -7
- package/dist/package.mjs.map +0 -1
- package/src/lib/dedent.ts +0 -14
|
@@ -1 +1 @@
|
|
|
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 {
|
|
1
|
+
{"version":3,"file":"buildSharedSliceType.mjs","names":[],"sources":["../../src/lib/buildSharedSliceType.ts"],"sourcesContent":["import type { SharedSliceModel } from \"@prismicio/client\";\nimport { source, stripIndent } from \"common-tags\";\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 { 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 (\n\t\t\tvariationModel.primary &&\n\t\t\tObject.keys(variationModel.primary).length > 0\n\t\t) {\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 = stripIndent`\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? source`\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: source`\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 = stripIndent`\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? source`\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: source`\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\tsource`\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<\"${\n\t\t\t\tvariationModel.id\n\t\t\t}\", ${\n\t\t\t\tprimaryInterfaceName\n\t\t\t\t\t? `Simplify<${primaryInterfaceName}>`\n\t\t\t\t\t: `Record<string, never>`\n\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\tsource`\n\t\t\t/**\n\t\t\t * Slice variation for *${humanReadableName}*\n\t\t\t */\n\t\t\ttype ${variationUnionName} = ${\n\t\t\tvariationNames.length > 0 ? variationsUnion : \"never\"\n\t\t}\n\t\t`,\n\t\tcode\n\t);\n\n\tcode = addSection(\n\t\tsource`\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<\"${\n\t\t\targs.model.id\n\t\t}\", ${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 } from "
|
|
1
|
+
import { pascalCase, pascalCaseTransformMerge } from "pascal-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(" "), { transform: pascalCaseTransformMerge });
|
|
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 } from \"
|
|
1
|
+
{"version":3,"file":"buildTypeName.mjs","names":[],"sources":["../../src/lib/buildTypeName.ts"],"sourcesContent":["import { pascalCase, pascalCaseTransformMerge } from \"pascal-case\";\n\nexport const buildTypeName = (...parts: string[]): string => {\n\tlet name = pascalCase(parts.filter(Boolean).join(\" \"), {\n\t\ttransform: pascalCaseTransformMerge,\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;CAC5D,IAAI,OAAO,WAAW,MAAM,OAAO,QAAQ,CAAC,KAAK,IAAI,EAAE,EACtD,WAAW,0BACX,CAAC;AAEF,KAAI,SAAS,KAAK,KAAK,GAAG,MAAM,GAC/B,QAAO,IAAI;AAGZ,QAAO"}
|
|
@@ -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;AACnD,QAAO,MAAM,OAAO,QAAQ,CAAC,KAAK,MAAM,IAAI"}
|
|
@@ -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;AACjE,QAAO,SAAS,OAAO,OAAO,EAAE,EAAE,GAAG,OAAO,OAAO,MAAM,KAAK,CAAC"}
|
|
@@ -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;AAEb,MAAK,IAAI,IAAI,GAAG,IAAI,KAAK,KAAK,QAAQ,KAAK;AAC1C,MAAI,IAAI,EACP,WAAU;EAGX,MAAM,UAAU,KAAK,KAAK;AAE1B,MACC,QAAQ,SACR,UAAU,QAAQ,UACjB,QAAQ,MAAM,SAAS,WAAW,QAAQ,MAAM,SAAS,UAE1D,WAAU,GAAG,QAAQ,KAAK;OACpB;AACN,OAAI,QAAQ,SAAS,SAAS;IAC7B,MAAM,kBAAkB,KAAK,KAAK,IAAI;AAEtC,QACE,gBAAgB,SAAS,UAAU,gBAAgB,SACnD,gBAAgB,SAChB,UAAU,gBAAgB,SAC1B,gBAAgB,MAAM,SAAS,eAC/B;AACD,eAAU,GAAG,QAAQ,KAAK;AAE1B;;;AAIF,aAAU,QAAQ;;;AAIpB,QAAO"}
|
|
@@ -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;AACtD,QAAO,OAAO,WAAW,OAAO,CAAC,OAAO,KAAK,UAAU,MAAM,CAAC,CAAC,OAAO,MAAM"}
|
|
@@ -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;AACjF,KAAI,UAAU,KAAK,MAGlB,QAAO,KAAK,MAAM,SAAS,KAAK,MAAM;UAC5B,UAAU,KAAK,SAAS,KAAK,MAAM,SAAS,cAGtD,QAAO,KAAK,MAAM;UACR,UAAU,KAAK,SAAS,KAAK,MAAM,SAAS,QAGtD,QAAO,KAAK,MAAM,YAAY,KAAK;UACzB,UAAU,KAAK;MAGrB,KAAK,MAAM,UAAU,WAAW,KAAK,MAAM,OAE9C,QAAO,KAAK,MAAM,OAAO,SAAS,KAAK;WAC7B,KAAK,MAAM,UAAU,cAAc,KAAK,MAElD,QAAO,KAAK,MAAM,YAAY,KAAK;;AAIrC,QAAO,KAAK,KAAK,KAAK"}
|
|
@@ -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;AAC5E,QAAO,KAAK,KACV,KAAK,YAAY;AACjB,MAAI,QAAQ,MACX,QAAO,QAAQ;WACL,QAAQ,MAClB,QAAO,0BAA0B;GAChC,OAAO,QAAQ;GACf,MAAM,QAAQ;GACd,CAAC;MAEF,QAAO,QAAQ;GAEf,CACD,KAAK,MAAM"}
|
package/dist/types.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { CustomTypeModel, SharedSliceModel } from "@prismicio/client";
|
|
2
|
+
|
|
2
3
|
//#region src/types.d.ts
|
|
3
|
-
|
|
4
|
+
type FieldConfigs = {
|
|
4
5
|
embed?: {
|
|
5
6
|
providerTypes?: Record<string, string>;
|
|
6
7
|
};
|
|
@@ -9,4 +10,5 @@ export type FieldConfigs = {
|
|
|
9
10
|
};
|
|
10
11
|
};
|
|
11
12
|
//#endregion
|
|
13
|
+
export { FieldConfigs };
|
|
12
14
|
//# 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":";;;KAyBY,YAAA;EACX,KAAA;IACC,aAAA,GAAgB,MAAA;EAAA;EAEjB,iBAAA;IACC,YAAA,GAAe,MAAA;EAAA;AAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prismic-ts-codegen",
|
|
3
|
-
"version": "0.1.30
|
|
3
|
+
"version": "0.1.30",
|
|
4
4
|
"description": "An experimental Prismic model-to-TypeScript-type generator",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"prismic",
|
|
@@ -42,24 +42,28 @@
|
|
|
42
42
|
"bench:watch": "vitest bench"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
45
|
+
"@prismicio/custom-types-client": "^1.3.1",
|
|
46
|
+
"common-tags": "^1.8.2",
|
|
47
|
+
"fast-glob": "^3.3.3",
|
|
48
|
+
"jiti": "^1.21.7",
|
|
49
|
+
"meow": "~12.0.1",
|
|
50
|
+
"node-fetch": "^3.3.2",
|
|
51
|
+
"pascal-case": "^3.1.2",
|
|
52
|
+
"quick-lru": "^6.1.2",
|
|
53
|
+
"zod": "^4.3.6"
|
|
51
54
|
},
|
|
52
55
|
"devDependencies": {
|
|
53
|
-
"@prismicio/client": "7.
|
|
54
|
-
"@prismicio/mock": "0.7.
|
|
55
|
-
"@types/
|
|
56
|
-
"@
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
56
|
+
"@prismicio/client": "7.18.0",
|
|
57
|
+
"@prismicio/mock": "0.7.1",
|
|
58
|
+
"@types/common-tags": "^1.8.1",
|
|
59
|
+
"@types/node": "^25.5.2",
|
|
60
|
+
"@vitest/coverage-v8": "^4.1.2",
|
|
61
|
+
"oxfmt": "^0.43.0",
|
|
62
|
+
"oxlint": "^1.58.0",
|
|
63
|
+
"prismic-ts-codegen-v0-1-11": "npm:prismic-ts-codegen@0.1.11",
|
|
64
|
+
"tsdown": "^0.21.7",
|
|
65
|
+
"typescript": "^6.0.2",
|
|
66
|
+
"vitest": "^4.1.2"
|
|
63
67
|
},
|
|
64
68
|
"peerDependencies": {
|
|
65
69
|
"@prismicio/client": "^6.6 || ^7",
|
|
@@ -74,6 +78,6 @@
|
|
|
74
78
|
}
|
|
75
79
|
},
|
|
76
80
|
"engines": {
|
|
77
|
-
"node": ">=
|
|
81
|
+
"node": ">=20"
|
|
78
82
|
}
|
|
79
83
|
}
|
package/src/cli/index.ts
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
import { existsSync, writeFileSync } from "fs";
|
|
2
|
-
import { parseArgs } from "node:util";
|
|
3
2
|
import { resolve as resolvePath } from "path";
|
|
4
3
|
|
|
5
|
-
import
|
|
4
|
+
import { stripIndent } from "common-tags";
|
|
5
|
+
import meow from "meow";
|
|
6
|
+
|
|
6
7
|
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
|
-
|
|
16
|
-
|
|
14
|
+
const cli = meow(
|
|
15
|
+
`
|
|
17
16
|
Usage:
|
|
18
17
|
prismic-ts-codegen [options...]
|
|
19
18
|
prismic-ts-codegen init [options...]
|
|
@@ -23,37 +22,22 @@ const HELP = dedent`
|
|
|
23
22
|
|
|
24
23
|
Options:
|
|
25
24
|
-c, --config <path> Path to a prismic-ts-codegen configuration file.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
options: {
|
|
36
|
-
config: { type: "string", short: "c" },
|
|
37
|
-
help: { type: "boolean", short: "h" },
|
|
38
|
-
version: { type: "boolean", short: "v" },
|
|
25
|
+
`,
|
|
26
|
+
{
|
|
27
|
+
importMeta: import.meta,
|
|
28
|
+
flags: {
|
|
29
|
+
config: {
|
|
30
|
+
type: "string",
|
|
31
|
+
shortFlag: "c",
|
|
32
|
+
isRequired: false,
|
|
33
|
+
},
|
|
39
34
|
},
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
}
|
|
35
|
+
},
|
|
36
|
+
);
|
|
54
37
|
|
|
55
|
-
|
|
56
|
-
|
|
38
|
+
const main = async () => {
|
|
39
|
+
if (cli.input[0] === "init") {
|
|
40
|
+
const configPath = cli.flags.config || "prismicCodegen.config.ts";
|
|
57
41
|
|
|
58
42
|
if (existsSync(configPath)) {
|
|
59
43
|
console.info(`\n${configPath} already exists.`);
|
|
@@ -61,7 +45,7 @@ const main = async () => {
|
|
|
61
45
|
let contents = "";
|
|
62
46
|
|
|
63
47
|
if (existsSync("slicemachine.config.json") || existsSync("sm.json")) {
|
|
64
|
-
contents =
|
|
48
|
+
contents = stripIndent`
|
|
65
49
|
import type { Config } from "prismic-ts-codegen";
|
|
66
50
|
|
|
67
51
|
const config: Config = {
|
|
@@ -72,7 +56,7 @@ const main = async () => {
|
|
|
72
56
|
export default config;
|
|
73
57
|
`;
|
|
74
58
|
} else {
|
|
75
|
-
contents =
|
|
59
|
+
contents = stripIndent`
|
|
76
60
|
import type { Config } from "prismic-ts-codegen";
|
|
77
61
|
|
|
78
62
|
const config: Config = {
|
|
@@ -88,7 +72,7 @@ const main = async () => {
|
|
|
88
72
|
console.info(`\nCreated prismic-ts-codegen config file: ${configPath}`);
|
|
89
73
|
}
|
|
90
74
|
} else {
|
|
91
|
-
const unvalidatedConfig =
|
|
75
|
+
const unvalidatedConfig = loadConfig({ path: cli.flags.config });
|
|
92
76
|
|
|
93
77
|
const result = configSchema.safeParse(unvalidatedConfig);
|
|
94
78
|
|
package/src/cli/loadConfig.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { existsSync } from "fs";
|
|
2
2
|
import { resolve as resolvePath } from "path";
|
|
3
3
|
|
|
4
|
-
import
|
|
4
|
+
import _jiti from "jiti";
|
|
5
5
|
|
|
6
6
|
import type { Config } from "./configSchema";
|
|
7
7
|
|
|
8
|
-
const jiti =
|
|
8
|
+
const jiti = _jiti(process.cwd());
|
|
9
9
|
|
|
10
|
-
const loadModuleWithJiti =
|
|
11
|
-
|
|
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;
|
|
12
14
|
};
|
|
13
15
|
|
|
14
16
|
const DEFAULT_CONFIG_PATHS = ["prismicCodegen.config.ts", "prismicCodegen.config.js"];
|
|
@@ -17,17 +19,17 @@ type LoadConfigConfig = {
|
|
|
17
19
|
path?: string;
|
|
18
20
|
};
|
|
19
21
|
|
|
20
|
-
export const loadConfig =
|
|
22
|
+
export const loadConfig = (config: LoadConfigConfig): Config => {
|
|
21
23
|
if (config.path) {
|
|
22
24
|
if (existsSync(config.path)) {
|
|
23
|
-
return
|
|
25
|
+
return loadModuleWithJiti(resolvePath(config.path));
|
|
24
26
|
} else {
|
|
25
27
|
throw new Error(`Config file does not exist: ${config.path}`);
|
|
26
28
|
}
|
|
27
29
|
} else {
|
|
28
30
|
for (const configPath of DEFAULT_CONFIG_PATHS) {
|
|
29
31
|
if (existsSync(configPath)) {
|
|
30
|
-
return
|
|
32
|
+
return loadModuleWithJiti(resolvePath(configPath));
|
|
31
33
|
}
|
|
32
34
|
}
|
|
33
35
|
}
|
package/src/cli/loadLocaleIDs.ts
CHANGED
package/src/cli/loadModels.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { readFileSync } from "fs";
|
|
2
2
|
|
|
3
3
|
import type { CustomTypeModel, SharedSliceModel } from "@prismicio/client";
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
import * as prismicCT from "@prismicio/custom-types-client";
|
|
5
|
+
import fg from "fast-glob";
|
|
6
|
+
import fetch from "node-fetch";
|
|
7
7
|
|
|
8
8
|
const isCustomTypeModel = (input: unknown): input is CustomTypeModel => {
|
|
9
9
|
return typeof input === "object" && input !== null && "json" in input;
|
|
@@ -14,10 +14,7 @@ const isSharedSliceModel = (input: unknown): input is SharedSliceModel => {
|
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
const readJSONFromGlob = async <T>(globs: string): Promise<T[]> => {
|
|
17
|
-
const paths = await
|
|
18
|
-
globs.split(",").map((path) => path.trim()),
|
|
19
|
-
{ expandDirectories: false },
|
|
20
|
-
);
|
|
17
|
+
const paths = await fg(globs.split(",").map((path) => path.trim()));
|
|
21
18
|
|
|
22
19
|
return paths.map((path) => {
|
|
23
20
|
const raw = readFileSync(path, "utf8");
|
|
@@ -26,27 +23,6 @@ const readJSONFromGlob = async <T>(globs: string): Promise<T[]> => {
|
|
|
26
23
|
});
|
|
27
24
|
};
|
|
28
25
|
|
|
29
|
-
const fetchCustomTypesAPI = async <T>(
|
|
30
|
-
path: string,
|
|
31
|
-
config: { repositoryName: string; customTypesAPIToken: string },
|
|
32
|
-
): Promise<T[]> => {
|
|
33
|
-
const url = new URL(path, CUSTOM_TYPES_API_ENDPOINT);
|
|
34
|
-
const res = await fetch(url, {
|
|
35
|
-
headers: {
|
|
36
|
-
repository: config.repositoryName,
|
|
37
|
-
Authorization: `Bearer ${config.customTypesAPIToken}`,
|
|
38
|
-
},
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
if (!res.ok) {
|
|
42
|
-
throw new Error(
|
|
43
|
-
`Failed to fetch models from the Custom Types API (${res.status}): ${await res.text()}`,
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return await res.json();
|
|
48
|
-
};
|
|
49
|
-
|
|
50
26
|
type LoadModelsConfig =
|
|
51
27
|
| {
|
|
52
28
|
localPaths?: string[];
|
|
@@ -68,10 +44,16 @@ export const loadModels = async (config: LoadModelsConfig): Promise<LoadModelsRe
|
|
|
68
44
|
const sharedSliceModels: Record<string, SharedSliceModel> = {};
|
|
69
45
|
|
|
70
46
|
if ("customTypesAPIToken" in config) {
|
|
47
|
+
const customTypesClient = prismicCT.createClient({
|
|
48
|
+
repositoryName: config.repositoryName,
|
|
49
|
+
token: config.customTypesAPIToken,
|
|
50
|
+
fetch,
|
|
51
|
+
});
|
|
52
|
+
|
|
71
53
|
if (config.fetchFromRepository) {
|
|
72
54
|
const [remoteCustomTypeModels, remoteSharedSliceModels] = await Promise.all([
|
|
73
|
-
|
|
74
|
-
|
|
55
|
+
customTypesClient.getAllCustomTypes(),
|
|
56
|
+
customTypesClient.getAllSharedSlices(),
|
|
75
57
|
]);
|
|
76
58
|
|
|
77
59
|
for (const customTypeModel of remoteCustomTypeModels) {
|
package/src/generateTypes.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { CustomTypeModel, SharedSliceModel } from "@prismicio/client";
|
|
2
|
+
import { source } from "common-tags";
|
|
2
3
|
import QuickLRU from "quick-lru";
|
|
3
4
|
|
|
4
5
|
import { addLine } from "./lib/addLine";
|
|
@@ -6,7 +7,6 @@ import { addSection } from "./lib/addSection";
|
|
|
6
7
|
import { buildCustomTypeType } from "./lib/buildCustomTypeType";
|
|
7
8
|
import { buildSharedSliceType } from "./lib/buildSharedSliceType";
|
|
8
9
|
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
|
+
source`
|
|
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
|
+
source`
|
|
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";
|
|
2
3
|
|
|
3
4
|
import { buildTypeName } from "../lib/buildTypeName";
|
|
4
5
|
import type { AuxiliaryType, FieldConfigs } from "../types";
|
|
5
6
|
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 = source`
|
|
59
59
|
/**
|
|
60
60
|
* Content for ${humanReadableName} documents
|
|
61
61
|
*/
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { CustomTypeModel } from "@prismicio/client";
|
|
2
|
+
import { source } from "common-tags";
|
|
2
3
|
import type QuickLRU from "quick-lru";
|
|
3
4
|
|
|
4
5
|
import { CUSTOM_TYPES_DOCUMENTATION_URL } from "../constants";
|
|
@@ -8,7 +9,6 @@ import { buildCustomTypeDataType } from "./buildCustomTypeDataType";
|
|
|
8
9
|
import { buildTypeName } from "./buildTypeName";
|
|
9
10
|
import { buildUnion } from "./buildUnion";
|
|
10
11
|
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
|
+
source`
|
|
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,4 +1,5 @@
|
|
|
1
1
|
import { type CustomTypeModelField, CustomTypeModelFieldType } from "@prismicio/client";
|
|
2
|
+
import { source, stripIndent } from "common-tags";
|
|
2
3
|
|
|
3
4
|
import type { AuxiliaryType, FieldConfigs, FieldPath } from "../types";
|
|
4
5
|
import { addLine } from "./addLine";
|
|
@@ -6,7 +7,6 @@ import { addSection } from "./addSection";
|
|
|
6
7
|
import { buildFieldDocs } from "./buildFieldDocs";
|
|
7
8
|
import { buildTypeName } from "./buildTypeName";
|
|
8
9
|
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: source`
|
|
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 = stripIndent`
|
|
366
366
|
/**
|
|
367
367
|
* Primary content in *${humanReadablePath}*
|
|
368
368
|
*/
|
|
369
369
|
`;
|
|
370
370
|
primaryCode = primaryFieldProperties.code
|
|
371
371
|
? addLine(
|
|
372
|
-
|
|
372
|
+
source`
|
|
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 = stripIndent`
|
|
418
418
|
/**
|
|
419
419
|
* Item content in *${humanReadablePath}*
|
|
420
420
|
*/
|
|
421
421
|
`;
|
|
422
422
|
itemCode = itemFieldProperties.code
|
|
423
423
|
? addLine(
|
|
424
|
-
|
|
424
|
+
source`
|
|
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: stripIndent`
|
|
443
443
|
/**
|
|
444
444
|
* Slice for *${getHumanReadablePath({
|
|
445
445
|
path: [
|