@reactionary/provider-fake 0.0.59 → 0.0.60

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/README.md CHANGED
@@ -1,7 +1,23 @@
1
- # Fake
1
+ # Fake provider for Reactionary
2
2
 
3
- This library was generated with [Nx](https://nx.dev).
3
+ ## Supports
4
4
 
5
- ## Building
6
5
 
7
- Run `nx build provider-fake` to build the library.
6
+
7
+ | Feature | Support | Notes |
8
+ | ----------- | ----------- | --------- |
9
+ | product | Full | |
10
+ | productSearch | Full | |
11
+ | identity | Full | |
12
+ | cart | Full | |
13
+ | checkout | Planned | |
14
+ | order | Planned | |
15
+ | inventory | Full | |
16
+ | price | Full | |
17
+ | category | Full | |
18
+ | store | Full | |
19
+
20
+
21
+ ## Notes
22
+
23
+ Not as many different product names as we would have liked. Maybe we need to create our own faker test data.
@@ -1,14 +1,15 @@
1
1
  import {
2
2
  ProductSchema,
3
- SearchResultSchema,
3
+ ProductSearchResultSchema,
4
4
  CategorySchema,
5
5
  CartSchema,
6
6
  InventorySchema,
7
7
  StoreSchema,
8
- PriceSchema
8
+ PriceSchema,
9
+ ProductSearchResultItemSchema
9
10
  } from "@reactionary/core";
10
11
  import { FakeProductProvider } from "../providers/product.provider.js";
11
- import { FakeSearchProvider } from "../providers/search.provider.js";
12
+ import { FakeSearchProvider } from "../providers/product-search.provider.js";
12
13
  import { FakeCategoryProvider } from "../providers/category.provider.js";
13
14
  import {
14
15
  FakeCartProvider,
@@ -26,10 +27,10 @@ function withFakeCapabilities(configuration, capabilities) {
26
27
  cache
27
28
  );
28
29
  }
29
- if (capabilities.search) {
30
- client.search = new FakeSearchProvider(
30
+ if (capabilities.productSearch) {
31
+ client.productSearch = new FakeSearchProvider(
31
32
  configuration,
32
- SearchResultSchema,
33
+ ProductSearchResultItemSchema,
33
34
  cache
34
35
  );
35
36
  }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@reactionary/provider-fake",
3
- "version": "0.0.59",
3
+ "version": "0.0.60",
4
4
  "main": "index.js",
5
5
  "types": "src/index.d.ts",
6
6
  "dependencies": {
7
- "@reactionary/core": "0.0.59",
7
+ "@reactionary/core": "0.0.60",
8
8
  "zod": "4.1.9",
9
9
  "@faker-js/faker": "^9.8.0"
10
10
  },
@@ -47,7 +47,7 @@ class FakeCartProvider extends CartProvider {
47
47
  const cartId = payload.cart.key || `cart-${this.generator.string.uuid()}`;
48
48
  const cart = await this.getById({ cart: { key: cartId } }, reqCtx);
49
49
  const existingItemIndex = cart.items.findIndex(
50
- (item) => item.sku.key === payload.sku.key
50
+ (item) => item.variant.sku === payload.variant.sku
51
51
  );
52
52
  if (existingItemIndex >= 0) {
53
53
  cart.items[existingItemIndex].quantity += payload.quantity;
@@ -55,7 +55,7 @@ class FakeCartProvider extends CartProvider {
55
55
  const price = this.generator.number.int({ min: 100, max: 1e5 }) / 100;
56
56
  cart.items.push({
57
57
  identifier: { key: `item-${Date.now()}` },
58
- sku: payload.sku,
58
+ variant: payload.variant,
59
59
  quantity: payload.quantity,
60
60
  price: {
61
61
  unitPrice: {
@@ -77,7 +77,7 @@ class FakeCartProvider extends CartProvider {
77
77
  }
78
78
  },
79
79
  product: {
80
- key: `product-for-${payload.sku.key}`
80
+ key: `product-for-${payload.variant.sku}`
81
81
  }
82
82
  });
83
83
  }
@@ -5,5 +5,5 @@ export * from "./identity.provider.js";
5
5
  export * from "./inventory.provider.js";
6
6
  export * from "./price.provider.js";
7
7
  export * from "./product.provider.js";
8
- export * from "./search.provider.js";
8
+ export * from "./product-search.provider.js";
9
9
  export * from "./store.provider.js";
@@ -1,4 +1,5 @@
1
1
  import {
2
+ InventoryIdentifierSchema,
2
3
  InventoryProvider
3
4
  } from "@reactionary/core";
4
5
  import { base, en, Faker } from "@faker-js/faker";
@@ -9,7 +10,7 @@ class FakeInventoryProvider extends InventoryProvider {
9
10
  }
10
11
  async getBySKU(payload, _reqCtx) {
11
12
  let hash = 0;
12
- const skuString = payload.sku.key;
13
+ const skuString = payload.variant.sku;
13
14
  for (let i = 0; i < skuString.length; i++) {
14
15
  hash = (hash << 5) - hash + skuString.charCodeAt(i);
15
16
  hash = hash & hash;
@@ -19,10 +20,10 @@ class FakeInventoryProvider extends InventoryProvider {
19
20
  locale: [en, base]
20
21
  });
21
22
  const model = this.newModel();
22
- model.identifier = {
23
- sku: payload.sku,
23
+ model.identifier = InventoryIdentifierSchema.parse({
24
+ variant: payload.variant,
24
25
  fulfillmentCenter: payload.fulfilmentCenter
25
- };
26
+ });
26
27
  model.sku = skuString;
27
28
  model.quantity = generator.number.int({ min: 0, max: 100 });
28
29
  if (model.quantity > 0) {
@@ -13,11 +13,11 @@ class FakePriceProvider extends PriceProvider {
13
13
  return result;
14
14
  }
15
15
  async getBySKU(payload, _reqCtx) {
16
- if (payload.sku.key === "unknown-sku") {
17
- return this.createEmptyPriceResult(payload.sku.key, _reqCtx.languageContext.currencyCode);
16
+ if (payload.variant.sku === "unknown-sku") {
17
+ return this.createEmptyPriceResult(payload.variant.sku, _reqCtx.languageContext.currencyCode);
18
18
  }
19
19
  let hash = 0;
20
- const skuString = payload.sku.key;
20
+ const skuString = payload.variant.sku;
21
21
  for (let i = 0; i < skuString.length; i++) {
22
22
  hash = (hash << 5) - hash + skuString.charCodeAt(i);
23
23
  hash = hash & hash;
@@ -29,7 +29,7 @@ class FakePriceProvider extends PriceProvider {
29
29
  const model = this.newModel();
30
30
  Object.assign(model, {
31
31
  identifier: {
32
- sku: payload.sku
32
+ variant: payload.variant
33
33
  },
34
34
  unitPrice: {
35
35
  value: generator.number.int({ min: 300, max: 1e5 }) / 100,
@@ -38,7 +38,7 @@ class FakePriceProvider extends PriceProvider {
38
38
  meta: {
39
39
  cache: {
40
40
  hit: false,
41
- key: payload.sku.key
41
+ key: payload.variant.sku
42
42
  },
43
43
  placeholder: false
44
44
  }
@@ -0,0 +1,135 @@
1
+ import {
2
+ ImageSchema,
3
+ ProductSearchProvider,
4
+ ProductSearchResultItemSchema,
5
+ ProductSearchResultSchema
6
+ } from "@reactionary/core";
7
+ import { Faker, en, base } from "@faker-js/faker";
8
+ import { jitter } from "../utilities/jitter.js";
9
+ class FakeSearchProvider extends ProductSearchProvider {
10
+ constructor(config, schema, cache) {
11
+ super(schema, cache);
12
+ this.config = config;
13
+ }
14
+ async queryByTerm(payload, _reqCtx) {
15
+ await jitter(this.config.jitter.mean, this.config.jitter.deviation);
16
+ const query = payload.search;
17
+ const querySpecificity = 20 - query.term.length - query.paginationOptions.pageNumber - query.facets.length;
18
+ const totalProducts = 10 * querySpecificity;
19
+ const totalPages = Math.ceil(totalProducts / query.paginationOptions.pageSize);
20
+ const productsOnPage = Math.min(totalProducts, query.paginationOptions.pageSize);
21
+ const productGenerator = new Faker({
22
+ seed: querySpecificity,
23
+ locale: [en, base]
24
+ });
25
+ const facetGenerator = new Faker({
26
+ seed: 100,
27
+ locale: [en, base]
28
+ });
29
+ const products = new Array();
30
+ const facets = new Array();
31
+ for (let i = 0; i < productsOnPage; i++) {
32
+ const srcUrl = productGenerator.image.urlPicsumPhotos({
33
+ height: 300,
34
+ width: 300,
35
+ grayscale: true,
36
+ blur: 8
37
+ });
38
+ const img = ImageSchema.parse({
39
+ sourceUrl: srcUrl,
40
+ altText: "Fake product image",
41
+ height: 300,
42
+ width: 300
43
+ });
44
+ products.push(
45
+ ProductSearchResultItemSchema.parse(
46
+ {
47
+ identifier: {
48
+ key: "product_" + productGenerator.commerce.isbn()
49
+ },
50
+ image: img,
51
+ name: productGenerator.commerce.productName(),
52
+ slug: productGenerator.lorem.slug(),
53
+ variants: [{
54
+ variant: {
55
+ sku: productGenerator.commerce.isbn()
56
+ },
57
+ image: img,
58
+ options: void 0
59
+ }]
60
+ }
61
+ )
62
+ );
63
+ }
64
+ const facetBase = ["color", "size"];
65
+ for (const base2 of facetBase) {
66
+ const facet = {
67
+ identifier: {
68
+ key: base2
69
+ },
70
+ name: base2,
71
+ values: []
72
+ };
73
+ for (let i = 0; i < 10; i++) {
74
+ const valueKey = i.toString();
75
+ const isActive = query.facets.find(
76
+ (x) => x.facet.key === facet.identifier.key && x.key === valueKey
77
+ ) !== void 0;
78
+ facet.values.push({
79
+ active: isActive,
80
+ count: facetGenerator.number.int({ min: 1, max: 50 }),
81
+ identifier: {
82
+ facet: {
83
+ key: facet.identifier.key
84
+ },
85
+ key: valueKey
86
+ },
87
+ name: facetGenerator.color.human()
88
+ });
89
+ }
90
+ facets.push(facet);
91
+ }
92
+ const result = ProductSearchResultSchema.parse({
93
+ identifier: {
94
+ term: query.term,
95
+ paginationOptions: {
96
+ pageNumber: query.paginationOptions.pageNumber,
97
+ pageSize: query.paginationOptions.pageSize
98
+ },
99
+ facets: query.facets,
100
+ filters: []
101
+ },
102
+ facets,
103
+ items: products,
104
+ pageNumber: query.paginationOptions.pageNumber,
105
+ pageSize: query.paginationOptions.pageSize,
106
+ totalCount: totalProducts,
107
+ totalPages,
108
+ meta: {
109
+ cache: {
110
+ hit: false,
111
+ key: ""
112
+ },
113
+ placeholder: false
114
+ }
115
+ });
116
+ const foo = this.childFunction();
117
+ return result;
118
+ }
119
+ parseFacetValue(facetValueIdentifier, label, count, reqCtx) {
120
+ throw new Error("Method not implemented.");
121
+ }
122
+ parseFacet(facetIdentifier, facetValue, reqCtx) {
123
+ throw new Error("Method not implemented.");
124
+ }
125
+ parseVariant(variant, product, reqCtx) {
126
+ throw new Error("Method not implemented.");
127
+ }
128
+ childFunction() {
129
+ const foo = 42;
130
+ return foo;
131
+ }
132
+ }
133
+ export {
134
+ FakeSearchProvider
135
+ };
@@ -1,7 +1,7 @@
1
1
  import { CapabilitiesSchema } from "@reactionary/core";
2
2
  const FakeCapabilitiesSchema = CapabilitiesSchema.pick({
3
3
  product: true,
4
- search: true,
4
+ productSearch: true,
5
5
  identity: true,
6
6
  category: true,
7
7
  cart: true,
@@ -1,12 +1,12 @@
1
- import type { Cache as ReactinaryCache, ProductProvider, SearchProvider, IdentityProvider, CategoryProvider, CartProvider, InventoryProvider, StoreProvider, PriceProvider } from '@reactionary/core';
1
+ import type { Cache as ReactinaryCache, ProductProvider, ProductSearchProvider, IdentityProvider, CategoryProvider, CartProvider, InventoryProvider, StoreProvider, PriceProvider } from '@reactionary/core';
2
2
  import type { FakeConfiguration } from '../schema/configuration.schema.js';
3
3
  import type { FakeCapabilities } from '../schema/capabilities.schema.js';
4
4
  type FakeClient<T extends FakeCapabilities> = (T['cart'] extends true ? {
5
5
  cart: CartProvider;
6
6
  } : object) & (T['product'] extends true ? {
7
7
  product: ProductProvider;
8
- } : object) & (T['search'] extends true ? {
9
- search: SearchProvider;
8
+ } : object) & (T['productSearch'] extends true ? {
9
+ productSearch: ProductSearchProvider;
10
10
  } : object) & (T['identity'] extends true ? {
11
11
  identity: IdentityProvider;
12
12
  } : object) & (T['category'] extends true ? {
@@ -5,5 +5,5 @@ export * from './identity.provider.js';
5
5
  export * from './inventory.provider.js';
6
6
  export * from './price.provider.js';
7
7
  export * from './product.provider.js';
8
- export * from './search.provider.js';
8
+ export * from './product-search.provider.js';
9
9
  export * from './store.provider.js';
@@ -0,0 +1,14 @@
1
+ import { ProductSearchProvider } from '@reactionary/core';
2
+ import type { ProductSearchResult, ProductSearchResultFacet, ProductSearchResultItem, Cache as ReactionaryCache, FacetIdentifier, FacetValueIdentifier, ProductSearchResultFacetValue, ProductSearchResultItemVariant } from '@reactionary/core';
3
+ import type { RequestContext, ProductSearchQueryByTerm } from '@reactionary/core';
4
+ import type z from 'zod';
5
+ import type { FakeConfiguration } from '../schema/configuration.schema.js';
6
+ export declare class FakeSearchProvider<T extends ProductSearchResultItem = ProductSearchResultItem> extends ProductSearchProvider<T> {
7
+ protected config: FakeConfiguration;
8
+ constructor(config: FakeConfiguration, schema: z.ZodType<T>, cache: ReactionaryCache);
9
+ queryByTerm(payload: ProductSearchQueryByTerm, _reqCtx: RequestContext): Promise<ProductSearchResult>;
10
+ protected parseFacetValue(facetValueIdentifier: FacetValueIdentifier, label: string, count: number, reqCtx: RequestContext): ProductSearchResultFacetValue;
11
+ protected parseFacet(facetIdentifier: FacetIdentifier, facetValue: unknown, reqCtx: RequestContext): ProductSearchResultFacet;
12
+ protected parseVariant(variant: unknown, product: unknown, reqCtx: RequestContext): ProductSearchResultItemVariant;
13
+ protected childFunction(): number;
14
+ }
@@ -3,10 +3,10 @@ export declare const FakeCapabilitiesSchema: z.ZodObject<{
3
3
  price: z.ZodOptional<z.ZodBoolean>;
4
4
  product: z.ZodOptional<z.ZodBoolean>;
5
5
  identity: z.ZodOptional<z.ZodBoolean>;
6
- search: z.ZodOptional<z.ZodBoolean>;
7
6
  cart: z.ZodOptional<z.ZodBoolean>;
8
7
  inventory: z.ZodOptional<z.ZodBoolean>;
9
8
  category: z.ZodOptional<z.ZodBoolean>;
10
9
  store: z.ZodOptional<z.ZodBoolean>;
10
+ productSearch: z.ZodOptional<z.ZodBoolean>;
11
11
  }, z.core.$loose>;
12
12
  export type FakeCapabilities = z.infer<typeof FakeCapabilitiesSchema>;
@@ -1,99 +0,0 @@
1
- import {
2
- SearchProvider
3
- } from "@reactionary/core";
4
- import { Faker, en, base } from "@faker-js/faker";
5
- import { jitter } from "../utilities/jitter.js";
6
- class FakeSearchProvider extends SearchProvider {
7
- constructor(config, schema, cache) {
8
- super(schema, cache);
9
- this.config = config;
10
- }
11
- async queryByTerm(payload, _reqCtx) {
12
- await jitter(this.config.jitter.mean, this.config.jitter.deviation);
13
- const query = payload.search;
14
- const querySpecificity = 20 - query.term.length - query.page - query.facets.length;
15
- const totalProducts = 10 * querySpecificity;
16
- const totalPages = Math.ceil(totalProducts / query.pageSize);
17
- const productsOnPage = Math.min(totalProducts, query.pageSize);
18
- const productGenerator = new Faker({
19
- seed: querySpecificity,
20
- locale: [en, base]
21
- });
22
- const facetGenerator = new Faker({
23
- seed: 100,
24
- locale: [en, base]
25
- });
26
- const products = new Array();
27
- const facets = new Array();
28
- for (let i = 0; i < productsOnPage; i++) {
29
- products.push({
30
- identifier: {
31
- key: productGenerator.commerce.isbn()
32
- },
33
- image: productGenerator.image.urlPicsumPhotos({
34
- height: 300,
35
- width: 300,
36
- grayscale: true,
37
- blur: 8
38
- }),
39
- name: productGenerator.commerce.productName(),
40
- slug: productGenerator.lorem.slug()
41
- });
42
- }
43
- const facetBase = ["color", "size"];
44
- for (const base2 of facetBase) {
45
- const facet = {
46
- identifier: {
47
- key: base2
48
- },
49
- name: base2,
50
- values: []
51
- };
52
- for (let i = 0; i < 10; i++) {
53
- const valueKey = i.toString();
54
- const isActive = query.facets.find(
55
- (x) => x.facet.key === facet.identifier.key && x.key === valueKey
56
- ) !== void 0;
57
- facet.values.push({
58
- active: isActive,
59
- count: facetGenerator.number.int({ min: 1, max: 50 }),
60
- identifier: {
61
- facet: {
62
- key: facet.identifier.key
63
- },
64
- key: valueKey
65
- },
66
- name: facetGenerator.color.human()
67
- });
68
- }
69
- facets.push(facet);
70
- }
71
- const result = {
72
- pages: totalPages,
73
- identifier: {
74
- term: query.term,
75
- page: query.page,
76
- facets: query.facets,
77
- pageSize: query.pageSize
78
- },
79
- facets,
80
- products,
81
- meta: {
82
- cache: {
83
- hit: false,
84
- key: ""
85
- },
86
- placeholder: false
87
- }
88
- };
89
- const foo = this.childFunction();
90
- return this.schema.parse(result);
91
- }
92
- childFunction() {
93
- const foo = 42;
94
- return foo;
95
- }
96
- }
97
- export {
98
- FakeSearchProvider
99
- };
@@ -1,11 +0,0 @@
1
- import { SearchProvider } from '@reactionary/core';
2
- import type { SearchResult, Cache as ReactionaryCache } from '@reactionary/core';
3
- import type { RequestContext, SearchQueryByTerm } from '@reactionary/core';
4
- import type z from 'zod';
5
- import type { FakeConfiguration } from '../schema/configuration.schema.js';
6
- export declare class FakeSearchProvider<T extends SearchResult = SearchResult> extends SearchProvider<T> {
7
- protected config: FakeConfiguration;
8
- constructor(config: FakeConfiguration, schema: z.ZodType<T>, cache: ReactionaryCache);
9
- queryByTerm(payload: SearchQueryByTerm, _reqCtx: RequestContext): Promise<SearchResult>;
10
- protected childFunction(): number;
11
- }