@reactionary/source 0.0.52 → 0.2.16
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/.env-template +19 -0
- package/.github/workflows/pull-request.yml +3 -1
- package/.github/workflows/release.yml +9 -0
- package/.vscode/extensions.json +0 -2
- package/LICENSE +21 -0
- package/README.md +175 -23
- package/core/package.json +6 -3
- package/core/src/cache/cache.interface.ts +1 -0
- package/core/src/cache/index.ts +4 -0
- package/core/src/cache/memory-cache.ts +30 -2
- package/core/src/cache/noop-cache.ts +15 -1
- package/core/src/cache/redis-cache.ts +20 -0
- package/core/src/client/client-builder.ts +71 -54
- package/core/src/client/client.ts +9 -47
- package/core/src/client/index.ts +2 -0
- package/core/src/decorators/index.ts +1 -0
- package/core/src/decorators/reactionary.decorator.ts +203 -34
- package/core/src/index.ts +6 -19
- package/core/src/initialization.ts +1 -18
- package/core/src/metrics/metrics.ts +67 -0
- package/core/src/providers/analytics.provider.ts +1 -6
- package/core/src/providers/base.provider.ts +5 -69
- package/core/src/providers/cart.provider.ts +15 -55
- package/core/src/providers/category.provider.ts +7 -11
- package/core/src/providers/checkout.provider.ts +17 -15
- package/core/src/providers/identity.provider.ts +6 -8
- package/core/src/providers/index.ts +2 -1
- package/core/src/providers/inventory.provider.ts +15 -5
- package/core/src/providers/order-search.provider.ts +29 -0
- package/core/src/providers/order.provider.ts +47 -15
- package/core/src/providers/price.provider.ts +30 -36
- package/core/src/providers/product-search.provider.ts +61 -0
- package/core/src/providers/product.provider.ts +71 -12
- package/core/src/providers/profile.provider.ts +74 -14
- package/core/src/providers/store.provider.ts +3 -5
- package/core/src/schemas/capabilities.schema.ts +10 -3
- package/core/src/schemas/errors/generic.error.ts +9 -0
- package/core/src/schemas/errors/index.ts +4 -0
- package/core/src/schemas/errors/invalid-input.error.ts +9 -0
- package/core/src/schemas/errors/invalid-output.error.ts +9 -0
- package/core/src/schemas/errors/not-found.error.ts +9 -0
- package/core/src/schemas/index.ts +7 -0
- package/core/src/schemas/models/analytics.model.ts +2 -1
- package/core/src/schemas/models/base.model.ts +6 -24
- package/core/src/schemas/models/cart.model.ts +5 -8
- package/core/src/schemas/models/category.model.ts +4 -9
- package/core/src/schemas/models/checkout.model.ts +6 -7
- package/core/src/schemas/models/cost.model.ts +4 -3
- package/core/src/schemas/models/currency.model.ts +2 -1
- package/core/src/schemas/models/identifiers.model.ts +106 -62
- package/core/src/schemas/models/identity.model.ts +10 -19
- package/core/src/schemas/models/index.ts +2 -1
- package/core/src/schemas/models/inventory.model.ts +8 -5
- package/core/src/schemas/models/order-search.model.ts +28 -0
- package/core/src/schemas/models/order.model.ts +20 -26
- package/core/src/schemas/models/payment.model.ts +14 -17
- package/core/src/schemas/models/price.model.ts +11 -11
- package/core/src/schemas/models/product-search.model.ts +42 -0
- package/core/src/schemas/models/product.model.ts +64 -22
- package/core/src/schemas/models/profile.model.ts +19 -22
- package/core/src/schemas/models/shipping-method.model.ts +24 -29
- package/core/src/schemas/models/store.model.ts +9 -5
- package/core/src/schemas/mutations/analytics.mutation.ts +8 -7
- package/core/src/schemas/mutations/base.mutation.ts +2 -1
- package/core/src/schemas/mutations/cart.mutation.ts +33 -33
- package/core/src/schemas/mutations/checkout.mutation.ts +23 -30
- package/core/src/schemas/mutations/identity.mutation.ts +4 -3
- package/core/src/schemas/mutations/profile.mutation.ts +38 -3
- package/core/src/schemas/queries/base.query.ts +2 -1
- package/core/src/schemas/queries/cart.query.ts +3 -3
- package/core/src/schemas/queries/category.query.ts +18 -18
- package/core/src/schemas/queries/checkout.query.ts +7 -9
- package/core/src/schemas/queries/identity.query.ts +2 -1
- package/core/src/schemas/queries/index.ts +2 -1
- package/core/src/schemas/queries/inventory.query.ts +5 -5
- package/core/src/schemas/queries/order-search.query.ts +10 -0
- package/core/src/schemas/queries/order.query.ts +3 -2
- package/core/src/schemas/queries/price.query.ts +10 -4
- package/core/src/schemas/queries/product-search.query.ts +16 -0
- package/core/src/schemas/queries/product.query.ts +13 -6
- package/core/src/schemas/queries/profile.query.ts +5 -2
- package/core/src/schemas/queries/store.query.ts +6 -5
- package/core/src/schemas/result.ts +107 -0
- package/core/src/schemas/session.schema.ts +4 -4
- package/core/src/test/reactionary.decorator.spec.ts +249 -0
- package/core/src/zod-utils.ts +19 -0
- package/core/tsconfig.json +1 -1
- package/core/tsconfig.spec.json +2 -26
- package/core/vitest.config.ts +14 -0
- package/documentation/1-purpose.md +114 -0
- package/documentation/2-getting-started.md +229 -0
- package/documentation/3-querying-and-changing-data.md +74 -0
- package/documentation/4-product-data.md +107 -0
- package/documentation/5-cart-and-checkout.md +211 -0
- package/documentation/6-product-search.md +143 -0
- package/documentation/7-marketing.md +3 -0
- package/eslint.config.mjs +1 -0
- package/examples/node/eslint.config.mjs +1 -4
- package/examples/node/package.json +10 -3
- package/examples/node/project.json +4 -1
- package/examples/node/src/basic/basic-node-provider-model-extension.spec.ts +22 -23
- package/examples/node/src/basic/basic-node-provider-query-extension.spec.ts +15 -11
- package/examples/node/src/basic/basic-node-setup.spec.ts +44 -28
- package/examples/node/src/basic/client-creation.spec.ts +53 -0
- package/examples/node/src/capabilities/cart.spec.ts +255 -0
- package/examples/node/src/capabilities/category.spec.ts +193 -0
- package/examples/node/src/capabilities/checkout.spec.ts +341 -0
- package/examples/node/src/capabilities/identity.spec.ts +93 -0
- package/examples/node/src/capabilities/inventory.spec.ts +66 -0
- package/examples/node/src/capabilities/order-search.spec.ts +265 -0
- package/examples/node/src/capabilities/order.spec.ts +91 -0
- package/examples/node/src/capabilities/price.spec.ts +51 -0
- package/examples/node/src/capabilities/product-search.spec.ts +293 -0
- package/examples/node/src/capabilities/product.spec.ts +122 -0
- package/examples/node/src/capabilities/profile.spec.ts +316 -0
- package/examples/node/src/capabilities/store.spec.ts +26 -0
- package/examples/node/src/utils.ts +147 -0
- package/examples/node/tsconfig.json +9 -12
- package/examples/node/tsconfig.lib.json +1 -2
- package/examples/node/tsconfig.spec.json +2 -14
- package/examples/node/vitest.config.ts +14 -0
- package/migrations.json +22 -5
- package/nx.json +8 -47
- package/package.json +24 -96
- package/providers/algolia/README.md +39 -2
- package/providers/algolia/package.json +2 -1
- package/providers/algolia/src/core/initialize.ts +7 -14
- package/providers/algolia/src/index.ts +2 -4
- package/providers/algolia/src/providers/index.ts +1 -0
- package/providers/algolia/src/providers/product-search.provider.ts +241 -0
- package/providers/algolia/src/schema/capabilities.schema.ts +2 -3
- package/providers/algolia/src/schema/index.ts +3 -0
- package/providers/algolia/src/schema/search.schema.ts +8 -8
- package/providers/algolia/tsconfig.json +1 -1
- package/providers/algolia/tsconfig.lib.json +1 -1
- package/providers/algolia/tsconfig.spec.json +2 -14
- package/providers/algolia/vitest.config.ts +14 -0
- package/providers/commercetools/README.md +30 -3
- package/providers/commercetools/package.json +2 -1
- package/providers/commercetools/src/core/client.ts +178 -99
- package/providers/commercetools/src/core/initialize.ts +130 -74
- package/providers/commercetools/src/core/token-cache.ts +45 -0
- package/providers/commercetools/src/index.ts +3 -2
- package/providers/commercetools/src/providers/cart.provider.ts +281 -341
- package/providers/commercetools/src/providers/category.provider.ts +223 -138
- package/providers/commercetools/src/providers/checkout.provider.ts +631 -449
- package/providers/commercetools/src/providers/identity.provider.ts +50 -29
- package/providers/commercetools/src/providers/index.ts +2 -2
- package/providers/commercetools/src/providers/inventory.provider.ts +76 -74
- package/providers/commercetools/src/providers/order-search.provider.ts +220 -0
- package/providers/commercetools/src/providers/order.provider.ts +96 -61
- package/providers/commercetools/src/providers/price.provider.ts +147 -117
- package/providers/commercetools/src/providers/product-search.provider.ts +528 -0
- package/providers/commercetools/src/providers/product.provider.ts +249 -74
- package/providers/commercetools/src/providers/profile.provider.ts +445 -28
- package/providers/commercetools/src/providers/store.provider.ts +54 -40
- package/providers/commercetools/src/schema/capabilities.schema.ts +3 -1
- package/providers/commercetools/src/schema/commercetools.schema.ts +17 -3
- package/providers/commercetools/src/schema/configuration.schema.ts +1 -0
- package/providers/commercetools/src/schema/session.schema.ts +7 -0
- package/providers/commercetools/src/test/caching.spec.ts +82 -0
- package/providers/commercetools/src/test/identity.spec.ts +109 -0
- package/providers/commercetools/src/test/test-utils.ts +21 -19
- package/providers/commercetools/tsconfig.json +1 -1
- package/providers/commercetools/tsconfig.lib.json +1 -1
- package/providers/commercetools/tsconfig.spec.json +2 -14
- package/providers/commercetools/vitest.config.ts +15 -0
- package/providers/fake/README.md +20 -4
- package/providers/fake/package.json +2 -1
- package/providers/fake/src/core/initialize.ts +47 -49
- package/providers/fake/src/providers/analytics.provider.ts +5 -7
- package/providers/fake/src/providers/cart.provider.ts +163 -92
- package/providers/fake/src/providers/category.provider.ts +78 -50
- package/providers/fake/src/providers/checkout.provider.ts +254 -0
- package/providers/fake/src/providers/identity.provider.ts +57 -65
- package/providers/fake/src/providers/index.ts +6 -2
- package/providers/fake/src/providers/inventory.provider.ts +40 -36
- package/providers/fake/src/providers/order-search.provider.ts +78 -0
- package/providers/fake/src/providers/order.provider.ts +106 -0
- package/providers/fake/src/providers/price.provider.ts +93 -41
- package/providers/fake/src/providers/product-search.provider.ts +206 -0
- package/providers/fake/src/providers/product.provider.ts +56 -41
- package/providers/fake/src/providers/profile.provider.ts +147 -0
- package/providers/fake/src/providers/store.provider.ts +30 -20
- package/providers/fake/src/schema/capabilities.schema.ts +5 -1
- package/providers/fake/src/test/cart.provider.spec.ts +59 -80
- package/providers/fake/src/test/category.provider.spec.ts +145 -87
- package/providers/fake/src/test/checkout.provider.spec.ts +222 -0
- package/providers/fake/src/test/order-search.provider.spec.ts +50 -0
- package/providers/fake/src/test/order.provider.spec.ts +44 -0
- package/providers/fake/src/test/price.provider.spec.ts +50 -45
- package/providers/fake/src/test/product.provider.spec.ts +15 -7
- package/providers/fake/src/test/profile.provider.spec.ts +167 -0
- package/providers/fake/tsconfig.json +1 -1
- package/providers/fake/tsconfig.lib.json +1 -1
- package/providers/fake/tsconfig.spec.json +2 -12
- package/providers/fake/vitest.config.ts +14 -0
- package/providers/medusa/README.md +30 -0
- package/providers/medusa/TESTING.md +98 -0
- package/providers/medusa/eslint.config.mjs +19 -0
- package/providers/medusa/package.json +22 -0
- package/providers/medusa/project.json +34 -0
- package/providers/medusa/src/core/client.ts +370 -0
- package/providers/medusa/src/core/initialize.ts +78 -0
- package/providers/medusa/src/index.ts +13 -0
- package/providers/medusa/src/providers/cart.provider.ts +575 -0
- package/providers/medusa/src/providers/category.provider.ts +247 -0
- package/providers/medusa/src/providers/checkout.provider.ts +636 -0
- package/providers/medusa/src/providers/identity.provider.ts +137 -0
- package/providers/medusa/src/providers/inventory.provider.ts +173 -0
- package/providers/medusa/src/providers/order-search.provider.ts +202 -0
- package/providers/medusa/src/providers/order.provider.ts +226 -0
- package/providers/medusa/src/providers/price.provider.ts +140 -0
- package/providers/medusa/src/providers/product-search.provider.ts +243 -0
- package/providers/medusa/src/providers/product.provider.ts +261 -0
- package/providers/medusa/src/providers/profile.provider.ts +392 -0
- package/providers/medusa/src/schema/capabilities.schema.ts +18 -0
- package/providers/medusa/src/schema/configuration.schema.ts +11 -0
- package/providers/medusa/src/schema/medusa.schema.ts +31 -0
- package/providers/medusa/src/test/cart.provider.spec.ts +240 -0
- package/providers/medusa/src/test/category.provider.spec.ts +231 -0
- package/providers/medusa/src/test/checkout.spec.ts +349 -0
- package/providers/medusa/src/test/identity.provider.spec.ts +122 -0
- package/providers/medusa/src/test/inventory.provider.spec.ts +88 -0
- package/providers/medusa/src/test/large-cart.provider.spec.ts +103 -0
- package/providers/medusa/src/test/price.provider.spec.ts +104 -0
- package/providers/medusa/src/test/product.provider.spec.ts +146 -0
- package/providers/medusa/src/test/search.provider.spec.ts +203 -0
- package/providers/medusa/src/test/test-utils.ts +13 -0
- package/providers/medusa/src/utils/medusa-helpers.ts +89 -0
- package/providers/medusa/tsconfig.json +21 -0
- package/providers/medusa/tsconfig.lib.json +9 -0
- package/providers/medusa/tsconfig.spec.json +4 -0
- package/providers/medusa/vitest.config.ts +15 -0
- package/providers/meilisearch/README.md +48 -0
- package/providers/meilisearch/eslint.config.mjs +22 -0
- package/providers/meilisearch/package.json +13 -0
- package/providers/meilisearch/project.json +34 -0
- package/providers/meilisearch/src/core/initialize.ts +21 -0
- package/providers/meilisearch/src/index.ts +6 -0
- package/providers/meilisearch/src/providers/index.ts +1 -0
- package/providers/meilisearch/src/providers/order-search.provider.ts +222 -0
- package/providers/meilisearch/src/providers/product-search.provider.ts +251 -0
- package/providers/meilisearch/src/schema/capabilities.schema.ts +10 -0
- package/providers/meilisearch/src/schema/configuration.schema.ts +11 -0
- package/providers/meilisearch/src/schema/index.ts +3 -0
- package/providers/meilisearch/src/schema/search.schema.ts +14 -0
- package/providers/meilisearch/tsconfig.json +24 -0
- package/providers/meilisearch/tsconfig.lib.json +10 -0
- package/providers/meilisearch/tsconfig.spec.json +4 -0
- package/providers/meilisearch/vitest.config.ts +14 -0
- package/providers/posthog/package.json +2 -1
- package/providers/posthog/tsconfig.json +1 -1
- package/tsconfig.base.json +5 -0
- package/vitest.config.ts +10 -0
- package/core/src/providers/search.provider.ts +0 -18
- package/core/src/schemas/models/search.model.ts +0 -36
- package/core/src/schemas/queries/search.query.ts +0 -9
- package/examples/next/.swcrc +0 -30
- package/examples/next/eslint.config.mjs +0 -21
- package/examples/next/index.d.ts +0 -6
- package/examples/next/next-env.d.ts +0 -5
- package/examples/next/next.config.js +0 -31
- package/examples/next/project.json +0 -9
- package/examples/next/public/.gitkeep +0 -0
- package/examples/next/public/favicon.ico +0 -0
- package/examples/next/src/app/global.css +0 -0
- package/examples/next/src/app/layout.tsx +0 -18
- package/examples/next/src/app/page.module.scss +0 -2
- package/examples/next/src/app/page.tsx +0 -47
- package/examples/next/src/instrumentation.ts +0 -9
- package/examples/next/tsconfig.json +0 -44
- package/examples/node/jest.config.ts +0 -10
- package/jest.config.ts +0 -6
- package/jest.preset.js +0 -3
- package/providers/algolia/jest.config.ts +0 -10
- package/providers/algolia/src/providers/product.provider.ts +0 -66
- package/providers/algolia/src/providers/search.provider.ts +0 -106
- package/providers/algolia/src/test/search.provider.spec.ts +0 -91
- package/providers/commercetools/jest.config.cjs +0 -10
- package/providers/commercetools/src/providers/search.provider.ts +0 -96
- package/providers/commercetools/src/test/cart.provider.spec.ts +0 -199
- package/providers/commercetools/src/test/category.provider.spec.ts +0 -168
- package/providers/commercetools/src/test/checkout.provider.spec.ts +0 -312
- package/providers/commercetools/src/test/identity.provider.spec.ts +0 -88
- package/providers/commercetools/src/test/inventory.provider.spec.ts +0 -41
- package/providers/commercetools/src/test/price.provider.spec.ts +0 -81
- package/providers/commercetools/src/test/product.provider.spec.ts +0 -80
- package/providers/commercetools/src/test/profile.provider.spec.ts +0 -49
- package/providers/commercetools/src/test/search.provider.spec.ts +0 -61
- package/providers/commercetools/src/test/store.provider.spec.ts +0 -37
- package/providers/fake/jest.config.cjs +0 -10
- package/providers/fake/src/providers/search.provider.ts +0 -132
|
@@ -1,74 +1,135 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
ImageSchema,
|
|
3
|
+
ProductAttributeIdentifierSchema,
|
|
4
|
+
ProductAttributeSchema,
|
|
5
|
+
ProductAttributeValueIdentifierSchema,
|
|
6
|
+
ProductAttributeValueSchema,
|
|
7
|
+
ProductProvider,
|
|
8
|
+
ProductQueryByIdSchema,
|
|
9
|
+
ProductQueryBySKUSchema,
|
|
10
|
+
ProductQueryBySlugSchema,
|
|
11
|
+
ProductSchema,
|
|
12
|
+
Reactionary,
|
|
13
|
+
success,
|
|
14
|
+
error,
|
|
3
15
|
} from '@reactionary/core';
|
|
4
|
-
import { CommercetoolsClient } from '../core/client.js';
|
|
5
|
-
import type { z } from 'zod';
|
|
6
16
|
import type { CommercetoolsConfiguration } from '../schema/configuration.schema.js';
|
|
7
|
-
import type {
|
|
8
|
-
|
|
9
|
-
|
|
17
|
+
import type {
|
|
18
|
+
ProductProjection,
|
|
19
|
+
ProductVariant as CTProductVariant,
|
|
20
|
+
Attribute as CTAttribute,
|
|
21
|
+
} from '@commercetools/platform-sdk';
|
|
22
|
+
import type {
|
|
23
|
+
Product,
|
|
24
|
+
ProductVariant,
|
|
25
|
+
ProductQueryById,
|
|
26
|
+
ProductQueryBySKU,
|
|
27
|
+
ProductQueryBySlug,
|
|
28
|
+
ProductVariantIdentifier,
|
|
29
|
+
RequestContext,
|
|
30
|
+
ProductAttribute,
|
|
31
|
+
ProductAttributeIdentifier,
|
|
32
|
+
ProductAttributeValue,
|
|
33
|
+
ProductAttributeValueIdentifier,
|
|
34
|
+
ProductIdentifier,
|
|
35
|
+
ProductVariantOption,
|
|
36
|
+
ProductOptionIdentifier,
|
|
37
|
+
Result,
|
|
38
|
+
} from '@reactionary/core';
|
|
39
|
+
import type { Cache, Image } from '@reactionary/core';
|
|
40
|
+
import type { CommercetoolsAPI } from '../core/client.js';
|
|
41
|
+
import type { NotFoundError } from '@reactionary/core';
|
|
10
42
|
|
|
11
|
-
export class CommercetoolsProductProvider
|
|
12
|
-
T extends Product = Product
|
|
13
|
-
> extends ProductProvider<T> {
|
|
43
|
+
export class CommercetoolsProductProvider extends ProductProvider {
|
|
14
44
|
protected config: CommercetoolsConfiguration;
|
|
45
|
+
protected commercetools: CommercetoolsAPI;
|
|
15
46
|
|
|
16
|
-
constructor(
|
|
17
|
-
|
|
47
|
+
constructor(
|
|
48
|
+
config: CommercetoolsConfiguration,
|
|
49
|
+
cache: Cache,
|
|
50
|
+
context: RequestContext,
|
|
51
|
+
commercetools: CommercetoolsAPI
|
|
52
|
+
) {
|
|
53
|
+
super(cache, context);
|
|
18
54
|
|
|
19
55
|
this.config = config;
|
|
56
|
+
this.commercetools = commercetools;
|
|
20
57
|
}
|
|
21
58
|
|
|
22
|
-
protected async getClient(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
59
|
+
protected async getClient() {
|
|
60
|
+
const client = await this.commercetools.getClient();
|
|
61
|
+
return client
|
|
62
|
+
.withProjectKey({ projectKey: this.config.projectKey })
|
|
63
|
+
.productProjections();
|
|
26
64
|
}
|
|
27
65
|
|
|
66
|
+
@Reactionary({
|
|
67
|
+
inputSchema: ProductQueryByIdSchema,
|
|
68
|
+
outputSchema: ProductSchema,
|
|
69
|
+
cache: true,
|
|
70
|
+
cacheTimeToLiveInSeconds: 300,
|
|
71
|
+
currencyDependentCaching: false,
|
|
72
|
+
localeDependentCaching: true
|
|
73
|
+
})
|
|
28
74
|
public override async getById(
|
|
29
|
-
payload: ProductQueryById
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return this.parseSingle(remote.body, reqCtx);
|
|
41
|
-
} catch(error) {
|
|
42
|
-
return this.createEmptyProduct(payload.id);
|
|
43
|
-
}
|
|
75
|
+
payload: ProductQueryById
|
|
76
|
+
): Promise<Result<Product>> {
|
|
77
|
+
const client = await this.getClient();
|
|
78
|
+
const remote = await client
|
|
79
|
+
.withKey({ key: payload.identifier.key })
|
|
80
|
+
.get()
|
|
81
|
+
.execute();
|
|
82
|
+
const value = this.parseSingle(remote.body);
|
|
83
|
+
|
|
84
|
+
return success(value);
|
|
44
85
|
}
|
|
45
86
|
|
|
87
|
+
@Reactionary({
|
|
88
|
+
inputSchema: ProductQueryBySlugSchema,
|
|
89
|
+
outputSchema: ProductSchema,
|
|
90
|
+
cache: true,
|
|
91
|
+
cacheTimeToLiveInSeconds: 300,
|
|
92
|
+
currencyDependentCaching: false,
|
|
93
|
+
localeDependentCaching: true
|
|
94
|
+
})
|
|
46
95
|
public override async getBySlug(
|
|
47
|
-
payload: ProductQueryBySlug
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const client = await this.getClient(reqCtx);
|
|
96
|
+
payload: ProductQueryBySlug
|
|
97
|
+
): Promise<Result<Product, NotFoundError>> {
|
|
98
|
+
const client = await this.getClient();
|
|
51
99
|
|
|
52
100
|
const remote = await client
|
|
53
101
|
.get({
|
|
54
102
|
queryArgs: {
|
|
55
|
-
|
|
56
|
-
'
|
|
57
|
-
|
|
103
|
+
// FIXME: Hardcoded locale
|
|
104
|
+
where: 'slug(en = :slug)',
|
|
105
|
+
'var.slug': payload.slug,
|
|
106
|
+
},
|
|
58
107
|
})
|
|
59
108
|
.execute();
|
|
60
109
|
|
|
61
110
|
if (remote.body.count === 0) {
|
|
62
|
-
return
|
|
111
|
+
return error<NotFoundError>({
|
|
112
|
+
type: 'NotFound',
|
|
113
|
+
identifier: payload.slug,
|
|
114
|
+
});
|
|
63
115
|
}
|
|
64
|
-
|
|
116
|
+
const result = this.parseSingle(remote.body.results[0]);
|
|
117
|
+
|
|
118
|
+
return success(result);
|
|
65
119
|
}
|
|
66
120
|
|
|
121
|
+
@Reactionary({
|
|
122
|
+
inputSchema: ProductQueryBySKUSchema,
|
|
123
|
+
outputSchema: ProductSchema,
|
|
124
|
+
cache: true,
|
|
125
|
+
cacheTimeToLiveInSeconds: 300,
|
|
126
|
+
currencyDependentCaching: false,
|
|
127
|
+
localeDependentCaching: true
|
|
128
|
+
})
|
|
67
129
|
public override async getBySKU(
|
|
68
|
-
payload: ProductQueryBySKU
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const client = await this.getClient(reqCtx);
|
|
130
|
+
payload: ProductQueryBySKU
|
|
131
|
+
): Promise<Result<Product>> {
|
|
132
|
+
const client = await this.getClient();
|
|
72
133
|
|
|
73
134
|
const remote = await client
|
|
74
135
|
.get({
|
|
@@ -76,53 +137,167 @@ export class CommercetoolsProductProvider<
|
|
|
76
137
|
staged: false,
|
|
77
138
|
limit: 1,
|
|
78
139
|
where: 'variants(sku in (:skus)) OR (masterVariant(sku in (:skus))) ',
|
|
79
|
-
'var.skus': [payload].map(p => p.sku
|
|
80
|
-
}
|
|
140
|
+
'var.skus': [payload].map((p) => p.variant.sku),
|
|
141
|
+
},
|
|
81
142
|
})
|
|
82
143
|
.execute();
|
|
83
144
|
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
protected override parseSingle(dataIn: unknown, reqCtx: RequestContext): T {
|
|
89
|
-
const data = dataIn as ProductProjection;
|
|
90
|
-
const base = this.newModel();
|
|
145
|
+
const result = this.parseSingle(remote.body.results[0]);
|
|
91
146
|
|
|
147
|
+
return success(result);
|
|
148
|
+
}
|
|
92
149
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
150
|
+
protected parseSingle(data: ProductProjection): Product {
|
|
151
|
+
const identifier = { key: data.key || data.id } satisfies ProductIdentifier;
|
|
152
|
+
const name = data.name[this.context.languageContext.locale];
|
|
153
|
+
const slug = data.slug[this.context.languageContext.locale];
|
|
96
154
|
|
|
155
|
+
let description = '';
|
|
97
156
|
if (data.description) {
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (data.masterVariant.images && data.masterVariant.images.length > 0) {
|
|
102
|
-
base.image = data.masterVariant.images[0].url;
|
|
157
|
+
description = data.description[this.context.languageContext.locale];
|
|
103
158
|
}
|
|
104
159
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
160
|
+
const variantLevelAttributes =
|
|
161
|
+
data.masterVariant.attributes?.map((x) => this.parseAttribute(x)) || [];
|
|
162
|
+
const productLevelAttributes =
|
|
163
|
+
data.attributes.map((x) => this.parseAttribute(x)) || [];
|
|
164
|
+
const sharedAttributes = [
|
|
165
|
+
...productLevelAttributes,
|
|
166
|
+
...variantLevelAttributes,
|
|
167
|
+
];
|
|
168
|
+
const mainVariant = this.parseVariant(data.masterVariant, data);
|
|
108
169
|
|
|
109
|
-
const
|
|
110
|
-
for (const variant of variants) {
|
|
111
|
-
if (variant.
|
|
112
|
-
|
|
113
|
-
identifier: { key: variant.sku },
|
|
114
|
-
});
|
|
170
|
+
const otherVariants = [];
|
|
171
|
+
for (const variant of data.variants || []) {
|
|
172
|
+
if (variant.id !== data.masterVariant.id) {
|
|
173
|
+
otherVariants.push(this.parseVariant(variant, data));
|
|
115
174
|
}
|
|
116
175
|
}
|
|
117
176
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
177
|
+
const result = {
|
|
178
|
+
identifier,
|
|
179
|
+
name,
|
|
180
|
+
slug,
|
|
181
|
+
description,
|
|
182
|
+
sharedAttributes,
|
|
183
|
+
mainVariant,
|
|
184
|
+
brand: '',
|
|
185
|
+
longDescription: '',
|
|
186
|
+
manufacturer: '',
|
|
187
|
+
options: [],
|
|
188
|
+
parentCategories: [],
|
|
189
|
+
published: true,
|
|
190
|
+
variants: otherVariants,
|
|
191
|
+
} satisfies Product;
|
|
192
|
+
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
122
195
|
|
|
123
|
-
|
|
196
|
+
/**
|
|
197
|
+
* Return true, if the attribute is a defining attribute (ie an option)
|
|
198
|
+
* @param attr a variant attribute
|
|
199
|
+
* @returns true if the attribute is an option
|
|
200
|
+
*/
|
|
201
|
+
protected isVariantAttributeAnOption(attr: CTAttribute): boolean {
|
|
202
|
+
// for now, the assumption is that any variant attribute is a defining attribute (ie an option)
|
|
203
|
+
// ideally this should be verified with the product type.
|
|
204
|
+
return true;
|
|
124
205
|
}
|
|
125
206
|
|
|
207
|
+
protected parseVariant(
|
|
208
|
+
variant: CTProductVariant,
|
|
209
|
+
product: ProductProjection
|
|
210
|
+
): ProductVariant {
|
|
211
|
+
const identifier = {
|
|
212
|
+
sku: variant.sku!,
|
|
213
|
+
} satisfies ProductVariantIdentifier;
|
|
214
|
+
|
|
215
|
+
const images = [
|
|
216
|
+
...(variant.images || []).map((img) =>
|
|
217
|
+
ImageSchema.parse({
|
|
218
|
+
sourceUrl: img.url,
|
|
219
|
+
altText: img.label || '',
|
|
220
|
+
width: img.dimensions?.w,
|
|
221
|
+
height: img.dimensions?.h,
|
|
222
|
+
} satisfies Image)
|
|
223
|
+
),
|
|
224
|
+
];
|
|
126
225
|
|
|
226
|
+
const options =
|
|
227
|
+
(variant.attributes ?? [])
|
|
228
|
+
.filter((attr) => this.isVariantAttributeAnOption(attr))
|
|
229
|
+
.map((attr) => {
|
|
230
|
+
const attrVal = this.parseAttributeValue(attr);
|
|
231
|
+
const optionIdentifier: ProductOptionIdentifier = {
|
|
232
|
+
key: attr.name,
|
|
233
|
+
};
|
|
234
|
+
const option: ProductVariantOption = {
|
|
235
|
+
identifier: optionIdentifier,
|
|
236
|
+
name: attr.name,
|
|
237
|
+
value: {
|
|
238
|
+
identifier: {
|
|
239
|
+
key: attrVal.value,
|
|
240
|
+
option: optionIdentifier,
|
|
241
|
+
},
|
|
242
|
+
label: attrVal.label,
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
return option;
|
|
246
|
+
}) || [];
|
|
127
247
|
|
|
248
|
+
const result = {
|
|
249
|
+
identifier,
|
|
250
|
+
images,
|
|
251
|
+
barcode: '',
|
|
252
|
+
ean: '',
|
|
253
|
+
gtin: '',
|
|
254
|
+
name: product.name[this.context.languageContext.locale],
|
|
255
|
+
options,
|
|
256
|
+
upc: '',
|
|
257
|
+
} satisfies ProductVariant;
|
|
258
|
+
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
protected parseAttribute(attr: CTAttribute): ProductAttribute {
|
|
263
|
+
const result = ProductAttributeSchema.parse({
|
|
264
|
+
identifier: ProductAttributeIdentifierSchema.parse({
|
|
265
|
+
key: attr.name,
|
|
266
|
+
} satisfies Partial<ProductAttributeIdentifier>),
|
|
267
|
+
group: '',
|
|
268
|
+
name: attr.name,
|
|
269
|
+
values: [this.parseAttributeValue(attr)],
|
|
270
|
+
} satisfies Partial<ProductAttribute>);
|
|
271
|
+
|
|
272
|
+
return result;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
protected parseAttributeValue(attr: CTAttribute): ProductAttributeValue {
|
|
276
|
+
let attrValue = '';
|
|
277
|
+
if (attr.value && Array.isArray(attr.value)) {
|
|
278
|
+
attrValue = attr.value[0];
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (attr.value && typeof attr.value === 'object') {
|
|
282
|
+
if (this.context.languageContext.locale in attr.value) {
|
|
283
|
+
attrValue = attr.value[this.context.languageContext.locale];
|
|
284
|
+
} else {
|
|
285
|
+
attrValue = '-';
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (typeof attr.value === 'string') {
|
|
290
|
+
attrValue = attr.value;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const attrVal = ProductAttributeValueSchema.parse({
|
|
294
|
+
identifier: ProductAttributeValueIdentifierSchema.parse({
|
|
295
|
+
key: attrValue,
|
|
296
|
+
} satisfies Partial<ProductAttributeValueIdentifier>),
|
|
297
|
+
value: String(attrValue),
|
|
298
|
+
label: String(attrValue),
|
|
299
|
+
} satisfies Partial<ProductAttributeValue>);
|
|
300
|
+
|
|
301
|
+
return attrVal;
|
|
302
|
+
}
|
|
128
303
|
}
|