@ninetailed/experience.js-utils 3.0.0-beta.32 → 3.0.0-beta.34

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/index.d.ts CHANGED
@@ -1,2 +1,11 @@
1
- export { ExperienceMapper } from './lib';
2
- export { Experience, Experiment, Audience, Config, Variant } from './types';
1
+ export { ExperienceMapper } from './lib/ExperienceMapper';
2
+ export { Audience } from './types/Audience';
3
+ export { Config } from './types/Config';
4
+ export { Experience } from './types/Experience';
5
+ export { Experiment } from './types/Experiment';
6
+ export { Variant } from './types/Variant';
7
+ export type { AudienceLike } from './types/Audience';
8
+ export type { ConfigLike } from './types/Config';
9
+ export type { ExperienceLike } from './types/Experience';
10
+ export type { ExperimentLike } from './types/Experiment';
11
+ export type { VariantLike } from './types/Variant';
package/index.js CHANGED
@@ -26,10 +26,15 @@ const Config = z.object({
26
26
  }]
27
27
  }])
28
28
  });
29
+ // export interface Config {
30
+ // distribution: number[];
31
+ // traffic: number;
32
+ // components: BaselineWithVariantRefs[];
33
+ // }
29
34
 
30
35
  const Variant = z.object({
31
36
  id: z.string()
32
- }).passthrough();
37
+ }).catchall(z.unknown());
33
38
 
34
39
  /**
35
40
  * Zod helper for parsing arrays and ignore items not specified in the schema
@@ -57,7 +62,7 @@ function isArray(item) {
57
62
  return Array.isArray(item);
58
63
  }
59
64
 
60
- const Experience = z.object({
65
+ const ExperienceSchema = z.object({
61
66
  id: z.string(),
62
67
  /**
63
68
  * The name of the experience (Short Text)
@@ -66,7 +71,7 @@ const Experience = z.object({
66
71
  /**
67
72
  * The type if the experience (nt_experiment | nt_personalization)
68
73
  */
69
- type: z.union([z.literal('nt_experiment'), z.literal('nt_personalization')]),
74
+ type: z.union([z.string().regex(/^nt_experiment$/g), z.string().regex(/^nt_personalization$/g)]),
70
75
  /**
71
76
  * The config of the experience (JSON)
72
77
  */
@@ -80,9 +85,51 @@ const Experience = z.object({
80
85
  */
81
86
  variants: zodArrayIgnoreUnknown(Variant).default([])
82
87
  });
88
+ const parse$1 = input => {
89
+ const output = ExperienceSchema.parse(input);
90
+ return Object.assign(Object.assign({}, output), {
91
+ variants: input.variants
92
+ });
93
+ };
94
+ const safeParse$1 = input => {
95
+ const output = ExperienceSchema.safeParse(input);
96
+ if (!output.success) {
97
+ return output;
98
+ }
99
+ return Object.assign(Object.assign({}, output), {
100
+ data: Object.assign(Object.assign({}, output.data), {
101
+ variants: input.variants
102
+ })
103
+ });
104
+ };
105
+ const Experience = Object.assign(Object.assign({}, ExperienceSchema), {
106
+ parse: parse$1,
107
+ safeParse: safeParse$1
108
+ });
83
109
 
84
- const Experiment = Experience.extend({
85
- type: z.literal('nt_experiment')
110
+ const ExperimentSchema = ExperienceSchema.extend({
111
+ type: z.string().regex(/^nt_experiment$/g)
112
+ });
113
+ const parse = input => {
114
+ const output = ExperimentSchema.parse(input);
115
+ return Object.assign(Object.assign({}, output), {
116
+ variants: input.variants
117
+ });
118
+ };
119
+ const safeParse = input => {
120
+ const output = ExperimentSchema.safeParse(input);
121
+ if (!output.success) {
122
+ return output;
123
+ }
124
+ return Object.assign(Object.assign({}, output), {
125
+ data: Object.assign(Object.assign({}, output.data), {
126
+ variants: input.variants
127
+ })
128
+ });
129
+ };
130
+ const Experiment = Object.assign(Object.assign({}, ExperimentSchema), {
131
+ parse,
132
+ safeParse
86
133
  });
87
134
 
88
135
  class ExperienceMapper {
@@ -102,33 +149,32 @@ class ExperienceMapper {
102
149
  config,
103
150
  variants
104
151
  } = parsedExperience.data;
152
+ const {
153
+ components,
154
+ traffic
155
+ } = config;
105
156
  return Object.assign(Object.assign({
106
157
  id,
107
- type
158
+ type: type
108
159
  }, audience ? {
109
160
  audience
110
161
  } : {}), {
111
- trafficAllocation: config.traffic,
162
+ trafficAllocation: traffic,
112
163
  distribution: config.distribution.map((percentage, index) => ({
113
164
  index,
114
165
  start: config.distribution.slice(0, index).reduce((a, b) => a + b, 0),
115
166
  end: config.distribution.slice(0, index + 1).reduce((a, b) => a + b, 0)
116
167
  })),
117
- components: config.components.map(component => {
118
- return {
119
- baseline: component.baseline,
120
- variants: component.variants.map(variant => {
121
- if (variant.hidden) {
122
- return variant;
123
- }
124
- const matchingVariant = variants.find(variantReference => variantReference.id === variant.id);
125
- if (!matchingVariant) {
126
- return null;
127
- }
128
- return matchingVariant;
129
- }).filter(variant => variant !== null)
130
- };
131
- })
168
+ components: components.map(component => ({
169
+ baseline: component.baseline,
170
+ variants: component.variants.map(variantRef => {
171
+ if (variantRef.hidden) {
172
+ return variantRef;
173
+ }
174
+ const matchingVariant = variants.find(variant => variant.id === variantRef.id);
175
+ return matchingVariant !== null && matchingVariant !== void 0 ? matchingVariant : null;
176
+ }).filter(variant => variant !== null)
177
+ }))
132
178
  });
133
179
  }
134
180
  static isExperimentEntry(experiment) {
@@ -1,12 +1,9 @@
1
- import { ExperienceConfiguration } from '@ninetailed/experience.js';
2
- import { z } from 'zod';
3
- import { Experience, Experiment } from '../types';
4
- declare type ExperienceEntry = z.input<typeof Experience>;
5
- declare type ExperimentEntry = z.input<typeof Experiment>;
1
+ import { ExperienceConfiguration, Reference } from '@ninetailed/experience.js';
2
+ import { Experience, ExperienceLike } from '../types/Experience';
3
+ import { ExperimentLike } from '../types/Experiment';
6
4
  export declare class ExperienceMapper {
7
- static isExperienceEntry(experience: ExperienceEntry): experience is Experience;
8
- static mapExperience(experience: ExperienceEntry): ExperienceConfiguration;
9
- static isExperimentEntry(experiment: ExperimentEntry): experiment is Experiment;
10
- static mapExperiment(experiment: ExperimentEntry): ExperienceConfiguration;
5
+ static isExperienceEntry<Variant extends Reference>(experience: ExperienceLike<Variant>): experience is Experience<Variant>;
6
+ static mapExperience<Variant extends Reference>(experience: ExperienceLike<Variant>): ExperienceConfiguration<Variant>;
7
+ static isExperimentEntry<Variant extends Reference>(experiment: ExperimentLike<Variant>): experiment is ExperimentLike<Variant>;
8
+ static mapExperiment<Variant extends Reference>(experiment: ExperimentLike<Variant>): ExperienceConfiguration<Variant>;
11
9
  }
12
- export {};
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@ninetailed/experience.js-utils",
3
- "version": "3.0.0-beta.32",
3
+ "version": "3.0.0-beta.34",
4
4
  "module": "./index.js",
5
5
  "main": "./index.js",
6
6
  "type": "module",
7
7
  "types": "./index.d.ts",
8
8
  "dependencies": {
9
- "@ninetailed/experience.js": "3.0.0-beta.32",
10
- "@ninetailed/experience.js-shared": "3.0.0-beta.32",
9
+ "@ninetailed/experience.js": "3.0.0-beta.34",
10
+ "@ninetailed/experience.js-shared": "3.0.0-beta.34",
11
11
  "zod": "3.20.2"
12
12
  },
13
13
  "peerDependencies": {}
@@ -6,4 +6,5 @@ export declare const Audience: z.ZodObject<{
6
6
  }, {
7
7
  id: string;
8
8
  }>;
9
+ export declare type AudienceLike = z.input<typeof Audience>;
9
10
  export declare type Audience = z.infer<typeof Audience>;
package/types/Config.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Baseline, VariantRef } from '@ninetailed/experience.js';
1
2
  import { z } from 'zod';
2
3
  export declare const Config: z.ZodObject<{
3
4
  distribution: z.ZodDefault<z.ZodArray<z.ZodNumber, "many">>;
@@ -62,4 +63,9 @@ export declare const Config: z.ZodObject<{
62
63
  }[];
63
64
  }[] | undefined;
64
65
  }>;
66
+ export declare type ConfigLike = z.input<typeof Config>;
65
67
  export declare type Config = z.infer<typeof Config>;
68
+ export interface BaselineWithVariantRefs {
69
+ baseline: Baseline;
70
+ variants: VariantRef[];
71
+ }