@websolutespa/bom-mixer-store 1.8.4 → 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 +16 -2
- package/dist/index.d.ts +13 -13
- package/dist/index.js +120 -32
- package/dist/index.mjs +120 -31
- package/package.json +1 -1
- package/src/api/api.ts +6 -14
- package/src/cached/cached.service.ts +18 -0
- package/src/remote/remote.service.ts +67 -18
- package/src/session/session.service.ts +25 -0
- package/src/store/store.handler.ts +57 -8
- package/src/vars/vars.handler.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
# @websolutespa/bom-mixer-store
|
|
2
2
|
|
|
3
|
+
## 1.8.6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Modified: RemoteService
|
|
8
|
+
|
|
9
|
+
## 1.8.5
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Modified: IQuerable
|
|
14
|
+
- Updated dependencies
|
|
15
|
+
- @websolutespa/bom-core@1.8.12
|
|
16
|
+
|
|
3
17
|
## 1.8.4
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
|
6
20
|
|
|
7
|
-
- Modified:
|
|
21
|
+
- Modified: decorateCategory\_
|
|
8
22
|
- Updated dependencies
|
|
9
23
|
- @websolutespa/bom-core@1.8.11
|
|
10
24
|
|
|
@@ -12,7 +26,7 @@
|
|
|
12
26
|
|
|
13
27
|
### Patch Changes
|
|
14
28
|
|
|
15
|
-
- Removed:
|
|
29
|
+
- Removed: optimizeMany\_
|
|
16
30
|
|
|
17
31
|
## 1.8.2
|
|
18
32
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
import { FetchRequestOptions, IEquatable, ICategory, IStore, QueryParams, ICategorized, IRoute, ISchema, IUndecoratedMenu, IMenu, IEntity, JsonService, IMemoryStore, IQuerable, IPagedResult } from '@websolutespa/bom-core';
|
|
2
2
|
import { NextApiRequest, NextApiResponse } from 'next';
|
|
3
3
|
import { NextRequest } from 'next/server';
|
|
4
|
-
import * as _websolutespa_bom_mixer_store from '@websolutespa/bom-mixer-store';
|
|
5
4
|
|
|
6
5
|
type ApiRequestOptions = FetchRequestOptions;
|
|
7
6
|
declare function getLocalApiRequestOptions(): ApiRequestOptions;
|
|
8
|
-
declare
|
|
9
|
-
token: string | null;
|
|
10
|
-
};
|
|
11
|
-
declare function getRemoteApiRequestOptions(): ApiRequestOptions;
|
|
7
|
+
declare function getRemoteApiRequestOptions(token?: string): ApiRequestOptions;
|
|
12
8
|
|
|
13
9
|
type ICrudMethod = 'get' | 'post' | 'put' | 'patch' | 'delete' | string;
|
|
14
10
|
type IHandler<T = any> = (request: NextApiRequest, response: NextApiResponse<T>) => Promise<NextApiResponse<T> | void>;
|
|
@@ -146,12 +142,16 @@ declare class RemoteService<T extends IEntity = IEntity> implements IQuerable<T>
|
|
|
146
142
|
key: string;
|
|
147
143
|
store: IStore;
|
|
148
144
|
constructor(key: string, store: IStore);
|
|
149
|
-
findOne<B extends T = T>(id: IEquatable, params?: QueryParams): Promise<B | undefined>;
|
|
150
|
-
findMany<B extends T = T>(params?: QueryParams): Promise<B[]>;
|
|
151
|
-
findPaged<B extends T = T>(params?: QueryParams): Promise<IPagedResult<B>>;
|
|
152
|
-
create<B extends T = T>(payload: Omit<B, 'id'
|
|
153
|
-
|
|
154
|
-
|
|
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>;
|
|
155
155
|
protected decorateOne_<B extends T = T>(item: B, params?: QueryParams): Promise<B>;
|
|
156
156
|
protected decorateMany_<B extends T = T>(items: B[], params?: QueryParams): Promise<B[]>;
|
|
157
157
|
}
|
|
@@ -250,6 +250,6 @@ declare function findRoute(store: IStore, pathname: string, href: string, hrefBe
|
|
|
250
250
|
|
|
251
251
|
declare function getStore<T extends IStore>(): Promise<T>;
|
|
252
252
|
|
|
253
|
-
declare function varsHandler():
|
|
253
|
+
declare function varsHandler(): IHandler<any>;
|
|
254
254
|
|
|
255
|
-
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 {
|
|
@@ -1433,6 +1429,19 @@ var CachedService = class extends import_bom_core11.JsonService {
|
|
|
1433
1429
|
async create(payload) {
|
|
1434
1430
|
return await this.service_.create(payload);
|
|
1435
1431
|
}
|
|
1432
|
+
async updateOne(id, payload, params = {}) {
|
|
1433
|
+
return await this.service_.updateOne(id, payload, params);
|
|
1434
|
+
}
|
|
1435
|
+
async updateMany(payload, params = {}) {
|
|
1436
|
+
return await this.service_.updateMany(payload, params);
|
|
1437
|
+
}
|
|
1438
|
+
async deleteOne(id, params = {}) {
|
|
1439
|
+
return await this.service_.deleteOne(id, params);
|
|
1440
|
+
}
|
|
1441
|
+
async deleteMany(params = {}) {
|
|
1442
|
+
return await this.service_.deleteMany(params);
|
|
1443
|
+
}
|
|
1444
|
+
// !!! old
|
|
1436
1445
|
async update(payload) {
|
|
1437
1446
|
return await this.service_.update(payload);
|
|
1438
1447
|
}
|
|
@@ -1494,14 +1503,14 @@ var RemoteService = class {
|
|
|
1494
1503
|
this.key = key;
|
|
1495
1504
|
this.store = store;
|
|
1496
1505
|
}
|
|
1497
|
-
async findOne(id, params = {}) {
|
|
1506
|
+
async findOne(id, params = {}, token) {
|
|
1498
1507
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1499
1508
|
params.draft = RemoteServiceStrategy.draft;
|
|
1500
1509
|
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1501
1510
|
const search = query ? `?${query}` : "";
|
|
1502
1511
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1503
1512
|
try {
|
|
1504
|
-
const item = await remoteApiGet(url);
|
|
1513
|
+
const item = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1505
1514
|
if (item) {
|
|
1506
1515
|
return await this.decorateOne_(item, params);
|
|
1507
1516
|
}
|
|
@@ -1510,45 +1519,84 @@ var RemoteService = class {
|
|
|
1510
1519
|
}
|
|
1511
1520
|
return;
|
|
1512
1521
|
}
|
|
1513
|
-
async findMany(params = {}) {
|
|
1522
|
+
async findMany(params = {}, token) {
|
|
1514
1523
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1515
1524
|
params.draft = RemoteServiceStrategy.draft;
|
|
1516
1525
|
params.pagination = false;
|
|
1517
1526
|
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1518
1527
|
const search = query ? `?${query}` : "";
|
|
1519
1528
|
const url = `/${this.key}${search}`;
|
|
1520
|
-
const response = await remoteApiGet(url);
|
|
1521
|
-
const items = Array.isArray(response
|
|
1529
|
+
const response = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1530
|
+
const items = Array.isArray(response) ? response : response.docs;
|
|
1522
1531
|
return await this.decorateMany_(items, params);
|
|
1523
1532
|
}
|
|
1524
|
-
async findPaged(params = {}) {
|
|
1533
|
+
async findPaged(params = {}, token) {
|
|
1525
1534
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1526
1535
|
params.draft = RemoteServiceStrategy.draft;
|
|
1527
1536
|
params.pagination = true;
|
|
1528
1537
|
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1529
1538
|
const search = query ? `?${query}` : "";
|
|
1530
1539
|
const url = `/${this.key}${search}`;
|
|
1531
|
-
const response = await remoteApiGet(url);
|
|
1540
|
+
const response = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1532
1541
|
let items = response.docs;
|
|
1533
1542
|
items = await this.decorateMany_(items, params);
|
|
1534
1543
|
return { ...response, docs: items };
|
|
1535
1544
|
}
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
const
|
|
1545
|
+
async create(payload, params = {}, token) {
|
|
1546
|
+
params.richText = String(params.richText) === "true" ? true : false;
|
|
1547
|
+
params.draft = RemoteServiceStrategy.draft;
|
|
1548
|
+
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1549
|
+
const search = query ? `?${query}` : "";
|
|
1550
|
+
const url = `/${this.key}${search}`;
|
|
1551
|
+
const result = await remoteApiPost(url, payload, getRemoteApiRequestOptions(token));
|
|
1540
1552
|
const item = result.doc ? result.doc : result;
|
|
1541
1553
|
return await this.decorateOne_(item, {});
|
|
1542
1554
|
}
|
|
1543
|
-
|
|
1544
|
-
|
|
1555
|
+
async updateOne(id, payload, params = {}, token) {
|
|
1556
|
+
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1557
|
+
const search = query ? `?${query}` : "";
|
|
1558
|
+
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1559
|
+
const result = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1560
|
+
const item = result.doc ? result.doc : result;
|
|
1561
|
+
return await this.decorateOne_(item, {});
|
|
1562
|
+
}
|
|
1563
|
+
async updateMany(payload, params = {}, token) {
|
|
1564
|
+
params.richText = String(params.richText) === "true" ? true : false;
|
|
1565
|
+
params.draft = RemoteServiceStrategy.draft;
|
|
1566
|
+
params.pagination = false;
|
|
1567
|
+
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1568
|
+
const search = query ? `?${query}` : "";
|
|
1569
|
+
const url = `/${this.key}${search}`;
|
|
1570
|
+
const response = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1571
|
+
const items = Array.isArray(response) ? response : response.docs;
|
|
1572
|
+
return await this.decorateMany_(items, params);
|
|
1573
|
+
}
|
|
1574
|
+
async deleteOne(id, params = {}, token) {
|
|
1575
|
+
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1576
|
+
const search = query ? `?${query}` : "";
|
|
1577
|
+
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1578
|
+
const item = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1579
|
+
return await this.decorateOne_(item, {});
|
|
1580
|
+
}
|
|
1581
|
+
async deleteMany(params = {}, token) {
|
|
1582
|
+
params.richText = String(params.richText) === "true" ? true : false;
|
|
1583
|
+
params.draft = RemoteServiceStrategy.draft;
|
|
1584
|
+
params.pagination = false;
|
|
1585
|
+
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1586
|
+
const search = query ? `?${query}` : "";
|
|
1587
|
+
const url = `/${this.key}${search}`;
|
|
1588
|
+
const response = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1589
|
+
const items = Array.isArray(response) ? response : response.docs;
|
|
1590
|
+
return await this.decorateMany_(items, params);
|
|
1591
|
+
}
|
|
1592
|
+
// !!! old
|
|
1593
|
+
async update(payload, params = {}, token) {
|
|
1545
1594
|
const url = `/${this.key}`;
|
|
1546
|
-
const result = await
|
|
1595
|
+
const result = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1547
1596
|
const item = result.doc ? result.doc : result;
|
|
1548
1597
|
return await this.decorateOne_(item, {});
|
|
1549
1598
|
}
|
|
1550
|
-
|
|
1551
|
-
async delete(id) {
|
|
1599
|
+
async delete(id, token) {
|
|
1552
1600
|
const params = {
|
|
1553
1601
|
where: {
|
|
1554
1602
|
id: {
|
|
@@ -1559,7 +1607,7 @@ var RemoteService = class {
|
|
|
1559
1607
|
const query = (0, import_bom_core13.qsSerialize)(params);
|
|
1560
1608
|
const search = query ? `?${query}` : "";
|
|
1561
1609
|
const url = `/${this.key}${search}`;
|
|
1562
|
-
const item = await remoteApiDelete(url);
|
|
1610
|
+
const item = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1563
1611
|
return await this.decorateOne_(item, {});
|
|
1564
1612
|
}
|
|
1565
1613
|
async decorateOne_(item, params = {}) {
|
|
@@ -1790,19 +1838,40 @@ function collectionPost(storePromise) {
|
|
|
1790
1838
|
function collectionPatch(storePromise) {
|
|
1791
1839
|
return async (request, response) => {
|
|
1792
1840
|
try {
|
|
1793
|
-
const
|
|
1794
|
-
|
|
1841
|
+
const { collections, ...query } = request.query;
|
|
1842
|
+
if (!Array.isArray(collections) || collections?.length === 0) {
|
|
1843
|
+
console.error("storeHandler.collectionUpdate", "method not allowed", collections);
|
|
1844
|
+
return response.status(405).send("method not allowed");
|
|
1845
|
+
}
|
|
1795
1846
|
if (!Array.isArray(collections) || collections.length !== 2) {
|
|
1796
1847
|
console.error("method not allowed", collections);
|
|
1797
1848
|
return response.status(405).send("method not allowed");
|
|
1798
1849
|
}
|
|
1799
1850
|
const collection = collections[0];
|
|
1851
|
+
const id = collections[1];
|
|
1852
|
+
const qs = Object.entries(query).map((kv) => kv.join("=")).join("&");
|
|
1853
|
+
const params = (0, import_bom_core14.qsDeserialize)(qs);
|
|
1854
|
+
if (!COLLECTIONS.includes(collection)) {
|
|
1855
|
+
console.error("storeHandler.collectionUpdate", "method not allowed", collection);
|
|
1856
|
+
return response.status(405).send("method not allowed");
|
|
1857
|
+
}
|
|
1800
1858
|
const store = await storePromise();
|
|
1801
1859
|
const service = store[collection];
|
|
1802
1860
|
if (!service) {
|
|
1803
|
-
console.error("method not allowed", collection);
|
|
1861
|
+
console.error("storeHandler.collectionUpdate", "method not allowed", collection);
|
|
1804
1862
|
return response.status(405).send("method not allowed");
|
|
1805
1863
|
}
|
|
1864
|
+
if (id) {
|
|
1865
|
+
const item = await service.updateOne((0, import_bom_core14.asEquatable)(id), params);
|
|
1866
|
+
if (item) {
|
|
1867
|
+
return response.status(200).json(item);
|
|
1868
|
+
} else {
|
|
1869
|
+
return response.status(404).send("not found");
|
|
1870
|
+
}
|
|
1871
|
+
} else {
|
|
1872
|
+
const items = await service.updateMany(params);
|
|
1873
|
+
return response.status(200).json(items);
|
|
1874
|
+
}
|
|
1806
1875
|
} catch (error) {
|
|
1807
1876
|
console.error("storeHandler.error.500", error);
|
|
1808
1877
|
throw error;
|
|
@@ -1812,20 +1881,40 @@ function collectionPatch(storePromise) {
|
|
|
1812
1881
|
function collectionDelete(storePromise) {
|
|
1813
1882
|
return async (request, response) => {
|
|
1814
1883
|
try {
|
|
1815
|
-
const
|
|
1816
|
-
|
|
1884
|
+
const { collections, ...query } = request.query;
|
|
1885
|
+
if (!Array.isArray(collections) || collections?.length === 0) {
|
|
1886
|
+
console.error("storeHandler.collectionDelete", "method not allowed", collections);
|
|
1887
|
+
return response.status(405).send("method not allowed");
|
|
1888
|
+
}
|
|
1817
1889
|
if (!Array.isArray(collections) || collections.length !== 2) {
|
|
1818
1890
|
console.error("method not allowed", collections);
|
|
1819
1891
|
return response.status(405).send("method not allowed");
|
|
1820
1892
|
}
|
|
1821
1893
|
const collection = collections[0];
|
|
1822
1894
|
const id = collections[1];
|
|
1895
|
+
const qs = Object.entries(query).map((kv) => kv.join("=")).join("&");
|
|
1896
|
+
const params = (0, import_bom_core14.qsDeserialize)(qs);
|
|
1897
|
+
if (!COLLECTIONS.includes(collection)) {
|
|
1898
|
+
console.error("storeHandler.collectionDelete", "method not allowed", collection);
|
|
1899
|
+
return response.status(405).send("method not allowed");
|
|
1900
|
+
}
|
|
1823
1901
|
const store = await storePromise();
|
|
1824
1902
|
const service = store[collection];
|
|
1825
1903
|
if (!service) {
|
|
1826
|
-
console.error("method not allowed", collection);
|
|
1904
|
+
console.error("storeHandler.collectionDelete", "method not allowed", collection);
|
|
1827
1905
|
return response.status(405).send("method not allowed");
|
|
1828
1906
|
}
|
|
1907
|
+
if (id) {
|
|
1908
|
+
const item = await service.deleteOne((0, import_bom_core14.asEquatable)(id), params);
|
|
1909
|
+
if (item) {
|
|
1910
|
+
return response.status(200).json(item);
|
|
1911
|
+
} else {
|
|
1912
|
+
return response.status(404).send("not found");
|
|
1913
|
+
}
|
|
1914
|
+
} else {
|
|
1915
|
+
const items = await service.deleteMany(params);
|
|
1916
|
+
return response.status(200).json(items);
|
|
1917
|
+
}
|
|
1829
1918
|
} catch (error) {
|
|
1830
1919
|
console.error("storeHandler.error.500", error);
|
|
1831
1920
|
throw error;
|
|
@@ -2407,7 +2496,6 @@ function varsHandler() {
|
|
|
2407
2496
|
MockPageService,
|
|
2408
2497
|
MockService,
|
|
2409
2498
|
NextPhase,
|
|
2410
|
-
REMOTE_API_SESSION,
|
|
2411
2499
|
RemoteMenuService,
|
|
2412
2500
|
RemotePageService,
|
|
2413
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 {
|
|
@@ -1327,6 +1324,19 @@ var CachedService = class extends JsonService3 {
|
|
|
1327
1324
|
async create(payload) {
|
|
1328
1325
|
return await this.service_.create(payload);
|
|
1329
1326
|
}
|
|
1327
|
+
async updateOne(id, payload, params = {}) {
|
|
1328
|
+
return await this.service_.updateOne(id, payload, params);
|
|
1329
|
+
}
|
|
1330
|
+
async updateMany(payload, params = {}) {
|
|
1331
|
+
return await this.service_.updateMany(payload, params);
|
|
1332
|
+
}
|
|
1333
|
+
async deleteOne(id, params = {}) {
|
|
1334
|
+
return await this.service_.deleteOne(id, params);
|
|
1335
|
+
}
|
|
1336
|
+
async deleteMany(params = {}) {
|
|
1337
|
+
return await this.service_.deleteMany(params);
|
|
1338
|
+
}
|
|
1339
|
+
// !!! old
|
|
1330
1340
|
async update(payload) {
|
|
1331
1341
|
return await this.service_.update(payload);
|
|
1332
1342
|
}
|
|
@@ -1388,14 +1398,14 @@ var RemoteService = class {
|
|
|
1388
1398
|
this.key = key;
|
|
1389
1399
|
this.store = store;
|
|
1390
1400
|
}
|
|
1391
|
-
async findOne(id, params = {}) {
|
|
1401
|
+
async findOne(id, params = {}, token) {
|
|
1392
1402
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1393
1403
|
params.draft = RemoteServiceStrategy.draft;
|
|
1394
1404
|
const query = qsSerialize2(params);
|
|
1395
1405
|
const search = query ? `?${query}` : "";
|
|
1396
1406
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1397
1407
|
try {
|
|
1398
|
-
const item = await remoteApiGet(url);
|
|
1408
|
+
const item = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1399
1409
|
if (item) {
|
|
1400
1410
|
return await this.decorateOne_(item, params);
|
|
1401
1411
|
}
|
|
@@ -1404,45 +1414,84 @@ var RemoteService = class {
|
|
|
1404
1414
|
}
|
|
1405
1415
|
return;
|
|
1406
1416
|
}
|
|
1407
|
-
async findMany(params = {}) {
|
|
1417
|
+
async findMany(params = {}, token) {
|
|
1408
1418
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1409
1419
|
params.draft = RemoteServiceStrategy.draft;
|
|
1410
1420
|
params.pagination = false;
|
|
1411
1421
|
const query = qsSerialize2(params);
|
|
1412
1422
|
const search = query ? `?${query}` : "";
|
|
1413
1423
|
const url = `/${this.key}${search}`;
|
|
1414
|
-
const response = await remoteApiGet(url);
|
|
1415
|
-
const items = Array.isArray(response
|
|
1424
|
+
const response = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1425
|
+
const items = Array.isArray(response) ? response : response.docs;
|
|
1416
1426
|
return await this.decorateMany_(items, params);
|
|
1417
1427
|
}
|
|
1418
|
-
async findPaged(params = {}) {
|
|
1428
|
+
async findPaged(params = {}, token) {
|
|
1419
1429
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1420
1430
|
params.draft = RemoteServiceStrategy.draft;
|
|
1421
1431
|
params.pagination = true;
|
|
1422
1432
|
const query = qsSerialize2(params);
|
|
1423
1433
|
const search = query ? `?${query}` : "";
|
|
1424
1434
|
const url = `/${this.key}${search}`;
|
|
1425
|
-
const response = await remoteApiGet(url);
|
|
1435
|
+
const response = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1426
1436
|
let items = response.docs;
|
|
1427
1437
|
items = await this.decorateMany_(items, params);
|
|
1428
1438
|
return { ...response, docs: items };
|
|
1429
1439
|
}
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
const
|
|
1440
|
+
async create(payload, params = {}, token) {
|
|
1441
|
+
params.richText = String(params.richText) === "true" ? true : false;
|
|
1442
|
+
params.draft = RemoteServiceStrategy.draft;
|
|
1443
|
+
const query = qsSerialize2(params);
|
|
1444
|
+
const search = query ? `?${query}` : "";
|
|
1445
|
+
const url = `/${this.key}${search}`;
|
|
1446
|
+
const result = await remoteApiPost(url, payload, getRemoteApiRequestOptions(token));
|
|
1434
1447
|
const item = result.doc ? result.doc : result;
|
|
1435
1448
|
return await this.decorateOne_(item, {});
|
|
1436
1449
|
}
|
|
1437
|
-
|
|
1438
|
-
|
|
1450
|
+
async updateOne(id, payload, params = {}, token) {
|
|
1451
|
+
const query = qsSerialize2(params);
|
|
1452
|
+
const search = query ? `?${query}` : "";
|
|
1453
|
+
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1454
|
+
const result = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1455
|
+
const item = result.doc ? result.doc : result;
|
|
1456
|
+
return await this.decorateOne_(item, {});
|
|
1457
|
+
}
|
|
1458
|
+
async updateMany(payload, params = {}, token) {
|
|
1459
|
+
params.richText = String(params.richText) === "true" ? true : false;
|
|
1460
|
+
params.draft = RemoteServiceStrategy.draft;
|
|
1461
|
+
params.pagination = false;
|
|
1462
|
+
const query = qsSerialize2(params);
|
|
1463
|
+
const search = query ? `?${query}` : "";
|
|
1464
|
+
const url = `/${this.key}${search}`;
|
|
1465
|
+
const response = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1466
|
+
const items = Array.isArray(response) ? response : response.docs;
|
|
1467
|
+
return await this.decorateMany_(items, params);
|
|
1468
|
+
}
|
|
1469
|
+
async deleteOne(id, params = {}, token) {
|
|
1470
|
+
const query = qsSerialize2(params);
|
|
1471
|
+
const search = query ? `?${query}` : "";
|
|
1472
|
+
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1473
|
+
const item = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1474
|
+
return await this.decorateOne_(item, {});
|
|
1475
|
+
}
|
|
1476
|
+
async deleteMany(params = {}, token) {
|
|
1477
|
+
params.richText = String(params.richText) === "true" ? true : false;
|
|
1478
|
+
params.draft = RemoteServiceStrategy.draft;
|
|
1479
|
+
params.pagination = false;
|
|
1480
|
+
const query = qsSerialize2(params);
|
|
1481
|
+
const search = query ? `?${query}` : "";
|
|
1482
|
+
const url = `/${this.key}${search}`;
|
|
1483
|
+
const response = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1484
|
+
const items = Array.isArray(response) ? response : response.docs;
|
|
1485
|
+
return await this.decorateMany_(items, params);
|
|
1486
|
+
}
|
|
1487
|
+
// !!! old
|
|
1488
|
+
async update(payload, params = {}, token) {
|
|
1439
1489
|
const url = `/${this.key}`;
|
|
1440
|
-
const result = await
|
|
1490
|
+
const result = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1441
1491
|
const item = result.doc ? result.doc : result;
|
|
1442
1492
|
return await this.decorateOne_(item, {});
|
|
1443
1493
|
}
|
|
1444
|
-
|
|
1445
|
-
async delete(id) {
|
|
1494
|
+
async delete(id, token) {
|
|
1446
1495
|
const params = {
|
|
1447
1496
|
where: {
|
|
1448
1497
|
id: {
|
|
@@ -1453,7 +1502,7 @@ var RemoteService = class {
|
|
|
1453
1502
|
const query = qsSerialize2(params);
|
|
1454
1503
|
const search = query ? `?${query}` : "";
|
|
1455
1504
|
const url = `/${this.key}${search}`;
|
|
1456
|
-
const item = await remoteApiDelete(url);
|
|
1505
|
+
const item = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1457
1506
|
return await this.decorateOne_(item, {});
|
|
1458
1507
|
}
|
|
1459
1508
|
async decorateOne_(item, params = {}) {
|
|
@@ -1684,19 +1733,40 @@ function collectionPost(storePromise) {
|
|
|
1684
1733
|
function collectionPatch(storePromise) {
|
|
1685
1734
|
return async (request, response) => {
|
|
1686
1735
|
try {
|
|
1687
|
-
const
|
|
1688
|
-
|
|
1736
|
+
const { collections, ...query } = request.query;
|
|
1737
|
+
if (!Array.isArray(collections) || collections?.length === 0) {
|
|
1738
|
+
console.error("storeHandler.collectionUpdate", "method not allowed", collections);
|
|
1739
|
+
return response.status(405).send("method not allowed");
|
|
1740
|
+
}
|
|
1689
1741
|
if (!Array.isArray(collections) || collections.length !== 2) {
|
|
1690
1742
|
console.error("method not allowed", collections);
|
|
1691
1743
|
return response.status(405).send("method not allowed");
|
|
1692
1744
|
}
|
|
1693
1745
|
const collection = collections[0];
|
|
1746
|
+
const id = collections[1];
|
|
1747
|
+
const qs = Object.entries(query).map((kv) => kv.join("=")).join("&");
|
|
1748
|
+
const params = qsDeserialize(qs);
|
|
1749
|
+
if (!COLLECTIONS.includes(collection)) {
|
|
1750
|
+
console.error("storeHandler.collectionUpdate", "method not allowed", collection);
|
|
1751
|
+
return response.status(405).send("method not allowed");
|
|
1752
|
+
}
|
|
1694
1753
|
const store = await storePromise();
|
|
1695
1754
|
const service = store[collection];
|
|
1696
1755
|
if (!service) {
|
|
1697
|
-
console.error("method not allowed", collection);
|
|
1756
|
+
console.error("storeHandler.collectionUpdate", "method not allowed", collection);
|
|
1698
1757
|
return response.status(405).send("method not allowed");
|
|
1699
1758
|
}
|
|
1759
|
+
if (id) {
|
|
1760
|
+
const item = await service.updateOne(asEquatable(id), params);
|
|
1761
|
+
if (item) {
|
|
1762
|
+
return response.status(200).json(item);
|
|
1763
|
+
} else {
|
|
1764
|
+
return response.status(404).send("not found");
|
|
1765
|
+
}
|
|
1766
|
+
} else {
|
|
1767
|
+
const items = await service.updateMany(params);
|
|
1768
|
+
return response.status(200).json(items);
|
|
1769
|
+
}
|
|
1700
1770
|
} catch (error) {
|
|
1701
1771
|
console.error("storeHandler.error.500", error);
|
|
1702
1772
|
throw error;
|
|
@@ -1706,20 +1776,40 @@ function collectionPatch(storePromise) {
|
|
|
1706
1776
|
function collectionDelete(storePromise) {
|
|
1707
1777
|
return async (request, response) => {
|
|
1708
1778
|
try {
|
|
1709
|
-
const
|
|
1710
|
-
|
|
1779
|
+
const { collections, ...query } = request.query;
|
|
1780
|
+
if (!Array.isArray(collections) || collections?.length === 0) {
|
|
1781
|
+
console.error("storeHandler.collectionDelete", "method not allowed", collections);
|
|
1782
|
+
return response.status(405).send("method not allowed");
|
|
1783
|
+
}
|
|
1711
1784
|
if (!Array.isArray(collections) || collections.length !== 2) {
|
|
1712
1785
|
console.error("method not allowed", collections);
|
|
1713
1786
|
return response.status(405).send("method not allowed");
|
|
1714
1787
|
}
|
|
1715
1788
|
const collection = collections[0];
|
|
1716
1789
|
const id = collections[1];
|
|
1790
|
+
const qs = Object.entries(query).map((kv) => kv.join("=")).join("&");
|
|
1791
|
+
const params = qsDeserialize(qs);
|
|
1792
|
+
if (!COLLECTIONS.includes(collection)) {
|
|
1793
|
+
console.error("storeHandler.collectionDelete", "method not allowed", collection);
|
|
1794
|
+
return response.status(405).send("method not allowed");
|
|
1795
|
+
}
|
|
1717
1796
|
const store = await storePromise();
|
|
1718
1797
|
const service = store[collection];
|
|
1719
1798
|
if (!service) {
|
|
1720
|
-
console.error("method not allowed", collection);
|
|
1799
|
+
console.error("storeHandler.collectionDelete", "method not allowed", collection);
|
|
1721
1800
|
return response.status(405).send("method not allowed");
|
|
1722
1801
|
}
|
|
1802
|
+
if (id) {
|
|
1803
|
+
const item = await service.deleteOne(asEquatable(id), params);
|
|
1804
|
+
if (item) {
|
|
1805
|
+
return response.status(200).json(item);
|
|
1806
|
+
} else {
|
|
1807
|
+
return response.status(404).send("not found");
|
|
1808
|
+
}
|
|
1809
|
+
} else {
|
|
1810
|
+
const items = await service.deleteMany(params);
|
|
1811
|
+
return response.status(200).json(items);
|
|
1812
|
+
}
|
|
1723
1813
|
} catch (error) {
|
|
1724
1814
|
console.error("storeHandler.error.500", error);
|
|
1725
1815
|
throw error;
|
|
@@ -2300,7 +2390,6 @@ export {
|
|
|
2300
2390
|
MockPageService,
|
|
2301
2391
|
MockService,
|
|
2302
2392
|
NextPhase,
|
|
2303
|
-
REMOTE_API_SESSION,
|
|
2304
2393
|
RemoteMenuService,
|
|
2305
2394
|
RemotePageService,
|
|
2306
2395
|
RemoteService,
|
package/package.json
CHANGED
package/src/api/api.ts
CHANGED
|
@@ -9,26 +9,18 @@ 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
|
-
/**
|
|
19
|
-
* authenticated end user JWT token bearer (end_users)
|
|
20
|
-
*/
|
|
12
|
+
export function getRemoteApiRequestOptions(token?: string): ApiRequestOptions {
|
|
13
|
+
if (token) {
|
|
14
|
+
// authenticated end user JWT token bearer (end_users)
|
|
21
15
|
return {
|
|
22
16
|
// mode: 'cors',
|
|
23
17
|
headers: {
|
|
24
|
-
Authorization: `
|
|
25
|
-
// Authorization: `
|
|
18
|
+
Authorization: `JWT ${token}`,
|
|
19
|
+
// Authorization: `Bearer ${token}`,
|
|
26
20
|
},
|
|
27
21
|
};
|
|
28
22
|
} else {
|
|
29
|
-
|
|
30
|
-
* guest user api key (users)
|
|
31
|
-
*/
|
|
23
|
+
// guest user api key (users)
|
|
32
24
|
return {
|
|
33
25
|
// mode: 'cors',
|
|
34
26
|
headers: {
|
|
@@ -104,6 +104,24 @@ export class CachedService<T extends IEntity = IEntity> extends JsonService<T> {
|
|
|
104
104
|
return await this.service_.create(payload);
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
+
async updateOne<B extends T = T>(id: IEquatable, payload: Partial<B>, params: Omit<QueryParams, 'where'> = {}): Promise<B> {
|
|
108
|
+
return await this.service_.updateOne(id, payload, params);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async updateMany<B extends T = T>(payload: Partial<B>, params: QueryParams = {}): Promise<B[]> {
|
|
112
|
+
return await this.service_.updateMany(payload, params);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async deleteOne<B extends T = T>(id: IEquatable, params: Omit<QueryParams, 'where'> = {}): Promise<B | undefined> {
|
|
116
|
+
return await this.service_.deleteOne(id, params);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async deleteMany<B extends T = T>(params: QueryParams = {}): Promise<B[]> {
|
|
120
|
+
return await this.service_.deleteMany(params);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// !!! old
|
|
124
|
+
|
|
107
125
|
async update<B extends T = T>(payload: Partial<B>): Promise<B> {
|
|
108
126
|
return await this.service_.update(payload);
|
|
109
127
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
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
|
-
import { remoteApiDelete, remoteApiGet,
|
|
4
|
+
import { remoteApiDelete, remoteApiGet, remoteApiPatch, remoteApiPost } from './remote.api';
|
|
4
5
|
|
|
5
6
|
export const RemoteServiceStrategy = {
|
|
6
7
|
draft: false,
|
|
@@ -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: QueryParams = {}): 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
|
|
55
|
-
const items: B[] = Array.isArray(response
|
|
55
|
+
const response = await remoteApiGet<B[] | { docs: B[] }>(url, getRemoteApiRequestOptions(token));
|
|
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,32 +65,80 @@ 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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
74
|
+
async create<B extends T = T>(payload: Omit<B, 'id'>, params: QueryParams = {}, token?: string): Promise<B> {
|
|
75
|
+
params.richText = String(params.richText) === 'true' ? true : false;
|
|
76
|
+
params.draft = RemoteServiceStrategy.draft;
|
|
77
|
+
const query = qsSerialize(params);
|
|
78
|
+
const search = query ? `?${query}` : '';
|
|
79
|
+
const url = `/${this.key}${search}`;
|
|
76
80
|
// console.log('RemoteService', this.key, 'create', url);
|
|
77
|
-
const result = await remoteApiPost<B | { doc: B }>(url, payload);
|
|
81
|
+
const result = await remoteApiPost<B | { doc: B }>(url, payload, getRemoteApiRequestOptions(token));
|
|
78
82
|
const item = (result.doc ? result.doc : result) as B;
|
|
79
83
|
return await this.decorateOne_(item, {});
|
|
80
84
|
}
|
|
81
85
|
|
|
82
|
-
|
|
83
|
-
|
|
86
|
+
async updateOne<B extends T = T>(id: IEquatable, payload: Partial<B>, params: Omit<QueryParams, 'where'> = {}, token?: string): Promise<B> {
|
|
87
|
+
const query = qsSerialize(params);
|
|
88
|
+
const search = query ? `?${query}` : '';
|
|
89
|
+
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
90
|
+
// console.log('RemoteService', this.key, 'updateOne', url);
|
|
91
|
+
const result = await remoteApiPatch<B | { doc: B }>(url, payload, getRemoteApiRequestOptions(token));
|
|
92
|
+
const item = (result.doc ? result.doc : result) as B;
|
|
93
|
+
return await this.decorateOne_(item, {});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async updateMany<B extends T = T>(payload: Partial<B>, params: QueryParams = {}, token?: string): Promise<B[]> {
|
|
97
|
+
params.richText = String(params.richText) === 'true' ? true : false;
|
|
98
|
+
params.draft = RemoteServiceStrategy.draft;
|
|
99
|
+
params.pagination = false;
|
|
100
|
+
const query = qsSerialize(params);
|
|
101
|
+
const search = query ? `?${query}` : '';
|
|
102
|
+
const url = `/${this.key}${search}`;
|
|
103
|
+
// console.log('RemoteService', this.key, 'updateMany', url);
|
|
104
|
+
const response = await remoteApiPatch<B[] | { docs: B[] }>(url, payload, getRemoteApiRequestOptions(token));
|
|
105
|
+
const items: B[] = Array.isArray(response) ? response : response.docs;
|
|
106
|
+
return await this.decorateMany_(items, params);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async deleteOne<B extends T = T>(id: IEquatable, params: Omit<QueryParams, 'where'> = {}, token?: string): Promise<B | undefined> {
|
|
110
|
+
const query = qsSerialize(params);
|
|
111
|
+
const search = query ? `?${query}` : '';
|
|
112
|
+
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
113
|
+
// console.log('RemoteService', this.key, 'deleteOne', url);
|
|
114
|
+
const item = await remoteApiDelete<B>(url, getRemoteApiRequestOptions(token));
|
|
115
|
+
return await this.decorateOne_(item, {});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async deleteMany<B extends T = T>(params: QueryParams = {}, token?: string): Promise<B[]> {
|
|
119
|
+
params.richText = String(params.richText) === 'true' ? true : false;
|
|
120
|
+
params.draft = RemoteServiceStrategy.draft;
|
|
121
|
+
params.pagination = false;
|
|
122
|
+
const query = qsSerialize(params);
|
|
123
|
+
const search = query ? `?${query}` : '';
|
|
124
|
+
const url = `/${this.key}${search}`;
|
|
125
|
+
// console.log('RemoteService', this.key, 'deleteMany', url);
|
|
126
|
+
const response = await remoteApiDelete<B[] | { docs: B[] }>(url, getRemoteApiRequestOptions(token));
|
|
127
|
+
const items: B[] = Array.isArray(response) ? response : response.docs;
|
|
128
|
+
return await this.decorateMany_(items, params);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// !!! old
|
|
132
|
+
|
|
133
|
+
async update<B extends T = T>(payload: Partial<B>, params: QueryParams = {}, token?: string): Promise<B> {
|
|
84
134
|
const url = `/${this.key}`;
|
|
85
135
|
// console.log('RemoteService', this.key, 'update', url);
|
|
86
|
-
const result = await
|
|
136
|
+
const result = await remoteApiPatch<B | { doc: B }>(url, payload, getRemoteApiRequestOptions(token));
|
|
87
137
|
const item = (result.doc ? result.doc : result) as B;
|
|
88
138
|
return await this.decorateOne_(item, {});
|
|
89
139
|
}
|
|
90
140
|
|
|
91
|
-
|
|
92
|
-
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> {
|
|
93
142
|
const params = {
|
|
94
143
|
where: {
|
|
95
144
|
id: {
|
|
@@ -101,7 +150,7 @@ export class RemoteService<T extends IEntity = IEntity> implements IQuerable<T>
|
|
|
101
150
|
const search = query ? `?${query}` : '';
|
|
102
151
|
const url = `/${this.key}${search}`;
|
|
103
152
|
// console.log('RemoteService', this.key, 'delete', url);
|
|
104
|
-
const item = await remoteApiDelete<B>(url);
|
|
153
|
+
const item = await remoteApiDelete<B>(url, getRemoteApiRequestOptions(token));
|
|
105
154
|
return await this.decorateOne_(item, {});
|
|
106
155
|
}
|
|
107
156
|
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
import { isDevelopment } from '@websolutespa/bom-core';
|
|
3
|
+
import * as http from 'http';
|
|
4
|
+
import { getIronSession, IronSession, SessionOptions } from 'iron-session';
|
|
5
|
+
|
|
6
|
+
export async function getSession<T extends object = Record<string, any>>(
|
|
7
|
+
request: (http.IncomingMessage | Request & { cookies: any }),
|
|
8
|
+
response?: Response | http.ServerResponse
|
|
9
|
+
): Promise<IronSession<T>> {
|
|
10
|
+
const COOKIE_NAME = 'websolutespa-next-js';
|
|
11
|
+
const COOKIE_PASSWORD = process.env.SECRET_COOKIE_PASSWORD as string || 'SECRET_COOKIE_PASSWORD_32_CHARS_LONG';
|
|
12
|
+
const sessionOptions: SessionOptions = {
|
|
13
|
+
cookieName: COOKIE_NAME,
|
|
14
|
+
// secure: true should be used in production (HTTPS) but can't be used in development (HTTP)
|
|
15
|
+
cookieOptions: {
|
|
16
|
+
secure: !isDevelopment,
|
|
17
|
+
},
|
|
18
|
+
password: COOKIE_PASSWORD,
|
|
19
|
+
};
|
|
20
|
+
const session =
|
|
21
|
+
'cookies' in request ?
|
|
22
|
+
await getIronSession<T>(request.cookies, sessionOptions) :
|
|
23
|
+
await getIronSession<T>(request, response, sessionOptions);
|
|
24
|
+
return session;
|
|
25
|
+
}
|
|
@@ -102,20 +102,45 @@ export function collectionPost<T extends IStore>(storePromise: () => Promise<T>)
|
|
|
102
102
|
export function collectionPatch<T extends IStore>(storePromise: () => Promise<T>) {
|
|
103
103
|
return async (request: NextApiRequest, response: NextApiResponse) => {
|
|
104
104
|
try {
|
|
105
|
-
const
|
|
106
|
-
|
|
105
|
+
const { collections, ...query } = request.query;
|
|
106
|
+
// console.log('query', query);
|
|
107
|
+
if (!Array.isArray(collections) || collections?.length === 0) {
|
|
108
|
+
console.error('storeHandler.collectionUpdate', 'method not allowed', collections);
|
|
109
|
+
return response.status(405).send('method not allowed');
|
|
110
|
+
}
|
|
107
111
|
if (!Array.isArray(collections) || collections.length !== 2) {
|
|
108
112
|
console.error('method not allowed', collections);
|
|
109
113
|
return response.status(405).send('method not allowed');
|
|
110
114
|
}
|
|
111
115
|
const collection: string = collections[0];
|
|
116
|
+
const id: string = collections[1];
|
|
117
|
+
const qs = Object.entries(query).map(kv => kv.join('=')).join('&');
|
|
118
|
+
const params = qsDeserialize(qs);
|
|
119
|
+
// console.log('params', params);
|
|
120
|
+
if (!COLLECTIONS.includes(collection)) {
|
|
121
|
+
console.error('storeHandler.collectionUpdate', 'method not allowed', collection);
|
|
122
|
+
return response.status(405).send('method not allowed');
|
|
123
|
+
}
|
|
112
124
|
const store = await storePromise();
|
|
113
125
|
const service = store[collection];
|
|
114
126
|
if (!service) {
|
|
115
|
-
console.error('method not allowed', collection);
|
|
127
|
+
console.error('storeHandler.collectionUpdate', 'method not allowed', collection);
|
|
116
128
|
return response.status(405).send('method not allowed');
|
|
117
129
|
}
|
|
118
|
-
//
|
|
130
|
+
// update a specific document by id
|
|
131
|
+
if (id) {
|
|
132
|
+
// console.log('storeHandler.collectionUpdate', collection, id, params);
|
|
133
|
+
const item = await service.updateOne(asEquatable(id), params);
|
|
134
|
+
if (item) {
|
|
135
|
+
return response.status(200).json(item);
|
|
136
|
+
} else {
|
|
137
|
+
return response.status(404).send('not found');
|
|
138
|
+
}
|
|
139
|
+
} else {
|
|
140
|
+
// console.log('storeHandler.collectionUpdate', collection, params);
|
|
141
|
+
const items = await service.updateMany(params);
|
|
142
|
+
return response.status(200).json(items);
|
|
143
|
+
}
|
|
119
144
|
} catch (error: any) {
|
|
120
145
|
console.error('storeHandler.error.500', error);
|
|
121
146
|
throw error;
|
|
@@ -127,21 +152,45 @@ export function collectionPatch<T extends IStore>(storePromise: () => Promise<T>
|
|
|
127
152
|
export function collectionDelete<T extends IStore>(storePromise: () => Promise<T>) {
|
|
128
153
|
return async (request: NextApiRequest, response: NextApiResponse) => {
|
|
129
154
|
try {
|
|
130
|
-
const
|
|
131
|
-
|
|
155
|
+
const { collections, ...query } = request.query;
|
|
156
|
+
// console.log('query', query);
|
|
157
|
+
if (!Array.isArray(collections) || collections?.length === 0) {
|
|
158
|
+
console.error('storeHandler.collectionDelete', 'method not allowed', collections);
|
|
159
|
+
return response.status(405).send('method not allowed');
|
|
160
|
+
}
|
|
132
161
|
if (!Array.isArray(collections) || collections.length !== 2) {
|
|
133
162
|
console.error('method not allowed', collections);
|
|
134
163
|
return response.status(405).send('method not allowed');
|
|
135
164
|
}
|
|
136
165
|
const collection: string = collections[0];
|
|
137
166
|
const id: string = collections[1];
|
|
167
|
+
const qs = Object.entries(query).map(kv => kv.join('=')).join('&');
|
|
168
|
+
const params = qsDeserialize(qs);
|
|
169
|
+
// console.log('params', params);
|
|
170
|
+
if (!COLLECTIONS.includes(collection)) {
|
|
171
|
+
console.error('storeHandler.collectionDelete', 'method not allowed', collection);
|
|
172
|
+
return response.status(405).send('method not allowed');
|
|
173
|
+
}
|
|
138
174
|
const store = await storePromise();
|
|
139
175
|
const service = store[collection];
|
|
140
176
|
if (!service) {
|
|
141
|
-
console.error('method not allowed', collection);
|
|
177
|
+
console.error('storeHandler.collectionDelete', 'method not allowed', collection);
|
|
142
178
|
return response.status(405).send('method not allowed');
|
|
143
179
|
}
|
|
144
|
-
//
|
|
180
|
+
// delete a specific document by id
|
|
181
|
+
if (id) {
|
|
182
|
+
// console.log('storeHandler.collectionDelete', collection, id, params);
|
|
183
|
+
const item = await service.deleteOne(asEquatable(id), params);
|
|
184
|
+
if (item) {
|
|
185
|
+
return response.status(200).json(item);
|
|
186
|
+
} else {
|
|
187
|
+
return response.status(404).send('not found');
|
|
188
|
+
}
|
|
189
|
+
} else {
|
|
190
|
+
// console.log('storeHandler.collectionDelete', collection, params);
|
|
191
|
+
const items = await service.deleteMany(params);
|
|
192
|
+
return response.status(200).json(items);
|
|
193
|
+
}
|
|
145
194
|
} catch (error: any) {
|
|
146
195
|
console.error('storeHandler.error.500', error);
|
|
147
196
|
throw error;
|
package/src/vars/vars.handler.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getHost, getOrigin, isDevelopment, isProduction } from '@websolutespa/bom-core';
|
|
2
2
|
import { getLayout } from '@websolutespa/bom-mixer-models';
|
|
3
|
-
import { apiHandler } from '@websolutespa/bom-mixer-store';
|
|
4
3
|
import { NextApiRequest, NextApiResponse } from 'next';
|
|
4
|
+
import { apiHandler } from '../api/api.handler';
|
|
5
5
|
|
|
6
6
|
const DISALLOWED_KEYS = [
|
|
7
7
|
'__NEXT_PRIVATE_STANDALONE_CONFIG',
|