@websolutespa/bom-mixer-store 1.8.5 → 1.8.6
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/CHANGELOG.md +6 -0
- package/dist/index.d.ts +12 -15
- package/dist/index.js +24 -29
- package/dist/index.mjs +24 -28
- package/package.json +1 -1
- package/src/api/api.ts +4 -48
- package/src/remote/remote.service.ts +21 -20
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -4,10 +4,7 @@ import { NextRequest } from 'next/server';
|
|
|
4
4
|
|
|
5
5
|
type ApiRequestOptions = FetchRequestOptions;
|
|
6
6
|
declare function getLocalApiRequestOptions(): ApiRequestOptions;
|
|
7
|
-
declare
|
|
8
|
-
token: string | null;
|
|
9
|
-
};
|
|
10
|
-
declare function getRemoteApiRequestOptions(): ApiRequestOptions;
|
|
7
|
+
declare function getRemoteApiRequestOptions(token?: string): ApiRequestOptions;
|
|
11
8
|
|
|
12
9
|
type ICrudMethod = 'get' | 'post' | 'put' | 'patch' | 'delete' | string;
|
|
13
10
|
type IHandler<T = any> = (request: NextApiRequest, response: NextApiResponse<T>) => Promise<NextApiResponse<T> | void>;
|
|
@@ -145,16 +142,16 @@ declare class RemoteService<T extends IEntity = IEntity> implements IQuerable<T>
|
|
|
145
142
|
key: string;
|
|
146
143
|
store: IStore;
|
|
147
144
|
constructor(key: string, store: IStore);
|
|
148
|
-
findOne<B extends T = T>(id: IEquatable, params?: Omit<QueryParams, 'where'
|
|
149
|
-
findMany<B extends T = T>(params?: QueryParams): Promise<B[]>;
|
|
150
|
-
findPaged<B extends T = T>(params?: QueryParams): Promise<IPagedResult<B>>;
|
|
151
|
-
create<B extends T = T>(payload: Omit<B, 'id'>, params?: QueryParams): Promise<B>;
|
|
152
|
-
updateOne<B extends T = T>(id: IEquatable, payload: Partial<B>, params?: Omit<QueryParams, 'where'
|
|
153
|
-
updateMany<B extends T = T>(payload: Partial<B>, params?: QueryParams): Promise<B[]>;
|
|
154
|
-
deleteOne<B extends T = T>(id: IEquatable, params?: Omit<QueryParams, 'where'
|
|
155
|
-
deleteMany<B extends T = T>(params?: QueryParams): Promise<B[]>;
|
|
156
|
-
update<B extends T = T>(payload: Partial<B>, params?: QueryParams): Promise<B>;
|
|
157
|
-
delete<B extends T = T>(id: IEquatable): Promise<B | undefined>;
|
|
145
|
+
findOne<B extends T = T>(id: IEquatable, params?: Omit<QueryParams, 'where'>, token?: string): Promise<B | undefined>;
|
|
146
|
+
findMany<B extends T = T>(params?: QueryParams, token?: string): Promise<B[]>;
|
|
147
|
+
findPaged<B extends T = T>(params?: QueryParams, token?: string): Promise<IPagedResult<B>>;
|
|
148
|
+
create<B extends T = T>(payload: Omit<B, 'id'>, params?: QueryParams, token?: string): Promise<B>;
|
|
149
|
+
updateOne<B extends T = T>(id: IEquatable, payload: Partial<B>, params?: Omit<QueryParams, 'where'>, token?: string): Promise<B>;
|
|
150
|
+
updateMany<B extends T = T>(payload: Partial<B>, params?: QueryParams, token?: string): Promise<B[]>;
|
|
151
|
+
deleteOne<B extends T = T>(id: IEquatable, params?: Omit<QueryParams, 'where'>, token?: string): Promise<B | undefined>;
|
|
152
|
+
deleteMany<B extends T = T>(params?: QueryParams, token?: string): Promise<B[]>;
|
|
153
|
+
update<B extends T = T>(payload: Partial<B>, params?: QueryParams, token?: string): Promise<B>;
|
|
154
|
+
delete<B extends T = T>(id: IEquatable, token?: string): Promise<B | undefined>;
|
|
158
155
|
protected decorateOne_<B extends T = T>(item: B, params?: QueryParams): Promise<B>;
|
|
159
156
|
protected decorateMany_<B extends T = T>(items: B[], params?: QueryParams): Promise<B[]>;
|
|
160
157
|
}
|
|
@@ -255,4 +252,4 @@ declare function getStore<T extends IStore>(): Promise<T>;
|
|
|
255
252
|
|
|
256
253
|
declare function varsHandler(): IHandler<any>;
|
|
257
254
|
|
|
258
|
-
export { ApiRequestOptions, BuildStrategy, COLLECTIONS, DEFAULT_DEPTH, IApiHandler, ICrudMethod, IField, IHandler, IPageResolvedRelation, KNOWN_COLLECTIONS, MAX_INT, MOCK_COLLECTIONS, MapTo, MockMenuService, MockPageService, MockService, NextPhase,
|
|
255
|
+
export { ApiRequestOptions, BuildStrategy, COLLECTIONS, DEFAULT_DEPTH, IApiHandler, ICrudMethod, IField, IHandler, IPageResolvedRelation, KNOWN_COLLECTIONS, MAX_INT, MOCK_COLLECTIONS, MapTo, MockMenuService, MockPageService, MockService, NextPhase, RemoteMenuService, RemotePageService, RemoteService, RemoteServiceStrategy, STATIC_COLLECTIONS, StaticService, StaticServiceConfig, StaticServiceSrc, StoreStrategy, apiHandler, buildStrategy, collectCategory_, collectionDelete, collectionGet, collectionPatch, collectionPost, decorateCategories_, decorateCategory_, decorateHref_, decorateHrefs_, decorateLocalizable_, decorateLocalizables_, decorateMenu_, decorateMenus_, decorateNav_, decorateNavs_, decorateRelation_, decorateRelations_, decorateSchema_, decorateSchemas_, decorateStaticRelation_, decorateStaticRelations_, errorHandler, findRoute, fromCache, getLocalApiRequestOptions, getLocalApiUrl, getMockStore, getMockStoreFromData_, getRemoteApiRequestOptions, getRemoteApiUrl, getRemoteStore, getStore, isApiRequest, isNextRequest, isStaticRequest, localApiDelete, localApiFetch, localApiGet, localApiPatch, localApiPost, localApiPut, mockApiDelete, mockApiFetch, mockApiGet, mockApiPatch, mockApiPost, mockApiPut, mockHandler, nextPhase, optimizeMany_, optimizeOne_, remoteApiDelete, remoteApiFetch, remoteApiGet, remoteApiPatch, remoteApiPost, remoteApiPut, routePost, staticCollections, storeApiDelete, storeApiGet, storeApiPatch, storeApiPost, storeApiPut, storeCacheDuration, storeHandler, storeIndexHandler, storeStrategy, varsHandler };
|
package/dist/index.js
CHANGED
|
@@ -40,7 +40,6 @@ __export(src_exports, {
|
|
|
40
40
|
MockPageService: () => MockPageService,
|
|
41
41
|
MockService: () => MockService,
|
|
42
42
|
NextPhase: () => NextPhase,
|
|
43
|
-
REMOTE_API_SESSION: () => REMOTE_API_SESSION,
|
|
44
43
|
RemoteMenuService: () => RemoteMenuService,
|
|
45
44
|
RemotePageService: () => RemotePageService,
|
|
46
45
|
RemoteService: () => RemoteService,
|
|
@@ -128,16 +127,13 @@ function getLocalApiRequestOptions() {
|
|
|
128
127
|
headers: {}
|
|
129
128
|
};
|
|
130
129
|
}
|
|
131
|
-
|
|
132
|
-
token
|
|
133
|
-
};
|
|
134
|
-
function getRemoteApiRequestOptions() {
|
|
135
|
-
if (REMOTE_API_SESSION.token) {
|
|
130
|
+
function getRemoteApiRequestOptions(token) {
|
|
131
|
+
if (token) {
|
|
136
132
|
return {
|
|
137
133
|
// mode: 'cors',
|
|
138
134
|
headers: {
|
|
139
|
-
Authorization: `
|
|
140
|
-
// Authorization: `
|
|
135
|
+
Authorization: `JWT ${token}`
|
|
136
|
+
// Authorization: `Bearer ${token}`,
|
|
141
137
|
}
|
|
142
138
|
};
|
|
143
139
|
} else {
|
|
@@ -1507,14 +1503,14 @@ var RemoteService = class {
|
|
|
1507
1503
|
this.key = key;
|
|
1508
1504
|
this.store = store;
|
|
1509
1505
|
}
|
|
1510
|
-
async findOne(id, params = {}) {
|
|
1506
|
+
async findOne(id, params = {}, token) {
|
|
1511
1507
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1512
1508
|
params.draft = RemoteServiceStrategy.draft;
|
|
1513
1509
|
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1514
1510
|
const search = query ? `?${query}` : "";
|
|
1515
1511
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1516
1512
|
try {
|
|
1517
|
-
const item = await remoteApiGet(url);
|
|
1513
|
+
const item = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1518
1514
|
if (item) {
|
|
1519
1515
|
return await this.decorateOne_(item, params);
|
|
1520
1516
|
}
|
|
@@ -1523,84 +1519,84 @@ var RemoteService = class {
|
|
|
1523
1519
|
}
|
|
1524
1520
|
return;
|
|
1525
1521
|
}
|
|
1526
|
-
async findMany(params = {}) {
|
|
1522
|
+
async findMany(params = {}, token) {
|
|
1527
1523
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1528
1524
|
params.draft = RemoteServiceStrategy.draft;
|
|
1529
1525
|
params.pagination = false;
|
|
1530
1526
|
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1531
1527
|
const search = query ? `?${query}` : "";
|
|
1532
1528
|
const url = `/${this.key}${search}`;
|
|
1533
|
-
const response = await remoteApiGet(url);
|
|
1529
|
+
const response = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1534
1530
|
const items = Array.isArray(response) ? response : response.docs;
|
|
1535
1531
|
return await this.decorateMany_(items, params);
|
|
1536
1532
|
}
|
|
1537
|
-
async findPaged(params = {}) {
|
|
1533
|
+
async findPaged(params = {}, token) {
|
|
1538
1534
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1539
1535
|
params.draft = RemoteServiceStrategy.draft;
|
|
1540
1536
|
params.pagination = true;
|
|
1541
1537
|
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1542
1538
|
const search = query ? `?${query}` : "";
|
|
1543
1539
|
const url = `/${this.key}${search}`;
|
|
1544
|
-
const response = await remoteApiGet(url);
|
|
1540
|
+
const response = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1545
1541
|
let items = response.docs;
|
|
1546
1542
|
items = await this.decorateMany_(items, params);
|
|
1547
1543
|
return { ...response, docs: items };
|
|
1548
1544
|
}
|
|
1549
|
-
async create(payload, params = {}) {
|
|
1545
|
+
async create(payload, params = {}, token) {
|
|
1550
1546
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1551
1547
|
params.draft = RemoteServiceStrategy.draft;
|
|
1552
1548
|
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1553
1549
|
const search = query ? `?${query}` : "";
|
|
1554
1550
|
const url = `/${this.key}${search}`;
|
|
1555
|
-
const result = await remoteApiPost(url, payload);
|
|
1551
|
+
const result = await remoteApiPost(url, payload, getRemoteApiRequestOptions(token));
|
|
1556
1552
|
const item = result.doc ? result.doc : result;
|
|
1557
1553
|
return await this.decorateOne_(item, {});
|
|
1558
1554
|
}
|
|
1559
|
-
async updateOne(id, payload, params = {}) {
|
|
1555
|
+
async updateOne(id, payload, params = {}, token) {
|
|
1560
1556
|
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1561
1557
|
const search = query ? `?${query}` : "";
|
|
1562
1558
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1563
|
-
const result = await remoteApiPatch(url, payload);
|
|
1559
|
+
const result = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1564
1560
|
const item = result.doc ? result.doc : result;
|
|
1565
1561
|
return await this.decorateOne_(item, {});
|
|
1566
1562
|
}
|
|
1567
|
-
async updateMany(payload, params = {}) {
|
|
1563
|
+
async updateMany(payload, params = {}, token) {
|
|
1568
1564
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1569
1565
|
params.draft = RemoteServiceStrategy.draft;
|
|
1570
1566
|
params.pagination = false;
|
|
1571
1567
|
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1572
1568
|
const search = query ? `?${query}` : "";
|
|
1573
1569
|
const url = `/${this.key}${search}`;
|
|
1574
|
-
const response = await remoteApiPatch(url, payload);
|
|
1570
|
+
const response = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1575
1571
|
const items = Array.isArray(response) ? response : response.docs;
|
|
1576
1572
|
return await this.decorateMany_(items, params);
|
|
1577
1573
|
}
|
|
1578
|
-
async deleteOne(id, params = {}) {
|
|
1574
|
+
async deleteOne(id, params = {}, token) {
|
|
1579
1575
|
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1580
1576
|
const search = query ? `?${query}` : "";
|
|
1581
1577
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1582
|
-
const item = await remoteApiDelete(url);
|
|
1578
|
+
const item = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1583
1579
|
return await this.decorateOne_(item, {});
|
|
1584
1580
|
}
|
|
1585
|
-
async deleteMany(params = {}) {
|
|
1581
|
+
async deleteMany(params = {}, token) {
|
|
1586
1582
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1587
1583
|
params.draft = RemoteServiceStrategy.draft;
|
|
1588
1584
|
params.pagination = false;
|
|
1589
1585
|
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1590
1586
|
const search = query ? `?${query}` : "";
|
|
1591
1587
|
const url = `/${this.key}${search}`;
|
|
1592
|
-
const response = await remoteApiDelete(url);
|
|
1588
|
+
const response = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1593
1589
|
const items = Array.isArray(response) ? response : response.docs;
|
|
1594
1590
|
return await this.decorateMany_(items, params);
|
|
1595
1591
|
}
|
|
1596
1592
|
// !!! old
|
|
1597
|
-
async update(payload, params = {}) {
|
|
1593
|
+
async update(payload, params = {}, token) {
|
|
1598
1594
|
const url = `/${this.key}`;
|
|
1599
|
-
const result = await remoteApiPatch(url, payload);
|
|
1595
|
+
const result = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1600
1596
|
const item = result.doc ? result.doc : result;
|
|
1601
1597
|
return await this.decorateOne_(item, {});
|
|
1602
1598
|
}
|
|
1603
|
-
async delete(id) {
|
|
1599
|
+
async delete(id, token) {
|
|
1604
1600
|
const params = {
|
|
1605
1601
|
where: {
|
|
1606
1602
|
id: {
|
|
@@ -1611,7 +1607,7 @@ var RemoteService = class {
|
|
|
1611
1607
|
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1612
1608
|
const search = query ? `?${query}` : "";
|
|
1613
1609
|
const url = `/${this.key}${search}`;
|
|
1614
|
-
const item = await remoteApiDelete(url);
|
|
1610
|
+
const item = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1615
1611
|
return await this.decorateOne_(item, {});
|
|
1616
1612
|
}
|
|
1617
1613
|
async decorateOne_(item, params = {}) {
|
|
@@ -2500,7 +2496,6 @@ function varsHandler() {
|
|
|
2500
2496
|
MockPageService,
|
|
2501
2497
|
MockService,
|
|
2502
2498
|
NextPhase,
|
|
2503
|
-
REMOTE_API_SESSION,
|
|
2504
2499
|
RemoteMenuService,
|
|
2505
2500
|
RemotePageService,
|
|
2506
2501
|
RemoteService,
|
package/dist/index.mjs
CHANGED
|
@@ -12,16 +12,13 @@ function getLocalApiRequestOptions() {
|
|
|
12
12
|
headers: {}
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
token
|
|
17
|
-
};
|
|
18
|
-
function getRemoteApiRequestOptions() {
|
|
19
|
-
if (REMOTE_API_SESSION.token) {
|
|
15
|
+
function getRemoteApiRequestOptions(token) {
|
|
16
|
+
if (token) {
|
|
20
17
|
return {
|
|
21
18
|
// mode: 'cors',
|
|
22
19
|
headers: {
|
|
23
|
-
Authorization: `
|
|
24
|
-
// Authorization: `
|
|
20
|
+
Authorization: `JWT ${token}`
|
|
21
|
+
// Authorization: `Bearer ${token}`,
|
|
25
22
|
}
|
|
26
23
|
};
|
|
27
24
|
} else {
|
|
@@ -1401,14 +1398,14 @@ var RemoteService = class {
|
|
|
1401
1398
|
this.key = key;
|
|
1402
1399
|
this.store = store;
|
|
1403
1400
|
}
|
|
1404
|
-
async findOne(id, params = {}) {
|
|
1401
|
+
async findOne(id, params = {}, token) {
|
|
1405
1402
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1406
1403
|
params.draft = RemoteServiceStrategy.draft;
|
|
1407
1404
|
const query = qsSerialize2(params);
|
|
1408
1405
|
const search = query ? `?${query}` : "";
|
|
1409
1406
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1410
1407
|
try {
|
|
1411
|
-
const item = await remoteApiGet(url);
|
|
1408
|
+
const item = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1412
1409
|
if (item) {
|
|
1413
1410
|
return await this.decorateOne_(item, params);
|
|
1414
1411
|
}
|
|
@@ -1417,84 +1414,84 @@ var RemoteService = class {
|
|
|
1417
1414
|
}
|
|
1418
1415
|
return;
|
|
1419
1416
|
}
|
|
1420
|
-
async findMany(params = {}) {
|
|
1417
|
+
async findMany(params = {}, token) {
|
|
1421
1418
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1422
1419
|
params.draft = RemoteServiceStrategy.draft;
|
|
1423
1420
|
params.pagination = false;
|
|
1424
1421
|
const query = qsSerialize2(params);
|
|
1425
1422
|
const search = query ? `?${query}` : "";
|
|
1426
1423
|
const url = `/${this.key}${search}`;
|
|
1427
|
-
const response = await remoteApiGet(url);
|
|
1424
|
+
const response = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1428
1425
|
const items = Array.isArray(response) ? response : response.docs;
|
|
1429
1426
|
return await this.decorateMany_(items, params);
|
|
1430
1427
|
}
|
|
1431
|
-
async findPaged(params = {}) {
|
|
1428
|
+
async findPaged(params = {}, token) {
|
|
1432
1429
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1433
1430
|
params.draft = RemoteServiceStrategy.draft;
|
|
1434
1431
|
params.pagination = true;
|
|
1435
1432
|
const query = qsSerialize2(params);
|
|
1436
1433
|
const search = query ? `?${query}` : "";
|
|
1437
1434
|
const url = `/${this.key}${search}`;
|
|
1438
|
-
const response = await remoteApiGet(url);
|
|
1435
|
+
const response = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1439
1436
|
let items = response.docs;
|
|
1440
1437
|
items = await this.decorateMany_(items, params);
|
|
1441
1438
|
return { ...response, docs: items };
|
|
1442
1439
|
}
|
|
1443
|
-
async create(payload, params = {}) {
|
|
1440
|
+
async create(payload, params = {}, token) {
|
|
1444
1441
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1445
1442
|
params.draft = RemoteServiceStrategy.draft;
|
|
1446
1443
|
const query = qsSerialize2(params);
|
|
1447
1444
|
const search = query ? `?${query}` : "";
|
|
1448
1445
|
const url = `/${this.key}${search}`;
|
|
1449
|
-
const result = await remoteApiPost(url, payload);
|
|
1446
|
+
const result = await remoteApiPost(url, payload, getRemoteApiRequestOptions(token));
|
|
1450
1447
|
const item = result.doc ? result.doc : result;
|
|
1451
1448
|
return await this.decorateOne_(item, {});
|
|
1452
1449
|
}
|
|
1453
|
-
async updateOne(id, payload, params = {}) {
|
|
1450
|
+
async updateOne(id, payload, params = {}, token) {
|
|
1454
1451
|
const query = qsSerialize2(params);
|
|
1455
1452
|
const search = query ? `?${query}` : "";
|
|
1456
1453
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1457
|
-
const result = await remoteApiPatch(url, payload);
|
|
1454
|
+
const result = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1458
1455
|
const item = result.doc ? result.doc : result;
|
|
1459
1456
|
return await this.decorateOne_(item, {});
|
|
1460
1457
|
}
|
|
1461
|
-
async updateMany(payload, params = {}) {
|
|
1458
|
+
async updateMany(payload, params = {}, token) {
|
|
1462
1459
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1463
1460
|
params.draft = RemoteServiceStrategy.draft;
|
|
1464
1461
|
params.pagination = false;
|
|
1465
1462
|
const query = qsSerialize2(params);
|
|
1466
1463
|
const search = query ? `?${query}` : "";
|
|
1467
1464
|
const url = `/${this.key}${search}`;
|
|
1468
|
-
const response = await remoteApiPatch(url, payload);
|
|
1465
|
+
const response = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1469
1466
|
const items = Array.isArray(response) ? response : response.docs;
|
|
1470
1467
|
return await this.decorateMany_(items, params);
|
|
1471
1468
|
}
|
|
1472
|
-
async deleteOne(id, params = {}) {
|
|
1469
|
+
async deleteOne(id, params = {}, token) {
|
|
1473
1470
|
const query = qsSerialize2(params);
|
|
1474
1471
|
const search = query ? `?${query}` : "";
|
|
1475
1472
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1476
|
-
const item = await remoteApiDelete(url);
|
|
1473
|
+
const item = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1477
1474
|
return await this.decorateOne_(item, {});
|
|
1478
1475
|
}
|
|
1479
|
-
async deleteMany(params = {}) {
|
|
1476
|
+
async deleteMany(params = {}, token) {
|
|
1480
1477
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1481
1478
|
params.draft = RemoteServiceStrategy.draft;
|
|
1482
1479
|
params.pagination = false;
|
|
1483
1480
|
const query = qsSerialize2(params);
|
|
1484
1481
|
const search = query ? `?${query}` : "";
|
|
1485
1482
|
const url = `/${this.key}${search}`;
|
|
1486
|
-
const response = await remoteApiDelete(url);
|
|
1483
|
+
const response = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1487
1484
|
const items = Array.isArray(response) ? response : response.docs;
|
|
1488
1485
|
return await this.decorateMany_(items, params);
|
|
1489
1486
|
}
|
|
1490
1487
|
// !!! old
|
|
1491
|
-
async update(payload, params = {}) {
|
|
1488
|
+
async update(payload, params = {}, token) {
|
|
1492
1489
|
const url = `/${this.key}`;
|
|
1493
|
-
const result = await remoteApiPatch(url, payload);
|
|
1490
|
+
const result = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1494
1491
|
const item = result.doc ? result.doc : result;
|
|
1495
1492
|
return await this.decorateOne_(item, {});
|
|
1496
1493
|
}
|
|
1497
|
-
async delete(id) {
|
|
1494
|
+
async delete(id, token) {
|
|
1498
1495
|
const params = {
|
|
1499
1496
|
where: {
|
|
1500
1497
|
id: {
|
|
@@ -1505,7 +1502,7 @@ var RemoteService = class {
|
|
|
1505
1502
|
const query = qsSerialize2(params);
|
|
1506
1503
|
const search = query ? `?${query}` : "";
|
|
1507
1504
|
const url = `/${this.key}${search}`;
|
|
1508
|
-
const item = await remoteApiDelete(url);
|
|
1505
|
+
const item = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1509
1506
|
return await this.decorateOne_(item, {});
|
|
1510
1507
|
}
|
|
1511
1508
|
async decorateOne_(item, params = {}) {
|
|
@@ -2393,7 +2390,6 @@ export {
|
|
|
2393
2390
|
MockPageService,
|
|
2394
2391
|
MockService,
|
|
2395
2392
|
NextPhase,
|
|
2396
|
-
REMOTE_API_SESSION,
|
|
2397
2393
|
RemoteMenuService,
|
|
2398
2394
|
RemotePageService,
|
|
2399
2395
|
RemoteService,
|
package/package.json
CHANGED
package/src/api/api.ts
CHANGED
|
@@ -9,40 +9,14 @@ export function getLocalApiRequestOptions(): ApiRequestOptions {
|
|
|
9
9
|
};
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
export
|
|
13
|
-
token
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export function getRemoteApiRequestOptions(): ApiRequestOptions {
|
|
17
|
-
if (REMOTE_API_SESSION.token) {
|
|
18
|
-
// authenticated end user JWT token bearer (end_users)
|
|
19
|
-
return {
|
|
20
|
-
// mode: 'cors',
|
|
21
|
-
headers: {
|
|
22
|
-
Authorization: `Bearer ${REMOTE_API_SESSION.token}`,
|
|
23
|
-
// Authorization: `JWT ${REMOTE_API_SESSION.token}`,
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
} else {
|
|
27
|
-
// guest user api key (users)
|
|
28
|
-
return {
|
|
29
|
-
// mode: 'cors',
|
|
30
|
-
headers: {
|
|
31
|
-
Authorization: `users API-Key ${process.env.STORE_API_KEY}`,
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/*
|
|
38
|
-
export async function getRemoteApiRequestOptions(): Promise<ApiRequestOptions> {
|
|
39
|
-
if (REMOTE_API_SESSION.token) {
|
|
12
|
+
export function getRemoteApiRequestOptions(token?: string): ApiRequestOptions {
|
|
13
|
+
if (token) {
|
|
40
14
|
// authenticated end user JWT token bearer (end_users)
|
|
41
15
|
return {
|
|
42
16
|
// mode: 'cors',
|
|
43
17
|
headers: {
|
|
44
|
-
Authorization: `
|
|
45
|
-
// Authorization: `
|
|
18
|
+
Authorization: `JWT ${token}`,
|
|
19
|
+
// Authorization: `Bearer ${token}`,
|
|
46
20
|
},
|
|
47
21
|
};
|
|
48
22
|
} else {
|
|
@@ -55,21 +29,3 @@ export async function getRemoteApiRequestOptions(): Promise<ApiRequestOptions> {
|
|
|
55
29
|
};
|
|
56
30
|
}
|
|
57
31
|
}
|
|
58
|
-
*/
|
|
59
|
-
|
|
60
|
-
/*
|
|
61
|
-
const session = await getSession<T>(request, response);
|
|
62
|
-
// console.log('getAuth.session', session);
|
|
63
|
-
if (session.token && session.exp && session.exp > Math.floor(new Date().getTime() / 1000)) {
|
|
64
|
-
REMOTE_API_SESSION.token = session.token;
|
|
65
|
-
} else {
|
|
66
|
-
session.token = undefined;
|
|
67
|
-
session.exp = undefined;
|
|
68
|
-
await session.save();
|
|
69
|
-
session.destroy();
|
|
70
|
-
REMOTE_API_SESSION.token = null;
|
|
71
|
-
}
|
|
72
|
-
return session;
|
|
73
|
-
*/
|
|
74
|
-
|
|
75
|
-
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { IEntity, IEquatable, IPagedResult, IQuerable, IStore, qsSerialize, QueryParams } from '@websolutespa/bom-core';
|
|
2
|
+
import { getRemoteApiRequestOptions } from '../api/api';
|
|
2
3
|
import { decorateSchema_, decorateSchemas_ } from '../decorators';
|
|
3
4
|
import { remoteApiDelete, remoteApiGet, remoteApiPatch, remoteApiPost } from './remote.api';
|
|
4
5
|
|
|
@@ -25,7 +26,7 @@ export class RemoteService<T extends IEntity = IEntity> implements IQuerable<T>
|
|
|
25
26
|
this.store = store;
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
async findOne<B extends T = T>(id: IEquatable, params: Omit<QueryParams, 'where'> = {}): Promise<B | undefined> {
|
|
29
|
+
async findOne<B extends T = T>(id: IEquatable, params: Omit<QueryParams, 'where'> = {}, token?: string): Promise<B | undefined> {
|
|
29
30
|
params.richText = String(params.richText) === 'true' ? true : false;
|
|
30
31
|
params.draft = RemoteServiceStrategy.draft;
|
|
31
32
|
const query = qsSerialize(params);
|
|
@@ -33,7 +34,7 @@ export class RemoteService<T extends IEntity = IEntity> implements IQuerable<T>
|
|
|
33
34
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
34
35
|
// console.log('RemoteService', this.key, 'findOne', url);
|
|
35
36
|
try {
|
|
36
|
-
const item = await remoteApiGet<B>(url);
|
|
37
|
+
const item = await remoteApiGet<B>(url, getRemoteApiRequestOptions(token));
|
|
37
38
|
if (item) {
|
|
38
39
|
return await this.decorateOne_(item, params);
|
|
39
40
|
}
|
|
@@ -43,7 +44,7 @@ export class RemoteService<T extends IEntity = IEntity> implements IQuerable<T>
|
|
|
43
44
|
return;
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
async findMany<B extends T = T>(params: QueryParams = {}): Promise<B[]> {
|
|
47
|
+
async findMany<B extends T = T>(params: QueryParams = {}, token?: string): Promise<B[]> {
|
|
47
48
|
params.richText = String(params.richText) === 'true' ? true : false;
|
|
48
49
|
params.draft = RemoteServiceStrategy.draft;
|
|
49
50
|
params.pagination = false;
|
|
@@ -51,12 +52,12 @@ export class RemoteService<T extends IEntity = IEntity> implements IQuerable<T>
|
|
|
51
52
|
const search = query ? `?${query}` : '';
|
|
52
53
|
const url = `/${this.key}${search}`;
|
|
53
54
|
// console.log('RemoteService', this.key, 'findMany', url);
|
|
54
|
-
const response = await remoteApiGet<B[] | { docs: B[] }>(url);
|
|
55
|
+
const response = await remoteApiGet<B[] | { docs: B[] }>(url, getRemoteApiRequestOptions(token));
|
|
55
56
|
const items: B[] = Array.isArray(response) ? response : response.docs;
|
|
56
57
|
return await this.decorateMany_(items, params);
|
|
57
58
|
}
|
|
58
59
|
|
|
59
|
-
async findPaged<B extends T = T>(params: QueryParams = {}): Promise<IPagedResult<B>> {
|
|
60
|
+
async findPaged<B extends T = T>(params: QueryParams = {}, token?: string): Promise<IPagedResult<B>> {
|
|
60
61
|
params.richText = String(params.richText) === 'true' ? true : false;
|
|
61
62
|
params.draft = RemoteServiceStrategy.draft;
|
|
62
63
|
params.pagination = true;
|
|
@@ -64,35 +65,35 @@ export class RemoteService<T extends IEntity = IEntity> implements IQuerable<T>
|
|
|
64
65
|
const search = query ? `?${query}` : '';
|
|
65
66
|
const url = `/${this.key}${search}`;
|
|
66
67
|
// console.log('RemoteService', this.key, 'findPaged', url);
|
|
67
|
-
const response = await remoteApiGet<IPagedResult<B>>(url);
|
|
68
|
+
const response = await remoteApiGet<IPagedResult<B>>(url, getRemoteApiRequestOptions(token));
|
|
68
69
|
let items: B[] = response.docs;
|
|
69
70
|
items = await this.decorateMany_(items, params);
|
|
70
71
|
return { ...response, docs: items };
|
|
71
72
|
}
|
|
72
73
|
|
|
73
|
-
async create<B extends T = T>(payload: Omit<B, 'id'>, params: QueryParams = {}): Promise<B> {
|
|
74
|
+
async create<B extends T = T>(payload: Omit<B, 'id'>, params: QueryParams = {}, token?: string): Promise<B> {
|
|
74
75
|
params.richText = String(params.richText) === 'true' ? true : false;
|
|
75
76
|
params.draft = RemoteServiceStrategy.draft;
|
|
76
77
|
const query = qsSerialize(params);
|
|
77
78
|
const search = query ? `?${query}` : '';
|
|
78
79
|
const url = `/${this.key}${search}`;
|
|
79
80
|
// console.log('RemoteService', this.key, 'create', url);
|
|
80
|
-
const result = await remoteApiPost<B | { doc: B }>(url, payload);
|
|
81
|
+
const result = await remoteApiPost<B | { doc: B }>(url, payload, getRemoteApiRequestOptions(token));
|
|
81
82
|
const item = (result.doc ? result.doc : result) as B;
|
|
82
83
|
return await this.decorateOne_(item, {});
|
|
83
84
|
}
|
|
84
85
|
|
|
85
|
-
async updateOne<B extends T = T>(id: IEquatable, payload: Partial<B>, params: Omit<QueryParams, 'where'> = {}): Promise<B> {
|
|
86
|
+
async updateOne<B extends T = T>(id: IEquatable, payload: Partial<B>, params: Omit<QueryParams, 'where'> = {}, token?: string): Promise<B> {
|
|
86
87
|
const query = qsSerialize(params);
|
|
87
88
|
const search = query ? `?${query}` : '';
|
|
88
89
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
89
90
|
// console.log('RemoteService', this.key, 'updateOne', url);
|
|
90
|
-
const result = await remoteApiPatch<B | { doc: B }>(url, payload);
|
|
91
|
+
const result = await remoteApiPatch<B | { doc: B }>(url, payload, getRemoteApiRequestOptions(token));
|
|
91
92
|
const item = (result.doc ? result.doc : result) as B;
|
|
92
93
|
return await this.decorateOne_(item, {});
|
|
93
94
|
}
|
|
94
95
|
|
|
95
|
-
async updateMany<B extends T = T>(payload: Partial<B>, params: QueryParams = {}): Promise<B[]> {
|
|
96
|
+
async updateMany<B extends T = T>(payload: Partial<B>, params: QueryParams = {}, token?: string): Promise<B[]> {
|
|
96
97
|
params.richText = String(params.richText) === 'true' ? true : false;
|
|
97
98
|
params.draft = RemoteServiceStrategy.draft;
|
|
98
99
|
params.pagination = false;
|
|
@@ -100,21 +101,21 @@ export class RemoteService<T extends IEntity = IEntity> implements IQuerable<T>
|
|
|
100
101
|
const search = query ? `?${query}` : '';
|
|
101
102
|
const url = `/${this.key}${search}`;
|
|
102
103
|
// console.log('RemoteService', this.key, 'updateMany', url);
|
|
103
|
-
const response = await remoteApiPatch<B[] | { docs: B[] }>(url, payload);
|
|
104
|
+
const response = await remoteApiPatch<B[] | { docs: B[] }>(url, payload, getRemoteApiRequestOptions(token));
|
|
104
105
|
const items: B[] = Array.isArray(response) ? response : response.docs;
|
|
105
106
|
return await this.decorateMany_(items, params);
|
|
106
107
|
}
|
|
107
108
|
|
|
108
|
-
async deleteOne<B extends T = T>(id: IEquatable, params: Omit<QueryParams, 'where'> = {}): Promise<B | undefined> {
|
|
109
|
+
async deleteOne<B extends T = T>(id: IEquatable, params: Omit<QueryParams, 'where'> = {}, token?: string): Promise<B | undefined> {
|
|
109
110
|
const query = qsSerialize(params);
|
|
110
111
|
const search = query ? `?${query}` : '';
|
|
111
112
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
112
113
|
// console.log('RemoteService', this.key, 'deleteOne', url);
|
|
113
|
-
const item = await remoteApiDelete<B>(url);
|
|
114
|
+
const item = await remoteApiDelete<B>(url, getRemoteApiRequestOptions(token));
|
|
114
115
|
return await this.decorateOne_(item, {});
|
|
115
116
|
}
|
|
116
117
|
|
|
117
|
-
async deleteMany<B extends T = T>(params: QueryParams = {}): Promise<B[]> {
|
|
118
|
+
async deleteMany<B extends T = T>(params: QueryParams = {}, token?: string): Promise<B[]> {
|
|
118
119
|
params.richText = String(params.richText) === 'true' ? true : false;
|
|
119
120
|
params.draft = RemoteServiceStrategy.draft;
|
|
120
121
|
params.pagination = false;
|
|
@@ -122,22 +123,22 @@ export class RemoteService<T extends IEntity = IEntity> implements IQuerable<T>
|
|
|
122
123
|
const search = query ? `?${query}` : '';
|
|
123
124
|
const url = `/${this.key}${search}`;
|
|
124
125
|
// console.log('RemoteService', this.key, 'deleteMany', url);
|
|
125
|
-
const response = await remoteApiDelete<B[] | { docs: B[] }>(url);
|
|
126
|
+
const response = await remoteApiDelete<B[] | { docs: B[] }>(url, getRemoteApiRequestOptions(token));
|
|
126
127
|
const items: B[] = Array.isArray(response) ? response : response.docs;
|
|
127
128
|
return await this.decorateMany_(items, params);
|
|
128
129
|
}
|
|
129
130
|
|
|
130
131
|
// !!! old
|
|
131
132
|
|
|
132
|
-
async update<B extends T = T>(payload: Partial<B>, params: QueryParams = {}): Promise<B> {
|
|
133
|
+
async update<B extends T = T>(payload: Partial<B>, params: QueryParams = {}, token?: string): Promise<B> {
|
|
133
134
|
const url = `/${this.key}`;
|
|
134
135
|
// console.log('RemoteService', this.key, 'update', url);
|
|
135
|
-
const result = await remoteApiPatch<B | { doc: B }>(url, payload);
|
|
136
|
+
const result = await remoteApiPatch<B | { doc: B }>(url, payload, getRemoteApiRequestOptions(token));
|
|
136
137
|
const item = (result.doc ? result.doc : result) as B;
|
|
137
138
|
return await this.decorateOne_(item, {});
|
|
138
139
|
}
|
|
139
140
|
|
|
140
|
-
async delete<B extends T = T>(id: IEquatable): Promise<B | undefined> {
|
|
141
|
+
async delete<B extends T = T>(id: IEquatable, token?: string): Promise<B | undefined> {
|
|
141
142
|
const params = {
|
|
142
143
|
where: {
|
|
143
144
|
id: {
|
|
@@ -149,7 +150,7 @@ export class RemoteService<T extends IEntity = IEntity> implements IQuerable<T>
|
|
|
149
150
|
const search = query ? `?${query}` : '';
|
|
150
151
|
const url = `/${this.key}${search}`;
|
|
151
152
|
// console.log('RemoteService', this.key, 'delete', url);
|
|
152
|
-
const item = await remoteApiDelete<B>(url);
|
|
153
|
+
const item = await remoteApiDelete<B>(url, getRemoteApiRequestOptions(token));
|
|
153
154
|
return await this.decorateOne_(item, {});
|
|
154
155
|
}
|
|
155
156
|
|