@reactionary/core 0.3.0 → 0.3.2

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.
Files changed (39) hide show
  1. package/client/client-builder.js +3 -4
  2. package/decorators/reactionary.decorator.js +2 -2
  3. package/initialization.js +9 -1
  4. package/package.json +1 -1
  5. package/providers/analytics.provider.js +62 -1
  6. package/providers/identity.provider.js +4 -0
  7. package/schemas/errors/invalid-input.error.js +1 -1
  8. package/schemas/errors/invalid-output.error.js +1 -1
  9. package/schemas/models/order.model.js +1 -1
  10. package/schemas/mutations/analytics/index.js +21 -0
  11. package/schemas/mutations/analytics/product-add-to-cart.mutation.js +19 -0
  12. package/schemas/mutations/analytics/product-details-view.mutation.js +10 -0
  13. package/schemas/mutations/analytics/product-summary-click.mutation.js +20 -0
  14. package/schemas/mutations/analytics/product-summary-view.mutation.js +19 -0
  15. package/schemas/mutations/analytics/purchase.mutation.js +10 -0
  16. package/schemas/mutations/index.js +1 -1
  17. package/schemas/session.schema.js +11 -2
  18. package/src/client/client.d.ts +2 -3
  19. package/src/initialization.d.ts +1 -1
  20. package/src/providers/analytics.provider.d.ts +14 -0
  21. package/src/providers/cart.provider.d.ts +3 -0
  22. package/src/providers/category.provider.d.ts +1 -0
  23. package/src/providers/identity.provider.d.ts +1 -0
  24. package/src/schemas/errors/invalid-input.error.d.ts +1 -1
  25. package/src/schemas/errors/invalid-output.error.d.ts +1 -1
  26. package/src/schemas/models/identifiers.model.d.ts +3 -0
  27. package/src/schemas/models/order.model.d.ts +1 -1
  28. package/src/schemas/mutations/analytics/index.d.ts +2441 -0
  29. package/src/schemas/mutations/analytics/product-add-to-cart.mutation.d.ts +32 -0
  30. package/src/schemas/mutations/analytics/product-details-view.mutation.d.ts +9 -0
  31. package/src/schemas/mutations/analytics/product-summary-click.mutation.d.ts +33 -0
  32. package/src/schemas/mutations/analytics/product-summary-view.mutation.d.ts +32 -0
  33. package/src/schemas/mutations/analytics/purchase.mutation.d.ts +2346 -0
  34. package/src/schemas/mutations/index.d.ts +1 -1
  35. package/src/schemas/queries/order-search.query.d.ts +3 -0
  36. package/src/schemas/session.schema.d.ts +73 -6
  37. package/src/zod-utils.d.ts +4 -1
  38. package/schemas/mutations/analytics.mutation.js +0 -20
  39. package/src/schemas/mutations/analytics.mutation.d.ts +0 -110
@@ -1,4 +1,5 @@
1
1
  import { NoOpCache } from "../cache/noop-cache.js";
2
+ import { MulticastAnalyticsProvider } from "../providers/analytics.provider.js";
2
3
  import {
3
4
  RequestContextSchema
4
5
  } from "../schemas/session.schema.js";
@@ -36,14 +37,12 @@ class ClientBuilder {
36
37
  ...provider
37
38
  };
38
39
  if (provider.analytics) {
39
- mergedAnalytics.push(...provider.analytics);
40
+ mergedAnalytics.push(provider.analytics);
40
41
  }
41
42
  }
42
- if (mergedAnalytics.length > 0) {
43
- client["analytics"] = mergedAnalytics;
44
- }
45
43
  const completeClient = {
46
44
  ...client,
45
+ analytics: new MulticastAnalyticsProvider(sharedCache, this.context, mergedAnalytics),
47
46
  cache: sharedCache
48
47
  };
49
48
  return completeClient;
@@ -135,7 +135,7 @@ function validateInput(input, schema) {
135
135
  if (!parse.success) {
136
136
  validated = error({
137
137
  type: "InvalidInput",
138
- error: parse.error
138
+ error: JSON.stringify(z.flattenError(parse.error))
139
139
  });
140
140
  }
141
141
  return validated;
@@ -150,7 +150,7 @@ function validateOutput(output, schema) {
150
150
  if (!parse.success) {
151
151
  validated = error({
152
152
  type: "InvalidOutput",
153
- error: parse.error
153
+ error: JSON.stringify(z.flattenError(parse.error))
154
154
  });
155
155
  }
156
156
  }
package/initialization.js CHANGED
@@ -13,7 +13,15 @@ function createInitialRequestContext() {
13
13
  countyCode: "",
14
14
  cityCode: ""
15
15
  },
16
- session: {},
16
+ session: {
17
+ identityContext: {
18
+ identity: {
19
+ type: "Anonymous"
20
+ },
21
+ lastUpdated: /* @__PURE__ */ new Date(),
22
+ personalizationKey: crypto.randomUUID()
23
+ }
24
+ },
17
25
  correlationId: "",
18
26
  isBot: false,
19
27
  clientIp: "",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactionary/core",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "main": "index.js",
5
5
  "types": "src/index.d.ts",
6
6
  "dependencies": {
@@ -1,9 +1,70 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __decorateClass = (decorators, target, key, kind) => {
4
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
5
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
6
+ if (decorator = decorators[i])
7
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
8
+ if (kind && result)
9
+ __defProp(target, key, result);
10
+ return result;
11
+ };
1
12
  import { BaseProvider } from "./base.provider.js";
13
+ import {
14
+ AnalyticsMutationSchema
15
+ } from "../schemas/index.js";
16
+ import { Reactionary } from "../decorators/reactionary.decorator.js";
2
17
  class AnalyticsProvider extends BaseProvider {
3
18
  getResourceName() {
4
19
  return "analytics";
5
20
  }
21
+ async track(event) {
22
+ switch (event.event) {
23
+ case "product-summary-view":
24
+ await this.processProductSummaryView(event);
25
+ break;
26
+ case "product-summary-click":
27
+ await this.processProductSummaryClick(event);
28
+ break;
29
+ case "product-details-view":
30
+ await this.processProductDetailsView(event);
31
+ break;
32
+ case "product-cart-add":
33
+ await this.processProductAddToCart(event);
34
+ break;
35
+ case "purchase":
36
+ await this.processPurchase(event);
37
+ break;
38
+ }
39
+ }
40
+ async processProductSummaryView(event) {
41
+ }
42
+ async processProductSummaryClick(event) {
43
+ }
44
+ async processProductDetailsView(event) {
45
+ }
46
+ async processProductAddToCart(event) {
47
+ }
48
+ async processPurchase(event) {
49
+ }
50
+ }
51
+ class MulticastAnalyticsProvider extends AnalyticsProvider {
52
+ constructor(cache, requestContext, providers) {
53
+ super(cache, requestContext);
54
+ this.providers = providers;
55
+ }
56
+ async track(event) {
57
+ for (const provider of this.providers) {
58
+ provider.track(event);
59
+ }
60
+ }
6
61
  }
62
+ __decorateClass([
63
+ Reactionary({
64
+ inputSchema: AnalyticsMutationSchema
65
+ })
66
+ ], MulticastAnalyticsProvider.prototype, "track", 1);
7
67
  export {
8
- AnalyticsProvider
68
+ AnalyticsProvider,
69
+ MulticastAnalyticsProvider
9
70
  };
@@ -3,6 +3,10 @@ class IdentityProvider extends BaseProvider {
3
3
  getResourceName() {
4
4
  return "identity";
5
5
  }
6
+ updateIdentityContext(identity) {
7
+ this.context.session.identityContext.lastUpdated = /* @__PURE__ */ new Date();
8
+ this.context.session.identityContext.identity = identity;
9
+ }
6
10
  }
7
11
  export {
8
12
  IdentityProvider
@@ -1,7 +1,7 @@
1
1
  import { z } from "zod";
2
2
  const InvalidInputErrorSchema = z.looseObject({
3
3
  type: z.literal("InvalidInput"),
4
- error: z.ZodError
4
+ error: z.string()
5
5
  });
6
6
  export {
7
7
  InvalidInputErrorSchema
@@ -1,7 +1,7 @@
1
1
  import { z } from "zod";
2
2
  const InvalidOutputErrorSchema = z.looseObject({
3
3
  type: z.literal("InvalidOutput"),
4
- error: z.ZodError
4
+ error: z.string()
5
5
  });
6
6
  export {
7
7
  InvalidOutputErrorSchema
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { CartIdentifierSchema, CartItemIdentifierSchema, IdentityIdentifierSchema, OrderIdentifierSchema, OrderInventoryStatusSchema, OrderItemIdentifierSchema, OrderStatusSchema, ProductVariantIdentifierSchema } from "../models/identifiers.model.js";
2
+ import { CartIdentifierSchema, IdentityIdentifierSchema, OrderIdentifierSchema, OrderInventoryStatusSchema, OrderItemIdentifierSchema, OrderStatusSchema, ProductVariantIdentifierSchema } from "../models/identifiers.model.js";
3
3
  import { BaseModelSchema } from "./base.model.js";
4
4
  import { AddressSchema } from "./profile.model.js";
5
5
  import { ShippingMethodSchema } from "./shipping-method.model.js";
@@ -0,0 +1,21 @@
1
+ import z from "zod";
2
+ import { AnalyticsMutationProductAddToCartEventSchema } from "./product-add-to-cart.mutation.js";
3
+ import { AnalyticsMutationProductDetailsViewEventSchema } from "./product-details-view.mutation.js";
4
+ import { AnalyticsMutationProductSummaryClickEventSchema } from "./product-summary-click.mutation.js";
5
+ import { AnalyticsMutationProductSummaryViewEventSchema } from "./product-summary-view.mutation.js";
6
+ import { AnalyticsMutationPurchaseEventSchema } from "./purchase.mutation.js";
7
+ const AnalyticsMutationSchema = z.discriminatedUnion("event", [
8
+ AnalyticsMutationProductSummaryViewEventSchema,
9
+ AnalyticsMutationProductSummaryClickEventSchema,
10
+ AnalyticsMutationProductDetailsViewEventSchema,
11
+ AnalyticsMutationProductAddToCartEventSchema,
12
+ AnalyticsMutationPurchaseEventSchema
13
+ ]);
14
+ export * from "./product-add-to-cart.mutation.js";
15
+ export * from "./product-details-view.mutation.js";
16
+ export * from "./product-summary-click.mutation.js";
17
+ export * from "./product-summary-view.mutation.js";
18
+ export * from "./purchase.mutation.js";
19
+ export {
20
+ AnalyticsMutationSchema
21
+ };
@@ -0,0 +1,19 @@
1
+ import z from "zod";
2
+ import {
3
+ ProductSearchIdentifierSchema,
4
+ ProductIdentifierSchema
5
+ } from "../../models/identifiers.model.js";
6
+ import { BaseMutationSchema } from "../base.mutation.js";
7
+ const AnalyticsMutationProductAddToCartEventSchema = BaseMutationSchema.extend({
8
+ event: z.literal("product-cart-add"),
9
+ source: z.discriminatedUnion("type", [
10
+ z.object({
11
+ type: z.literal("search"),
12
+ identifier: ProductSearchIdentifierSchema
13
+ })
14
+ ]).optional(),
15
+ product: ProductIdentifierSchema
16
+ });
17
+ export {
18
+ AnalyticsMutationProductAddToCartEventSchema
19
+ };
@@ -0,0 +1,10 @@
1
+ import z from "zod";
2
+ import { ProductIdentifierSchema } from "../../models/identifiers.model.js";
3
+ import { BaseMutationSchema } from "../base.mutation.js";
4
+ const AnalyticsMutationProductDetailsViewEventSchema = BaseMutationSchema.extend({
5
+ event: z.literal("product-details-view"),
6
+ product: ProductIdentifierSchema
7
+ });
8
+ export {
9
+ AnalyticsMutationProductDetailsViewEventSchema
10
+ };
@@ -0,0 +1,20 @@
1
+ import {
2
+ ProductIdentifierSchema,
3
+ ProductSearchIdentifierSchema
4
+ } from "../../models/identifiers.model.js";
5
+ import { BaseMutationSchema } from "../base.mutation.js";
6
+ import { z } from "zod";
7
+ const AnalyticsMutationProductSummaryClickEventSchema = BaseMutationSchema.extend({
8
+ event: z.literal("product-summary-click"),
9
+ product: ProductIdentifierSchema,
10
+ source: z.discriminatedUnion("type", [
11
+ z.object({
12
+ type: z.literal("search"),
13
+ identifier: ProductSearchIdentifierSchema
14
+ })
15
+ ]).optional(),
16
+ position: z.number().min(0)
17
+ });
18
+ export {
19
+ AnalyticsMutationProductSummaryClickEventSchema
20
+ };
@@ -0,0 +1,19 @@
1
+ import {
2
+ ProductIdentifierSchema,
3
+ ProductSearchIdentifierSchema
4
+ } from "../../models/identifiers.model.js";
5
+ import { BaseMutationSchema } from "../base.mutation.js";
6
+ import { z } from "zod";
7
+ const AnalyticsMutationProductSummaryViewEventSchema = BaseMutationSchema.extend({
8
+ event: z.literal("product-summary-view"),
9
+ source: z.discriminatedUnion("type", [
10
+ z.object({
11
+ type: z.literal("search"),
12
+ identifier: ProductSearchIdentifierSchema
13
+ })
14
+ ]).optional(),
15
+ products: z.array(ProductIdentifierSchema)
16
+ });
17
+ export {
18
+ AnalyticsMutationProductSummaryViewEventSchema
19
+ };
@@ -0,0 +1,10 @@
1
+ import z from "zod";
2
+ import { BaseMutationSchema } from "../base.mutation.js";
3
+ import { OrderSchema } from "../../models/order.model.js";
4
+ const AnalyticsMutationPurchaseEventSchema = BaseMutationSchema.extend({
5
+ event: z.literal("purchase"),
6
+ order: OrderSchema
7
+ });
8
+ export {
9
+ AnalyticsMutationPurchaseEventSchema
10
+ };
@@ -1,4 +1,4 @@
1
- export * from "./analytics.mutation.js";
1
+ export * from "./analytics/index.js";
2
2
  export * from "./base.mutation.js";
3
3
  export * from "./cart.mutation.js";
4
4
  export * from "./identity.mutation.js";
@@ -1,11 +1,19 @@
1
1
  import { z } from "zod";
2
- import { WebStoreIdentifierSchema } from "./models/identifiers.model.js";
2
+ import { IdentityIdentifierSchema, WebStoreIdentifierSchema } from "./models/identifiers.model.js";
3
3
  import { CurrencySchema } from "./models/currency.model.js";
4
+ import { IdentitySchema } from "./models/identity.model.js";
4
5
  const LanguageContextSchema = z.looseObject({
5
6
  locale: z.string().default("en-US"),
6
7
  currencyCode: CurrencySchema.default(() => CurrencySchema.parse({}))
7
8
  });
8
- const SessionSchema = z.record(z.string(), z.any());
9
+ const IdentityContextSchema = z.looseObject({
10
+ identity: IdentitySchema,
11
+ personalizationKey: z.string(),
12
+ lastUpdated: z.date()
13
+ });
14
+ const SessionSchema = z.record(z.string(), z.any()).and(z.object({
15
+ identityContext: IdentityContextSchema
16
+ }));
9
17
  const TaxJurisdictionSchema = z.object({
10
18
  countryCode: z.string().default("US"),
11
19
  stateCode: z.string().default(""),
@@ -24,6 +32,7 @@ const RequestContextSchema = z.looseObject({
24
32
  referrer: z.string().default("").describe("The referrer URL, if available.")
25
33
  });
26
34
  export {
35
+ IdentityContextSchema,
27
36
  LanguageContextSchema,
28
37
  RequestContextSchema,
29
38
  SessionSchema,
@@ -1,4 +1,3 @@
1
- import type { AnalyticsProvider } from "../providers/analytics.provider.js";
2
1
  import type { ProductProvider } from "../providers/product.provider.js";
3
2
  import type { ProductSearchProvider } from "../providers/product-search.provider.js";
4
3
  import type { IdentityProvider } from '../providers/identity.provider.js';
@@ -7,7 +6,7 @@ import type { PriceProvider } from "../providers/price.provider.js";
7
6
  import type { InventoryProvider } from "../providers/inventory.provider.js";
8
7
  import type { Cache } from "../cache/cache.interface.js";
9
8
  import type { CategoryProvider } from "../providers/category.provider.js";
10
- import type { CheckoutProvider, OrderProvider, ProfileProvider, StoreProvider } from "../providers/index.js";
9
+ import type { AnalyticsProvider, CheckoutProvider, OrderProvider, ProfileProvider, StoreProvider } from "../providers/index.js";
11
10
  import type { OrderSearchProvider } from "../providers/order-search.provider.js";
12
11
  export interface Client {
13
12
  product: ProductProvider;
@@ -16,7 +15,7 @@ export interface Client {
16
15
  cache: Cache;
17
16
  cart: CartProvider;
18
17
  checkout: CheckoutProvider;
19
- analytics: Array<AnalyticsProvider>;
18
+ analytics: AnalyticsProvider;
20
19
  price: PriceProvider;
21
20
  inventory: InventoryProvider;
22
21
  category: CategoryProvider;
@@ -1,2 +1,2 @@
1
- import type { RequestContext } from "./schemas/session.schema.js";
1
+ import type { RequestContext } from './schemas/session.schema.js';
2
2
  export declare function createInitialRequestContext(): RequestContext;
@@ -1,4 +1,18 @@
1
+ import type { RequestContext } from '../schemas/session.schema.js';
1
2
  import { BaseProvider } from './base.provider.js';
3
+ import type { Cache } from '../cache/cache.interface.js';
4
+ import { type AnalyticsMutation, type AnalyticsMutationProductAddToCartEvent, type AnalyticsMutationProductDetailsViewEvent, type AnalyticsMutationProductSummaryClickEvent, type AnalyticsMutationProductSummaryViewEvent, type AnalyticsMutationPurchaseEvent } from '../schemas/index.js';
2
5
  export declare abstract class AnalyticsProvider extends BaseProvider {
3
6
  protected getResourceName(): string;
7
+ track(event: AnalyticsMutation): Promise<void>;
8
+ protected processProductSummaryView(event: AnalyticsMutationProductSummaryViewEvent): Promise<void>;
9
+ protected processProductSummaryClick(event: AnalyticsMutationProductSummaryClickEvent): Promise<void>;
10
+ protected processProductDetailsView(event: AnalyticsMutationProductDetailsViewEvent): Promise<void>;
11
+ protected processProductAddToCart(event: AnalyticsMutationProductAddToCartEvent): Promise<void>;
12
+ protected processPurchase(event: AnalyticsMutationPurchaseEvent): Promise<void>;
13
+ }
14
+ export declare class MulticastAnalyticsProvider extends AnalyticsProvider {
15
+ protected providers: Array<AnalyticsProvider>;
16
+ constructor(cache: Cache, requestContext: RequestContext, providers: Array<AnalyticsProvider>);
17
+ track(event: AnalyticsMutation): Promise<void>;
4
18
  }
@@ -5,6 +5,9 @@ import type { CartMutationApplyCoupon, CartMutationChangeCurrency, CartMutationD
5
5
  import type { CartQueryById } from "../schemas/queries/cart.query.js";
6
6
  import type { Result } from "../schemas/result.js";
7
7
  import { BaseProvider } from "./base.provider.js";
8
+ /**
9
+ * @group Providers
10
+ */
8
11
  export declare abstract class CartProvider extends BaseProvider {
9
12
  /**
10
13
  * Get cart by ID.
@@ -8,6 +8,7 @@ import { BaseProvider } from "./base.provider.js";
8
8
  *
9
9
  * We only allow fetching one hierachy level at a time, for now. This is to avoid development patterns of "fetch 5000 categories in one go.."
10
10
  *
11
+ * @group Foo
11
12
  */
12
13
  export declare abstract class CategoryProvider extends BaseProvider {
13
14
  /**
@@ -9,4 +9,5 @@ export declare abstract class IdentityProvider extends BaseProvider {
9
9
  abstract logout(payload: IdentityMutationLogout): Promise<Result<Identity>>;
10
10
  abstract register(payload: IdentityMutationRegister): Promise<Result<Identity>>;
11
11
  protected getResourceName(): string;
12
+ protected updateIdentityContext(identity: Identity): void;
12
13
  }
@@ -2,6 +2,6 @@ import { z } from 'zod';
2
2
  import type { InferType } from '../../zod-utils.js';
3
3
  export declare const InvalidInputErrorSchema: z.ZodObject<{
4
4
  type: z.ZodLiteral<"InvalidInput">;
5
- error: z.core.$constructor<z.ZodError<unknown>, z.core.$ZodIssue[]>;
5
+ error: z.ZodString;
6
6
  }, z.core.$loose>;
7
7
  export type InvalidInputError = InferType<typeof InvalidInputErrorSchema>;
@@ -2,6 +2,6 @@ import { z } from 'zod';
2
2
  import type { InferType } from '../../zod-utils.js';
3
3
  export declare const InvalidOutputErrorSchema: z.ZodObject<{
4
4
  type: z.ZodLiteral<"InvalidOutput">;
5
- error: z.core.$constructor<z.ZodError<unknown>, z.core.$ZodIssue[]>;
5
+ error: z.ZodString;
6
6
  }, z.core.$loose>;
7
7
  export type InvalidOutputError = InferType<typeof InvalidOutputErrorSchema>;
@@ -128,6 +128,9 @@ export declare const ProductSearchIdentifierSchema: z.ZodObject<{
128
128
  key: z.ZodString;
129
129
  }, z.core.$strip>>;
130
130
  }, z.core.$loose>;
131
+ /**
132
+ * Bar
133
+ */
131
134
  export declare const OrderSearchIdentifierSchema: z.ZodObject<{
132
135
  term: z.ZodString;
133
136
  partNumber: z.ZodOptional<z.ZodArray<z.ZodString>>;
@@ -3102,7 +3102,7 @@ export declare const OrderSchema: z.ZodObject<{
3102
3102
  key: z.ZodString;
3103
3103
  }, z.core.$loose>>;
3104
3104
  }, z.core.$loose>;
3105
- export type OrderStatus = InferType<typeof OrderStatusSchema>;
3105
+ export type OrderStatus = z.infer<typeof OrderStatusSchema>;
3106
3106
  export type OrderInventoryStatus = InferType<typeof OrderInventoryStatusSchema>;
3107
3107
  export type OrderItem = InferType<typeof OrderItemSchema>;
3108
3108
  export type Order = InferType<typeof OrderSchema>;