@reactionary/core 0.0.69 → 0.0.71

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.
@@ -1,7 +1,4 @@
1
- import {
2
- trace,
3
- SpanKind
4
- } from "@opentelemetry/api";
1
+ import { trace, SpanKind } from "@opentelemetry/api";
5
2
  const TRACER_NAME = "@reactionary";
6
3
  const TRACER_VERSION = "0.0.1";
7
4
  let globalTracer = null;
@@ -36,7 +33,6 @@ class ReactionaryDecoratorOptions {
36
33
  this.cacheTimeToLiveInSeconds = 60;
37
34
  }
38
35
  }
39
- ;
40
36
  function Reactionary(options) {
41
37
  return function(target, propertyKey, descriptor) {
42
38
  const original = descriptor.value;
@@ -48,42 +44,63 @@ function Reactionary(options) {
48
44
  );
49
45
  }
50
46
  descriptor.value = async function(...args) {
51
- const tracer = getTracer();
52
- return tracer.startActiveSpan(
53
- propertyKey.toString(),
54
- { kind: SpanKind.SERVER },
55
- async (span) => {
56
- const cacheKey = this.generateCacheKeyForQuery(scope, args[0]);
57
- const fromCache = await this.cache.get(cacheKey, this.schema);
58
- let result = fromCache;
59
- if (!result) {
60
- result = await original.apply(this, args);
61
- const dependencyIds = this.generateDependencyIdsForModel(result);
62
- this.cache.put(cacheKey, result, {
63
- ttlSeconds: configuration.cacheTimeToLiveInSeconds,
64
- dependencyIds
65
- });
66
- }
67
- span.end();
68
- if (!result) {
69
- return result;
70
- }
71
- if (result instanceof Array) {
72
- return result;
73
- }
74
- return this.assert(result);
47
+ return traceSpan(scope, async () => {
48
+ const input = validateInput(args[0], configuration.inputSchema);
49
+ const cacheKey = this.generateCacheKeyForQuery(scope, input);
50
+ const fromCache = await this.cache.get(cacheKey, this.schema);
51
+ let result = fromCache;
52
+ if (!result) {
53
+ result = await original.apply(this, [input]);
54
+ const dependencyIds = this.generateDependencyIdsForModel(result);
55
+ this.cache.put(cacheKey, result, {
56
+ ttlSeconds: configuration.cacheTimeToLiveInSeconds,
57
+ dependencyIds
58
+ });
75
59
  }
76
- );
60
+ return validateOutput(result, configuration.outputSchema);
61
+ });
77
62
  };
78
63
  return descriptor;
79
64
  };
80
65
  }
81
- function validateInput() {
82
- console.log("foo");
66
+ function validateInput(input, schema) {
67
+ if (!schema) {
68
+ return input;
69
+ }
70
+ const parsed = schema.parse(input);
71
+ return parsed;
72
+ }
73
+ function validateOutput(output, schema) {
74
+ if (!schema) {
75
+ return output;
76
+ }
77
+ const parsed = schema.parse(output);
78
+ return parsed;
79
+ }
80
+ async function traceSpan(name, fn) {
81
+ const tracer = getTracer();
82
+ return tracer.startActiveSpan(name, async (span) => {
83
+ try {
84
+ return fn();
85
+ } catch (err) {
86
+ if (err instanceof Error) {
87
+ span.recordException(err);
88
+ span.setStatus({ code: 2, message: err.message });
89
+ } else {
90
+ span.recordException({ message: String(err) });
91
+ span.setStatus({ code: 2, message: String(err) });
92
+ }
93
+ throw err;
94
+ } finally {
95
+ span.end();
96
+ }
97
+ });
83
98
  }
84
99
  export {
85
100
  Reactionary,
86
101
  ReactionaryDecoratorOptions,
87
102
  getTracer,
88
- validateInput
103
+ traceSpan,
104
+ validateInput,
105
+ validateOutput
89
106
  };
package/index.js CHANGED
@@ -140,11 +140,11 @@ import {
140
140
  import {
141
141
  BaseQuerySchema,
142
142
  CartQueryByIdSchema,
143
- CategoryQueryById,
144
- CategoryQueryBySlug,
145
- CategoryQueryForBreadcrumb,
146
- CategoryQueryForChildCategories,
147
- CategoryQueryForTopCategories,
143
+ CategoryQueryByIdSchema,
144
+ CategoryQueryBySlugSchema,
145
+ CategoryQueryForBreadcrumbSchema,
146
+ CategoryQueryForChildCategoriesSchema,
147
+ CategoryQueryForTopCategoriesSchema,
148
148
  CheckoutQueryByIdSchema,
149
149
  CheckoutQueryForAvailablePaymentMethodsSchema,
150
150
  CheckoutQueryForAvailableShippingMethodsSchema,
@@ -197,11 +197,11 @@ export {
197
197
  CategoryIdentifierSchema,
198
198
  CategoryPaginatedResultSchema,
199
199
  CategoryProvider,
200
- CategoryQueryById,
201
- CategoryQueryBySlug,
202
- CategoryQueryForBreadcrumb,
203
- CategoryQueryForChildCategories,
204
- CategoryQueryForTopCategories,
200
+ CategoryQueryByIdSchema,
201
+ CategoryQueryBySlugSchema,
202
+ CategoryQueryForBreadcrumbSchema,
203
+ CategoryQueryForChildCategoriesSchema,
204
+ CategoryQueryForTopCategoriesSchema,
205
205
  CategorySchema,
206
206
  CheckoutIdentifierSchema,
207
207
  CheckoutItemIdentifierSchema,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactionary/core",
3
- "version": "0.0.69",
3
+ "version": "0.0.71",
4
4
  "main": "index.js",
5
5
  "types": "src/index.d.ts",
6
6
  "dependencies": {
@@ -10,7 +10,8 @@ const CapabilitiesSchema = z.looseObject({
10
10
  inventory: z.boolean(),
11
11
  price: z.boolean(),
12
12
  category: z.boolean(),
13
- store: z.boolean()
13
+ store: z.boolean(),
14
+ profile: z.boolean()
14
15
  });
15
16
  export {
16
17
  CapabilitiesSchema
@@ -1,15 +1,15 @@
1
1
  import { z } from "zod";
2
- import { CartIdentifierSchema, AddressSchema, PaymentInstructionIdentifierSchema, PaymentInstructionSchema, ShippingInstructionSchema } from "../models/index.js";
2
+ import { CartIdentifierSchema, AddressSchema, PaymentInstructionIdentifierSchema, PaymentInstructionSchema, ShippingInstructionSchema, CartSchema } from "../models/index.js";
3
3
  import { BaseMutationSchema } from "./base.mutation.js";
4
4
  const CheckoutMutationInitiateCheckoutSchema = BaseMutationSchema.extend({
5
- cart: CartIdentifierSchema.required(),
6
- billingAddress: AddressSchema.omit({ identifier: true }).optional(),
5
+ cart: CartSchema.required(),
6
+ billingAddress: AddressSchema.omit({ identifier: true, meta: true }).optional(),
7
7
  notificationEmail: z.string().optional(),
8
8
  notificationPhone: z.string().optional()
9
9
  });
10
10
  const CheckoutMutationSetShippingAddressSchema = BaseMutationSchema.extend({
11
11
  checkout: CartIdentifierSchema.required(),
12
- shippingAddress: AddressSchema.omit({ identifier: true }).required()
12
+ shippingAddress: AddressSchema.omit({ identifier: true, meta: true }).required()
13
13
  });
14
14
  const CheckoutMutationFinalizeCheckoutSchema = BaseMutationSchema.extend({
15
15
  checkout: CartIdentifierSchema.required()
@@ -23,7 +23,7 @@ const CheckoutMutationRemovePaymentInstructionSchema = BaseMutationSchema.extend
23
23
  checkout: CartIdentifierSchema.required()
24
24
  });
25
25
  const CheckoutMutationSetShippingInstructionSchema = BaseMutationSchema.extend({
26
- shippingInstruction: ShippingInstructionSchema.omit({ meta: true, status: true, identifier: true }).required(),
26
+ shippingInstruction: ShippingInstructionSchema.omit({ meta: true }).required(),
27
27
  checkout: CartIdentifierSchema.required()
28
28
  });
29
29
  export {
@@ -2,26 +2,26 @@ import { z } from "zod";
2
2
  import { CategoryIdentifierSchema } from "../models/identifiers.model.js";
3
3
  import { BaseQuerySchema } from "./base.query.js";
4
4
  import { PaginationOptionsSchema } from "../models/base.model.js";
5
- const CategoryQueryById = BaseQuerySchema.extend({
5
+ const CategoryQueryByIdSchema = BaseQuerySchema.extend({
6
6
  id: CategoryIdentifierSchema.default(() => CategoryIdentifierSchema.parse({}))
7
7
  });
8
- const CategoryQueryBySlug = BaseQuerySchema.extend({
8
+ const CategoryQueryBySlugSchema = BaseQuerySchema.extend({
9
9
  slug: z.string().default("")
10
10
  });
11
- const CategoryQueryForBreadcrumb = BaseQuerySchema.extend({
11
+ const CategoryQueryForBreadcrumbSchema = BaseQuerySchema.extend({
12
12
  id: CategoryIdentifierSchema.default(() => CategoryIdentifierSchema.parse({}))
13
13
  });
14
- const CategoryQueryForChildCategories = BaseQuerySchema.extend({
14
+ const CategoryQueryForChildCategoriesSchema = BaseQuerySchema.extend({
15
15
  parentId: CategoryIdentifierSchema.default(() => CategoryIdentifierSchema.parse({})),
16
16
  paginationOptions: PaginationOptionsSchema.default(() => PaginationOptionsSchema.parse({}))
17
17
  });
18
- const CategoryQueryForTopCategories = BaseQuerySchema.extend({
18
+ const CategoryQueryForTopCategoriesSchema = BaseQuerySchema.extend({
19
19
  paginationOptions: PaginationOptionsSchema.default(() => PaginationOptionsSchema.parse({}))
20
20
  });
21
21
  export {
22
- CategoryQueryById,
23
- CategoryQueryBySlug,
24
- CategoryQueryForBreadcrumb,
25
- CategoryQueryForChildCategories,
26
- CategoryQueryForTopCategories
22
+ CategoryQueryByIdSchema,
23
+ CategoryQueryBySlugSchema,
24
+ CategoryQueryForBreadcrumbSchema,
25
+ CategoryQueryForChildCategoriesSchema,
26
+ CategoryQueryForTopCategoriesSchema
27
27
  };
@@ -1,7 +1,9 @@
1
+ import { z } from "zod";
1
2
  import { BaseQuerySchema } from "./base.query.js";
2
3
  import { ProductVariantIdentifierSchema } from "../models/identifiers.model.js";
3
4
  const PriceQueryBySkuSchema = BaseQuerySchema.extend({
4
- variant: ProductVariantIdentifierSchema.required()
5
+ variant: ProductVariantIdentifierSchema.required(),
6
+ type: z.literal(["List", "Offer"]).default("List")
5
7
  });
6
8
  export {
7
9
  PriceQueryBySkuSchema
@@ -1,5 +1,6 @@
1
1
  import type { BaseProvider } from '../providers/index.js';
2
2
  import type { Tracer } from '@opentelemetry/api';
3
+ import type { z } from 'zod';
3
4
  export declare function getTracer(): Tracer;
4
5
  /**
5
6
  * The options associated with annotating a provider function and marking
@@ -27,8 +28,14 @@ export declare class ReactionaryDecoratorOptions {
27
28
  * given query.
28
29
  */
29
30
  cacheTimeToLiveInSeconds: number;
30
- inputSchema?: any;
31
- outputSchema?: any;
31
+ /**
32
+ * The schema for the input (query or mutation) type, for validation purposes
33
+ */
34
+ inputSchema?: z.ZodType;
35
+ /**
36
+ * The schema for the primary output type, for validation purposes
37
+ */
38
+ outputSchema?: z.ZodType;
32
39
  }
33
40
  /**
34
41
  * Decorator for provider functions to provide functionality such as caching, tracing and type-checked
@@ -36,4 +43,16 @@ export declare class ReactionaryDecoratorOptions {
36
43
  * providers.
37
44
  */
38
45
  export declare function Reactionary(options: Partial<ReactionaryDecoratorOptions>): (target: BaseProvider, propertyKey: string | symbol, descriptor: PropertyDescriptor) => PropertyDescriptor;
39
- export declare function validateInput(): void;
46
+ /**
47
+ * Utility function to handle input validation.
48
+ */
49
+ export declare function validateInput(input: any, schema: z.ZodType | undefined): any;
50
+ /**
51
+ * Utility function to handle output validation.
52
+ */
53
+ export declare function validateOutput(output: any, schema: z.ZodType | undefined): any;
54
+ /**
55
+ * Utility function to wrap entry / exit into decorated functions in a
56
+ * traced span for OTEL handling.
57
+ */
58
+ export declare function traceSpan<T>(name: string, fn: () => Promise<T> | T): Promise<T>;
package/src/index.d.ts CHANGED
@@ -12,6 +12,6 @@ export { AddressIdentifierSchema, AddressSchema, AnalyticsEventSchema, Anonymous
12
12
  export type { Address, AddressIdentifier, AnalyticsEvent, AnonymousIdentity, BaseModel, CacheInformation, Cart, CartIdentifier, CartItem, CartItemIdentifier, Category, CategoryIdentifier, CategoryPaginatedResult, Checkout, CheckoutIdentifier, CheckoutItem, CheckoutItemIdentifier, CostBreakDown, Currency, FacetIdentifier, FacetValueIdentifier, FulfillmentCenterIdentifier, GuestIdentity, IdentifierType, Identity, IdentityIdentifier, Image, Inventory, InventoryIdentifier, ItemCostBreakdown, Meta, MonetaryAmount, Order, OrderIdentifier, OrderItem, OrderItemIdentifier, PaginationOptions, PaymentInstruction, PaymentInstructionIdentifier, PaymentMethod, PaymentMethodIdentifier, PaymentStatus, PickupPoint, PickupPointIdentifier, Price, PriceIdentifier, Product, ProductAttribute, ProductAttributeIdentifier, ProductAttributeValue, ProductAttributeValueIdentifier, ProductIdentifier, ProductOption, ProductOptionIdentifier, ProductOptionValue, ProductOptionValueIdentifier, ProductSearchResult, ProductSearchResultFacet, ProductSearchResultFacetValue, ProductSearchResultItem, ProductSearchResultItemVariant, ProductVariant, ProductVariantIdentifier, ProductVariantOption, Profile, RegisteredIdentity, SearchIdentifier, ShippingInstruction, ShippingMethod, ShippingMethodIdentifier, Store, StoreIdentifier, TieredPrice, WebStoreIdentifier, } from './schemas/models/index.js';
13
13
  export { AnalyticsMutationSchema, AnalyticsMutationSearchEventSchema, AnalyticsMutationSearchProductClickEventSchema, BaseMutationSchema, CartMutationAddPaymentMethodSchema, CartMutationApplyCouponSchema, CartMutationChangeCurrencySchema, CartMutationCheckoutSchema, CartMutationDeleteCartSchema, CartMutationItemAddSchema, CartMutationItemQuantityChangeSchema, CartMutationItemRemoveSchema, CartMutationRemoveCouponSchema, CartMutationRemovePaymentMethodSchema, CartMutationSetBillingAddressSchema, CartMutationSetShippingInfoSchema, CheckoutMutationAddPaymentInstructionSchema, CheckoutMutationFinalizeCheckoutSchema, CheckoutMutationInitiateCheckoutSchema, CheckoutMutationRemovePaymentInstructionSchema, CheckoutMutationSetShippingAddressSchema, CheckoutMutationSetShippingInstructionSchema, IdentityMutationLoginSchema, IdentityMutationLogoutSchema, IdentityMutationRegisterSchema, ProfileMutationUpdateSchema, } from './schemas/mutations/index.js';
14
14
  export type { AnalyticsMutation, AnalyticsMutationSearchEvent, AnalyticsMutationSearchProductClickEvent, BaseMutation, CartMutationAddPaymentMethod, CartMutationApplyCoupon, CartMutationChangeCurrency, CartMutationCheckout, CartMutationDeleteCart, CartMutationItemAdd, CartMutationItemQuantityChange, CartMutationItemRemove, CartMutationRemoveCoupon, CartMutationRemovePaymentMethod, CartMutationSetBillingAddress, CartMutationSetShippingInfo, CheckoutMutationAddPaymentInstruction, CheckoutMutationFinalizeCheckout, CheckoutMutationInitiateCheckout, CheckoutMutationRemovePaymentInstruction, CheckoutMutationSetShippingAddress, CheckoutMutationSetShippingInstruction, IdentityMutationLogin, IdentityMutationLogout, IdentityMutationRegister, ProfileMutationUpdate, } from './schemas/mutations/index.js';
15
- export { BaseQuerySchema, CartQueryByIdSchema, CategoryQueryById, CategoryQueryBySlug, CategoryQueryForBreadcrumb, CategoryQueryForChildCategories, CategoryQueryForTopCategories, CheckoutQueryByIdSchema, CheckoutQueryForAvailablePaymentMethodsSchema, CheckoutQueryForAvailableShippingMethodsSchema, IdentityQuerySelfSchema, InventoryQueryBySKUSchema, OrderQueryByIdSchema, PriceQueryBySkuSchema, ProductQueryByIdSchema, ProductQueryBySKUSchema, ProductQueryBySlugSchema, ProductQueryVariantsSchema, ProductSearchQueryByTermSchema, ProfileQuerySelfSchema, StoreQueryByProximitySchema, } from './schemas/queries/index.js';
16
- export type { BaseQuery, CartQueryById, CheckoutQueryById, CheckoutQueryForAvailablePaymentMethods, CheckoutQueryForAvailableShippingMethods, IdentityQuerySelf, InventoryQueryBySKU, OrderQueryById, PriceQueryBySku, ProductQueryById, ProductQueryBySKU, ProductQueryBySlug, ProductQueryVariants, ProductSearchQueryByTerm, ProfileQuerySelf, StoreQueryByProximity, } from './schemas/queries/index.js';
15
+ export { BaseQuerySchema, CartQueryByIdSchema, CategoryQueryByIdSchema, CategoryQueryBySlugSchema, CategoryQueryForBreadcrumbSchema, CategoryQueryForChildCategoriesSchema, CategoryQueryForTopCategoriesSchema, CheckoutQueryByIdSchema, CheckoutQueryForAvailablePaymentMethodsSchema, CheckoutQueryForAvailableShippingMethodsSchema, IdentityQuerySelfSchema, InventoryQueryBySKUSchema, OrderQueryByIdSchema, PriceQueryBySkuSchema, ProductQueryByIdSchema, ProductQueryBySKUSchema, ProductQueryBySlugSchema, ProductQueryVariantsSchema, ProductSearchQueryByTermSchema, ProfileQuerySelfSchema, StoreQueryByProximitySchema, } from './schemas/queries/index.js';
16
+ export type { BaseQuery, CartQueryById, CheckoutQueryById, CategoryQueryById, CategoryQueryBySlug, CategoryQueryForBreadcrumb, CategoryQueryForChildCategories, CategoryQueryForTopCategories, CheckoutQueryForAvailablePaymentMethods, CheckoutQueryForAvailableShippingMethods, IdentityQuerySelf, InventoryQueryBySKU, OrderQueryById, PriceQueryBySku, ProductQueryById, ProductQueryBySKU, ProductQueryBySlug, ProductQueryVariants, ProductSearchQueryByTerm, ProfileQuerySelf, StoreQueryByProximity, } from './schemas/queries/index.js';
17
17
  export { createInitialRequestContext } from './initialization.js';
@@ -11,5 +11,6 @@ export declare const CapabilitiesSchema: z.ZodObject<{
11
11
  price: z.ZodBoolean;
12
12
  category: z.ZodBoolean;
13
13
  store: z.ZodBoolean;
14
+ profile: z.ZodBoolean;
14
15
  }, z.core.$loose>;
15
16
  export type Capabilities = z.infer<typeof CapabilitiesSchema>;