@contentstack/utils
Advanced tools
| function assertPlainObject(value, message) { | ||
| if (value === null || value === undefined) { | ||
| throw new TypeError(message); | ||
| } | ||
| if (typeof value !== 'object' || Array.isArray(value)) { | ||
| throw new TypeError(message); | ||
| } | ||
| } | ||
| function requireEntryUid(entry) { | ||
| var uid = entry.uid; | ||
| if (typeof uid !== 'string' || uid.length === 0) { | ||
| throw new Error('Entry uid is required. The entry must include a non-empty uid string.'); | ||
| } | ||
| return uid; | ||
| } | ||
| function resolveContentTypeUid(entry, contentTypeUid) { | ||
| var fromEntry = entry._content_type_uid; | ||
| if (typeof fromEntry === 'string' && fromEntry.length > 0) { | ||
| return fromEntry; | ||
| } | ||
| if (typeof contentTypeUid === 'string' && contentTypeUid.length > 0) { | ||
| return contentTypeUid; | ||
| } | ||
| return ''; | ||
| } | ||
| function collectVariantAliases(entry) { | ||
| var publishDetails = entry.publish_details; | ||
| if (!publishDetails || typeof publishDetails !== 'object' || Array.isArray(publishDetails)) { | ||
| return []; | ||
| } | ||
| var variants = publishDetails.variants; | ||
| if (!variants || typeof variants !== 'object' || Array.isArray(variants)) { | ||
| return []; | ||
| } | ||
| var out = []; | ||
| var map = variants; | ||
| for (var _i = 0, _a = Object.keys(map); _i < _a.length; _i++) { | ||
| var key = _a[_i]; | ||
| var v = map[key]; | ||
| if (!v || typeof v !== 'object' || Array.isArray(v)) { | ||
| continue; | ||
| } | ||
| var alias = v.alias; | ||
| if (typeof alias === 'string' && alias.length > 0) { | ||
| out.push(alias); | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
| function mapEntryToResult(entry, contentTypeUid) { | ||
| return { | ||
| entry_uid: requireEntryUid(entry), | ||
| contenttype_uid: resolveContentTypeUid(entry, contentTypeUid), | ||
| variants: collectVariantAliases(entry), | ||
| }; | ||
| } | ||
| export function getVariantAliases(entryOrEntries, contentTypeUid) { | ||
| if (Array.isArray(entryOrEntries)) { | ||
| return entryOrEntries.map(function (e) { | ||
| assertPlainObject(e, 'Each entry must be a plain object with a uid.'); | ||
| return mapEntryToResult(e, contentTypeUid); | ||
| }); | ||
| } | ||
| assertPlainObject(entryOrEntries, 'Entry is required. Provide a CDA entry object with a uid.'); | ||
| return mapEntryToResult(entryOrEntries, contentTypeUid); | ||
| } | ||
| /** | ||
| * Serialises variant alias results for use as an HTML `data-csvariants` attribute value. | ||
| * | ||
| * @param entries - CDA entries to process (same rules as {@link getVariantAliases} for each item). | ||
| * @param contentTypeUid - Applied when an entry lacks `_content_type_uid`. | ||
| * @returns `{ "data-csvariants": "<JSON string of VariantAliasesResult[]>" }`. | ||
| * @throws TypeError if `entries` is null/undefined or not an array, or any element is not a plain object. | ||
| * @throws Error if any entry has no non-empty `uid`. | ||
| */ | ||
| export function getVariantMetadataTags(entries, contentTypeUid) { | ||
| if (entries === null || entries === undefined) { | ||
| throw new TypeError('Entries array is required. Provide an array of CDA entry objects.'); | ||
| } | ||
| if (!Array.isArray(entries)) { | ||
| throw new TypeError('Entries must be an array of CDA entry objects.'); | ||
| } | ||
| var payload = getVariantAliases(entries, contentTypeUid); | ||
| return { 'data-csvariants': JSON.stringify(payload) }; | ||
| } | ||
| //# sourceMappingURL=variant-aliases.js.map |
| {"version":3,"file":"variant-aliases.js","sourceRoot":"","sources":["../../src/variant-aliases.ts"],"names":[],"mappings":"AAYA,SAAS,iBAAiB,CAAC,KAAc,EAAE,OAAe;IACxD,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,EAAE;QACzC,MAAM,IAAI,SAAS,CAAC,OAAO,CAAC,CAAC;KAC9B;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;QACrD,MAAM,IAAI,SAAS,CAAC,OAAO,CAAC,CAAC;KAC9B;AACH,CAAC;AAED,SAAS,eAAe,CAAC,KAA8B;IACrD,IAAM,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC;IACtB,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG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B,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;IAC3D,OAAO,EAAE,iBAAiB,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC;AACxD,CAAC"} |
| /** | ||
| * Shape returned by {@link getVariantAliases} for interoperability with other Utils SDKs (snake_case JSON keys). | ||
| */ | ||
| export interface VariantAliasesResult { | ||
| entry_uid: string; | ||
| contenttype_uid: string; | ||
| variants: string[]; | ||
| } | ||
| /** CDA entry JSON: at minimum includes `uid`; may include `_content_type_uid` and `publish_details.variants`. */ | ||
| export type CDAEntryLike = Record<string, unknown>; | ||
| /** | ||
| * Extracts variant **alias** strings from `publish_details.variants` on a CDA entry. | ||
| * Only present when the entry was fetched with the `x-cs-variant-uid` header set to variant **aliases** (not UIDs). | ||
| * | ||
| * @param entry - Single CDA entry object (must include `uid`). | ||
| * @param contentTypeUid - Used when `entry._content_type_uid` is missing. Otherwise omitted or empty string yields `contenttype_uid: ""`. | ||
| * @returns `{ entry_uid, contenttype_uid, variants }` with snake_case keys for cross-SDK JSON parity. | ||
| * @throws TypeError if `entry` is null/undefined or not a plain object. | ||
| * @throws Error if `entry` has no non-empty `uid`. | ||
| */ | ||
| export declare function getVariantAliases(entry: CDAEntryLike, contentTypeUid?: string): VariantAliasesResult; | ||
| /** | ||
| * Extracts variant aliases for each entry in order. | ||
| * | ||
| * @param entries - Array of CDA entry objects. | ||
| * @param contentTypeUid - Applied when an entry lacks `_content_type_uid`. | ||
| * @returns One result object per input entry. | ||
| * @throws TypeError if `entries` is null/undefined or not an array, or any element is not a plain object. | ||
| * @throws Error if any entry has no non-empty `uid`. | ||
| */ | ||
| export declare function getVariantAliases(entries: CDAEntryLike[], contentTypeUid?: string): VariantAliasesResult[]; | ||
| /** | ||
| * Serialises variant alias results for use as an HTML `data-csvariants` attribute value. | ||
| * | ||
| * @param entries - CDA entries to process (same rules as {@link getVariantAliases} for each item). | ||
| * @param contentTypeUid - Applied when an entry lacks `_content_type_uid`. | ||
| * @returns `{ "data-csvariants": "<JSON string of VariantAliasesResult[]>" }`. | ||
| * @throws TypeError if `entries` is null/undefined or not an array, or any element is not a plain object. | ||
| * @throws Error if any entry has no non-empty `uid`. | ||
| */ | ||
| export declare function getVariantMetadataTags(entries: CDAEntryLike[], contentTypeUid?: string): { | ||
| 'data-csvariants': string; | ||
| }; |
@@ -14,2 +14,3 @@ export { attributeToString } from './Models/metadata-model'; | ||
| export { getContentstackEndpoint } from './endpoints'; | ||
| export { getVariantAliases, getVariantMetadataTags } from './variant-aliases'; | ||
| //# sourceMappingURL=index.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAwB,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAClF,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAGlE,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACvD,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACvD,OAAO,EAAE,OAAO,IAAI,IAAI,EAAC,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AACtD,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAC3C,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAA;AAC3B,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAC7D,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAA;AAC7D,OAAO,EAAE,uBAAuB,EAAyB,MAAM,aAAa,CAAA"} | ||
| {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAwB,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAClF,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAGlE,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACvD,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACvD,OAAO,EAAE,OAAO,IAAI,IAAI,EAAC,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AACtD,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAC3C,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAA;AAC3B,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAC7D,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAA;AAC7D,OAAO,EAAE,uBAAuB,EAAyB,MAAM,aAAa,CAAA;AAC5E,OAAO,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA"} |
@@ -17,1 +17,3 @@ export { Next, Option, RenderNode, RenderMark, RenderItem, RenderContentType, RenderOption } from './options/index'; | ||
| export { getContentstackEndpoint, ContentstackEndpoints } from './endpoints'; | ||
| export { getVariantAliases, getVariantMetadataTags } from './variant-aliases'; | ||
| export type { VariantAliasesResult, CDAEntryLike } from './variant-aliases'; |
+1
-1
| { | ||
| "name": "@contentstack/utils", | ||
| "version": "1.8.0", | ||
| "version": "1.9.0", | ||
| "description": "Contentstack utilities for Javascript", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.es.js", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
358237
6.05%90
3.45%3718
6.23%