@smess/networking 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -25
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- package/dist/scripts/real-test.js +188 -15
- package/dist/src/core/base.service.wrapper.js +1 -1
- package/dist/src/core/base.service.wrapper.test.js +1 -1
- package/dist/src/services/auth.service.d.ts +57 -0
- package/dist/src/services/auth.service.js +96 -0
- package/dist/src/services/auth.service.test.d.ts +1 -0
- package/dist/src/services/auth.service.test.js +131 -0
- package/dist/src/services/customer.service.d.ts +43 -0
- package/dist/src/services/customer.service.js +75 -0
- package/dist/src/services/customer.service.test.d.ts +1 -0
- package/dist/src/services/customer.service.test.js +98 -0
- package/dist/src/services/order.service.d.ts +60 -0
- package/dist/src/services/order.service.js +94 -0
- package/dist/src/services/order.service.test.d.ts +1 -0
- package/dist/src/services/order.service.test.js +137 -0
- package/dist/src/services/product.service.d.ts +21 -0
- package/dist/src/services/product.service.js +47 -0
- package/dist/src/services/product.service.test.d.ts +1 -0
- package/dist/src/services/product.service.test.js +54 -0
- package/dist/src/services/smess.service.d.ts +36 -0
- package/dist/src/services/smess.service.js +136 -0
- package/dist/src/services/smess.service.test.d.ts +1 -0
- package/dist/src/services/smess.service.test.js +102 -0
- package/dist/src/services/store.service.d.ts +39 -0
- package/dist/src/services/store.service.js +78 -0
- package/dist/src/services/store.service.test.d.ts +1 -0
- package/dist/src/services/store.service.test.js +106 -0
- package/dist/src/services/user.service.d.ts +16 -0
- package/dist/src/services/user.service.js +39 -0
- package/dist/src/services/user.service.test.d.ts +1 -0
- package/dist/src/services/user.service.test.js +47 -0
- package/dist/src/types/types.d.ts +1 -3
- package/package.json +1 -1
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ProviderType, NetworkingConfig, NetworkingUIEventWrapped, HttpResponse } from '../types/types';
|
|
2
|
+
import { SmessService } from './smess.service';
|
|
3
|
+
import { BehaviorSubject, Observable } from 'rxjs';
|
|
4
|
+
export type CustomerTarget = 'register' | 'get-profile' | 'update-profile';
|
|
5
|
+
export interface CustomerRegisterRequest {
|
|
6
|
+
client_id?: string;
|
|
7
|
+
email: string;
|
|
8
|
+
password: string;
|
|
9
|
+
phone_number?: string;
|
|
10
|
+
name?: string;
|
|
11
|
+
address?: string;
|
|
12
|
+
tax_identification_number?: string | null;
|
|
13
|
+
}
|
|
14
|
+
export interface CustomerUpdateRequest {
|
|
15
|
+
name: string;
|
|
16
|
+
address: string;
|
|
17
|
+
postal_code: string;
|
|
18
|
+
city: string;
|
|
19
|
+
state: string | null;
|
|
20
|
+
country: string | null;
|
|
21
|
+
contact_email: string | null;
|
|
22
|
+
phone_number: string | null;
|
|
23
|
+
}
|
|
24
|
+
export declare class CustomerProvider implements ProviderType {
|
|
25
|
+
private readonly config;
|
|
26
|
+
private readonly target;
|
|
27
|
+
private readonly $authToken;
|
|
28
|
+
constructor(config: NetworkingConfig, target: CustomerTarget, $authToken: BehaviorSubject<string | null>);
|
|
29
|
+
get baseURL(): string;
|
|
30
|
+
get path(): string;
|
|
31
|
+
get method(): string;
|
|
32
|
+
get headers(): Record<string, string>;
|
|
33
|
+
getPath(...queryParams: unknown[]): string;
|
|
34
|
+
}
|
|
35
|
+
export declare class CustomerService {
|
|
36
|
+
private readonly config;
|
|
37
|
+
private readonly $authToken;
|
|
38
|
+
constructor(config: NetworkingConfig, $authToken: BehaviorSubject<string | null>);
|
|
39
|
+
provider(target: CustomerTarget): SmessService<CustomerProvider>;
|
|
40
|
+
register(payload: CustomerRegisterRequest): Observable<NetworkingUIEventWrapped<HttpResponse>>;
|
|
41
|
+
getProfile(): Observable<NetworkingUIEventWrapped<HttpResponse>>;
|
|
42
|
+
updateProfile(payload: CustomerUpdateRequest): Observable<NetworkingUIEventWrapped<HttpResponse>>;
|
|
43
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CustomerService = exports.CustomerProvider = void 0;
|
|
4
|
+
const smess_service_1 = require("./smess.service");
|
|
5
|
+
class CustomerProvider {
|
|
6
|
+
constructor(config, target, $authToken) {
|
|
7
|
+
this.config = config;
|
|
8
|
+
this.target = target;
|
|
9
|
+
this.$authToken = $authToken;
|
|
10
|
+
}
|
|
11
|
+
get baseURL() {
|
|
12
|
+
return this.config.version ? `${this.config.baseUrl}/${this.config.version}` : this.config.baseUrl;
|
|
13
|
+
}
|
|
14
|
+
get path() {
|
|
15
|
+
switch (this.target) {
|
|
16
|
+
case 'register':
|
|
17
|
+
return '/authentication/customer/register';
|
|
18
|
+
case 'get-profile':
|
|
19
|
+
return '/authentication/customer/get-profile';
|
|
20
|
+
case 'update-profile':
|
|
21
|
+
return '/authentication/customer/update';
|
|
22
|
+
default:
|
|
23
|
+
return '/authentication/customer';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
get method() {
|
|
27
|
+
switch (this.target) {
|
|
28
|
+
case 'get-profile':
|
|
29
|
+
return 'GET';
|
|
30
|
+
case 'update-profile':
|
|
31
|
+
return 'PUT';
|
|
32
|
+
default:
|
|
33
|
+
return 'POST';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
get headers() {
|
|
37
|
+
let headers = smess_service_1.SmessService.getHeaders(smess_service_1.Accept.V1, this.$authToken.value);
|
|
38
|
+
if (this.config.clientId) {
|
|
39
|
+
headers[smess_service_1.SMESSHeader.ClientId] = this.config.clientId;
|
|
40
|
+
}
|
|
41
|
+
if (this.config.apiKey) {
|
|
42
|
+
headers[smess_service_1.SMESSHeader.ApiKey] = this.config.apiKey;
|
|
43
|
+
}
|
|
44
|
+
return smess_service_1.SmessService.filterHeaders(headers);
|
|
45
|
+
}
|
|
46
|
+
getPath(...queryParams) {
|
|
47
|
+
let definedPath = this.path;
|
|
48
|
+
const params = definedPath.match(/\$\{(.+?)\}/g);
|
|
49
|
+
params === null || params === void 0 ? void 0 : params.forEach((value, index) => {
|
|
50
|
+
definedPath = definedPath.replace(value, String(queryParams[index]));
|
|
51
|
+
});
|
|
52
|
+
return definedPath;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.CustomerProvider = CustomerProvider;
|
|
56
|
+
class CustomerService {
|
|
57
|
+
constructor(config, $authToken) {
|
|
58
|
+
this.config = config;
|
|
59
|
+
this.$authToken = $authToken;
|
|
60
|
+
}
|
|
61
|
+
provider(target) {
|
|
62
|
+
const targetWrapper = new CustomerProvider(this.config, target, this.$authToken);
|
|
63
|
+
return new smess_service_1.SmessService(targetWrapper);
|
|
64
|
+
}
|
|
65
|
+
register(payload) {
|
|
66
|
+
return this.provider('register').request(payload);
|
|
67
|
+
}
|
|
68
|
+
getProfile() {
|
|
69
|
+
return this.provider('get-profile').request();
|
|
70
|
+
}
|
|
71
|
+
updateProfile(payload) {
|
|
72
|
+
return this.provider('update-profile').request(payload);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.CustomerService = CustomerService;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const customer_service_1 = require("./customer.service");
|
|
4
|
+
const types_1 = require("../types/types");
|
|
5
|
+
const rxjs_1 = require("rxjs");
|
|
6
|
+
describe('CustomerService', () => {
|
|
7
|
+
const config = {
|
|
8
|
+
baseUrl: 'https://api.example.com',
|
|
9
|
+
isProd: false,
|
|
10
|
+
clientId: 'test-client-id',
|
|
11
|
+
apiKey: 'test-api-key',
|
|
12
|
+
};
|
|
13
|
+
let service;
|
|
14
|
+
let $authToken;
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
$authToken = new rxjs_1.BehaviorSubject(null);
|
|
17
|
+
service = new customer_service_1.CustomerService(config, $authToken);
|
|
18
|
+
});
|
|
19
|
+
it('register success', async () => {
|
|
20
|
+
const payload = {
|
|
21
|
+
email: 'test@example.com',
|
|
22
|
+
password: 'pass',
|
|
23
|
+
};
|
|
24
|
+
const response = { data: { id: 1, email: 'test@example.com' } };
|
|
25
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
26
|
+
request: () => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.succeeded(response)),
|
|
27
|
+
});
|
|
28
|
+
const result = await service.register(payload).toPromise();
|
|
29
|
+
expect(result === null || result === void 0 ? void 0 : result.hasSuccess).toBe(true);
|
|
30
|
+
expect(result === null || result === void 0 ? void 0 : result.response).toEqual(response);
|
|
31
|
+
});
|
|
32
|
+
it('register error', async () => {
|
|
33
|
+
const payload = {
|
|
34
|
+
email: 'test@example.com',
|
|
35
|
+
password: 'pass',
|
|
36
|
+
};
|
|
37
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
38
|
+
request: () => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.failed(types_1.AlertModel.default())),
|
|
39
|
+
});
|
|
40
|
+
const result = await service.register(payload).toPromise();
|
|
41
|
+
expect(result === null || result === void 0 ? void 0 : result.hasFailed).toBe(true);
|
|
42
|
+
expect(result === null || result === void 0 ? void 0 : result.alertModel).toBeInstanceOf(types_1.AlertModel);
|
|
43
|
+
});
|
|
44
|
+
it('getProfile success', async () => {
|
|
45
|
+
const response = { data: { id: 1, email: 'test@example.com' } };
|
|
46
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
47
|
+
request: () => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.succeeded(response)),
|
|
48
|
+
});
|
|
49
|
+
const result = await service.getProfile().toPromise();
|
|
50
|
+
expect(result === null || result === void 0 ? void 0 : result.hasSuccess).toBe(true);
|
|
51
|
+
expect(result === null || result === void 0 ? void 0 : result.response).toEqual(response);
|
|
52
|
+
});
|
|
53
|
+
it('getProfile error', async () => {
|
|
54
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
55
|
+
request: () => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.failed(types_1.AlertModel.default())),
|
|
56
|
+
});
|
|
57
|
+
const result = await service.getProfile().toPromise();
|
|
58
|
+
expect(result === null || result === void 0 ? void 0 : result.hasFailed).toBe(true);
|
|
59
|
+
expect(result === null || result === void 0 ? void 0 : result.alertModel).toBeInstanceOf(types_1.AlertModel);
|
|
60
|
+
});
|
|
61
|
+
it('updateProfile success', async () => {
|
|
62
|
+
const payload = {
|
|
63
|
+
name: 'Test',
|
|
64
|
+
address: '123 Main St',
|
|
65
|
+
postal_code: '12345',
|
|
66
|
+
city: 'City',
|
|
67
|
+
state: null,
|
|
68
|
+
country: null,
|
|
69
|
+
contact_email: null,
|
|
70
|
+
phone_number: null,
|
|
71
|
+
};
|
|
72
|
+
const response = { data: { id: 1, name: 'Test' } };
|
|
73
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
74
|
+
request: () => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.succeeded(response)),
|
|
75
|
+
});
|
|
76
|
+
const result = await service.updateProfile(payload).toPromise();
|
|
77
|
+
expect(result === null || result === void 0 ? void 0 : result.hasSuccess).toBe(true);
|
|
78
|
+
expect(result === null || result === void 0 ? void 0 : result.response).toEqual(response);
|
|
79
|
+
});
|
|
80
|
+
it('updateProfile error', async () => {
|
|
81
|
+
const payload = {
|
|
82
|
+
name: 'Test',
|
|
83
|
+
address: '123 Main St',
|
|
84
|
+
postal_code: '12345',
|
|
85
|
+
city: 'City',
|
|
86
|
+
state: null,
|
|
87
|
+
country: null,
|
|
88
|
+
contact_email: null,
|
|
89
|
+
phone_number: null,
|
|
90
|
+
};
|
|
91
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
92
|
+
request: () => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.failed(types_1.AlertModel.default())),
|
|
93
|
+
});
|
|
94
|
+
const result = await service.updateProfile(payload).toPromise();
|
|
95
|
+
expect(result === null || result === void 0 ? void 0 : result.hasFailed).toBe(true);
|
|
96
|
+
expect(result === null || result === void 0 ? void 0 : result.alertModel).toBeInstanceOf(types_1.AlertModel);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { ProviderType, NetworkingConfig, NetworkingUIEventWrapped, HttpResponse } from '../types/types';
|
|
2
|
+
import { SmessService } from './smess.service';
|
|
3
|
+
import { BehaviorSubject, Observable } from 'rxjs';
|
|
4
|
+
export type OrderTarget = 'order-list' | 'order-create' | 'order-delete' | 'order-get-draft' | 'order-update' | 'order-get';
|
|
5
|
+
export interface CreateOrderRequest extends UpdateOrderRequest {
|
|
6
|
+
}
|
|
7
|
+
export interface UpdateOrderRequest {
|
|
8
|
+
external_id?: string;
|
|
9
|
+
order_date: string;
|
|
10
|
+
type?: string;
|
|
11
|
+
status?: string;
|
|
12
|
+
payment_status?: string;
|
|
13
|
+
delivery_note?: string;
|
|
14
|
+
delivery_fee?: number;
|
|
15
|
+
payment_method?: string;
|
|
16
|
+
payment_period?: string;
|
|
17
|
+
discount_in_percent?: number;
|
|
18
|
+
due_payment_amount?: number;
|
|
19
|
+
total_gst?: number;
|
|
20
|
+
total_exclude_gst?: number;
|
|
21
|
+
total_include_gst?: number;
|
|
22
|
+
order_items: OrderItemRequest[];
|
|
23
|
+
}
|
|
24
|
+
export interface OrderItemRequest {
|
|
25
|
+
product_name: string;
|
|
26
|
+
product_category: string;
|
|
27
|
+
supplier?: string;
|
|
28
|
+
supplier_code?: string;
|
|
29
|
+
supplier_item_name?: string;
|
|
30
|
+
unit: string;
|
|
31
|
+
quantity: number;
|
|
32
|
+
price_include_taxes: number;
|
|
33
|
+
vat_in_percent: number;
|
|
34
|
+
discount_in_percent?: number;
|
|
35
|
+
page_number?: number;
|
|
36
|
+
item_row_in_page?: number;
|
|
37
|
+
}
|
|
38
|
+
export declare class OrderProvider implements ProviderType {
|
|
39
|
+
private readonly config;
|
|
40
|
+
private readonly target;
|
|
41
|
+
private readonly $authToken;
|
|
42
|
+
constructor(config: NetworkingConfig, target: OrderTarget, $authToken: BehaviorSubject<string | null>);
|
|
43
|
+
get baseURL(): string;
|
|
44
|
+
get path(): string;
|
|
45
|
+
get method(): string;
|
|
46
|
+
get headers(): Record<string, string>;
|
|
47
|
+
getPath(...queryParams: unknown[]): string;
|
|
48
|
+
}
|
|
49
|
+
export declare class OrderService {
|
|
50
|
+
private readonly config;
|
|
51
|
+
private readonly $authToken;
|
|
52
|
+
constructor(config: NetworkingConfig, $authToken: BehaviorSubject<string | null>);
|
|
53
|
+
provider(target: OrderTarget): SmessService<OrderProvider>;
|
|
54
|
+
list(): Observable<NetworkingUIEventWrapped<HttpResponse>>;
|
|
55
|
+
create(payload: CreateOrderRequest): Observable<NetworkingUIEventWrapped<HttpResponse>>;
|
|
56
|
+
delete(id: string): Observable<NetworkingUIEventWrapped<HttpResponse>>;
|
|
57
|
+
getDraft(): Observable<NetworkingUIEventWrapped<HttpResponse>>;
|
|
58
|
+
update(id: string, payload: UpdateOrderRequest): Observable<NetworkingUIEventWrapped<HttpResponse>>;
|
|
59
|
+
get(id: string): Observable<NetworkingUIEventWrapped<HttpResponse>>;
|
|
60
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OrderService = exports.OrderProvider = void 0;
|
|
4
|
+
const smess_service_1 = require("./smess.service");
|
|
5
|
+
class OrderProvider {
|
|
6
|
+
constructor(config, target, $authToken) {
|
|
7
|
+
this.config = config;
|
|
8
|
+
this.target = target;
|
|
9
|
+
this.$authToken = $authToken;
|
|
10
|
+
}
|
|
11
|
+
get baseURL() {
|
|
12
|
+
return this.config.version ? `${this.config.baseUrl}/${this.config.version}` : this.config.baseUrl;
|
|
13
|
+
}
|
|
14
|
+
get path() {
|
|
15
|
+
switch (this.target) {
|
|
16
|
+
case 'order-list':
|
|
17
|
+
return '/order/list';
|
|
18
|
+
case 'order-create':
|
|
19
|
+
return '/order/create';
|
|
20
|
+
case 'order-delete':
|
|
21
|
+
return '/order/${id}';
|
|
22
|
+
case 'order-get-draft':
|
|
23
|
+
return '/order/get-draft';
|
|
24
|
+
case 'order-update':
|
|
25
|
+
case 'order-get':
|
|
26
|
+
default:
|
|
27
|
+
return '/order/${id}';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
get method() {
|
|
31
|
+
switch (this.target) {
|
|
32
|
+
case 'order-list':
|
|
33
|
+
case 'order-get':
|
|
34
|
+
case 'order-get-draft':
|
|
35
|
+
return 'GET';
|
|
36
|
+
case 'order-create':
|
|
37
|
+
return 'POST';
|
|
38
|
+
case 'order-update':
|
|
39
|
+
return 'PUT';
|
|
40
|
+
case 'order-delete':
|
|
41
|
+
return 'DELETE';
|
|
42
|
+
default:
|
|
43
|
+
return 'GET';
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
get headers() {
|
|
47
|
+
let headers = smess_service_1.SmessService.getHeaders(smess_service_1.Accept.V1, this.$authToken.value);
|
|
48
|
+
if (this.config.clientId) {
|
|
49
|
+
headers[smess_service_1.SMESSHeader.ClientId] = this.config.clientId;
|
|
50
|
+
}
|
|
51
|
+
if (this.config.apiKey) {
|
|
52
|
+
headers[smess_service_1.SMESSHeader.ApiKey] = this.config.apiKey;
|
|
53
|
+
}
|
|
54
|
+
return smess_service_1.SmessService.filterHeaders(headers);
|
|
55
|
+
}
|
|
56
|
+
getPath(...queryParams) {
|
|
57
|
+
let definedPath = this.path;
|
|
58
|
+
const params = definedPath.match(/\$\{(.+?)\}/g);
|
|
59
|
+
params === null || params === void 0 ? void 0 : params.forEach((value, index) => {
|
|
60
|
+
definedPath = definedPath.replace(value, String(queryParams[index]));
|
|
61
|
+
});
|
|
62
|
+
return definedPath;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.OrderProvider = OrderProvider;
|
|
66
|
+
class OrderService {
|
|
67
|
+
constructor(config, $authToken) {
|
|
68
|
+
this.config = config;
|
|
69
|
+
this.$authToken = $authToken;
|
|
70
|
+
}
|
|
71
|
+
provider(target) {
|
|
72
|
+
const targetWrapper = new OrderProvider(this.config, target, this.$authToken);
|
|
73
|
+
return new smess_service_1.SmessService(targetWrapper);
|
|
74
|
+
}
|
|
75
|
+
list() {
|
|
76
|
+
return this.provider('order-list').request();
|
|
77
|
+
}
|
|
78
|
+
create(payload) {
|
|
79
|
+
return this.provider('order-create').request(payload);
|
|
80
|
+
}
|
|
81
|
+
delete(id) {
|
|
82
|
+
return this.provider('order-delete').request(null, undefined, id);
|
|
83
|
+
}
|
|
84
|
+
getDraft() {
|
|
85
|
+
return this.provider('order-get-draft').request();
|
|
86
|
+
}
|
|
87
|
+
update(id, payload) {
|
|
88
|
+
return this.provider('order-update').request(payload, undefined, id);
|
|
89
|
+
}
|
|
90
|
+
get(id) {
|
|
91
|
+
return this.provider('order-get').request(null, undefined, id);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.OrderService = OrderService;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const order_service_1 = require("./order.service");
|
|
4
|
+
const types_1 = require("../types/types");
|
|
5
|
+
const rxjs_1 = require("rxjs");
|
|
6
|
+
describe('OrderService', () => {
|
|
7
|
+
const config = {
|
|
8
|
+
baseUrl: 'https://api.example.com',
|
|
9
|
+
isProd: false,
|
|
10
|
+
clientId: 'test-client-id',
|
|
11
|
+
apiKey: 'test-api-key',
|
|
12
|
+
};
|
|
13
|
+
let service;
|
|
14
|
+
let $authToken;
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
$authToken = new rxjs_1.BehaviorSubject(null);
|
|
17
|
+
service = new order_service_1.OrderService(config, $authToken);
|
|
18
|
+
});
|
|
19
|
+
it('list success', async () => {
|
|
20
|
+
const response = { data: [{ id: '1', status: 'draft' }] };
|
|
21
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
22
|
+
request: () => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.succeeded(response)),
|
|
23
|
+
});
|
|
24
|
+
const result = await service.list().toPromise();
|
|
25
|
+
expect(result === null || result === void 0 ? void 0 : result.hasSuccess).toBe(true);
|
|
26
|
+
expect(result === null || result === void 0 ? void 0 : result.response).toEqual(response);
|
|
27
|
+
});
|
|
28
|
+
it('list error', async () => {
|
|
29
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
30
|
+
request: () => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.failed(types_1.AlertModel.default())),
|
|
31
|
+
});
|
|
32
|
+
const result = await service.list().toPromise();
|
|
33
|
+
expect(result === null || result === void 0 ? void 0 : result.hasFailed).toBe(true);
|
|
34
|
+
expect(result === null || result === void 0 ? void 0 : result.alertModel).toBeInstanceOf(types_1.AlertModel);
|
|
35
|
+
});
|
|
36
|
+
it('create success', async () => {
|
|
37
|
+
const payload = {
|
|
38
|
+
order_date: '2024-01-01',
|
|
39
|
+
order_items: [],
|
|
40
|
+
};
|
|
41
|
+
const response = { data: { id: '1', status: 'created' } };
|
|
42
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
43
|
+
request: () => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.succeeded(response)),
|
|
44
|
+
});
|
|
45
|
+
const result = await service.create(payload).toPromise();
|
|
46
|
+
expect(result === null || result === void 0 ? void 0 : result.hasSuccess).toBe(true);
|
|
47
|
+
expect(result === null || result === void 0 ? void 0 : result.response).toEqual(response);
|
|
48
|
+
});
|
|
49
|
+
it('create error', async () => {
|
|
50
|
+
const payload = {
|
|
51
|
+
order_date: '2024-01-01',
|
|
52
|
+
order_items: [],
|
|
53
|
+
};
|
|
54
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
55
|
+
request: () => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.failed(types_1.AlertModel.default())),
|
|
56
|
+
});
|
|
57
|
+
const result = await service.create(payload).toPromise();
|
|
58
|
+
expect(result === null || result === void 0 ? void 0 : result.hasFailed).toBe(true);
|
|
59
|
+
expect(result === null || result === void 0 ? void 0 : result.alertModel).toBeInstanceOf(types_1.AlertModel);
|
|
60
|
+
});
|
|
61
|
+
it('delete success', async () => {
|
|
62
|
+
const response = { data: { id: '1', status: 'deleted' } };
|
|
63
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
64
|
+
request: (_payload, _mockResponse, id) => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.succeeded(response)),
|
|
65
|
+
});
|
|
66
|
+
const result = await service.delete('1').toPromise();
|
|
67
|
+
expect(result === null || result === void 0 ? void 0 : result.hasSuccess).toBe(true);
|
|
68
|
+
expect(result === null || result === void 0 ? void 0 : result.response).toEqual(response);
|
|
69
|
+
});
|
|
70
|
+
it('delete error', async () => {
|
|
71
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
72
|
+
request: (_payload, _mockResponse, id) => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.failed(types_1.AlertModel.default())),
|
|
73
|
+
});
|
|
74
|
+
const result = await service.delete('1').toPromise();
|
|
75
|
+
expect(result === null || result === void 0 ? void 0 : result.hasFailed).toBe(true);
|
|
76
|
+
expect(result === null || result === void 0 ? void 0 : result.alertModel).toBeInstanceOf(types_1.AlertModel);
|
|
77
|
+
});
|
|
78
|
+
it('getDraft success', async () => {
|
|
79
|
+
const response = { data: { id: 'draft', status: 'draft' } };
|
|
80
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
81
|
+
request: () => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.succeeded(response)),
|
|
82
|
+
});
|
|
83
|
+
const result = await service.getDraft().toPromise();
|
|
84
|
+
expect(result === null || result === void 0 ? void 0 : result.hasSuccess).toBe(true);
|
|
85
|
+
expect(result === null || result === void 0 ? void 0 : result.response).toEqual(response);
|
|
86
|
+
});
|
|
87
|
+
it('getDraft error', async () => {
|
|
88
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
89
|
+
request: () => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.failed(types_1.AlertModel.default())),
|
|
90
|
+
});
|
|
91
|
+
const result = await service.getDraft().toPromise();
|
|
92
|
+
expect(result === null || result === void 0 ? void 0 : result.hasFailed).toBe(true);
|
|
93
|
+
expect(result === null || result === void 0 ? void 0 : result.alertModel).toBeInstanceOf(types_1.AlertModel);
|
|
94
|
+
});
|
|
95
|
+
it('update success', async () => {
|
|
96
|
+
const payload = {
|
|
97
|
+
order_date: '2024-01-01',
|
|
98
|
+
order_items: [],
|
|
99
|
+
};
|
|
100
|
+
const response = { data: { id: '1', status: 'updated' } };
|
|
101
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
102
|
+
request: (_payload, _mockResponse, id) => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.succeeded(response)),
|
|
103
|
+
});
|
|
104
|
+
const result = await service.update('1', payload).toPromise();
|
|
105
|
+
expect(result === null || result === void 0 ? void 0 : result.hasSuccess).toBe(true);
|
|
106
|
+
expect(result === null || result === void 0 ? void 0 : result.response).toEqual(response);
|
|
107
|
+
});
|
|
108
|
+
it('update error', async () => {
|
|
109
|
+
const payload = {
|
|
110
|
+
order_date: '2024-01-01',
|
|
111
|
+
order_items: [],
|
|
112
|
+
};
|
|
113
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
114
|
+
request: (_payload, _mockResponse, id) => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.failed(types_1.AlertModel.default())),
|
|
115
|
+
});
|
|
116
|
+
const result = await service.update('1', payload).toPromise();
|
|
117
|
+
expect(result === null || result === void 0 ? void 0 : result.hasFailed).toBe(true);
|
|
118
|
+
expect(result === null || result === void 0 ? void 0 : result.alertModel).toBeInstanceOf(types_1.AlertModel);
|
|
119
|
+
});
|
|
120
|
+
it('get success', async () => {
|
|
121
|
+
const response = { data: { id: '1', status: 'fetched' } };
|
|
122
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
123
|
+
request: (_payload, _mockResponse, id) => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.succeeded(response)),
|
|
124
|
+
});
|
|
125
|
+
const result = await service.get('1').toPromise();
|
|
126
|
+
expect(result === null || result === void 0 ? void 0 : result.hasSuccess).toBe(true);
|
|
127
|
+
expect(result === null || result === void 0 ? void 0 : result.response).toEqual(response);
|
|
128
|
+
});
|
|
129
|
+
it('get error', async () => {
|
|
130
|
+
jest.spyOn(service, 'provider').mockReturnValue({
|
|
131
|
+
request: (_payload, _mockResponse, id) => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.failed(types_1.AlertModel.default())),
|
|
132
|
+
});
|
|
133
|
+
const result = await service.get('1').toPromise();
|
|
134
|
+
expect(result === null || result === void 0 ? void 0 : result.hasFailed).toBe(true);
|
|
135
|
+
expect(result === null || result === void 0 ? void 0 : result.alertModel).toBeInstanceOf(types_1.AlertModel);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ProductService = exports.ProductProvider = void 0;
|
|
4
|
+
const base_provider_1 = require("../core/base.provider");
|
|
5
|
+
const base_service_wrapper_1 = require("../core/base.service.wrapper");
|
|
6
|
+
// ProductProvider implements ProviderType for product endpoints
|
|
7
|
+
class ProductProvider extends base_provider_1.BaseProvider {
|
|
8
|
+
constructor(config, { productId, method } = {}) {
|
|
9
|
+
super({
|
|
10
|
+
config,
|
|
11
|
+
path: productId ? `/products/${productId}` : '/products',
|
|
12
|
+
method: method || 'GET',
|
|
13
|
+
headers: { 'Content-Type': 'application/json' },
|
|
14
|
+
getPath: () => (productId ? `/products/${productId}` : '/products'),
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.ProductProvider = ProductProvider;
|
|
19
|
+
// ProductService provides high-level methods for product API
|
|
20
|
+
class ProductService extends base_service_wrapper_1.BaseServiceWrapper {
|
|
21
|
+
constructor(config, productId, method) {
|
|
22
|
+
super(new ProductProvider(config, { productId, method }));
|
|
23
|
+
this.config = config;
|
|
24
|
+
}
|
|
25
|
+
// Fetch a product by ID
|
|
26
|
+
fetchProductById(id) {
|
|
27
|
+
this.provider = new ProductProvider(this.config, { productId: id });
|
|
28
|
+
return this.request();
|
|
29
|
+
}
|
|
30
|
+
// Fetch all products
|
|
31
|
+
fetchAllProducts() {
|
|
32
|
+
this.provider = new ProductProvider(this.config);
|
|
33
|
+
return this.request();
|
|
34
|
+
}
|
|
35
|
+
// Create a new product (POST)
|
|
36
|
+
createProduct(product) {
|
|
37
|
+
this.provider = new ProductProvider(this.config, { method: 'POST' });
|
|
38
|
+
return this.request(product);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.ProductService = ProductService;
|
|
42
|
+
// Example usage:
|
|
43
|
+
// setNetworkingConfig({ baseUrl: 'https://fakestoreapi.com', isProd: false });
|
|
44
|
+
// const productService = new ProductService();
|
|
45
|
+
// productService.fetchProductById(1).subscribe(result => console.log(result));
|
|
46
|
+
// productService.fetchAllProducts().subscribe(result => console.log(result));
|
|
47
|
+
// productService.createProduct({ name: 'New Product', price: 99.99 }).subscribe(result => console.log(result));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const product_service_1 = require("./product.service");
|
|
4
|
+
const types_1 = require("../types/types");
|
|
5
|
+
const rxjs_1 = require("rxjs");
|
|
6
|
+
const base_service_wrapper_1 = require("../core/base.service.wrapper");
|
|
7
|
+
describe('ProductService', () => {
|
|
8
|
+
const config = {
|
|
9
|
+
baseUrl: 'https://jsonplaceholder.typicode.com',
|
|
10
|
+
isProd: false,
|
|
11
|
+
clientId: 'test-client',
|
|
12
|
+
apiKey: 'test-key',
|
|
13
|
+
};
|
|
14
|
+
const product = { id: 1, name: 'Product', price: 10 };
|
|
15
|
+
let requestSpy;
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
requestSpy = jest.spyOn(base_service_wrapper_1.BaseServiceWrapper.prototype, 'request').mockImplementation(() => (0, rxjs_1.of)(types_1.NetworkingUIEventWrapped.succeeded(product)));
|
|
18
|
+
});
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
requestSpy.mockRestore();
|
|
21
|
+
});
|
|
22
|
+
it('should fetch product by id', done => {
|
|
23
|
+
const service = new product_service_1.ProductService(config, 1);
|
|
24
|
+
service.fetchProductById(1).subscribe(result => {
|
|
25
|
+
expect(result.response).toEqual(product);
|
|
26
|
+
done();
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
it('should fetch all products', done => {
|
|
30
|
+
const service = new product_service_1.ProductService(config);
|
|
31
|
+
service.fetchAllProducts().subscribe(result => {
|
|
32
|
+
expect(result.response).toEqual(product);
|
|
33
|
+
done();
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
it('should create a product', done => {
|
|
37
|
+
const service = new product_service_1.ProductService(config);
|
|
38
|
+
service.createProduct({ name: 'New Product', price: 99.99 }).subscribe(result => {
|
|
39
|
+
expect(result.response).toEqual(product);
|
|
40
|
+
done();
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
it('request should handle error', done => {
|
|
44
|
+
requestSpy.mockImplementationOnce(() => (0, rxjs_1.throwError)(() => new Error('Test error')));
|
|
45
|
+
const service = new product_service_1.ProductService(config);
|
|
46
|
+
service.request('error').subscribe({
|
|
47
|
+
error: err => {
|
|
48
|
+
expect(err).toBeInstanceOf(Error);
|
|
49
|
+
expect(err.message).toBe('Test error');
|
|
50
|
+
done();
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
});
|