@reactionary/core 0.1.12 → 0.2.1
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/cache/memory-cache.js +1 -1
- package/decorators/reactionary.decorator.js +50 -16
- package/package.json +1 -1
- package/providers/base.provider.js +0 -15
- package/providers/cart.provider.js +0 -39
- package/providers/inventory.provider.js +1 -8
- package/providers/order.provider.js +0 -1
- package/providers/price.provider.js +0 -7
- package/providers/product.provider.js +0 -7
- package/schemas/errors/generic.error.js +8 -0
- package/schemas/errors/index.js +4 -0
- package/schemas/errors/invalid-input.error.js +8 -0
- package/schemas/errors/invalid-output.error.js +8 -0
- package/schemas/errors/not-found.error.js +8 -0
- package/schemas/index.js +2 -0
- package/schemas/models/base.model.js +1 -14
- package/schemas/mutations/cart.mutation.js +1 -1
- package/schemas/mutations/checkout.mutation.js +4 -4
- package/schemas/result.js +51 -0
- package/src/cache/memory-cache.d.ts +2 -1
- package/src/decorators/reactionary.decorator.d.ts +3 -2
- package/src/providers/base.provider.d.ts +0 -3
- package/src/providers/cart.provider.d.ts +11 -10
- package/src/providers/category.provider.d.ts +6 -6
- package/src/providers/checkout.provider.d.ts +11 -9
- package/src/providers/identity.provider.d.ts +5 -4
- package/src/providers/inventory.provider.d.ts +3 -1
- package/src/providers/order.provider.d.ts +3 -1
- package/src/providers/price.provider.d.ts +3 -3
- package/src/providers/product-search.provider.d.ts +3 -3
- package/src/providers/product.provider.d.ts +5 -3
- package/src/providers/profile.provider.d.ts +3 -2
- package/src/providers/store.provider.d.ts +2 -1
- package/src/schemas/errors/generic.error.d.ts +7 -0
- package/src/schemas/errors/index.d.ts +4 -0
- package/src/schemas/errors/invalid-input.error.d.ts +7 -0
- package/src/schemas/errors/invalid-output.error.d.ts +7 -0
- package/src/schemas/errors/not-found.error.d.ts +7 -0
- package/src/schemas/index.d.ts +2 -0
- package/src/schemas/models/analytics.model.d.ts +1 -9
- package/src/schemas/models/base.model.d.ts +1 -29
- package/src/schemas/models/cart.model.d.ts +0 -7
- package/src/schemas/models/category.model.d.ts +0 -21
- package/src/schemas/models/checkout.model.d.ts +0 -35
- package/src/schemas/models/identity.model.d.ts +0 -42
- package/src/schemas/models/inventory.model.d.ts +0 -7
- package/src/schemas/models/order.model.d.ts +0 -28
- package/src/schemas/models/payment.model.d.ts +0 -14
- package/src/schemas/models/price.model.d.ts +0 -7
- package/src/schemas/models/product-search.model.d.ts +0 -21
- package/src/schemas/models/product.model.d.ts +0 -7
- package/src/schemas/models/profile.model.d.ts +0 -35
- package/src/schemas/models/shipping-method.model.d.ts +0 -14
- package/src/schemas/models/store.model.d.ts +0 -7
- package/src/schemas/mutations/cart.mutation.d.ts +2 -16
- package/src/schemas/mutations/checkout.mutation.d.ts +1 -8
- package/src/schemas/queries/product-search.query.d.ts +0 -7
- package/src/schemas/result.d.ts +55 -0
package/cache/memory-cache.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getReactionaryCacheMeter } from "../metrics/metrics.js";
|
|
2
|
+
import { error, NotFoundErrorSchema, success } from "../schemas/index.js";
|
|
2
3
|
class MemoryCache {
|
|
3
4
|
constructor() {
|
|
4
5
|
this.entries = new Array();
|
|
@@ -13,7 +14,6 @@ class MemoryCache {
|
|
|
13
14
|
return null;
|
|
14
15
|
}
|
|
15
16
|
const parsed = schema.parse(c.value);
|
|
16
|
-
parsed.meta.cache.hit = true;
|
|
17
17
|
this.meter.hits.add(1, {
|
|
18
18
|
"labels.cache_type": "memory"
|
|
19
19
|
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { trace } from "@opentelemetry/api";
|
|
2
2
|
import { getReactionaryMeter } from "../metrics/metrics.js";
|
|
3
|
+
import { error, success } from "../schemas/result.js";
|
|
3
4
|
const TRACER_NAME = "@reactionary";
|
|
4
5
|
const TRACER_VERSION = "0.0.1";
|
|
5
6
|
let globalTracer = null;
|
|
@@ -56,26 +57,43 @@ function Reactionary(options) {
|
|
|
56
57
|
meter.requestInProgress.add(1, attributes);
|
|
57
58
|
try {
|
|
58
59
|
const input = validateInput(args[0], configuration.inputSchema);
|
|
59
|
-
|
|
60
|
+
if (!input.success) {
|
|
61
|
+
return input;
|
|
62
|
+
}
|
|
63
|
+
const cacheKey = this.generateCacheKeyForQuery(scope, input.value);
|
|
60
64
|
const fromCache = await this.cache.get(
|
|
61
65
|
cacheKey,
|
|
62
66
|
options.inputSchema
|
|
63
67
|
);
|
|
64
|
-
let result
|
|
65
|
-
if (!
|
|
66
|
-
result = await original.apply(this, [input]);
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
let result;
|
|
69
|
+
if (!fromCache) {
|
|
70
|
+
result = await original.apply(this, [input.value]);
|
|
71
|
+
const r = result;
|
|
72
|
+
if (r.success) {
|
|
73
|
+
const dependencyIds = this.generateDependencyIdsForModel(r.value);
|
|
74
|
+
this.cache.put(cacheKey, r.value, {
|
|
75
|
+
ttlSeconds: configuration.cacheTimeToLiveInSeconds,
|
|
76
|
+
dependencyIds
|
|
77
|
+
});
|
|
78
|
+
}
|
|
72
79
|
} else {
|
|
80
|
+
result = success(fromCache);
|
|
73
81
|
cacheStatus = "hit";
|
|
74
82
|
}
|
|
75
|
-
|
|
83
|
+
const validatedResult = validateOutput(result, configuration.outputSchema);
|
|
84
|
+
if (validatedResult.success) {
|
|
85
|
+
validatedResult.meta.cache.hit = !!fromCache;
|
|
86
|
+
validatedResult.meta.cache.key = cacheKey;
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
76
89
|
} catch (err) {
|
|
90
|
+
console.error(err);
|
|
77
91
|
status = "error";
|
|
78
|
-
|
|
92
|
+
const result = error({
|
|
93
|
+
type: "Generic",
|
|
94
|
+
message: "REDACTED"
|
|
95
|
+
});
|
|
96
|
+
return result;
|
|
79
97
|
} finally {
|
|
80
98
|
const duration = performance.now() - startTime;
|
|
81
99
|
const finalAttributes = {
|
|
@@ -94,17 +112,33 @@ function Reactionary(options) {
|
|
|
94
112
|
}
|
|
95
113
|
function validateInput(input, schema) {
|
|
96
114
|
if (!schema) {
|
|
97
|
-
return input;
|
|
115
|
+
return success(input);
|
|
116
|
+
}
|
|
117
|
+
let validated = success(input);
|
|
118
|
+
const parse = schema.safeParse(input);
|
|
119
|
+
if (!parse.success) {
|
|
120
|
+
validated = error({
|
|
121
|
+
type: "InvalidInput",
|
|
122
|
+
error: parse.error
|
|
123
|
+
});
|
|
98
124
|
}
|
|
99
|
-
|
|
100
|
-
return parsed;
|
|
125
|
+
return validated;
|
|
101
126
|
}
|
|
102
127
|
function validateOutput(output, schema) {
|
|
103
128
|
if (!schema) {
|
|
104
129
|
return output;
|
|
105
130
|
}
|
|
106
|
-
|
|
107
|
-
|
|
131
|
+
let validated = output;
|
|
132
|
+
if (output.success) {
|
|
133
|
+
const parse = schema.safeParse(output.value);
|
|
134
|
+
if (!parse.success) {
|
|
135
|
+
validated = error({
|
|
136
|
+
type: "InvalidOutput",
|
|
137
|
+
error: parse.error
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return validated;
|
|
108
142
|
}
|
|
109
143
|
async function traceSpan(name, fn) {
|
|
110
144
|
const tracer = getTracer();
|
package/package.json
CHANGED
|
@@ -19,21 +19,6 @@ class BaseProvider {
|
|
|
19
19
|
const queryHash = h.hash(query);
|
|
20
20
|
return `${scope}:${queryHash}`;
|
|
21
21
|
}
|
|
22
|
-
generateCacheKeyPaginatedResult(resultSetName, res) {
|
|
23
|
-
const type = this.getResourceName();
|
|
24
|
-
const langPart = this.context.languageContext.locale;
|
|
25
|
-
const currencyPart = this.context.languageContext.currencyCode || "default";
|
|
26
|
-
const storePart = this.context.storeIdentifier?.key || "default";
|
|
27
|
-
return `${type}-${resultSetName}-paginated|pageNumber:${res.pageNumber}|pageSize:${res.pageSize}|store:${storePart}|lang:${langPart}|currency:${currencyPart}`;
|
|
28
|
-
}
|
|
29
|
-
generateCacheKeySingle(identifier) {
|
|
30
|
-
const type = this.getResourceName();
|
|
31
|
-
const idPart = Object.entries(identifier).map(([k, v]) => `${k}:${v}`).join("#");
|
|
32
|
-
const langPart = this.context.languageContext.locale;
|
|
33
|
-
const currencyPart = this.context.languageContext.currencyCode || "default";
|
|
34
|
-
const storePart = this.context.storeIdentifier?.key || "default";
|
|
35
|
-
return `${type}-${idPart}|store:${storePart}|lang:${langPart}|currency:${currencyPart}`;
|
|
36
|
-
}
|
|
37
22
|
}
|
|
38
23
|
export {
|
|
39
24
|
BaseProvider
|
|
@@ -1,44 +1,5 @@
|
|
|
1
1
|
import { BaseProvider } from "./base.provider.js";
|
|
2
2
|
class CartProvider extends BaseProvider {
|
|
3
|
-
createEmptyCart() {
|
|
4
|
-
const cart = {
|
|
5
|
-
identifier: { key: "" },
|
|
6
|
-
meta: { placeholder: true, cache: { hit: true, key: "empty-cart" } },
|
|
7
|
-
description: "",
|
|
8
|
-
items: [],
|
|
9
|
-
name: "",
|
|
10
|
-
price: {
|
|
11
|
-
grandTotal: {
|
|
12
|
-
value: 0,
|
|
13
|
-
currency: "XXX"
|
|
14
|
-
},
|
|
15
|
-
totalDiscount: {
|
|
16
|
-
value: 0,
|
|
17
|
-
currency: "XXX"
|
|
18
|
-
},
|
|
19
|
-
totalProductPrice: {
|
|
20
|
-
value: 0,
|
|
21
|
-
currency: "XXX"
|
|
22
|
-
},
|
|
23
|
-
totalShipping: {
|
|
24
|
-
value: 0,
|
|
25
|
-
currency: "XXX"
|
|
26
|
-
},
|
|
27
|
-
totalSurcharge: {
|
|
28
|
-
value: 0,
|
|
29
|
-
currency: "XXX"
|
|
30
|
-
},
|
|
31
|
-
totalTax: {
|
|
32
|
-
value: 0,
|
|
33
|
-
currency: "XXX"
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
userId: {
|
|
37
|
-
userId: ""
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
return cart;
|
|
41
|
-
}
|
|
42
3
|
getResourceName() {
|
|
43
4
|
return "cart";
|
|
44
5
|
}
|
|
@@ -7,14 +7,7 @@ class InventoryProvider extends BaseProvider {
|
|
|
7
7
|
const inventory = {
|
|
8
8
|
identifier: key,
|
|
9
9
|
quantity: 0,
|
|
10
|
-
status: "outOfStock"
|
|
11
|
-
meta: {
|
|
12
|
-
cache: {
|
|
13
|
-
hit: false,
|
|
14
|
-
key: this.generateCacheKeySingle(key)
|
|
15
|
-
},
|
|
16
|
-
placeholder: true
|
|
17
|
-
}
|
|
10
|
+
status: "outOfStock"
|
|
18
11
|
};
|
|
19
12
|
return inventory;
|
|
20
13
|
}
|
|
@@ -19,13 +19,6 @@ class PriceProvider extends BaseProvider {
|
|
|
19
19
|
unitPrice: {
|
|
20
20
|
value: -1,
|
|
21
21
|
currency: this.context.languageContext.currencyCode
|
|
22
|
-
},
|
|
23
|
-
meta: {
|
|
24
|
-
cache: {
|
|
25
|
-
hit: false,
|
|
26
|
-
key: `price-${sku}-${this.context.languageContext.currencyCode}`
|
|
27
|
-
},
|
|
28
|
-
placeholder: true
|
|
29
22
|
}
|
|
30
23
|
};
|
|
31
24
|
return price;
|
package/schemas/index.js
CHANGED
|
@@ -1,22 +1,11 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
const
|
|
3
|
-
hit: z.boolean().default(false),
|
|
4
|
-
key: z.string().default("")
|
|
5
|
-
});
|
|
6
|
-
const MetaSchema = z.looseObject({
|
|
7
|
-
cache: CacheInformationSchema.default(() => CacheInformationSchema.parse({})),
|
|
8
|
-
placeholder: z.boolean().default(false).describe("Whether or not the entity exists in a remote system, or is a default placeholder.")
|
|
9
|
-
});
|
|
10
|
-
const BaseModelSchema = z.looseObject({
|
|
11
|
-
meta: MetaSchema.default(() => MetaSchema.parse({}))
|
|
12
|
-
});
|
|
2
|
+
const BaseModelSchema = z.looseObject({});
|
|
13
3
|
const PaginationOptionsSchema = z.looseObject({
|
|
14
4
|
pageNumber: z.number().default(1).describe("Current page number, starting from 1"),
|
|
15
5
|
pageSize: z.number().min(1).max(50).default(20).describe("Number of items per page")
|
|
16
6
|
});
|
|
17
7
|
function createPaginatedResponseSchema(itemSchema) {
|
|
18
8
|
return z.object({
|
|
19
|
-
meta: MetaSchema.default(() => MetaSchema.parse({})),
|
|
20
9
|
pageNumber: z.number().min(1).describe("Current page number, starting from 1"),
|
|
21
10
|
pageSize: z.number().min(1).describe("Number of items per page"),
|
|
22
11
|
totalCount: z.number().min(0).describe("Total number of items available"),
|
|
@@ -32,9 +21,7 @@ const ImageSchema = z.looseObject({
|
|
|
32
21
|
});
|
|
33
22
|
export {
|
|
34
23
|
BaseModelSchema,
|
|
35
|
-
CacheInformationSchema,
|
|
36
24
|
ImageSchema,
|
|
37
|
-
MetaSchema,
|
|
38
25
|
PaginationOptionsSchema,
|
|
39
26
|
createPaginatedResponseSchema
|
|
40
27
|
};
|
|
@@ -5,7 +5,7 @@ import { AddressSchema } from "../models/profile.model.js";
|
|
|
5
5
|
import { CurrencySchema } from "../models/currency.model.js";
|
|
6
6
|
import { MonetaryAmountSchema } from "../models/price.model.js";
|
|
7
7
|
const CartMutationItemAddSchema = BaseMutationSchema.extend({
|
|
8
|
-
cart: CartIdentifierSchema,
|
|
8
|
+
cart: CartIdentifierSchema.optional(),
|
|
9
9
|
variant: ProductVariantIdentifierSchema,
|
|
10
10
|
quantity: z.number()
|
|
11
11
|
});
|
|
@@ -3,19 +3,19 @@ import { CartIdentifierSchema, AddressSchema, PaymentInstructionIdentifierSchema
|
|
|
3
3
|
import { BaseMutationSchema } from "./base.mutation.js";
|
|
4
4
|
const CheckoutMutationInitiateCheckoutSchema = BaseMutationSchema.extend({
|
|
5
5
|
cart: CartSchema,
|
|
6
|
-
billingAddress: AddressSchema.omit({ identifier: true
|
|
6
|
+
billingAddress: AddressSchema.omit({ identifier: 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,
|
|
12
|
-
shippingAddress: AddressSchema.omit({ identifier: true
|
|
12
|
+
shippingAddress: AddressSchema.omit({ identifier: true })
|
|
13
13
|
});
|
|
14
14
|
const CheckoutMutationFinalizeCheckoutSchema = BaseMutationSchema.extend({
|
|
15
15
|
checkout: CartIdentifierSchema
|
|
16
16
|
});
|
|
17
17
|
const CheckoutMutationAddPaymentInstructionSchema = BaseMutationSchema.extend({
|
|
18
|
-
paymentInstruction: PaymentInstructionSchema.omit({
|
|
18
|
+
paymentInstruction: PaymentInstructionSchema.omit({ status: true, identifier: true }),
|
|
19
19
|
checkout: CartIdentifierSchema
|
|
20
20
|
});
|
|
21
21
|
const CheckoutMutationRemovePaymentInstructionSchema = BaseMutationSchema.extend({
|
|
@@ -23,7 +23,7 @@ const CheckoutMutationRemovePaymentInstructionSchema = BaseMutationSchema.extend
|
|
|
23
23
|
checkout: CartIdentifierSchema
|
|
24
24
|
});
|
|
25
25
|
const CheckoutMutationSetShippingInstructionSchema = BaseMutationSchema.extend({
|
|
26
|
-
shippingInstruction: ShippingInstructionSchema
|
|
26
|
+
shippingInstruction: ShippingInstructionSchema,
|
|
27
27
|
checkout: CartIdentifierSchema
|
|
28
28
|
});
|
|
29
29
|
export {
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { trace } from "@opentelemetry/api";
|
|
2
|
+
function assertSuccess(result) {
|
|
3
|
+
if (!result.success) {
|
|
4
|
+
throw new Error(
|
|
5
|
+
`Expected Result.success = true, but got false. Error: ${result.error}`
|
|
6
|
+
);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function assertError(result) {
|
|
10
|
+
if (result.success) {
|
|
11
|
+
throw new Error(`Expected failure but got success: ${result.value}`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function unwrapValue(result) {
|
|
15
|
+
assertSuccess(result);
|
|
16
|
+
return result.value;
|
|
17
|
+
}
|
|
18
|
+
function unwrapError(result) {
|
|
19
|
+
assertError(result);
|
|
20
|
+
return result.error;
|
|
21
|
+
}
|
|
22
|
+
function success(value) {
|
|
23
|
+
return {
|
|
24
|
+
success: true,
|
|
25
|
+
value,
|
|
26
|
+
meta: {
|
|
27
|
+
trace: trace.getActiveSpan()?.spanContext().traceId || "",
|
|
28
|
+
cache: {
|
|
29
|
+
hit: false,
|
|
30
|
+
key: ""
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function error(error2) {
|
|
36
|
+
return {
|
|
37
|
+
success: false,
|
|
38
|
+
error: error2,
|
|
39
|
+
meta: {
|
|
40
|
+
trace: trace.getActiveSpan()?.spanContext().traceId || ""
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
assertError,
|
|
46
|
+
assertSuccess,
|
|
47
|
+
error,
|
|
48
|
+
success,
|
|
49
|
+
unwrapError,
|
|
50
|
+
unwrapValue
|
|
51
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { BaseModel } from '../schemas/models/index.js';
|
|
2
|
+
import type { Result } from '../schemas/result.js';
|
|
2
3
|
import type { Cache, CacheEntryOptions } from './cache.interface.js';
|
|
3
4
|
import type z from 'zod';
|
|
4
5
|
/**
|
|
@@ -13,7 +14,7 @@ export declare class MemoryCache implements Cache {
|
|
|
13
14
|
}[];
|
|
14
15
|
protected meter: import("../metrics/metrics.js").ReactionaryCacheMetrics;
|
|
15
16
|
get<T extends BaseModel>(key: string, schema: z.ZodType<T>): Promise<T | null>;
|
|
16
|
-
put(key: string, value: unknown
|
|
17
|
+
put(key: string, value: Result<unknown>, options: CacheEntryOptions): Promise<void>;
|
|
17
18
|
invalidate(dependencyIds: Array<string>): Promise<void>;
|
|
18
19
|
clear(): Promise<void>;
|
|
19
20
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Tracer } from '@opentelemetry/api';
|
|
2
2
|
import type { z } from 'zod';
|
|
3
3
|
import type { BaseProvider } from '../providers/index.js';
|
|
4
|
+
import { type Result } from '../schemas/result.js';
|
|
4
5
|
export declare function getTracer(): Tracer;
|
|
5
6
|
/**
|
|
6
7
|
* The options associated with annotating a provider function and marking
|
|
@@ -46,11 +47,11 @@ export declare function Reactionary(options: Partial<ReactionaryDecoratorOptions
|
|
|
46
47
|
/**
|
|
47
48
|
* Utility function to handle input validation.
|
|
48
49
|
*/
|
|
49
|
-
export declare function validateInput(input:
|
|
50
|
+
export declare function validateInput<T>(input: T, schema: z.ZodType | undefined): Result<T>;
|
|
50
51
|
/**
|
|
51
52
|
* Utility function to handle output validation.
|
|
52
53
|
*/
|
|
53
|
-
export declare function validateOutput(output:
|
|
54
|
+
export declare function validateOutput(output: Result<unknown>, schema: z.ZodType | undefined): Result<unknown>;
|
|
54
55
|
/**
|
|
55
56
|
* Utility function to wrap entry / exit into decorated functions in a
|
|
56
57
|
* traced span for OTEL handling.
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { Cache } from '../cache/cache.interface.js';
|
|
2
2
|
import { type RequestContext } from '../schemas/session.schema.js';
|
|
3
|
-
import type { IdentifierType } from '../schemas/models/identifiers.model.js';
|
|
4
3
|
/**
|
|
5
4
|
* Base capability provider, responsible for mutations (changes) and queries (fetches)
|
|
6
5
|
* for a given business object domain.
|
|
@@ -11,8 +10,6 @@ export declare abstract class BaseProvider {
|
|
|
11
10
|
constructor(cache: Cache, context: RequestContext);
|
|
12
11
|
generateDependencyIdsForModel(model: unknown): Array<string>;
|
|
13
12
|
protected generateCacheKeyForQuery(scope: string, query: object): string;
|
|
14
|
-
protected generateCacheKeyPaginatedResult(resultSetName: string, res: any): string;
|
|
15
|
-
protected generateCacheKeySingle(identifier: IdentifierType): string;
|
|
16
13
|
/**
|
|
17
14
|
* Returns the abstract resource name provided by the remote system.
|
|
18
15
|
*/
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import type { NotFoundError } from "../schemas/index.js";
|
|
1
2
|
import type { Cart } from "../schemas/models/cart.model.js";
|
|
2
3
|
import type { CartIdentifier } from "../schemas/models/identifiers.model.js";
|
|
3
4
|
import type { CartMutationApplyCoupon, CartMutationChangeCurrency, CartMutationDeleteCart, CartMutationItemAdd, CartMutationItemQuantityChange, CartMutationItemRemove, CartMutationRemoveCoupon } from "../schemas/mutations/cart.mutation.js";
|
|
4
5
|
import type { CartQueryById } from "../schemas/queries/cart.query.js";
|
|
6
|
+
import type { Result } from "../schemas/result.js";
|
|
5
7
|
import { BaseProvider } from "./base.provider.js";
|
|
6
8
|
export declare abstract class CartProvider extends BaseProvider {
|
|
7
9
|
/**
|
|
@@ -11,14 +13,14 @@ export declare abstract class CartProvider extends BaseProvider {
|
|
|
11
13
|
* @param payload
|
|
12
14
|
* @param session
|
|
13
15
|
*/
|
|
14
|
-
abstract getById(payload: CartQueryById): Promise<Cart
|
|
16
|
+
abstract getById(payload: CartQueryById): Promise<Result<Cart, NotFoundError>>;
|
|
15
17
|
/**
|
|
16
18
|
* Get the active cart id for the user.
|
|
17
19
|
*
|
|
18
20
|
* Usecase: Most common usecase during site load, or after login. You want to get the active cart for the user, so you can display it in the minicart.
|
|
19
21
|
* @param session
|
|
20
22
|
*/
|
|
21
|
-
abstract getActiveCartId(): Promise<CartIdentifier
|
|
23
|
+
abstract getActiveCartId(): Promise<Result<CartIdentifier, NotFoundError>>;
|
|
22
24
|
/**
|
|
23
25
|
* Add item to cart. If no cart exists, create a new one. Returns the updated and recalculated cart.
|
|
24
26
|
* Does not automatically consolidate items, so if you want to have second add of same item to increase quantity,
|
|
@@ -28,7 +30,7 @@ export declare abstract class CartProvider extends BaseProvider {
|
|
|
28
30
|
* @param payload
|
|
29
31
|
* @param session
|
|
30
32
|
*/
|
|
31
|
-
abstract add(payload: CartMutationItemAdd): Promise<Cart
|
|
33
|
+
abstract add(payload: CartMutationItemAdd): Promise<Result<Cart>>;
|
|
32
34
|
/**
|
|
33
35
|
* Remove item from cart. If the cart is empty after removal, delete the cart. Returns the updated and recalculated cart.
|
|
34
36
|
*
|
|
@@ -36,7 +38,7 @@ export declare abstract class CartProvider extends BaseProvider {
|
|
|
36
38
|
* @param payload
|
|
37
39
|
* @param session
|
|
38
40
|
*/
|
|
39
|
-
abstract remove(payload: CartMutationItemRemove): Promise<Cart
|
|
41
|
+
abstract remove(payload: CartMutationItemRemove): Promise<Result<Cart>>;
|
|
40
42
|
/**
|
|
41
43
|
* Change quantity of item in cart. If the cart is empty after change, delete the cart. Returns the updated and recalculated cart.
|
|
42
44
|
* Changing quantity to 0 is not allowed. Use the remove call instead. This is done to avoid accidental removal of item.
|
|
@@ -46,7 +48,7 @@ export declare abstract class CartProvider extends BaseProvider {
|
|
|
46
48
|
* @param payload
|
|
47
49
|
* @param session
|
|
48
50
|
*/
|
|
49
|
-
abstract changeQuantity(payload: CartMutationItemQuantityChange): Promise<Cart
|
|
51
|
+
abstract changeQuantity(payload: CartMutationItemQuantityChange): Promise<Result<Cart>>;
|
|
50
52
|
/**
|
|
51
53
|
* Deletes the entire cart.
|
|
52
54
|
*
|
|
@@ -54,7 +56,7 @@ export declare abstract class CartProvider extends BaseProvider {
|
|
|
54
56
|
* @param payload
|
|
55
57
|
* @param session
|
|
56
58
|
*/
|
|
57
|
-
abstract deleteCart(payload: CartMutationDeleteCart): Promise<
|
|
59
|
+
abstract deleteCart(payload: CartMutationDeleteCart): Promise<Result<void>>;
|
|
58
60
|
/**
|
|
59
61
|
* Applies a coupon code to the cart. Returns the updated and recalculated cart.
|
|
60
62
|
*
|
|
@@ -62,7 +64,7 @@ export declare abstract class CartProvider extends BaseProvider {
|
|
|
62
64
|
* @param payload
|
|
63
65
|
* @param session
|
|
64
66
|
*/
|
|
65
|
-
abstract applyCouponCode(payload: CartMutationApplyCoupon): Promise<Cart
|
|
67
|
+
abstract applyCouponCode(payload: CartMutationApplyCoupon): Promise<Result<Cart>>;
|
|
66
68
|
/**
|
|
67
69
|
* Removes a coupon code from the cart. Returns the updated and recalculated cart.
|
|
68
70
|
*
|
|
@@ -70,7 +72,7 @@ export declare abstract class CartProvider extends BaseProvider {
|
|
|
70
72
|
* @param payload
|
|
71
73
|
* @param session
|
|
72
74
|
*/
|
|
73
|
-
abstract removeCouponCode(payload: CartMutationRemoveCoupon): Promise<Cart
|
|
75
|
+
abstract removeCouponCode(payload: CartMutationRemoveCoupon): Promise<Result<Cart>>;
|
|
74
76
|
/**
|
|
75
77
|
* Changes the currency of the cart.
|
|
76
78
|
*
|
|
@@ -78,7 +80,6 @@ export declare abstract class CartProvider extends BaseProvider {
|
|
|
78
80
|
* @param newCurrency
|
|
79
81
|
* @param session
|
|
80
82
|
*/
|
|
81
|
-
abstract changeCurrency(payload: CartMutationChangeCurrency): Promise<Cart
|
|
82
|
-
protected createEmptyCart(): Cart;
|
|
83
|
+
abstract changeCurrency(payload: CartMutationChangeCurrency): Promise<Result<Cart>>;
|
|
83
84
|
protected getResourceName(): string;
|
|
84
85
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Category, CategoryPaginatedResult } from "../schemas/index.js";
|
|
1
|
+
import type { Category, CategoryPaginatedResult, NotFoundError, Result } from "../schemas/index.js";
|
|
2
2
|
import type { CategoryQueryById, CategoryQueryBySlug, CategoryQueryForBreadcrumb, CategoryQueryForChildCategories, CategoryQueryForTopCategories } from "../schemas/queries/category.query.js";
|
|
3
3
|
import { BaseProvider } from "./base.provider.js";
|
|
4
4
|
/**
|
|
@@ -25,7 +25,7 @@ export declare abstract class CategoryProvider extends BaseProvider {
|
|
|
25
25
|
* @param id
|
|
26
26
|
* @param session
|
|
27
27
|
*/
|
|
28
|
-
abstract getById(payload: CategoryQueryById): Promise<Category
|
|
28
|
+
abstract getById(payload: CategoryQueryById): Promise<Result<Category, NotFoundError>>;
|
|
29
29
|
/**
|
|
30
30
|
* Gets a single category by its seo slug
|
|
31
31
|
*
|
|
@@ -33,7 +33,7 @@ export declare abstract class CategoryProvider extends BaseProvider {
|
|
|
33
33
|
* @param slug the slug
|
|
34
34
|
* @param session
|
|
35
35
|
*/
|
|
36
|
-
abstract getBySlug(payload: CategoryQueryBySlug): Promise<Category
|
|
36
|
+
abstract getBySlug(payload: CategoryQueryBySlug): Promise<Result<Category, NotFoundError>>;
|
|
37
37
|
/**
|
|
38
38
|
* Gets the breadcrumb path to the category, i.e. all parents up to the root.
|
|
39
39
|
* The returned order is from root to leaf.
|
|
@@ -42,7 +42,7 @@ export declare abstract class CategoryProvider extends BaseProvider {
|
|
|
42
42
|
* @param id
|
|
43
43
|
* @param session
|
|
44
44
|
*/
|
|
45
|
-
abstract getBreadcrumbPathToCategory(payload: CategoryQueryForBreadcrumb): Promise<Category[]
|
|
45
|
+
abstract getBreadcrumbPathToCategory(payload: CategoryQueryForBreadcrumb): Promise<Result<Category[]>>;
|
|
46
46
|
/**
|
|
47
47
|
* Finds all child categories of a given category.
|
|
48
48
|
*
|
|
@@ -53,7 +53,7 @@ export declare abstract class CategoryProvider extends BaseProvider {
|
|
|
53
53
|
* @param id The ID of the parent category.
|
|
54
54
|
* @param session The session information.
|
|
55
55
|
*/
|
|
56
|
-
abstract findChildCategories(payload: CategoryQueryForChildCategories): Promise<CategoryPaginatedResult
|
|
56
|
+
abstract findChildCategories(payload: CategoryQueryForChildCategories): Promise<Result<CategoryPaginatedResult>>;
|
|
57
57
|
/**
|
|
58
58
|
* Returns all top categories, i.e. categories without a parent.
|
|
59
59
|
*
|
|
@@ -61,6 +61,6 @@ export declare abstract class CategoryProvider extends BaseProvider {
|
|
|
61
61
|
* @param paginationOptions
|
|
62
62
|
* @param session
|
|
63
63
|
*/
|
|
64
|
-
abstract findTopCategories(payload: CategoryQueryForTopCategories): Promise<CategoryPaginatedResult
|
|
64
|
+
abstract findTopCategories(payload: CategoryQueryForTopCategories): Promise<Result<CategoryPaginatedResult>>;
|
|
65
65
|
protected getResourceName(): string;
|
|
66
66
|
}
|