@smess/networking 1.0.24 → 1.0.26
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/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/services/customer.service.d.ts +29 -1
- package/dist/services/customer.service.js +35 -0
- package/dist/services/customer.service.test.js +174 -0
- package/dist/{src/services → services}/employee.service.js +4 -3
- package/dist/{src/services → services}/employee.service.test.js +13 -6
- package/dist/services/utils/withAuthReset.d.ts +3 -0
- package/dist/services/utils/withAuthReset.js +8 -0
- package/package.json +1 -1
- package/dist/scripts/dev-test.d.ts +0 -1
- package/dist/scripts/dev-test.js +0 -803
- package/dist/scripts/real-test.d.ts +0 -1
- package/dist/scripts/real-test.js +0 -341
- package/dist/src/core/axios.d.ts +0 -51
- package/dist/src/core/axios.js +0 -65
- package/dist/src/core/axios.observable.d.ts +0 -11
- package/dist/src/core/axios.observable.js +0 -58
- package/dist/src/core/base.provider.advanced.test.d.ts +0 -1
- package/dist/src/core/base.provider.advanced.test.js +0 -29
- package/dist/src/core/base.provider.d.ts +0 -15
- package/dist/src/core/base.provider.js +0 -13
- package/dist/src/core/base.provider.test.d.ts +0 -1
- package/dist/src/core/base.provider.test.js +0 -36
- package/dist/src/core/base.service.wrapper.advanced.test.d.ts +0 -1
- package/dist/src/core/base.service.wrapper.advanced.test.js +0 -39
- package/dist/src/core/base.service.wrapper.d.ts +0 -7
- package/dist/src/core/base.service.wrapper.js +0 -11
- package/dist/src/core/base.service.wrapper.test.d.ts +0 -1
- package/dist/src/core/base.service.wrapper.test.js +0 -25
- package/dist/src/core/rx.http.service.d.ts +0 -51
- package/dist/src/core/rx.http.service.js +0 -65
- package/dist/src/index.d.ts +0 -17
- package/dist/src/index.js +0 -21
- package/dist/src/services/auth.service.d.ts +0 -65
- package/dist/src/services/auth.service.js +0 -197
- package/dist/src/services/auth.service.test.d.ts +0 -1
- package/dist/src/services/auth.service.test.js +0 -334
- package/dist/src/services/cache.service.d.ts +0 -31
- package/dist/src/services/cache.service.js +0 -77
- package/dist/src/services/cache.service.test.d.ts +0 -1
- package/dist/src/services/cache.service.test.js +0 -88
- package/dist/src/services/customer.service.d.ts +0 -71
- package/dist/src/services/customer.service.js +0 -114
- package/dist/src/services/customer.service.test.d.ts +0 -1
- package/dist/src/services/customer.service.test.js +0 -274
- package/dist/src/services/logger.service.d.ts +0 -31
- package/dist/src/services/logger.service.js +0 -93
- package/dist/src/services/logger.service.test.d.ts +0 -1
- package/dist/src/services/logger.service.test.js +0 -31
- package/dist/src/services/order.service.d.ts +0 -73
- package/dist/src/services/order.service.js +0 -97
- package/dist/src/services/order.service.test.d.ts +0 -1
- package/dist/src/services/order.service.test.js +0 -163
- package/dist/src/services/product.service.d.ts +0 -21
- package/dist/src/services/product.service.js +0 -42
- package/dist/src/services/product.service.test.d.ts +0 -1
- package/dist/src/services/product.service.test.js +0 -61
- package/dist/src/services/smess.service.d.ts +0 -36
- package/dist/src/services/smess.service.js +0 -128
- package/dist/src/services/smess.service.test.d.ts +0 -1
- package/dist/src/services/smess.service.test.js +0 -103
- package/dist/src/services/store.cart.service.d.ts +0 -158
- package/dist/src/services/store.cart.service.js +0 -177
- package/dist/src/services/store.cart.service.test.d.ts +0 -1
- package/dist/src/services/store.cart.service.test.js +0 -394
- package/dist/src/services/store.product.service.d.ts +0 -123
- package/dist/src/services/store.product.service.js +0 -102
- package/dist/src/services/store.product.service.test.d.ts +0 -1
- package/dist/src/services/store.product.service.test.js +0 -319
- package/dist/src/services/store.service.d.ts +0 -79
- package/dist/src/services/store.service.js +0 -235
- package/dist/src/services/store.service.test.d.ts +0 -1
- package/dist/src/services/store.service.test.js +0 -274
- package/dist/src/services/user.service.d.ts +0 -16
- package/dist/src/services/user.service.js +0 -37
- package/dist/src/services/user.service.test.d.ts +0 -1
- package/dist/src/services/user.service.test.js +0 -52
- package/dist/src/types/types.d.ts +0 -93
- package/dist/src/types/types.js +0 -82
- package/dist/src/types/types.test.d.ts +0 -1
- package/dist/src/types/types.test.js +0 -25
- package/dist/src/utils/cache-import.d.ts +0 -3
- package/dist/src/utils/cache-import.js +0 -2
- package/dist/src/utils/networking-import.d.ts +0 -4
- package/dist/src/utils/networking-import.js +0 -3
- package/dist/src/utils/rxjs-import.d.ts +0 -5
- package/dist/src/utils/rxjs-import.js +0 -5
- /package/dist/{src/services → services}/employee.service.d.ts +0 -0
- /package/dist/{src/services → services}/employee.service.test.d.ts +0 -0
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { Accept, SMESSHeader, SmessService } from "./smess.service";
|
|
2
|
-
export class OrderProvider {
|
|
3
|
-
constructor(config, target, $authToken) {
|
|
4
|
-
this.config = config;
|
|
5
|
-
this.target = target;
|
|
6
|
-
this.$authToken = $authToken;
|
|
7
|
-
}
|
|
8
|
-
get baseURL() {
|
|
9
|
-
return this.config.version
|
|
10
|
-
? `${this.config.baseUrl}/${this.config.version}`
|
|
11
|
-
: this.config.baseUrl;
|
|
12
|
-
}
|
|
13
|
-
get path() {
|
|
14
|
-
switch (this.target) {
|
|
15
|
-
case "order-list":
|
|
16
|
-
return "/order/list";
|
|
17
|
-
case "order-create":
|
|
18
|
-
return "/order/create";
|
|
19
|
-
case "order-delete":
|
|
20
|
-
return "/order/${id}";
|
|
21
|
-
case "order-get-draft":
|
|
22
|
-
return "/order/get-draft";
|
|
23
|
-
case "order-collection":
|
|
24
|
-
return "/order/collection";
|
|
25
|
-
case "order-update":
|
|
26
|
-
case "order-get":
|
|
27
|
-
default:
|
|
28
|
-
return "/order/${id}";
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
get method() {
|
|
32
|
-
switch (this.target) {
|
|
33
|
-
case "order-list":
|
|
34
|
-
case "order-get":
|
|
35
|
-
case "order-get-draft":
|
|
36
|
-
case "order-collection":
|
|
37
|
-
return "GET";
|
|
38
|
-
case "order-create":
|
|
39
|
-
return "POST";
|
|
40
|
-
case "order-update":
|
|
41
|
-
return "PUT";
|
|
42
|
-
case "order-delete":
|
|
43
|
-
return "DELETE";
|
|
44
|
-
default:
|
|
45
|
-
return "GET";
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
get headers() {
|
|
49
|
-
let headers = SmessService.getHeaders(Accept.V1, this.$authToken.value);
|
|
50
|
-
if (this.config.clientId) {
|
|
51
|
-
headers[SMESSHeader.ClientId] = this.config.clientId;
|
|
52
|
-
}
|
|
53
|
-
if (this.config.apiKey) {
|
|
54
|
-
headers[SMESSHeader.ApiKey] = this.config.apiKey;
|
|
55
|
-
}
|
|
56
|
-
return SmessService.filterHeaders(headers);
|
|
57
|
-
}
|
|
58
|
-
getPath(...queryParams) {
|
|
59
|
-
let definedPath = this.path;
|
|
60
|
-
const params = definedPath.match(/\$\{(.+?)\}/g);
|
|
61
|
-
params?.forEach((value, index) => {
|
|
62
|
-
definedPath = definedPath.replace(value, String(queryParams[index]));
|
|
63
|
-
});
|
|
64
|
-
return definedPath;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
export class OrderService {
|
|
68
|
-
constructor(config, $authToken) {
|
|
69
|
-
this.config = config;
|
|
70
|
-
this.$authToken = $authToken;
|
|
71
|
-
}
|
|
72
|
-
provider(target) {
|
|
73
|
-
const targetWrapper = new OrderProvider(this.config, target, this.$authToken);
|
|
74
|
-
return new SmessService(targetWrapper);
|
|
75
|
-
}
|
|
76
|
-
list() {
|
|
77
|
-
return this.provider("order-list").request();
|
|
78
|
-
}
|
|
79
|
-
create(payload) {
|
|
80
|
-
return this.provider("order-create").request(payload);
|
|
81
|
-
}
|
|
82
|
-
delete(id) {
|
|
83
|
-
return this.provider("order-delete").request(null, undefined, id);
|
|
84
|
-
}
|
|
85
|
-
getDraft() {
|
|
86
|
-
return this.provider("order-get-draft").request();
|
|
87
|
-
}
|
|
88
|
-
update(id, payload) {
|
|
89
|
-
return this.provider("order-update").request(payload, undefined, id);
|
|
90
|
-
}
|
|
91
|
-
get(id) {
|
|
92
|
-
return this.provider("order-get").request(null, undefined, id);
|
|
93
|
-
}
|
|
94
|
-
collection(payload) {
|
|
95
|
-
return this.provider("order-collection").request(payload, undefined, undefined);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import { OrderService, } from "./order.service";
|
|
2
|
-
import { NetworkingUIEventWrapped, AlertModel, } from "../types/types";
|
|
3
|
-
import { of, BehaviorSubject } from "rxjs";
|
|
4
|
-
describe("OrderService", () => {
|
|
5
|
-
const config = {
|
|
6
|
-
baseUrl: "https://api.example.com",
|
|
7
|
-
isProd: false,
|
|
8
|
-
clientId: "test-client-id",
|
|
9
|
-
apiKey: "test-api-key",
|
|
10
|
-
};
|
|
11
|
-
let service;
|
|
12
|
-
let $authToken;
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
$authToken = new BehaviorSubject(null);
|
|
15
|
-
service = new OrderService(config, $authToken);
|
|
16
|
-
});
|
|
17
|
-
it("list success", async () => {
|
|
18
|
-
const response = { data: [{ id: "1", status: "draft" }] };
|
|
19
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
20
|
-
request: () => of(NetworkingUIEventWrapped.succeeded(response)),
|
|
21
|
-
});
|
|
22
|
-
const result = await service.list().toPromise();
|
|
23
|
-
expect(result?.hasSuccess).toBe(true);
|
|
24
|
-
expect(result?.response).toEqual(response);
|
|
25
|
-
});
|
|
26
|
-
it("list error", async () => {
|
|
27
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
28
|
-
request: () => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
|
|
29
|
-
});
|
|
30
|
-
const result = await service.list().toPromise();
|
|
31
|
-
expect(result?.hasFailed).toBe(true);
|
|
32
|
-
expect(result?.alertModel).toBeInstanceOf(AlertModel);
|
|
33
|
-
});
|
|
34
|
-
it("create success", async () => {
|
|
35
|
-
const payload = {
|
|
36
|
-
order_date: "2024-01-01",
|
|
37
|
-
order_items: [],
|
|
38
|
-
};
|
|
39
|
-
const response = { data: { id: "1", status: "created" } };
|
|
40
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
41
|
-
request: () => of(NetworkingUIEventWrapped.succeeded(response)),
|
|
42
|
-
});
|
|
43
|
-
const result = await service.create(payload).toPromise();
|
|
44
|
-
expect(result?.hasSuccess).toBe(true);
|
|
45
|
-
expect(result?.response).toEqual(response);
|
|
46
|
-
});
|
|
47
|
-
it("create error", async () => {
|
|
48
|
-
const payload = {
|
|
49
|
-
order_date: "2024-01-01",
|
|
50
|
-
order_items: [],
|
|
51
|
-
};
|
|
52
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
53
|
-
request: () => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
|
|
54
|
-
});
|
|
55
|
-
const result = await service.create(payload).toPromise();
|
|
56
|
-
expect(result?.hasFailed).toBe(true);
|
|
57
|
-
expect(result?.alertModel).toBeInstanceOf(AlertModel);
|
|
58
|
-
});
|
|
59
|
-
it("delete success", async () => {
|
|
60
|
-
const response = { data: { id: "1", status: "deleted" } };
|
|
61
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
62
|
-
request: (_payload, _mockResponse, id) => of(NetworkingUIEventWrapped.succeeded(response)),
|
|
63
|
-
});
|
|
64
|
-
const result = await service.delete("1").toPromise();
|
|
65
|
-
expect(result?.hasSuccess).toBe(true);
|
|
66
|
-
expect(result?.response).toEqual(response);
|
|
67
|
-
});
|
|
68
|
-
it("delete error", async () => {
|
|
69
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
70
|
-
request: (_payload, _mockResponse, id) => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
|
|
71
|
-
});
|
|
72
|
-
const result = await service.delete("1").toPromise();
|
|
73
|
-
expect(result?.hasFailed).toBe(true);
|
|
74
|
-
expect(result?.alertModel).toBeInstanceOf(AlertModel);
|
|
75
|
-
});
|
|
76
|
-
it("getDraft success", async () => {
|
|
77
|
-
const response = { data: { id: "draft", status: "draft" } };
|
|
78
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
79
|
-
request: () => of(NetworkingUIEventWrapped.succeeded(response)),
|
|
80
|
-
});
|
|
81
|
-
const result = await service.getDraft().toPromise();
|
|
82
|
-
expect(result?.hasSuccess).toBe(true);
|
|
83
|
-
expect(result?.response).toEqual(response);
|
|
84
|
-
});
|
|
85
|
-
it("getDraft error", async () => {
|
|
86
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
87
|
-
request: () => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
|
|
88
|
-
});
|
|
89
|
-
const result = await service.getDraft().toPromise();
|
|
90
|
-
expect(result?.hasFailed).toBe(true);
|
|
91
|
-
expect(result?.alertModel).toBeInstanceOf(AlertModel);
|
|
92
|
-
});
|
|
93
|
-
it("update success", async () => {
|
|
94
|
-
const payload = {
|
|
95
|
-
order_date: "2024-01-01",
|
|
96
|
-
order_items: [],
|
|
97
|
-
};
|
|
98
|
-
const response = { data: { id: "1", status: "updated" } };
|
|
99
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
100
|
-
request: (_payload, _mockResponse, id) => of(NetworkingUIEventWrapped.succeeded(response)),
|
|
101
|
-
});
|
|
102
|
-
const result = await service.update("1", payload).toPromise();
|
|
103
|
-
expect(result?.hasSuccess).toBe(true);
|
|
104
|
-
expect(result?.response).toEqual(response);
|
|
105
|
-
});
|
|
106
|
-
it("update error", async () => {
|
|
107
|
-
const payload = {
|
|
108
|
-
order_date: "2024-01-01",
|
|
109
|
-
order_items: [],
|
|
110
|
-
};
|
|
111
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
112
|
-
request: (_payload, _mockResponse, id) => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
|
|
113
|
-
});
|
|
114
|
-
const result = await service.update("1", payload).toPromise();
|
|
115
|
-
expect(result?.hasFailed).toBe(true);
|
|
116
|
-
expect(result?.alertModel).toBeInstanceOf(AlertModel);
|
|
117
|
-
});
|
|
118
|
-
it("get success", async () => {
|
|
119
|
-
const response = { data: { id: "1", status: "fetched" } };
|
|
120
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
121
|
-
request: (_payload, _mockResponse, id) => of(NetworkingUIEventWrapped.succeeded(response)),
|
|
122
|
-
});
|
|
123
|
-
const result = await service.get("1").toPromise();
|
|
124
|
-
expect(result?.hasSuccess).toBe(true);
|
|
125
|
-
expect(result?.response).toEqual(response);
|
|
126
|
-
});
|
|
127
|
-
it("get error", async () => {
|
|
128
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
129
|
-
request: (_payload, _mockResponse, id) => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
|
|
130
|
-
});
|
|
131
|
-
const result = await service.get("1").toPromise();
|
|
132
|
-
expect(result?.hasFailed).toBe(true);
|
|
133
|
-
expect(result?.alertModel).toBeInstanceOf(AlertModel);
|
|
134
|
-
});
|
|
135
|
-
it("collection success", async () => {
|
|
136
|
-
const payload = {
|
|
137
|
-
page: 1,
|
|
138
|
-
per_page: 10,
|
|
139
|
-
search: "test order",
|
|
140
|
-
status: "pending",
|
|
141
|
-
payment_status: "paid",
|
|
142
|
-
};
|
|
143
|
-
const response = { data: { orders: [], total: 0 } };
|
|
144
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
145
|
-
request: () => of(NetworkingUIEventWrapped.succeeded(response)),
|
|
146
|
-
});
|
|
147
|
-
const result = await service.collection(payload).toPromise();
|
|
148
|
-
expect(result?.hasSuccess).toBe(true);
|
|
149
|
-
expect(result?.response).toEqual(response);
|
|
150
|
-
});
|
|
151
|
-
it("collection error", async () => {
|
|
152
|
-
const payload = {
|
|
153
|
-
page: 1,
|
|
154
|
-
per_page: 10,
|
|
155
|
-
};
|
|
156
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
157
|
-
request: () => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
|
|
158
|
-
});
|
|
159
|
-
const result = await service.collection(payload).toPromise();
|
|
160
|
-
expect(result?.hasFailed).toBe(true);
|
|
161
|
-
expect(result?.alertModel).toBeInstanceOf(AlertModel);
|
|
162
|
-
});
|
|
163
|
-
});
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { BaseProvider } from "../core/base.provider";
|
|
2
|
-
import { BaseServiceWrapper } from "../core/base.service.wrapper";
|
|
3
|
-
import { NetworkingConfig } from "../types/types";
|
|
4
|
-
export interface Product {
|
|
5
|
-
id: number;
|
|
6
|
-
name: string;
|
|
7
|
-
price: number;
|
|
8
|
-
}
|
|
9
|
-
export declare class ProductProvider extends BaseProvider {
|
|
10
|
-
constructor(config: NetworkingConfig, { productId, method }?: {
|
|
11
|
-
productId?: number;
|
|
12
|
-
method?: string;
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
export declare class ProductService extends BaseServiceWrapper<ProductProvider> {
|
|
16
|
-
private config;
|
|
17
|
-
constructor(config: NetworkingConfig, productId?: number, method?: string);
|
|
18
|
-
fetchProductById(id: number): import("rxjs").Observable<import("../types/types").NetworkingUIEventWrapped<Product>>;
|
|
19
|
-
fetchAllProducts(): import("rxjs").Observable<import("../types/types").NetworkingUIEventWrapped<Product[]>>;
|
|
20
|
-
createProduct(product: Omit<Product, "id">): import("rxjs").Observable<import("../types/types").NetworkingUIEventWrapped<Product>>;
|
|
21
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { BaseProvider } from "../core/base.provider";
|
|
2
|
-
import { BaseServiceWrapper } from "../core/base.service.wrapper";
|
|
3
|
-
// ProductProvider implements ProviderType for product endpoints
|
|
4
|
-
export class ProductProvider extends BaseProvider {
|
|
5
|
-
constructor(config, { productId, method } = {}) {
|
|
6
|
-
super({
|
|
7
|
-
config,
|
|
8
|
-
path: productId ? `/products/${productId}` : "/products",
|
|
9
|
-
method: method || "GET",
|
|
10
|
-
headers: { "Content-Type": "application/json" },
|
|
11
|
-
getPath: () => (productId ? `/products/${productId}` : "/products"),
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
// ProductService provides high-level methods for product API
|
|
16
|
-
export class ProductService extends BaseServiceWrapper {
|
|
17
|
-
constructor(config, productId, method) {
|
|
18
|
-
super(new ProductProvider(config, { productId, method }));
|
|
19
|
-
this.config = config;
|
|
20
|
-
}
|
|
21
|
-
// Fetch a product by ID
|
|
22
|
-
fetchProductById(id) {
|
|
23
|
-
this.provider = new ProductProvider(this.config, { productId: id });
|
|
24
|
-
return this.request();
|
|
25
|
-
}
|
|
26
|
-
// Fetch all products
|
|
27
|
-
fetchAllProducts() {
|
|
28
|
-
this.provider = new ProductProvider(this.config);
|
|
29
|
-
return this.request();
|
|
30
|
-
}
|
|
31
|
-
// Create a new product (POST)
|
|
32
|
-
createProduct(product) {
|
|
33
|
-
this.provider = new ProductProvider(this.config, { method: "POST" });
|
|
34
|
-
return this.request(product);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
// Example usage:
|
|
38
|
-
// setNetworkingConfig({ baseUrl: 'https://fakestoreapi.com', isProd: false });
|
|
39
|
-
// const productService = new ProductService();
|
|
40
|
-
// productService.fetchProductById(1).subscribe(result => console.log(result));
|
|
41
|
-
// productService.fetchAllProducts().subscribe(result => console.log(result));
|
|
42
|
-
// productService.createProduct({ name: 'New Product', price: 99.99 }).subscribe(result => console.log(result));
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { ProductService } from "./product.service";
|
|
2
|
-
import { NetworkingUIEventWrapped } from "../types/types";
|
|
3
|
-
import { of, throwError } from "rxjs";
|
|
4
|
-
import { BaseServiceWrapper } from "../core/base.service.wrapper";
|
|
5
|
-
describe("ProductService", () => {
|
|
6
|
-
const config = {
|
|
7
|
-
baseUrl: "https://jsonplaceholder.typicode.com",
|
|
8
|
-
isProd: false,
|
|
9
|
-
clientId: "test-client",
|
|
10
|
-
apiKey: "test-key",
|
|
11
|
-
};
|
|
12
|
-
const product = { id: 1, name: "Product", price: 10 };
|
|
13
|
-
let requestSpy;
|
|
14
|
-
beforeEach(() => {
|
|
15
|
-
requestSpy = jest
|
|
16
|
-
.spyOn(BaseServiceWrapper.prototype, "request")
|
|
17
|
-
.mockImplementation(() => of(NetworkingUIEventWrapped.succeeded(product)));
|
|
18
|
-
});
|
|
19
|
-
afterEach(() => {
|
|
20
|
-
requestSpy.mockRestore();
|
|
21
|
-
});
|
|
22
|
-
it("should fetch product by id", (done) => {
|
|
23
|
-
const service = new ProductService(config, 1);
|
|
24
|
-
service
|
|
25
|
-
.fetchProductById(1)
|
|
26
|
-
.subscribe((result) => {
|
|
27
|
-
expect(result.response).toEqual(product);
|
|
28
|
-
done();
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
it("should fetch all products", (done) => {
|
|
32
|
-
const service = new ProductService(config);
|
|
33
|
-
service
|
|
34
|
-
.fetchAllProducts()
|
|
35
|
-
.subscribe((result) => {
|
|
36
|
-
expect(result.response).toEqual(product);
|
|
37
|
-
done();
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
it("should create a product", (done) => {
|
|
41
|
-
const service = new ProductService(config);
|
|
42
|
-
service
|
|
43
|
-
.createProduct({ name: "New Product", price: 99.99 })
|
|
44
|
-
.subscribe((result) => {
|
|
45
|
-
expect(result.response).toEqual(product);
|
|
46
|
-
done();
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
it("request should handle error", (done) => {
|
|
50
|
-
requestSpy.mockImplementationOnce(() => throwError(() => new Error("Test error")));
|
|
51
|
-
const service = new ProductService(config);
|
|
52
|
-
// Simulate error by calling a method that uses request
|
|
53
|
-
service.fetchProductById(1).subscribe({
|
|
54
|
-
error: (err) => {
|
|
55
|
-
expect(err).toBeInstanceOf(Error);
|
|
56
|
-
expect(err.message).toBe("Test error");
|
|
57
|
-
done();
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
});
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { HeaderType, NetworkingUIEventWrapped, ProviderType } from "../types/types";
|
|
2
|
-
import { Observable } from "../utils/rxjs-import";
|
|
3
|
-
export declare enum SMESSHeader {
|
|
4
|
-
ContentType = "Content-Type",
|
|
5
|
-
Authorization = "Authorization",
|
|
6
|
-
Accept = "Accept",
|
|
7
|
-
ApiKey = "X-Api-Key",
|
|
8
|
-
ClientId = "X-Client-Id"
|
|
9
|
-
}
|
|
10
|
-
export declare enum ContentType {
|
|
11
|
-
Json = "application/json",
|
|
12
|
-
FormURLEncoded = "application/x-www-form-urlencoded"
|
|
13
|
-
}
|
|
14
|
-
export declare enum Accept {
|
|
15
|
-
V1 = "application/json",
|
|
16
|
-
V2 = "application/vnd.smess.v2+json",
|
|
17
|
-
V3 = "application/vnd.smess.v3+json",
|
|
18
|
-
V4 = "application/vnd.smess.v4+json",
|
|
19
|
-
V5 = "application/vnd.smess.v5+json",
|
|
20
|
-
V6 = "application/vnd.smess.v6+json",
|
|
21
|
-
V7 = "application/vnd.smess.v7+json",
|
|
22
|
-
V8 = "application/vnd.smess.v8+json",
|
|
23
|
-
V9 = "application/vnd.smess.v9+json",
|
|
24
|
-
V10 = "application/vnd.smess.v10+json"
|
|
25
|
-
}
|
|
26
|
-
export declare class SmessService<T extends ProviderType> {
|
|
27
|
-
private httpService;
|
|
28
|
-
private target;
|
|
29
|
-
constructor(target: T);
|
|
30
|
-
static getHeaders(version: Accept, authorization?: string | null, options?: HeaderType): Partial<Record<SMESSHeader, string | undefined>>;
|
|
31
|
-
static filterHeaders(headers: Partial<Record<SMESSHeader, string | undefined>>): Record<string, string>;
|
|
32
|
-
private static commonHeaders;
|
|
33
|
-
request<R = unknown>(request?: unknown, mockResponse?: R, ...queryParams: unknown[]): Observable<NetworkingUIEventWrapped<R>>;
|
|
34
|
-
private transformAxiosError;
|
|
35
|
-
private transformAxiosResponse;
|
|
36
|
-
}
|
|
@@ -1,128 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,103 +0,0 @@
|
|
|
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
|
-
});
|