UNPKG

@ninetailed/experience.js-utils-contentful

Version:
309 lines (293 loc) 9.95 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var zod = require('zod'); var experience_jsShared = require('@ninetailed/experience.js-shared'); var experience_jsUtils = require('@ninetailed/experience.js-utils'); /** * This does not work anymore from zod > 3.21.0: * * We have to cast the result of passthrough() to z.ZodObject<{}> to make it work, * as the inferred type changed to {} & { [k: string]: unknown; } * It was {} before, so we do the type cast to get it back to {}. */ // eslint-disable-next-line @typescript-eslint/ban-types const EntryFields = zod.z.object({}).passthrough(); const EntryLink = zod.z.object({ type: zod.z.string().optional(), linkType: zod.z.string().optional(), id: zod.z.string() }); const LinkedEntity = zod.z.object({ sys: EntryLink }); const EntrySchema = zod.z.object({ sys: zod.z.object({ type: zod.z.string().optional(), id: zod.z.string(), createdAt: zod.z.string().optional(), updatedAt: zod.z.string().optional(), locale: zod.z.string().optional(), revision: zod.z.number().optional(), space: LinkedEntity.optional(), environment: LinkedEntity.optional(), contentType: LinkedEntity.optional() }), fields: EntryFields, metadata: zod.z.object({ tags: zod.z.array(zod.z.object({ sys: EntryLink.extend({ linkType: zod.z.string() }) })) }).optional() }); const parse$2 = input => { const output = EntrySchema.parse(input); return Object.assign(Object.assign({}, output), { fields: input.fields }); }; const safeParse$2 = input => { const output = EntrySchema.safeParse(input); if (!output.success) { return output; } return Object.assign(Object.assign({}, output), { data: Object.assign(Object.assign({}, output.data), { fields: input.fields }) }); }; const Entry = Object.assign(Object.assign({}, EntrySchema), { parse: parse$2, safeParse: safeParse$2 }); const AudienceEntryFields = EntryFields.extend({ /** * The name of the audience (Short Text) */ nt_name: zod.z.string(), /** * The description of the audience (Short Text) */ nt_description: zod.z.string().optional(), /** * The internal id of the audience (Short Text) */ nt_audience_id: zod.z.string() }); const AudienceEntry = EntrySchema.extend({ fields: AudienceEntryFields }); class AudienceMapper { static mapAudience(audience) { return { id: audience.fields.nt_audience_id, name: audience.fields.nt_name || '', description: audience.fields.nt_description || '' }; } } AudienceMapper.isAudienceEntry = entry => { return AudienceEntry.safeParse(entry).success; }; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } const ExperienceEntryFields = zod.z.object({ /** * The name of the experience (Short Text) */ nt_name: zod.z.string(), /** * The description of the experience (Short Text) */ nt_description: zod.z.string().optional().nullable(), /** * The type if the experience (nt_experiment | nt_personalization) */ nt_type: zod.z.union([zod.z.string(), zod.z.string()]), /** * The config of the experience (JSON) */ nt_config: experience_jsUtils.Config.optional().nullable().default(null).transform(val => { return val !== null && val !== void 0 ? val : { traffic: 0, distribution: [0.5, 0.5], components: [], sticky: false }; }), /** * The audience of the experience (Audience) */ nt_audience: AudienceEntry.optional().nullable(), /** * All used variants of the experience (Contentful references to other Content Types) */ nt_variants: zod.z.array(EntrySchema).optional() }); const ExperienceEntrySchema = EntrySchema.extend({ fields: ExperienceEntryFields }); const parse$1 = input => { const output = ExperienceEntrySchema.parse(input); return Object.assign(Object.assign({}, output), { fields: Object.assign(Object.assign({}, output.fields), { nt_variants: input.fields.nt_variants || [] }) }); }; const safeParse$1 = input => { const output = ExperienceEntrySchema.safeParse(input); if (!output.success) { return output; } return Object.assign(Object.assign({}, output), { data: Object.assign(Object.assign({}, output.data), { fields: Object.assign(Object.assign({}, output.data.fields), { nt_variants: input.fields.nt_variants || [] }) }) }); }; const ExperienceEntry = Object.assign(Object.assign({}, ExperienceEntrySchema), { parse: parse$1, safeParse: safeParse$1 }); const ExperimentEntrySchema = ExperienceEntrySchema.extend({ fields: ExperienceEntryFields.extend({ nt_type: zod.z.string().regex(/^nt_experiment$/g) }) }); const parse = input => { const output = ExperimentEntrySchema.parse(input); return Object.assign(Object.assign({}, output), { fields: Object.assign(Object.assign({}, output.fields), { nt_variants: input.fields.nt_variants || [] }) }); }; const safeParse = input => { const output = ExperimentEntrySchema.safeParse(input); if (!output.success) { return output; } return Object.assign(Object.assign({}, output), { data: Object.assign(Object.assign({}, output.data), { fields: Object.assign(Object.assign({}, output.data.fields), { nt_variants: input.fields.nt_variants || [] }) }) }); }; const ExperimentEntry = Object.assign(Object.assign({}, ExperimentEntrySchema), { parse, safeParse }); function mapAudience(ctfAudienceEntry) { return { id: ctfAudienceEntry.fields.nt_audience_id, name: ctfAudienceEntry.fields.nt_name, description: ctfAudienceEntry.fields.nt_description || '' }; } function createExperience(id, fields, variants) { const { nt_name, nt_description, nt_type, nt_audience, nt_config } = fields; return Object.assign(Object.assign({ id, name: nt_name, description: nt_description || '', type: nt_type }, nt_audience ? { audience: mapAudience(nt_audience) } : {}), { config: nt_config, variants }); } function validateExperienceEntry(entry) { const parsedExperience = ExperienceEntry.safeParse(entry); if (!parsedExperience.success) { experience_jsShared.logger.warn('[Ninetailed Contentful ExperienceMapper]', 'Error parsing experience', parsedExperience.error.format()); throw new Error(`[Ninetailed Contentful ExperienceMapper] The Experience Input is not valid. Please filter data first with "ExperienceMapper.isExperienceEntry".\n${JSON.stringify(parsedExperience.error.format(), null, 2)}`); } return parsedExperience.data; } class ExperienceMapper { static isExperienceEntry(entry) { return ExperienceEntry.safeParse(entry).success; } static mapExperience(ctfEntry) { const { sys, fields } = validateExperienceEntry(ctfEntry); const variants = fields.nt_variants.map(variant => Object.assign(Object.assign({}, variant), { id: variant.sys.id })); const experience = createExperience(sys.id, fields, variants); return experience_jsUtils.ExperienceMapper.mapExperience(experience); } static mapCustomExperience(ctfEntry, mapFn) { const { sys, fields } = validateExperienceEntry(ctfEntry); const variants = fields.nt_variants.map(mapFn); const experience = createExperience(sys.id, fields, variants); return experience_jsUtils.ExperienceMapper.mapExperience(experience); } static mapCustomExperienceAsync(ctfEntry, mapFn) { return __awaiter(this, void 0, void 0, function* () { const { sys, fields } = validateExperienceEntry(ctfEntry); const variants = yield Promise.all(fields.nt_variants.map(mapFn)); const experience = createExperience(sys.id, fields, variants); return experience_jsUtils.ExperienceMapper.mapExperience(experience); }); } static isExperiment(entry) { return ExperimentEntry.safeParse(entry).success; } static mapExperiment(entry) { return ExperienceMapper.mapCustomExperience(entry, () => ({ id: '' })); } static mapBaselineWithExperiences(entry) { return entry.fields.nt_experiences.filter(ExperienceMapper.isExperienceEntry).map(experience => ExperienceMapper.mapExperience(experience)); } } const isEntry = entry => { return Entry.safeParse(entry).success; }; exports.AudienceMapper = AudienceMapper; exports.ExperienceMapper = ExperienceMapper; exports.isEntry = isEntry;