@websolutespa/bom-mixer-store 1.8.5 → 1.8.7
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 +14 -0
- package/dist/index.d.ts +12 -15
- package/dist/index.js +121 -81
- package/dist/index.mjs +82 -41
- package/package.json +1 -1
- package/src/api/api.ts +4 -48
- package/src/remote/remote.service.ts +21 -20
- package/src/session/session.service.ts +26 -8
- package/src/store/store.handler.ts +24 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @websolutespa/bom-mixer-store
|
|
2
2
|
|
|
3
|
+
## 1.8.7
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Modified: storeHandler
|
|
8
|
+
- Updated dependencies
|
|
9
|
+
- @websolutespa/bom-core@1.8.15
|
|
10
|
+
|
|
11
|
+
## 1.8.6
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Modified: RemoteService
|
|
16
|
+
|
|
3
17
|
## 1.8.5
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
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 {
|
|
@@ -859,7 +855,37 @@ async function mockApiDelete(url, options = {}) {
|
|
|
859
855
|
}
|
|
860
856
|
|
|
861
857
|
// src/store/store.handler.ts
|
|
862
|
-
var
|
|
858
|
+
var import_bom_core15 = require("@websolutespa/bom-core");
|
|
859
|
+
|
|
860
|
+
// src/session/session.service.ts
|
|
861
|
+
var import_bom_core9 = require("@websolutespa/bom-core");
|
|
862
|
+
var import_iron_session = require("iron-session");
|
|
863
|
+
async function getSession(request, response) {
|
|
864
|
+
const COOKIE_NAME = "websolutespa-next-js";
|
|
865
|
+
const COOKIE_PASSWORD = process.env.SECRET_COOKIE_PASSWORD || "SECRET_COOKIE_PASSWORD_32_CHARS_LONG";
|
|
866
|
+
const sessionOptions = {
|
|
867
|
+
cookieName: COOKIE_NAME,
|
|
868
|
+
// secure: true should be used in production (HTTPS) but can't be used in development (HTTP)
|
|
869
|
+
cookieOptions: {
|
|
870
|
+
secure: !import_bom_core9.isDevelopment
|
|
871
|
+
},
|
|
872
|
+
password: COOKIE_PASSWORD
|
|
873
|
+
};
|
|
874
|
+
const session = await (0, import_iron_session.getIronSession)(request, response, sessionOptions);
|
|
875
|
+
return session;
|
|
876
|
+
}
|
|
877
|
+
async function getSessionToken(request, response) {
|
|
878
|
+
const session = await getSession(request, response);
|
|
879
|
+
if (session.token && session.exp && session.exp > Math.floor((/* @__PURE__ */ new Date()).getTime() / 1e3)) {
|
|
880
|
+
return session.token;
|
|
881
|
+
} else {
|
|
882
|
+
session.token = void 0;
|
|
883
|
+
session.exp = void 0;
|
|
884
|
+
await session.save();
|
|
885
|
+
session.destroy();
|
|
886
|
+
return;
|
|
887
|
+
}
|
|
888
|
+
}
|
|
863
889
|
|
|
864
890
|
// src/static/static.ts
|
|
865
891
|
var staticCollections = [{
|
|
@@ -1156,7 +1182,7 @@ var KNOWN_COLLECTIONS = [
|
|
|
1156
1182
|
var COLLECTIONS = [...ENV_COLLECTIONS, ...KNOWN_COLLECTIONS];
|
|
1157
1183
|
|
|
1158
1184
|
// src/mock/mock.store.ts
|
|
1159
|
-
var
|
|
1185
|
+
var import_bom_core11 = require("@websolutespa/bom-core");
|
|
1160
1186
|
|
|
1161
1187
|
// src/fs/fs.service.ts
|
|
1162
1188
|
var fs = __toESM(require("fs"));
|
|
@@ -1184,8 +1210,8 @@ async function fsReadJson(pathname) {
|
|
|
1184
1210
|
}
|
|
1185
1211
|
|
|
1186
1212
|
// src/static/static.service.ts
|
|
1187
|
-
var
|
|
1188
|
-
var StaticService = class extends
|
|
1213
|
+
var import_bom_core10 = require("@websolutespa/bom-core");
|
|
1214
|
+
var StaticService = class extends import_bom_core10.JsonService {
|
|
1189
1215
|
constructor(config, store) {
|
|
1190
1216
|
super([]);
|
|
1191
1217
|
this.loaded = false;
|
|
@@ -1213,7 +1239,7 @@ var StaticService = class extends import_bom_core9.JsonService {
|
|
|
1213
1239
|
const config = this.config;
|
|
1214
1240
|
const src = config.src;
|
|
1215
1241
|
if (typeof src === "string") {
|
|
1216
|
-
const response = await (0,
|
|
1242
|
+
const response = await (0, import_bom_core10.httpGet)(src, {});
|
|
1217
1243
|
items = response.items;
|
|
1218
1244
|
} else if (typeof src === "function") {
|
|
1219
1245
|
if (src instanceof Promise) {
|
|
@@ -1329,7 +1355,7 @@ function getMockStoreFromData_(data) {
|
|
|
1329
1355
|
return store;
|
|
1330
1356
|
}
|
|
1331
1357
|
async function getStoreData_() {
|
|
1332
|
-
if (
|
|
1358
|
+
if (import_bom_core11.isDevelopment) {
|
|
1333
1359
|
const path = require("path");
|
|
1334
1360
|
const pathname = path.join(process.cwd(), ".mixer", "store.json");
|
|
1335
1361
|
return await fsReadJson(pathname) || {};
|
|
@@ -1345,17 +1371,17 @@ async function getMockStore_() {
|
|
|
1345
1371
|
}
|
|
1346
1372
|
async function getMockStore() {
|
|
1347
1373
|
return fromCache(async () => {
|
|
1348
|
-
if (!
|
|
1374
|
+
if (!import_bom_core11.isDevelopment) {
|
|
1349
1375
|
console.warn("getMockStore");
|
|
1350
1376
|
}
|
|
1351
1377
|
const store = await getMockStore_();
|
|
1352
1378
|
return store;
|
|
1353
|
-
},
|
|
1379
|
+
}, import_bom_core11.isDevelopment ? void 0 : "STORE_MOCK_");
|
|
1354
1380
|
}
|
|
1355
1381
|
|
|
1356
1382
|
// src/cached/cached.service.ts
|
|
1357
|
-
var
|
|
1358
|
-
var CachedService = class extends
|
|
1383
|
+
var import_bom_core12 = require("@websolutespa/bom-core");
|
|
1384
|
+
var CachedService = class extends import_bom_core12.JsonService {
|
|
1359
1385
|
constructor(service, options = {}) {
|
|
1360
1386
|
super([]);
|
|
1361
1387
|
this.cache_ = /* @__PURE__ */ new Map();
|
|
@@ -1394,7 +1420,7 @@ var CachedService = class extends import_bom_core11.JsonService {
|
|
|
1394
1420
|
});
|
|
1395
1421
|
}
|
|
1396
1422
|
async findOne(id, params = {}) {
|
|
1397
|
-
const serializedParams = (0,
|
|
1423
|
+
const serializedParams = (0, import_bom_core12.qsSerialize)(params);
|
|
1398
1424
|
const key = `findOne[${id}]?${serializedParams}`;
|
|
1399
1425
|
return await this.fromCache_(async () => {
|
|
1400
1426
|
return await this.service_.findOne(id, params);
|
|
@@ -1403,7 +1429,7 @@ var CachedService = class extends import_bom_core11.JsonService {
|
|
|
1403
1429
|
async findMany(params = {}) {
|
|
1404
1430
|
if (storeStrategy === "decoratedApi" /* DecoratedApi */) {
|
|
1405
1431
|
const { market, locale, ...otherParams } = params;
|
|
1406
|
-
const serializedParams = (0,
|
|
1432
|
+
const serializedParams = (0, import_bom_core12.qsSerialize)({ market, locale });
|
|
1407
1433
|
const key = `findMany?${serializedParams}`;
|
|
1408
1434
|
let items = await this.fromCache_(async () => {
|
|
1409
1435
|
return await this.service_.findMany({ market, locale });
|
|
@@ -1416,7 +1442,7 @@ var CachedService = class extends import_bom_core11.JsonService {
|
|
|
1416
1442
|
}
|
|
1417
1443
|
return items;
|
|
1418
1444
|
} else {
|
|
1419
|
-
const serializedParams = (0,
|
|
1445
|
+
const serializedParams = (0, import_bom_core12.qsSerialize)(params);
|
|
1420
1446
|
const key = `findMany?${serializedParams}`;
|
|
1421
1447
|
return await this.fromCache_(async () => {
|
|
1422
1448
|
return await this.service_.findMany(params);
|
|
@@ -1424,7 +1450,7 @@ var CachedService = class extends import_bom_core11.JsonService {
|
|
|
1424
1450
|
}
|
|
1425
1451
|
}
|
|
1426
1452
|
async findPaged(params = {}) {
|
|
1427
|
-
const serializedParams = (0,
|
|
1453
|
+
const serializedParams = (0, import_bom_core12.qsSerialize)(params);
|
|
1428
1454
|
const key = `findPaged?${serializedParams}`;
|
|
1429
1455
|
return await this.fromCache_(async () => {
|
|
1430
1456
|
return await this.service_.findPaged(params);
|
|
@@ -1455,25 +1481,25 @@ var CachedService = class extends import_bom_core11.JsonService {
|
|
|
1455
1481
|
};
|
|
1456
1482
|
|
|
1457
1483
|
// src/remote/remote.service.ts
|
|
1458
|
-
var
|
|
1484
|
+
var import_bom_core14 = require("@websolutespa/bom-core");
|
|
1459
1485
|
|
|
1460
1486
|
// src/remote/remote.api.ts
|
|
1461
|
-
var
|
|
1487
|
+
var import_bom_core13 = require("@websolutespa/bom-core");
|
|
1462
1488
|
var API_URL3 = process.env.STORE_URL;
|
|
1463
1489
|
var API_BASE4 = process.env.STORE_API || "/api";
|
|
1464
1490
|
function getRemoteApiUrl(pathname) {
|
|
1465
1491
|
let url = `${API_URL3}${API_BASE4}${pathname}`;
|
|
1466
|
-
url =
|
|
1492
|
+
url = import_bom_core13.isBrowser ? url : url.replace("://localhost", "://127.0.0.1");
|
|
1467
1493
|
return url;
|
|
1468
1494
|
}
|
|
1469
1495
|
async function remoteApiFetch(pathname, options = {}) {
|
|
1470
|
-
if (
|
|
1496
|
+
if (import_bom_core13.isBrowser) {
|
|
1471
1497
|
throw "Unauthorized: remote api cannot be fetched client side";
|
|
1472
1498
|
}
|
|
1473
1499
|
const url = getRemoteApiUrl(pathname);
|
|
1474
|
-
const apiOptions = (0,
|
|
1500
|
+
const apiOptions = (0, import_bom_core13.merge)({ ...getRemoteApiRequestOptions() }, options);
|
|
1475
1501
|
console.log("remoteApiFetch", apiOptions.method, url);
|
|
1476
|
-
const apiResponse = await (0,
|
|
1502
|
+
const apiResponse = await (0, import_bom_core13.httpFetch)(url, apiOptions);
|
|
1477
1503
|
return apiResponse;
|
|
1478
1504
|
}
|
|
1479
1505
|
async function remoteApiGet(url, options = {}) {
|
|
@@ -1507,14 +1533,14 @@ var RemoteService = class {
|
|
|
1507
1533
|
this.key = key;
|
|
1508
1534
|
this.store = store;
|
|
1509
1535
|
}
|
|
1510
|
-
async findOne(id, params = {}) {
|
|
1536
|
+
async findOne(id, params = {}, token) {
|
|
1511
1537
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1512
1538
|
params.draft = RemoteServiceStrategy.draft;
|
|
1513
|
-
const query = (0,
|
|
1539
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1514
1540
|
const search = query ? `?${query}` : "";
|
|
1515
1541
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1516
1542
|
try {
|
|
1517
|
-
const item = await remoteApiGet(url);
|
|
1543
|
+
const item = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1518
1544
|
if (item) {
|
|
1519
1545
|
return await this.decorateOne_(item, params);
|
|
1520
1546
|
}
|
|
@@ -1523,84 +1549,84 @@ var RemoteService = class {
|
|
|
1523
1549
|
}
|
|
1524
1550
|
return;
|
|
1525
1551
|
}
|
|
1526
|
-
async findMany(params = {}) {
|
|
1552
|
+
async findMany(params = {}, token) {
|
|
1527
1553
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1528
1554
|
params.draft = RemoteServiceStrategy.draft;
|
|
1529
1555
|
params.pagination = false;
|
|
1530
|
-
const query = (0,
|
|
1556
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1531
1557
|
const search = query ? `?${query}` : "";
|
|
1532
1558
|
const url = `/${this.key}${search}`;
|
|
1533
|
-
const response = await remoteApiGet(url);
|
|
1559
|
+
const response = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1534
1560
|
const items = Array.isArray(response) ? response : response.docs;
|
|
1535
1561
|
return await this.decorateMany_(items, params);
|
|
1536
1562
|
}
|
|
1537
|
-
async findPaged(params = {}) {
|
|
1563
|
+
async findPaged(params = {}, token) {
|
|
1538
1564
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1539
1565
|
params.draft = RemoteServiceStrategy.draft;
|
|
1540
1566
|
params.pagination = true;
|
|
1541
|
-
const query = (0,
|
|
1567
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1542
1568
|
const search = query ? `?${query}` : "";
|
|
1543
1569
|
const url = `/${this.key}${search}`;
|
|
1544
|
-
const response = await remoteApiGet(url);
|
|
1570
|
+
const response = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1545
1571
|
let items = response.docs;
|
|
1546
1572
|
items = await this.decorateMany_(items, params);
|
|
1547
1573
|
return { ...response, docs: items };
|
|
1548
1574
|
}
|
|
1549
|
-
async create(payload, params = {}) {
|
|
1575
|
+
async create(payload, params = {}, token) {
|
|
1550
1576
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1551
1577
|
params.draft = RemoteServiceStrategy.draft;
|
|
1552
|
-
const query = (0,
|
|
1578
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1553
1579
|
const search = query ? `?${query}` : "";
|
|
1554
1580
|
const url = `/${this.key}${search}`;
|
|
1555
|
-
const result = await remoteApiPost(url, payload);
|
|
1581
|
+
const result = await remoteApiPost(url, payload, getRemoteApiRequestOptions(token));
|
|
1556
1582
|
const item = result.doc ? result.doc : result;
|
|
1557
1583
|
return await this.decorateOne_(item, {});
|
|
1558
1584
|
}
|
|
1559
|
-
async updateOne(id, payload, params = {}) {
|
|
1560
|
-
const query = (0,
|
|
1585
|
+
async updateOne(id, payload, params = {}, token) {
|
|
1586
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1561
1587
|
const search = query ? `?${query}` : "";
|
|
1562
1588
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1563
|
-
const result = await remoteApiPatch(url, payload);
|
|
1589
|
+
const result = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1564
1590
|
const item = result.doc ? result.doc : result;
|
|
1565
1591
|
return await this.decorateOne_(item, {});
|
|
1566
1592
|
}
|
|
1567
|
-
async updateMany(payload, params = {}) {
|
|
1593
|
+
async updateMany(payload, params = {}, token) {
|
|
1568
1594
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1569
1595
|
params.draft = RemoteServiceStrategy.draft;
|
|
1570
1596
|
params.pagination = false;
|
|
1571
|
-
const query = (0,
|
|
1597
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1572
1598
|
const search = query ? `?${query}` : "";
|
|
1573
1599
|
const url = `/${this.key}${search}`;
|
|
1574
|
-
const response = await remoteApiPatch(url, payload);
|
|
1600
|
+
const response = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1575
1601
|
const items = Array.isArray(response) ? response : response.docs;
|
|
1576
1602
|
return await this.decorateMany_(items, params);
|
|
1577
1603
|
}
|
|
1578
|
-
async deleteOne(id, params = {}) {
|
|
1579
|
-
const query = (0,
|
|
1604
|
+
async deleteOne(id, params = {}, token) {
|
|
1605
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1580
1606
|
const search = query ? `?${query}` : "";
|
|
1581
1607
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1582
|
-
const item = await remoteApiDelete(url);
|
|
1608
|
+
const item = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1583
1609
|
return await this.decorateOne_(item, {});
|
|
1584
1610
|
}
|
|
1585
|
-
async deleteMany(params = {}) {
|
|
1611
|
+
async deleteMany(params = {}, token) {
|
|
1586
1612
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1587
1613
|
params.draft = RemoteServiceStrategy.draft;
|
|
1588
1614
|
params.pagination = false;
|
|
1589
|
-
const query = (0,
|
|
1615
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1590
1616
|
const search = query ? `?${query}` : "";
|
|
1591
1617
|
const url = `/${this.key}${search}`;
|
|
1592
|
-
const response = await remoteApiDelete(url);
|
|
1618
|
+
const response = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1593
1619
|
const items = Array.isArray(response) ? response : response.docs;
|
|
1594
1620
|
return await this.decorateMany_(items, params);
|
|
1595
1621
|
}
|
|
1596
1622
|
// !!! old
|
|
1597
|
-
async update(payload, params = {}) {
|
|
1623
|
+
async update(payload, params = {}, token) {
|
|
1598
1624
|
const url = `/${this.key}`;
|
|
1599
|
-
const result = await remoteApiPatch(url, payload);
|
|
1625
|
+
const result = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1600
1626
|
const item = result.doc ? result.doc : result;
|
|
1601
1627
|
return await this.decorateOne_(item, {});
|
|
1602
1628
|
}
|
|
1603
|
-
async delete(id) {
|
|
1629
|
+
async delete(id, token) {
|
|
1604
1630
|
const params = {
|
|
1605
1631
|
where: {
|
|
1606
1632
|
id: {
|
|
@@ -1608,10 +1634,10 @@ var RemoteService = class {
|
|
|
1608
1634
|
}
|
|
1609
1635
|
}
|
|
1610
1636
|
};
|
|
1611
|
-
const query = (0,
|
|
1637
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1612
1638
|
const search = query ? `?${query}` : "";
|
|
1613
1639
|
const url = `/${this.key}${search}`;
|
|
1614
|
-
const item = await remoteApiDelete(url);
|
|
1640
|
+
const item = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1615
1641
|
return await this.decorateOne_(item, {});
|
|
1616
1642
|
}
|
|
1617
1643
|
async decorateOne_(item, params = {}) {
|
|
@@ -1784,7 +1810,7 @@ function collectionGet(storePromise) {
|
|
|
1784
1810
|
const collection = collections[0];
|
|
1785
1811
|
const id = collections[1];
|
|
1786
1812
|
const qs = Object.entries(query).map((kv) => kv.join("=")).join("&");
|
|
1787
|
-
const params = (0,
|
|
1813
|
+
const params = (0, import_bom_core15.qsDeserialize)(qs);
|
|
1788
1814
|
if (!COLLECTIONS.includes(collection)) {
|
|
1789
1815
|
console.error("storeHandler.collectionGet", "method not allowed", collection);
|
|
1790
1816
|
return response.status(405).send("method not allowed");
|
|
@@ -1795,18 +1821,19 @@ function collectionGet(storePromise) {
|
|
|
1795
1821
|
console.error("storeHandler.collectionGet", "method not allowed", collection);
|
|
1796
1822
|
return response.status(405).send("method not allowed");
|
|
1797
1823
|
}
|
|
1824
|
+
const token = await getSessionToken(request, response);
|
|
1798
1825
|
if (id) {
|
|
1799
|
-
const item = await service.findOne((0,
|
|
1826
|
+
const item = await service.findOne((0, import_bom_core15.asEquatable)(id), params, token);
|
|
1800
1827
|
if (item) {
|
|
1801
1828
|
return response.status(200).json(item);
|
|
1802
1829
|
} else {
|
|
1803
1830
|
return response.status(404).send("not found");
|
|
1804
1831
|
}
|
|
1805
1832
|
} else if (String(params.pagination) === "true") {
|
|
1806
|
-
const pagination = await service.findPaged(params);
|
|
1833
|
+
const pagination = await service.findPaged(params, token);
|
|
1807
1834
|
return response.status(200).json(pagination);
|
|
1808
1835
|
} else {
|
|
1809
|
-
const items = await service.findMany(params);
|
|
1836
|
+
const items = await service.findMany(params, token);
|
|
1810
1837
|
return response.status(200).json(items);
|
|
1811
1838
|
}
|
|
1812
1839
|
} catch (error) {
|
|
@@ -1824,6 +1851,13 @@ function collectionPost(storePromise) {
|
|
|
1824
1851
|
return response.status(405).send("method not allowed");
|
|
1825
1852
|
}
|
|
1826
1853
|
const collection = collections[0];
|
|
1854
|
+
const id = collections[1];
|
|
1855
|
+
if (id) {
|
|
1856
|
+
console.error("method not allowed", collection);
|
|
1857
|
+
return response.status(405).send("method not allowed");
|
|
1858
|
+
}
|
|
1859
|
+
const qs = Object.entries(query).map((kv) => kv.join("=")).join("&");
|
|
1860
|
+
const params = (0, import_bom_core15.qsDeserialize)(qs);
|
|
1827
1861
|
const store = await storePromise();
|
|
1828
1862
|
const service = store[collection];
|
|
1829
1863
|
if (!service) {
|
|
@@ -1833,6 +1867,10 @@ function collectionPost(storePromise) {
|
|
|
1833
1867
|
if (collection === "route") {
|
|
1834
1868
|
return routePost(store, request, response);
|
|
1835
1869
|
}
|
|
1870
|
+
const token = await getSessionToken(request, response);
|
|
1871
|
+
const payload = request.body;
|
|
1872
|
+
const item = await service.create(payload, params, token);
|
|
1873
|
+
return response.status(200).json(item);
|
|
1836
1874
|
} catch (error) {
|
|
1837
1875
|
console.error("storeHandler.error.500", error);
|
|
1838
1876
|
throw error;
|
|
@@ -1854,7 +1892,7 @@ function collectionPatch(storePromise) {
|
|
|
1854
1892
|
const collection = collections[0];
|
|
1855
1893
|
const id = collections[1];
|
|
1856
1894
|
const qs = Object.entries(query).map((kv) => kv.join("=")).join("&");
|
|
1857
|
-
const params = (0,
|
|
1895
|
+
const params = (0, import_bom_core15.qsDeserialize)(qs);
|
|
1858
1896
|
if (!COLLECTIONS.includes(collection)) {
|
|
1859
1897
|
console.error("storeHandler.collectionUpdate", "method not allowed", collection);
|
|
1860
1898
|
return response.status(405).send("method not allowed");
|
|
@@ -1865,15 +1903,17 @@ function collectionPatch(storePromise) {
|
|
|
1865
1903
|
console.error("storeHandler.collectionUpdate", "method not allowed", collection);
|
|
1866
1904
|
return response.status(405).send("method not allowed");
|
|
1867
1905
|
}
|
|
1906
|
+
const token = await getSessionToken(request, response);
|
|
1907
|
+
const payload = request.body;
|
|
1868
1908
|
if (id) {
|
|
1869
|
-
const item = await service.updateOne((0,
|
|
1909
|
+
const item = await service.updateOne((0, import_bom_core15.asEquatable)(id), payload, params, token);
|
|
1870
1910
|
if (item) {
|
|
1871
1911
|
return response.status(200).json(item);
|
|
1872
1912
|
} else {
|
|
1873
1913
|
return response.status(404).send("not found");
|
|
1874
1914
|
}
|
|
1875
1915
|
} else {
|
|
1876
|
-
const items = await service.updateMany(params);
|
|
1916
|
+
const items = await service.updateMany(payload, params, token);
|
|
1877
1917
|
return response.status(200).json(items);
|
|
1878
1918
|
}
|
|
1879
1919
|
} catch (error) {
|
|
@@ -1897,7 +1937,7 @@ function collectionDelete(storePromise) {
|
|
|
1897
1937
|
const collection = collections[0];
|
|
1898
1938
|
const id = collections[1];
|
|
1899
1939
|
const qs = Object.entries(query).map((kv) => kv.join("=")).join("&");
|
|
1900
|
-
const params = (0,
|
|
1940
|
+
const params = (0, import_bom_core15.qsDeserialize)(qs);
|
|
1901
1941
|
if (!COLLECTIONS.includes(collection)) {
|
|
1902
1942
|
console.error("storeHandler.collectionDelete", "method not allowed", collection);
|
|
1903
1943
|
return response.status(405).send("method not allowed");
|
|
@@ -1908,15 +1948,16 @@ function collectionDelete(storePromise) {
|
|
|
1908
1948
|
console.error("storeHandler.collectionDelete", "method not allowed", collection);
|
|
1909
1949
|
return response.status(405).send("method not allowed");
|
|
1910
1950
|
}
|
|
1951
|
+
const token = await getSessionToken(request, response);
|
|
1911
1952
|
if (id) {
|
|
1912
|
-
const item = await service.deleteOne((0,
|
|
1953
|
+
const item = await service.deleteOne((0, import_bom_core15.asEquatable)(id), params, token);
|
|
1913
1954
|
if (item) {
|
|
1914
1955
|
return response.status(200).json(item);
|
|
1915
1956
|
} else {
|
|
1916
1957
|
return response.status(404).send("not found");
|
|
1917
1958
|
}
|
|
1918
1959
|
} else {
|
|
1919
|
-
const items = await service.deleteMany(params);
|
|
1960
|
+
const items = await service.deleteMany(params, token);
|
|
1920
1961
|
return response.status(200).json(items);
|
|
1921
1962
|
}
|
|
1922
1963
|
} catch (error) {
|
|
@@ -1957,8 +1998,8 @@ function storeIndexHandler() {
|
|
|
1957
1998
|
return apiHandler({
|
|
1958
1999
|
get: async (request, response) => {
|
|
1959
2000
|
try {
|
|
1960
|
-
const market =
|
|
1961
|
-
const locale =
|
|
2001
|
+
const market = import_bom_core15.defaultMarket;
|
|
2002
|
+
const locale = import_bom_core15.defaultLocale;
|
|
1962
2003
|
if (!Array.isArray(COLLECTIONS) || COLLECTIONS?.length === 0) {
|
|
1963
2004
|
console.error("apiHome.collectionGet", "collections not found", COLLECTIONS);
|
|
1964
2005
|
return response.status(405).send("method not allowed");
|
|
@@ -2225,7 +2266,7 @@ async function storeApiDelete(url, options = {}) {
|
|
|
2225
2266
|
}
|
|
2226
2267
|
|
|
2227
2268
|
// src/vars/vars.handler.ts
|
|
2228
|
-
var
|
|
2269
|
+
var import_bom_core17 = require("@websolutespa/bom-core");
|
|
2229
2270
|
|
|
2230
2271
|
// ../models/src/label/label.service.ts
|
|
2231
2272
|
async function getLabels(params = {}) {
|
|
@@ -2256,7 +2297,7 @@ async function getMenus(params = {}) {
|
|
|
2256
2297
|
}
|
|
2257
2298
|
|
|
2258
2299
|
// ../models/src/route/route.service.ts
|
|
2259
|
-
var
|
|
2300
|
+
var import_bom_core16 = require("@websolutespa/bom-core");
|
|
2260
2301
|
async function getRouteLinkTree(market, locale) {
|
|
2261
2302
|
const store = await getStore();
|
|
2262
2303
|
const routes = await store.route.findMany({
|
|
@@ -2270,7 +2311,7 @@ async function getRouteLinkTree(market, locale) {
|
|
|
2270
2311
|
}
|
|
2271
2312
|
});
|
|
2272
2313
|
const categories = await store.category.findMany({ market, locale });
|
|
2273
|
-
const rootCategory = (0,
|
|
2314
|
+
const rootCategory = (0, import_bom_core16.getRootCategory)(categories);
|
|
2274
2315
|
if (rootCategory) {
|
|
2275
2316
|
const root = categoryToRouteLink(routes, categories, rootCategory, locale);
|
|
2276
2317
|
return root;
|
|
@@ -2278,13 +2319,13 @@ async function getRouteLinkTree(market, locale) {
|
|
|
2278
2319
|
return void 0;
|
|
2279
2320
|
}
|
|
2280
2321
|
function categoryToRouteLink(routes, categories, category, locale) {
|
|
2281
|
-
const route = (0,
|
|
2322
|
+
const route = (0, import_bom_core16.findRouteOfCategory)(routes, category.id);
|
|
2282
2323
|
if (!route) {
|
|
2283
2324
|
return void 0;
|
|
2284
2325
|
}
|
|
2285
|
-
const rootCategory = (0,
|
|
2326
|
+
const rootCategory = (0, import_bom_core16.getRootCategory)(categories);
|
|
2286
2327
|
const childCategories = categories.filter((x) => {
|
|
2287
|
-
const parentId = (0,
|
|
2328
|
+
const parentId = (0, import_bom_core16.asCategoryId)(x.category);
|
|
2288
2329
|
return rootCategory && rootCategory.id === category.id ? x.id !== rootCategory.id && (parentId === category.id || !parentId) : parentId === category.id;
|
|
2289
2330
|
});
|
|
2290
2331
|
const routeLink = newRouteLink(category, route, locale);
|
|
@@ -2296,7 +2337,7 @@ function newRouteLink(category, route, locale) {
|
|
|
2296
2337
|
const id = route?.page || category.id;
|
|
2297
2338
|
const media = category.media || route?.media;
|
|
2298
2339
|
const schema = route?.template || route?.schema || category?.schema || "unknown";
|
|
2299
|
-
const title = (0,
|
|
2340
|
+
const title = (0, import_bom_core16.localize)(category.title || route?.title || "untitled", locale);
|
|
2300
2341
|
const routeLink = {
|
|
2301
2342
|
category: category.id,
|
|
2302
2343
|
href,
|
|
@@ -2373,10 +2414,10 @@ function varsHandler() {
|
|
|
2373
2414
|
const { locale = defaultLocale3, market = defaultMaket } = request.query || {};
|
|
2374
2415
|
const layout = typeof locale === "string" && typeof market === "string" ? await getLayout(market, locale) : void 0;
|
|
2375
2416
|
const info = {
|
|
2376
|
-
production:
|
|
2377
|
-
development:
|
|
2378
|
-
origin: (0,
|
|
2379
|
-
host: (0,
|
|
2417
|
+
production: import_bom_core17.isProduction,
|
|
2418
|
+
development: import_bom_core17.isDevelopment,
|
|
2419
|
+
origin: (0, import_bom_core17.getOrigin)(request.headers),
|
|
2420
|
+
host: (0, import_bom_core17.getHost)(request.headers),
|
|
2380
2421
|
headers: { ...sortKeys(request.headers) },
|
|
2381
2422
|
env: { ...getFilteredEnv(process.env) },
|
|
2382
2423
|
layout: { ...sortKeys(layout) }
|
|
@@ -2500,7 +2541,6 @@ function varsHandler() {
|
|
|
2500
2541
|
MockPageService,
|
|
2501
2542
|
MockService,
|
|
2502
2543
|
NextPhase,
|
|
2503
|
-
REMOTE_API_SESSION,
|
|
2504
2544
|
RemoteMenuService,
|
|
2505
2545
|
RemotePageService,
|
|
2506
2546
|
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 {
|
|
@@ -755,6 +752,36 @@ async function mockApiDelete(url, options = {}) {
|
|
|
755
752
|
// src/store/store.handler.ts
|
|
756
753
|
import { asEquatable, defaultLocale, defaultMarket, qsDeserialize } from "@websolutespa/bom-core";
|
|
757
754
|
|
|
755
|
+
// src/session/session.service.ts
|
|
756
|
+
import { isDevelopment } from "@websolutespa/bom-core";
|
|
757
|
+
import { getIronSession } from "iron-session";
|
|
758
|
+
async function getSession(request, response) {
|
|
759
|
+
const COOKIE_NAME = "websolutespa-next-js";
|
|
760
|
+
const COOKIE_PASSWORD = process.env.SECRET_COOKIE_PASSWORD || "SECRET_COOKIE_PASSWORD_32_CHARS_LONG";
|
|
761
|
+
const sessionOptions = {
|
|
762
|
+
cookieName: COOKIE_NAME,
|
|
763
|
+
// secure: true should be used in production (HTTPS) but can't be used in development (HTTP)
|
|
764
|
+
cookieOptions: {
|
|
765
|
+
secure: !isDevelopment
|
|
766
|
+
},
|
|
767
|
+
password: COOKIE_PASSWORD
|
|
768
|
+
};
|
|
769
|
+
const session = await getIronSession(request, response, sessionOptions);
|
|
770
|
+
return session;
|
|
771
|
+
}
|
|
772
|
+
async function getSessionToken(request, response) {
|
|
773
|
+
const session = await getSession(request, response);
|
|
774
|
+
if (session.token && session.exp && session.exp > Math.floor((/* @__PURE__ */ new Date()).getTime() / 1e3)) {
|
|
775
|
+
return session.token;
|
|
776
|
+
} else {
|
|
777
|
+
session.token = void 0;
|
|
778
|
+
session.exp = void 0;
|
|
779
|
+
await session.save();
|
|
780
|
+
session.destroy();
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
758
785
|
// src/static/static.ts
|
|
759
786
|
var staticCollections = [{
|
|
760
787
|
src: "https://actarian.github.io/cms-i18n-react/api/continent.json",
|
|
@@ -1050,7 +1077,7 @@ var KNOWN_COLLECTIONS = [
|
|
|
1050
1077
|
var COLLECTIONS = [...ENV_COLLECTIONS, ...KNOWN_COLLECTIONS];
|
|
1051
1078
|
|
|
1052
1079
|
// src/mock/mock.store.ts
|
|
1053
|
-
import { isDevelopment } from "@websolutespa/bom-core";
|
|
1080
|
+
import { isDevelopment as isDevelopment2 } from "@websolutespa/bom-core";
|
|
1054
1081
|
|
|
1055
1082
|
// src/fs/fs.service.ts
|
|
1056
1083
|
import * as fs from "fs";
|
|
@@ -1223,7 +1250,7 @@ function getMockStoreFromData_(data) {
|
|
|
1223
1250
|
return store;
|
|
1224
1251
|
}
|
|
1225
1252
|
async function getStoreData_() {
|
|
1226
|
-
if (
|
|
1253
|
+
if (isDevelopment2) {
|
|
1227
1254
|
const path = __require("path");
|
|
1228
1255
|
const pathname = path.join(process.cwd(), ".mixer", "store.json");
|
|
1229
1256
|
return await fsReadJson(pathname) || {};
|
|
@@ -1239,12 +1266,12 @@ async function getMockStore_() {
|
|
|
1239
1266
|
}
|
|
1240
1267
|
async function getMockStore() {
|
|
1241
1268
|
return fromCache(async () => {
|
|
1242
|
-
if (!
|
|
1269
|
+
if (!isDevelopment2) {
|
|
1243
1270
|
console.warn("getMockStore");
|
|
1244
1271
|
}
|
|
1245
1272
|
const store = await getMockStore_();
|
|
1246
1273
|
return store;
|
|
1247
|
-
},
|
|
1274
|
+
}, isDevelopment2 ? void 0 : "STORE_MOCK_");
|
|
1248
1275
|
}
|
|
1249
1276
|
|
|
1250
1277
|
// src/cached/cached.service.ts
|
|
@@ -1401,14 +1428,14 @@ var RemoteService = class {
|
|
|
1401
1428
|
this.key = key;
|
|
1402
1429
|
this.store = store;
|
|
1403
1430
|
}
|
|
1404
|
-
async findOne(id, params = {}) {
|
|
1431
|
+
async findOne(id, params = {}, token) {
|
|
1405
1432
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1406
1433
|
params.draft = RemoteServiceStrategy.draft;
|
|
1407
1434
|
const query = qsSerialize2(params);
|
|
1408
1435
|
const search = query ? `?${query}` : "";
|
|
1409
1436
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1410
1437
|
try {
|
|
1411
|
-
const item = await remoteApiGet(url);
|
|
1438
|
+
const item = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1412
1439
|
if (item) {
|
|
1413
1440
|
return await this.decorateOne_(item, params);
|
|
1414
1441
|
}
|
|
@@ -1417,84 +1444,84 @@ var RemoteService = class {
|
|
|
1417
1444
|
}
|
|
1418
1445
|
return;
|
|
1419
1446
|
}
|
|
1420
|
-
async findMany(params = {}) {
|
|
1447
|
+
async findMany(params = {}, token) {
|
|
1421
1448
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1422
1449
|
params.draft = RemoteServiceStrategy.draft;
|
|
1423
1450
|
params.pagination = false;
|
|
1424
1451
|
const query = qsSerialize2(params);
|
|
1425
1452
|
const search = query ? `?${query}` : "";
|
|
1426
1453
|
const url = `/${this.key}${search}`;
|
|
1427
|
-
const response = await remoteApiGet(url);
|
|
1454
|
+
const response = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1428
1455
|
const items = Array.isArray(response) ? response : response.docs;
|
|
1429
1456
|
return await this.decorateMany_(items, params);
|
|
1430
1457
|
}
|
|
1431
|
-
async findPaged(params = {}) {
|
|
1458
|
+
async findPaged(params = {}, token) {
|
|
1432
1459
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1433
1460
|
params.draft = RemoteServiceStrategy.draft;
|
|
1434
1461
|
params.pagination = true;
|
|
1435
1462
|
const query = qsSerialize2(params);
|
|
1436
1463
|
const search = query ? `?${query}` : "";
|
|
1437
1464
|
const url = `/${this.key}${search}`;
|
|
1438
|
-
const response = await remoteApiGet(url);
|
|
1465
|
+
const response = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
1439
1466
|
let items = response.docs;
|
|
1440
1467
|
items = await this.decorateMany_(items, params);
|
|
1441
1468
|
return { ...response, docs: items };
|
|
1442
1469
|
}
|
|
1443
|
-
async create(payload, params = {}) {
|
|
1470
|
+
async create(payload, params = {}, token) {
|
|
1444
1471
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1445
1472
|
params.draft = RemoteServiceStrategy.draft;
|
|
1446
1473
|
const query = qsSerialize2(params);
|
|
1447
1474
|
const search = query ? `?${query}` : "";
|
|
1448
1475
|
const url = `/${this.key}${search}`;
|
|
1449
|
-
const result = await remoteApiPost(url, payload);
|
|
1476
|
+
const result = await remoteApiPost(url, payload, getRemoteApiRequestOptions(token));
|
|
1450
1477
|
const item = result.doc ? result.doc : result;
|
|
1451
1478
|
return await this.decorateOne_(item, {});
|
|
1452
1479
|
}
|
|
1453
|
-
async updateOne(id, payload, params = {}) {
|
|
1480
|
+
async updateOne(id, payload, params = {}, token) {
|
|
1454
1481
|
const query = qsSerialize2(params);
|
|
1455
1482
|
const search = query ? `?${query}` : "";
|
|
1456
1483
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1457
|
-
const result = await remoteApiPatch(url, payload);
|
|
1484
|
+
const result = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1458
1485
|
const item = result.doc ? result.doc : result;
|
|
1459
1486
|
return await this.decorateOne_(item, {});
|
|
1460
1487
|
}
|
|
1461
|
-
async updateMany(payload, params = {}) {
|
|
1488
|
+
async updateMany(payload, params = {}, token) {
|
|
1462
1489
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1463
1490
|
params.draft = RemoteServiceStrategy.draft;
|
|
1464
1491
|
params.pagination = false;
|
|
1465
1492
|
const query = qsSerialize2(params);
|
|
1466
1493
|
const search = query ? `?${query}` : "";
|
|
1467
1494
|
const url = `/${this.key}${search}`;
|
|
1468
|
-
const response = await remoteApiPatch(url, payload);
|
|
1495
|
+
const response = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1469
1496
|
const items = Array.isArray(response) ? response : response.docs;
|
|
1470
1497
|
return await this.decorateMany_(items, params);
|
|
1471
1498
|
}
|
|
1472
|
-
async deleteOne(id, params = {}) {
|
|
1499
|
+
async deleteOne(id, params = {}, token) {
|
|
1473
1500
|
const query = qsSerialize2(params);
|
|
1474
1501
|
const search = query ? `?${query}` : "";
|
|
1475
1502
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1476
|
-
const item = await remoteApiDelete(url);
|
|
1503
|
+
const item = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1477
1504
|
return await this.decorateOne_(item, {});
|
|
1478
1505
|
}
|
|
1479
|
-
async deleteMany(params = {}) {
|
|
1506
|
+
async deleteMany(params = {}, token) {
|
|
1480
1507
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1481
1508
|
params.draft = RemoteServiceStrategy.draft;
|
|
1482
1509
|
params.pagination = false;
|
|
1483
1510
|
const query = qsSerialize2(params);
|
|
1484
1511
|
const search = query ? `?${query}` : "";
|
|
1485
1512
|
const url = `/${this.key}${search}`;
|
|
1486
|
-
const response = await remoteApiDelete(url);
|
|
1513
|
+
const response = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1487
1514
|
const items = Array.isArray(response) ? response : response.docs;
|
|
1488
1515
|
return await this.decorateMany_(items, params);
|
|
1489
1516
|
}
|
|
1490
1517
|
// !!! old
|
|
1491
|
-
async update(payload, params = {}) {
|
|
1518
|
+
async update(payload, params = {}, token) {
|
|
1492
1519
|
const url = `/${this.key}`;
|
|
1493
|
-
const result = await remoteApiPatch(url, payload);
|
|
1520
|
+
const result = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
1494
1521
|
const item = result.doc ? result.doc : result;
|
|
1495
1522
|
return await this.decorateOne_(item, {});
|
|
1496
1523
|
}
|
|
1497
|
-
async delete(id) {
|
|
1524
|
+
async delete(id, token) {
|
|
1498
1525
|
const params = {
|
|
1499
1526
|
where: {
|
|
1500
1527
|
id: {
|
|
@@ -1505,7 +1532,7 @@ var RemoteService = class {
|
|
|
1505
1532
|
const query = qsSerialize2(params);
|
|
1506
1533
|
const search = query ? `?${query}` : "";
|
|
1507
1534
|
const url = `/${this.key}${search}`;
|
|
1508
|
-
const item = await remoteApiDelete(url);
|
|
1535
|
+
const item = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
1509
1536
|
return await this.decorateOne_(item, {});
|
|
1510
1537
|
}
|
|
1511
1538
|
async decorateOne_(item, params = {}) {
|
|
@@ -1689,18 +1716,19 @@ function collectionGet(storePromise) {
|
|
|
1689
1716
|
console.error("storeHandler.collectionGet", "method not allowed", collection);
|
|
1690
1717
|
return response.status(405).send("method not allowed");
|
|
1691
1718
|
}
|
|
1719
|
+
const token = await getSessionToken(request, response);
|
|
1692
1720
|
if (id) {
|
|
1693
|
-
const item = await service.findOne(asEquatable(id), params);
|
|
1721
|
+
const item = await service.findOne(asEquatable(id), params, token);
|
|
1694
1722
|
if (item) {
|
|
1695
1723
|
return response.status(200).json(item);
|
|
1696
1724
|
} else {
|
|
1697
1725
|
return response.status(404).send("not found");
|
|
1698
1726
|
}
|
|
1699
1727
|
} else if (String(params.pagination) === "true") {
|
|
1700
|
-
const pagination = await service.findPaged(params);
|
|
1728
|
+
const pagination = await service.findPaged(params, token);
|
|
1701
1729
|
return response.status(200).json(pagination);
|
|
1702
1730
|
} else {
|
|
1703
|
-
const items = await service.findMany(params);
|
|
1731
|
+
const items = await service.findMany(params, token);
|
|
1704
1732
|
return response.status(200).json(items);
|
|
1705
1733
|
}
|
|
1706
1734
|
} catch (error) {
|
|
@@ -1718,6 +1746,13 @@ function collectionPost(storePromise) {
|
|
|
1718
1746
|
return response.status(405).send("method not allowed");
|
|
1719
1747
|
}
|
|
1720
1748
|
const collection = collections[0];
|
|
1749
|
+
const id = collections[1];
|
|
1750
|
+
if (id) {
|
|
1751
|
+
console.error("method not allowed", collection);
|
|
1752
|
+
return response.status(405).send("method not allowed");
|
|
1753
|
+
}
|
|
1754
|
+
const qs = Object.entries(query).map((kv) => kv.join("=")).join("&");
|
|
1755
|
+
const params = qsDeserialize(qs);
|
|
1721
1756
|
const store = await storePromise();
|
|
1722
1757
|
const service = store[collection];
|
|
1723
1758
|
if (!service) {
|
|
@@ -1727,6 +1762,10 @@ function collectionPost(storePromise) {
|
|
|
1727
1762
|
if (collection === "route") {
|
|
1728
1763
|
return routePost(store, request, response);
|
|
1729
1764
|
}
|
|
1765
|
+
const token = await getSessionToken(request, response);
|
|
1766
|
+
const payload = request.body;
|
|
1767
|
+
const item = await service.create(payload, params, token);
|
|
1768
|
+
return response.status(200).json(item);
|
|
1730
1769
|
} catch (error) {
|
|
1731
1770
|
console.error("storeHandler.error.500", error);
|
|
1732
1771
|
throw error;
|
|
@@ -1759,15 +1798,17 @@ function collectionPatch(storePromise) {
|
|
|
1759
1798
|
console.error("storeHandler.collectionUpdate", "method not allowed", collection);
|
|
1760
1799
|
return response.status(405).send("method not allowed");
|
|
1761
1800
|
}
|
|
1801
|
+
const token = await getSessionToken(request, response);
|
|
1802
|
+
const payload = request.body;
|
|
1762
1803
|
if (id) {
|
|
1763
|
-
const item = await service.updateOne(asEquatable(id), params);
|
|
1804
|
+
const item = await service.updateOne(asEquatable(id), payload, params, token);
|
|
1764
1805
|
if (item) {
|
|
1765
1806
|
return response.status(200).json(item);
|
|
1766
1807
|
} else {
|
|
1767
1808
|
return response.status(404).send("not found");
|
|
1768
1809
|
}
|
|
1769
1810
|
} else {
|
|
1770
|
-
const items = await service.updateMany(params);
|
|
1811
|
+
const items = await service.updateMany(payload, params, token);
|
|
1771
1812
|
return response.status(200).json(items);
|
|
1772
1813
|
}
|
|
1773
1814
|
} catch (error) {
|
|
@@ -1802,15 +1843,16 @@ function collectionDelete(storePromise) {
|
|
|
1802
1843
|
console.error("storeHandler.collectionDelete", "method not allowed", collection);
|
|
1803
1844
|
return response.status(405).send("method not allowed");
|
|
1804
1845
|
}
|
|
1846
|
+
const token = await getSessionToken(request, response);
|
|
1805
1847
|
if (id) {
|
|
1806
|
-
const item = await service.deleteOne(asEquatable(id), params);
|
|
1848
|
+
const item = await service.deleteOne(asEquatable(id), params, token);
|
|
1807
1849
|
if (item) {
|
|
1808
1850
|
return response.status(200).json(item);
|
|
1809
1851
|
} else {
|
|
1810
1852
|
return response.status(404).send("not found");
|
|
1811
1853
|
}
|
|
1812
1854
|
} else {
|
|
1813
|
-
const items = await service.deleteMany(params);
|
|
1855
|
+
const items = await service.deleteMany(params, token);
|
|
1814
1856
|
return response.status(200).json(items);
|
|
1815
1857
|
}
|
|
1816
1858
|
} catch (error) {
|
|
@@ -2119,7 +2161,7 @@ async function storeApiDelete(url, options = {}) {
|
|
|
2119
2161
|
}
|
|
2120
2162
|
|
|
2121
2163
|
// src/vars/vars.handler.ts
|
|
2122
|
-
import { getHost, getOrigin, isDevelopment as
|
|
2164
|
+
import { getHost, getOrigin, isDevelopment as isDevelopment3, isProduction } from "@websolutespa/bom-core";
|
|
2123
2165
|
|
|
2124
2166
|
// ../models/src/label/label.service.ts
|
|
2125
2167
|
async function getLabels(params = {}) {
|
|
@@ -2268,7 +2310,7 @@ function varsHandler() {
|
|
|
2268
2310
|
const layout = typeof locale === "string" && typeof market === "string" ? await getLayout(market, locale) : void 0;
|
|
2269
2311
|
const info = {
|
|
2270
2312
|
production: isProduction,
|
|
2271
|
-
development:
|
|
2313
|
+
development: isDevelopment3,
|
|
2272
2314
|
origin: getOrigin(request.headers),
|
|
2273
2315
|
host: getHost(request.headers),
|
|
2274
2316
|
headers: { ...sortKeys(request.headers) },
|
|
@@ -2393,7 +2435,6 @@ export {
|
|
|
2393
2435
|
MockPageService,
|
|
2394
2436
|
MockService,
|
|
2395
2437
|
NextPhase,
|
|
2396
|
-
REMOTE_API_SESSION,
|
|
2397
2438
|
RemoteMenuService,
|
|
2398
2439
|
RemotePageService,
|
|
2399
2440
|
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
|
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
2
1
|
import { isDevelopment } from '@websolutespa/bom-core';
|
|
3
|
-
import * as http from 'http';
|
|
4
2
|
import { getIronSession, IronSession, SessionOptions } from 'iron-session';
|
|
3
|
+
import { NextApiRequest, NextApiResponse } from 'next';
|
|
4
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
5
5
|
|
|
6
6
|
export async function getSession<T extends object = Record<string, any>>(
|
|
7
|
-
request:
|
|
8
|
-
response
|
|
7
|
+
request: NextRequest | NextApiRequest,
|
|
8
|
+
response: NextResponse | NextApiResponse
|
|
9
9
|
): Promise<IronSession<T>> {
|
|
10
10
|
const COOKIE_NAME = 'websolutespa-next-js';
|
|
11
11
|
const COOKIE_PASSWORD = process.env.SECRET_COOKIE_PASSWORD as string || 'SECRET_COOKIE_PASSWORD_32_CHARS_LONG';
|
|
@@ -17,9 +17,27 @@ export async function getSession<T extends object = Record<string, any>>(
|
|
|
17
17
|
},
|
|
18
18
|
password: COOKIE_PASSWORD,
|
|
19
19
|
};
|
|
20
|
-
const session =
|
|
21
|
-
'cookies' in request ?
|
|
22
|
-
await getIronSession<T>(request.cookies, sessionOptions) :
|
|
23
|
-
await getIronSession<T>(request, response, sessionOptions);
|
|
20
|
+
const session = await getIronSession<T>(request, response, sessionOptions);
|
|
24
21
|
return session;
|
|
25
22
|
}
|
|
23
|
+
|
|
24
|
+
export type SessionWithToken<T extends object = Record<string, any>> = T & Partial<{
|
|
25
|
+
token: string;
|
|
26
|
+
exp: number;
|
|
27
|
+
}>;
|
|
28
|
+
|
|
29
|
+
export async function getSessionToken<T extends SessionWithToken>(
|
|
30
|
+
request: NextRequest | NextApiRequest,
|
|
31
|
+
response: NextResponse | NextApiResponse
|
|
32
|
+
): Promise<string | undefined> {
|
|
33
|
+
const session = await getSession<T>(request, response);
|
|
34
|
+
if (session.token && session.exp && session.exp > Math.floor(new Date().getTime() / 1000)) {
|
|
35
|
+
return session.token;
|
|
36
|
+
} else {
|
|
37
|
+
session.token = undefined;
|
|
38
|
+
session.exp = undefined;
|
|
39
|
+
await session.save();
|
|
40
|
+
session.destroy();
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { IRedirect, IRoute, IStore, asEquatable, defaultLocale, defaultMarket, qsDeserialize } from '@websolutespa/bom-core';
|
|
1
|
+
import { IEntity, IRedirect, IRoute, IStore, asEquatable, defaultLocale, defaultMarket, qsDeserialize } from '@websolutespa/bom-core';
|
|
2
2
|
import { NextApiRequest, NextApiResponse } from 'next';
|
|
3
3
|
import { apiHandler } from '../api/api.handler';
|
|
4
|
+
import { getSessionToken } from '../session/session.service';
|
|
4
5
|
import { COLLECTIONS } from './store';
|
|
5
6
|
import { getStore } from './store.service';
|
|
6
7
|
|
|
@@ -44,10 +45,11 @@ export function collectionGet<T extends IStore>(storePromise: () => Promise<T>)
|
|
|
44
45
|
console.error('storeHandler.collectionGet', 'method not allowed', collection);
|
|
45
46
|
return response.status(405).send('method not allowed');
|
|
46
47
|
}
|
|
48
|
+
const token = await getSessionToken(request, response);
|
|
47
49
|
// find a specific document by id
|
|
48
50
|
if (id) {
|
|
49
51
|
// console.log('storeHandler.collectionGet', collection, id, params);
|
|
50
|
-
const item = await service.findOne(asEquatable(id), params);
|
|
52
|
+
const item = await service.findOne(asEquatable(id), params, token);
|
|
51
53
|
if (item) {
|
|
52
54
|
return response.status(200).json(item);
|
|
53
55
|
} else {
|
|
@@ -56,11 +58,11 @@ export function collectionGet<T extends IStore>(storePromise: () => Promise<T>)
|
|
|
56
58
|
// find paginated documents
|
|
57
59
|
} else if (String(params.pagination) === 'true') {
|
|
58
60
|
// console.log('storeHandler.collectionGet', collection, params);
|
|
59
|
-
const pagination = await service.findPaged(params);
|
|
61
|
+
const pagination = await service.findPaged(params, token);
|
|
60
62
|
return response.status(200).json(pagination);
|
|
61
63
|
} else {
|
|
62
64
|
// console.log('storeHandler.collectionGet', collection, params);
|
|
63
|
-
const items = await service.findMany(params);
|
|
65
|
+
const items = await service.findMany(params, token);
|
|
64
66
|
return response.status(200).json(items);
|
|
65
67
|
}
|
|
66
68
|
} catch (error: any) {
|
|
@@ -80,6 +82,13 @@ export function collectionPost<T extends IStore>(storePromise: () => Promise<T>)
|
|
|
80
82
|
return response.status(405).send('method not allowed');
|
|
81
83
|
}
|
|
82
84
|
const collection: string = collections[0];
|
|
85
|
+
const id: string = collections[1];
|
|
86
|
+
if (id) {
|
|
87
|
+
console.error('method not allowed', collection);
|
|
88
|
+
return response.status(405).send('method not allowed');
|
|
89
|
+
}
|
|
90
|
+
const qs = Object.entries(query).map(kv => kv.join('=')).join('&');
|
|
91
|
+
const params = qsDeserialize(qs);
|
|
83
92
|
const store = await storePromise();
|
|
84
93
|
const service = store[collection];
|
|
85
94
|
if (!service) {
|
|
@@ -90,7 +99,10 @@ export function collectionPost<T extends IStore>(storePromise: () => Promise<T>)
|
|
|
90
99
|
if (collection === 'route') {
|
|
91
100
|
return routePost(store, request, response);
|
|
92
101
|
}
|
|
93
|
-
|
|
102
|
+
const token = await getSessionToken(request, response);
|
|
103
|
+
const payload: Omit<IEntity, 'id'> = request.body;
|
|
104
|
+
const item = await service.create(payload, params, token);
|
|
105
|
+
return response.status(200).json(item);
|
|
94
106
|
} catch (error: any) {
|
|
95
107
|
console.error('storeHandler.error.500', error);
|
|
96
108
|
throw error;
|
|
@@ -127,10 +139,12 @@ export function collectionPatch<T extends IStore>(storePromise: () => Promise<T>
|
|
|
127
139
|
console.error('storeHandler.collectionUpdate', 'method not allowed', collection);
|
|
128
140
|
return response.status(405).send('method not allowed');
|
|
129
141
|
}
|
|
142
|
+
const token = await getSessionToken(request, response);
|
|
143
|
+
const payload: Partial<IEntity> = request.body;
|
|
130
144
|
// update a specific document by id
|
|
131
145
|
if (id) {
|
|
132
146
|
// console.log('storeHandler.collectionUpdate', collection, id, params);
|
|
133
|
-
const item = await service.updateOne(asEquatable(id), params);
|
|
147
|
+
const item = await service.updateOne(asEquatable(id), payload, params, token);
|
|
134
148
|
if (item) {
|
|
135
149
|
return response.status(200).json(item);
|
|
136
150
|
} else {
|
|
@@ -138,7 +152,7 @@ export function collectionPatch<T extends IStore>(storePromise: () => Promise<T>
|
|
|
138
152
|
}
|
|
139
153
|
} else {
|
|
140
154
|
// console.log('storeHandler.collectionUpdate', collection, params);
|
|
141
|
-
const items = await service.updateMany(params);
|
|
155
|
+
const items = await service.updateMany(payload, params, token);
|
|
142
156
|
return response.status(200).json(items);
|
|
143
157
|
}
|
|
144
158
|
} catch (error: any) {
|
|
@@ -177,10 +191,11 @@ export function collectionDelete<T extends IStore>(storePromise: () => Promise<T
|
|
|
177
191
|
console.error('storeHandler.collectionDelete', 'method not allowed', collection);
|
|
178
192
|
return response.status(405).send('method not allowed');
|
|
179
193
|
}
|
|
194
|
+
const token = await getSessionToken(request, response);
|
|
180
195
|
// delete a specific document by id
|
|
181
196
|
if (id) {
|
|
182
197
|
// console.log('storeHandler.collectionDelete', collection, id, params);
|
|
183
|
-
const item = await service.deleteOne(asEquatable(id), params);
|
|
198
|
+
const item = await service.deleteOne(asEquatable(id), params, token);
|
|
184
199
|
if (item) {
|
|
185
200
|
return response.status(200).json(item);
|
|
186
201
|
} else {
|
|
@@ -188,7 +203,7 @@ export function collectionDelete<T extends IStore>(storePromise: () => Promise<T
|
|
|
188
203
|
}
|
|
189
204
|
} else {
|
|
190
205
|
// console.log('storeHandler.collectionDelete', collection, params);
|
|
191
|
-
const items = await service.deleteMany(params);
|
|
206
|
+
const items = await service.deleteMany(params, token);
|
|
192
207
|
return response.status(200).json(items);
|
|
193
208
|
}
|
|
194
209
|
} catch (error: any) {
|