@smess/networking 1.0.23 → 1.0.24

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 (83) hide show
  1. package/dist/scripts/dev-test.d.ts +1 -0
  2. package/dist/scripts/dev-test.js +803 -0
  3. package/dist/scripts/real-test.d.ts +1 -0
  4. package/dist/scripts/real-test.js +341 -0
  5. package/dist/src/core/axios.d.ts +51 -0
  6. package/dist/src/core/axios.js +65 -0
  7. package/dist/src/core/axios.observable.d.ts +11 -0
  8. package/dist/src/core/axios.observable.js +58 -0
  9. package/dist/src/core/base.provider.advanced.test.d.ts +1 -0
  10. package/dist/src/core/base.provider.advanced.test.js +29 -0
  11. package/dist/src/core/base.provider.d.ts +15 -0
  12. package/dist/src/core/base.provider.js +13 -0
  13. package/dist/src/core/base.provider.test.d.ts +1 -0
  14. package/dist/src/core/base.provider.test.js +36 -0
  15. package/dist/src/core/base.service.wrapper.advanced.test.d.ts +1 -0
  16. package/dist/src/core/base.service.wrapper.advanced.test.js +39 -0
  17. package/dist/src/core/base.service.wrapper.d.ts +7 -0
  18. package/dist/src/core/base.service.wrapper.js +11 -0
  19. package/dist/src/core/base.service.wrapper.test.d.ts +1 -0
  20. package/dist/src/core/base.service.wrapper.test.js +25 -0
  21. package/dist/src/core/rx.http.service.d.ts +51 -0
  22. package/dist/src/core/rx.http.service.js +65 -0
  23. package/dist/src/index.d.ts +17 -0
  24. package/dist/src/index.js +21 -0
  25. package/dist/src/services/auth.service.d.ts +65 -0
  26. package/dist/src/services/auth.service.js +197 -0
  27. package/dist/src/services/auth.service.test.d.ts +1 -0
  28. package/dist/src/services/auth.service.test.js +334 -0
  29. package/dist/src/services/cache.service.d.ts +31 -0
  30. package/dist/src/services/cache.service.js +77 -0
  31. package/dist/src/services/cache.service.test.d.ts +1 -0
  32. package/dist/src/services/cache.service.test.js +88 -0
  33. package/dist/src/services/customer.service.d.ts +71 -0
  34. package/dist/src/services/customer.service.js +114 -0
  35. package/dist/src/services/customer.service.test.d.ts +1 -0
  36. package/dist/src/services/customer.service.test.js +274 -0
  37. package/dist/src/services/employee.service.d.ts +73 -0
  38. package/dist/src/services/employee.service.js +73 -0
  39. package/dist/src/services/employee.service.test.d.ts +1 -0
  40. package/dist/src/services/employee.service.test.js +120 -0
  41. package/dist/src/services/logger.service.d.ts +31 -0
  42. package/dist/src/services/logger.service.js +93 -0
  43. package/dist/src/services/logger.service.test.d.ts +1 -0
  44. package/dist/src/services/logger.service.test.js +31 -0
  45. package/dist/src/services/order.service.d.ts +73 -0
  46. package/dist/src/services/order.service.js +97 -0
  47. package/dist/src/services/order.service.test.d.ts +1 -0
  48. package/dist/src/services/order.service.test.js +163 -0
  49. package/dist/src/services/product.service.d.ts +21 -0
  50. package/dist/src/services/product.service.js +42 -0
  51. package/dist/src/services/product.service.test.d.ts +1 -0
  52. package/dist/src/services/product.service.test.js +61 -0
  53. package/dist/src/services/smess.service.d.ts +36 -0
  54. package/dist/src/services/smess.service.js +128 -0
  55. package/dist/src/services/smess.service.test.d.ts +1 -0
  56. package/dist/src/services/smess.service.test.js +103 -0
  57. package/dist/src/services/store.cart.service.d.ts +158 -0
  58. package/dist/src/services/store.cart.service.js +177 -0
  59. package/dist/src/services/store.cart.service.test.d.ts +1 -0
  60. package/dist/src/services/store.cart.service.test.js +394 -0
  61. package/dist/src/services/store.product.service.d.ts +123 -0
  62. package/dist/src/services/store.product.service.js +102 -0
  63. package/dist/src/services/store.product.service.test.d.ts +1 -0
  64. package/dist/src/services/store.product.service.test.js +319 -0
  65. package/dist/src/services/store.service.d.ts +79 -0
  66. package/dist/src/services/store.service.js +235 -0
  67. package/dist/src/services/store.service.test.d.ts +1 -0
  68. package/dist/src/services/store.service.test.js +274 -0
  69. package/dist/src/services/user.service.d.ts +16 -0
  70. package/dist/src/services/user.service.js +37 -0
  71. package/dist/src/services/user.service.test.d.ts +1 -0
  72. package/dist/src/services/user.service.test.js +52 -0
  73. package/dist/src/types/types.d.ts +93 -0
  74. package/dist/src/types/types.js +82 -0
  75. package/dist/src/types/types.test.d.ts +1 -0
  76. package/dist/src/types/types.test.js +25 -0
  77. package/dist/src/utils/cache-import.d.ts +3 -0
  78. package/dist/src/utils/cache-import.js +2 -0
  79. package/dist/src/utils/networking-import.d.ts +4 -0
  80. package/dist/src/utils/networking-import.js +3 -0
  81. package/dist/src/utils/rxjs-import.d.ts +5 -0
  82. package/dist/src/utils/rxjs-import.js +5 -0
  83. package/package.json +1 -1
@@ -0,0 +1,128 @@
1
+ import { AlertButtonModel, AlertModel, NetworkingUIEventWrapped, } from "../types/types";
2
+ import { catchError, mergeMap, of, startWith, } from "../utils/rxjs-import";
3
+ import HttpService from "../core/rx.http.service";
4
+ import { $logger } from "../services/logger.service";
5
+ export var SMESSHeader;
6
+ (function (SMESSHeader) {
7
+ SMESSHeader["ContentType"] = "Content-Type";
8
+ SMESSHeader["Authorization"] = "Authorization";
9
+ SMESSHeader["Accept"] = "Accept";
10
+ SMESSHeader["ApiKey"] = "X-Api-Key";
11
+ SMESSHeader["ClientId"] = "X-Client-Id";
12
+ })(SMESSHeader || (SMESSHeader = {}));
13
+ export var ContentType;
14
+ (function (ContentType) {
15
+ ContentType["Json"] = "application/json";
16
+ ContentType["FormURLEncoded"] = "application/x-www-form-urlencoded";
17
+ })(ContentType || (ContentType = {}));
18
+ export var Accept;
19
+ (function (Accept) {
20
+ Accept["V1"] = "application/json";
21
+ Accept["V2"] = "application/vnd.smess.v2+json";
22
+ Accept["V3"] = "application/vnd.smess.v3+json";
23
+ Accept["V4"] = "application/vnd.smess.v4+json";
24
+ Accept["V5"] = "application/vnd.smess.v5+json";
25
+ Accept["V6"] = "application/vnd.smess.v6+json";
26
+ Accept["V7"] = "application/vnd.smess.v7+json";
27
+ Accept["V8"] = "application/vnd.smess.v8+json";
28
+ Accept["V9"] = "application/vnd.smess.v9+json";
29
+ Accept["V10"] = "application/vnd.smess.v10+json";
30
+ })(Accept || (Accept = {}));
31
+ export class SmessService {
32
+ constructor(target) {
33
+ this.target = target;
34
+ // Set default headers
35
+ $logger.info("SmessService initialized", {
36
+ target: this.target,
37
+ baseURL: this.target.baseURL,
38
+ method: this.target.method,
39
+ headers: this.target.headers,
40
+ }, "SmessService");
41
+ this.httpService = HttpService.create({
42
+ baseURL: this.target.baseURL,
43
+ headers: this.target.headers,
44
+ method: this.target.method,
45
+ });
46
+ this.httpService.interceptors.response.use((response) => response, async (error) => {
47
+ const originalRequest = error.config;
48
+ if (error.response?.status === 401 && !originalRequest._retry) {
49
+ originalRequest._retry = true;
50
+ $logger.info("SmessService constructor - 401 retry", originalRequest, "SmessService");
51
+ // Implement token refresh logic here
52
+ // Then retry the original request
53
+ // return httpService.request(originalRequest);
54
+ }
55
+ return Promise.reject(error);
56
+ });
57
+ }
58
+ static getHeaders(version, authorization = null, options) {
59
+ const headers = this.commonHeaders(version, options);
60
+ if (authorization) {
61
+ headers["Authorization"] = 'Bearer ' + authorization;
62
+ }
63
+ return headers;
64
+ }
65
+ static filterHeaders(headers) {
66
+ const filteredHeaders = Object.fromEntries(Object.entries(headers).filter(([_, value]) => value !== undefined && value !== null && value !== ""));
67
+ return filteredHeaders;
68
+ }
69
+ static commonHeaders(version, options) {
70
+ const { shouldAddClientIdHeader = true } = options ?? {};
71
+ const headers = {
72
+ Accept: version,
73
+ Authorization: "",
74
+ "Content-Type": ContentType.Json,
75
+ "X-Api-Key": "",
76
+ };
77
+ return headers;
78
+ }
79
+ // MARK: - Public
80
+ request(request, mockResponse, ...queryParams) {
81
+ const url = queryParams.length
82
+ ? this.target.getPath(...queryParams)
83
+ : this.target.path;
84
+ const config = { url };
85
+ let observable;
86
+ if (this.target.method === "GET") {
87
+ if (request) {
88
+ config.data = request;
89
+ config.params = request;
90
+ }
91
+ observable = this.httpService.get(url, config);
92
+ }
93
+ else if (this.target.method === "POST") {
94
+ observable = this.httpService.post(url, request, config);
95
+ }
96
+ else if (this.target.method === "PATCH") {
97
+ observable = this.httpService.patch(url, request, config);
98
+ }
99
+ else if (this.target.method === "PUT") {
100
+ observable = this.httpService.put(url, request, config);
101
+ }
102
+ else {
103
+ observable = this.httpService.request(config);
104
+ }
105
+ return observable.pipe(catchError((error) => this.transformAxiosError(error, mockResponse)), mergeMap((data) => this.transformAxiosResponse(data)), startWith(NetworkingUIEventWrapped.waiting()));
106
+ }
107
+ // MARK: - Private
108
+ transformAxiosError(error, mockResponse) {
109
+ $logger.error("transformAxiosError", error, "SmessService");
110
+ const message = error instanceof Error ? error.message : String(error);
111
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Required for axios compatibility
112
+ const response = error.response;
113
+ const httpErrorCode = response ? response.status : 0;
114
+ const title = response?.statusText;
115
+ const alertModel = new AlertModel(message, httpErrorCode, true, title, response?.data, [AlertButtonModel.buttonYes()]);
116
+ if (mockResponse) {
117
+ return of(NetworkingUIEventWrapped.succeeded(mockResponse));
118
+ }
119
+ return of(NetworkingUIEventWrapped.failed(alertModel));
120
+ }
121
+ transformAxiosResponse(response) {
122
+ $logger.info("transformAxiosResponse", response, "SmessService");
123
+ if (response instanceof NetworkingUIEventWrapped) {
124
+ return of(response);
125
+ }
126
+ return of(NetworkingUIEventWrapped.succeeded(response.data));
127
+ }
128
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,103 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any -- Required for test mocks */
2
+ import { SmessService } from "./smess.service";
3
+ import { NetworkingUIEventWrapped, } from "../types/types";
4
+ import { of, throwError } from "rxjs";
5
+ describe("SmessService", () => {
6
+ const config = {
7
+ baseUrl: "https://api.example.com",
8
+ isProd: false,
9
+ clientId: "test-client",
10
+ apiKey: "test-key",
11
+ };
12
+ const provider = {
13
+ baseURL: config.baseUrl,
14
+ path: "/resource",
15
+ method: "GET",
16
+ headers: {},
17
+ getPath: () => "/resource",
18
+ };
19
+ it("should return succeeded for GET", (done) => {
20
+ const service = new SmessService(provider);
21
+ service["httpService"] = {
22
+ get: jest.fn(() => of({ data: { foo: "bar" } })),
23
+ post: jest.fn(),
24
+ patch: jest.fn(),
25
+ put: jest.fn(),
26
+ request: jest.fn(),
27
+ interceptors: { response: { use: jest.fn() } },
28
+ };
29
+ service.request().subscribe((result) => {
30
+ if (result.hasSuccess) {
31
+ expect(result.response).toEqual({ foo: "bar" });
32
+ done();
33
+ }
34
+ });
35
+ });
36
+ it("should return succeeded for POST", (done) => {
37
+ const postProvider = { ...provider, method: "POST" };
38
+ const service = new SmessService(postProvider);
39
+ service["httpService"] = {
40
+ get: jest.fn(),
41
+ post: jest.fn(() => of({ data: { foo: "baz" } })),
42
+ patch: jest.fn(),
43
+ put: jest.fn(),
44
+ request: jest.fn(),
45
+ interceptors: { response: { use: jest.fn() } },
46
+ };
47
+ service
48
+ .request({ some: "data" })
49
+ .subscribe((result) => {
50
+ if (result.hasSuccess) {
51
+ expect(result.response).toEqual({ foo: "baz" });
52
+ done();
53
+ }
54
+ });
55
+ });
56
+ it("should handle error", (done) => {
57
+ const service = new SmessService(provider);
58
+ service["httpService"] = {
59
+ get: jest.fn(() => throwError(() => new Error("Test error"))),
60
+ post: jest.fn(),
61
+ patch: jest.fn(),
62
+ put: jest.fn(),
63
+ request: jest.fn(),
64
+ interceptors: { response: { use: jest.fn() } },
65
+ };
66
+ service.request().subscribe((result) => {
67
+ if (result.hasFailed) {
68
+ expect(result).toBeInstanceOf(NetworkingUIEventWrapped);
69
+ expect(result.hasFailed).toBe(true);
70
+ expect(result.alertModel).toBeTruthy();
71
+ expect(result.alertModel?.message).toBe("Test error");
72
+ done();
73
+ }
74
+ });
75
+ });
76
+ it("should return mock response if provided", (done) => {
77
+ const service = new SmessService(provider);
78
+ service["httpService"] = {
79
+ get: jest.fn(() => throwError(() => new Error("Test error"))),
80
+ post: jest.fn(),
81
+ patch: jest.fn(),
82
+ put: jest.fn(),
83
+ request: jest.fn((payload, mockResponse) => {
84
+ if (payload === "error") {
85
+ return throwError(() => new Error("Test error"));
86
+ }
87
+ if (mockResponse !== undefined) {
88
+ return of(NetworkingUIEventWrapped.succeeded(mockResponse));
89
+ }
90
+ return of({ data: { foo: "mock" } });
91
+ }),
92
+ interceptors: { response: { use: jest.fn() } },
93
+ };
94
+ service
95
+ .request(undefined, { foo: "mock" })
96
+ .subscribe((result) => {
97
+ if (result.hasSuccess) {
98
+ expect(result.response).toEqual({ foo: "mock" });
99
+ done();
100
+ }
101
+ });
102
+ });
103
+ });
@@ -0,0 +1,158 @@
1
+ import { ProviderType, NetworkingConfig, NetworkingUIEventWrapped, HttpResponse } from "../types/types";
2
+ import { SmessService } from "./smess.service";
3
+ import { BehaviorSubject, Observable } from "rxjs";
4
+ export type StoreCartTarget = "cart-get" | "cart-update" | "cart-add-item" | "cart-update-item" | "cart-delete-item" | "cart-delete-all-items" | "cart-batch-items";
5
+ export interface BillingInformation {
6
+ email?: string;
7
+ phone_number?: string;
8
+ street?: string;
9
+ city?: string;
10
+ postal_code?: string;
11
+ state?: string;
12
+ country?: string;
13
+ [key: string]: unknown;
14
+ }
15
+ export interface ShippingInformation {
16
+ email?: string;
17
+ phone_number?: string;
18
+ street?: string;
19
+ city?: string;
20
+ postal_code?: string;
21
+ state?: string;
22
+ country?: string;
23
+ [key: string]: unknown;
24
+ }
25
+ export interface CartItem {
26
+ [key: string]: unknown;
27
+ }
28
+ export interface StoreInfo {
29
+ [key: string]: unknown;
30
+ }
31
+ export interface Cart {
32
+ dateCreated: string;
33
+ dateModified: string;
34
+ discountTotal: number;
35
+ discountTax: number;
36
+ shippingTotal: number;
37
+ shippingTax: number;
38
+ cartTax: number;
39
+ total: number;
40
+ totalTax: number;
41
+ customerId: string | null;
42
+ billingInformation: BillingInformation | null;
43
+ shippingInformation: ShippingInformation | null;
44
+ paymentMethod: string;
45
+ deliveryType: string;
46
+ transactionId: string | null;
47
+ customerIpAddress: string;
48
+ customerUserAgent: string;
49
+ customerNote: string | null;
50
+ cartHash: string;
51
+ cartItems: CartItem[];
52
+ paymentUrl: string | null;
53
+ store: StoreInfo | null;
54
+ [key: string]: unknown;
55
+ }
56
+ export interface CartUpdateRequest {
57
+ status?: string;
58
+ customer_note?: string;
59
+ customer_id?: string;
60
+ payment_method?: string;
61
+ delivery_type?: string;
62
+ billing_information?: BillingInformation;
63
+ shipping_information?: ShippingInformation;
64
+ [key: string]: unknown;
65
+ }
66
+ export interface CartAddItemRequest {
67
+ product_id: string;
68
+ quantity: number;
69
+ [key: string]: unknown;
70
+ }
71
+ export interface CartUpdateItemQuantityRequest {
72
+ quantity: number;
73
+ }
74
+ export interface CartBatchItemsRequest {
75
+ create?: Array<{
76
+ product_id: string;
77
+ quantity: number;
78
+ [key: string]: unknown;
79
+ }>;
80
+ update?: Array<{
81
+ product_id: string;
82
+ quantity: number;
83
+ [key: string]: unknown;
84
+ }>;
85
+ [key: string]: unknown;
86
+ }
87
+ export declare class StoreCartProvider implements ProviderType {
88
+ private readonly config;
89
+ private readonly target;
90
+ private readonly $cartHash;
91
+ private readonly $storeId;
92
+ constructor(config: NetworkingConfig, target: StoreCartTarget, $cartHash: BehaviorSubject<string | null>, $storeId: BehaviorSubject<string | null>);
93
+ get baseURL(): string;
94
+ get path(): string;
95
+ get method(): string;
96
+ get headers(): Record<string, string>;
97
+ getPath(...queryParams: unknown[]): string;
98
+ }
99
+ export interface StoreCartServiceType {
100
+ getCart(): Observable<NetworkingUIEventWrapped<HttpResponse>>;
101
+ updateCart(payload: CartUpdateRequest): Observable<NetworkingUIEventWrapped<HttpResponse>>;
102
+ addItem(payload: CartAddItemRequest): Observable<NetworkingUIEventWrapped<HttpResponse>>;
103
+ batchItems(payload: CartBatchItemsRequest): Observable<NetworkingUIEventWrapped<HttpResponse>>;
104
+ updateItemQuantity(itemId: string, payload: CartUpdateItemQuantityRequest): Observable<NetworkingUIEventWrapped<HttpResponse>>;
105
+ deleteItem(itemId: string): Observable<NetworkingUIEventWrapped<HttpResponse>>;
106
+ deleteAllItems(): Observable<NetworkingUIEventWrapped<HttpResponse>>;
107
+ }
108
+ export declare class StoreCartService implements StoreCartServiceType {
109
+ private readonly config;
110
+ private readonly $cartHash;
111
+ private readonly $storeId;
112
+ constructor(config: NetworkingConfig, $storeId?: BehaviorSubject<string | null>);
113
+ get currentCartHash(): string | null;
114
+ get cartHash$(): BehaviorSubject<string | null>;
115
+ get currentStoreId(): string | null;
116
+ get storeId$(): BehaviorSubject<string | null>;
117
+ setStoreId(storeId: string | null): void;
118
+ provider(target: StoreCartTarget): SmessService<StoreCartProvider>;
119
+ /**
120
+ * Update the cart hash from cart response
121
+ */
122
+ private updateCartHash;
123
+ /**
124
+ * Get the current cart
125
+ * GET api/v1/store/cart
126
+ */
127
+ getCart(): Observable<NetworkingUIEventWrapped<HttpResponse>>;
128
+ /**
129
+ * Update cart (e.g., add notes, apply coupon)
130
+ * PUT api/v1/store/cart
131
+ */
132
+ updateCart(payload: CartUpdateRequest): Observable<NetworkingUIEventWrapped<HttpResponse>>;
133
+ /**
134
+ * Add item to cart
135
+ * POST api/v1/store/cart/items
136
+ */
137
+ addItem(payload: CartAddItemRequest): Observable<NetworkingUIEventWrapped<HttpResponse>>;
138
+ /**
139
+ * Batch create/update items in cart
140
+ * POST api/v1/store/cart/items/batch
141
+ */
142
+ batchItems(payload: CartBatchItemsRequest): Observable<NetworkingUIEventWrapped<HttpResponse>>;
143
+ /**
144
+ * Update item quantity in cart
145
+ * PUT api/v1/store/cart/items/{id}
146
+ */
147
+ updateItemQuantity(itemId: string, payload: CartUpdateItemQuantityRequest): Observable<NetworkingUIEventWrapped<HttpResponse>>;
148
+ /**
149
+ * Delete item from cart
150
+ * DELETE api/v1/store/cart/items/{id}
151
+ */
152
+ deleteItem(itemId: string): Observable<NetworkingUIEventWrapped<HttpResponse>>;
153
+ /**
154
+ * Delete all items from cart
155
+ * DELETE api/v1/store/cart/items
156
+ */
157
+ deleteAllItems(): Observable<NetworkingUIEventWrapped<HttpResponse>>;
158
+ }
@@ -0,0 +1,177 @@
1
+ import { Accept, SMESSHeader, SmessService } from "./smess.service";
2
+ import { BehaviorSubject } from "rxjs";
3
+ import { tap } from "rxjs/operators";
4
+ export class StoreCartProvider {
5
+ constructor(config, target, $cartHash, $storeId) {
6
+ this.config = config;
7
+ this.target = target;
8
+ this.$cartHash = $cartHash;
9
+ this.$storeId = $storeId;
10
+ }
11
+ get baseURL() {
12
+ return this.config.version
13
+ ? `${this.config.baseUrl}/${this.config.version}`
14
+ : this.config.baseUrl;
15
+ }
16
+ get path() {
17
+ switch (this.target) {
18
+ case "cart-get":
19
+ return "/store/cart";
20
+ case "cart-update":
21
+ return "/store/cart";
22
+ case "cart-add-item":
23
+ return "/store/cart/items";
24
+ case "cart-update-item":
25
+ return "/store/cart/items/\${id}";
26
+ case "cart-delete-item":
27
+ return "/store/cart/items/\${id}";
28
+ case "cart-delete-all-items":
29
+ return "/store/cart/items";
30
+ case "cart-batch-items":
31
+ return "/store/cart/items/batch";
32
+ default:
33
+ return "/store/cart";
34
+ }
35
+ }
36
+ get method() {
37
+ switch (this.target) {
38
+ case "cart-get":
39
+ return "GET";
40
+ case "cart-update":
41
+ return "PUT";
42
+ case "cart-add-item":
43
+ return "POST";
44
+ case "cart-update-item":
45
+ return "PUT";
46
+ case "cart-delete-item":
47
+ return "DELETE";
48
+ case "cart-delete-all-items":
49
+ return "DELETE";
50
+ case "cart-batch-items":
51
+ return "POST";
52
+ default:
53
+ return "GET";
54
+ }
55
+ }
56
+ get headers() {
57
+ let headers = SmessService.getHeaders(Accept.V1, null);
58
+ if (this.config.clientId) {
59
+ headers[SMESSHeader.ClientId] = this.config.clientId;
60
+ }
61
+ if (this.config.apiKey) {
62
+ headers[SMESSHeader.ApiKey] = this.config.apiKey;
63
+ }
64
+ const filteredHeaders = SmessService.filterHeaders(headers);
65
+ // Add X-Nonce-Token header if cartHash is available
66
+ if (this.$cartHash.value) {
67
+ filteredHeaders["X-Nonce-Token"] = this.$cartHash.value;
68
+ }
69
+ // Add X-Store-Id header if storeId is available
70
+ if (this.$storeId.value) {
71
+ filteredHeaders["X-Store-Id"] = this.$storeId.value;
72
+ }
73
+ return filteredHeaders;
74
+ }
75
+ getPath(...queryParams) {
76
+ let definedPath = this.path;
77
+ const params = definedPath.match(/\$\{(.+?)\}/g);
78
+ params?.forEach((value, index) => {
79
+ definedPath = definedPath.replace(value, String(queryParams[index]));
80
+ });
81
+ return definedPath;
82
+ }
83
+ }
84
+ export class StoreCartService {
85
+ constructor(config, $storeId) {
86
+ this.config = config;
87
+ this.$cartHash = new BehaviorSubject(null);
88
+ // Use the provided BehaviorSubject or create a new one
89
+ this.$storeId = $storeId || new BehaviorSubject(null);
90
+ }
91
+ // Getter to access the current cart hash
92
+ get currentCartHash() {
93
+ return this.$cartHash.value;
94
+ }
95
+ // Getter to access the cart hash as observable
96
+ get cartHash$() {
97
+ return this.$cartHash;
98
+ }
99
+ // Getter to access the current store ID
100
+ get currentStoreId() {
101
+ return this.$storeId.value;
102
+ }
103
+ // Getter to access the store ID as observable
104
+ get storeId$() {
105
+ return this.$storeId;
106
+ }
107
+ // Method to update the store ID
108
+ setStoreId(storeId) {
109
+ this.$storeId.next(storeId);
110
+ }
111
+ provider(target) {
112
+ const targetWrapper = new StoreCartProvider(this.config, target, this.$cartHash, this.$storeId);
113
+ return new SmessService(targetWrapper);
114
+ }
115
+ /**
116
+ * Update the cart hash from cart response
117
+ */
118
+ updateCartHash(result) {
119
+ if (result.hasSuccess && result.response) {
120
+ const response = result.response;
121
+ // Try to extract cartHash from different possible structures
122
+ const cartHash = response.data?.cartHash || response.cartHash;
123
+ if (cartHash) {
124
+ this.$cartHash.next(cartHash);
125
+ }
126
+ }
127
+ }
128
+ /**
129
+ * Get the current cart
130
+ * GET api/v1/store/cart
131
+ */
132
+ getCart() {
133
+ return this.provider("cart-get").request().pipe(tap((result) => this.updateCartHash(result)));
134
+ }
135
+ /**
136
+ * Update cart (e.g., add notes, apply coupon)
137
+ * PUT api/v1/store/cart
138
+ */
139
+ updateCart(payload) {
140
+ return this.provider("cart-update").request(payload).pipe(tap((result) => this.updateCartHash(result)));
141
+ }
142
+ /**
143
+ * Add item to cart
144
+ * POST api/v1/store/cart/items
145
+ */
146
+ addItem(payload) {
147
+ return this.provider("cart-add-item").request(payload).pipe(tap((result) => this.updateCartHash(result)));
148
+ }
149
+ /**
150
+ * Batch create/update items in cart
151
+ * POST api/v1/store/cart/items/batch
152
+ */
153
+ batchItems(payload) {
154
+ return this.provider("cart-batch-items").request(payload).pipe(tap((result) => this.updateCartHash(result)));
155
+ }
156
+ /**
157
+ * Update item quantity in cart
158
+ * PUT api/v1/store/cart/items/{id}
159
+ */
160
+ updateItemQuantity(itemId, payload) {
161
+ return this.provider("cart-update-item").request(payload, undefined, itemId).pipe(tap((result) => this.updateCartHash(result)));
162
+ }
163
+ /**
164
+ * Delete item from cart
165
+ * DELETE api/v1/store/cart/items/{id}
166
+ */
167
+ deleteItem(itemId) {
168
+ return this.provider("cart-delete-item").request(undefined, undefined, itemId);
169
+ }
170
+ /**
171
+ * Delete all items from cart
172
+ * DELETE api/v1/store/cart/items
173
+ */
174
+ deleteAllItems() {
175
+ return this.provider("cart-delete-all-items").request();
176
+ }
177
+ }
@@ -0,0 +1 @@
1
+ export {};