@websolutespa/bom-mixer-store 1.8.6 → 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 +8 -0
- package/dist/index.js +97 -52
- package/dist/index.mjs +58 -13
- package/package.json +1 -1
- package/src/session/session.service.ts +26 -8
- package/src/store/store.handler.ts +24 -9
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -855,7 +855,37 @@ async function mockApiDelete(url, options = {}) {
|
|
|
855
855
|
}
|
|
856
856
|
|
|
857
857
|
// src/store/store.handler.ts
|
|
858
|
-
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
|
+
}
|
|
859
889
|
|
|
860
890
|
// src/static/static.ts
|
|
861
891
|
var staticCollections = [{
|
|
@@ -1152,7 +1182,7 @@ var KNOWN_COLLECTIONS = [
|
|
|
1152
1182
|
var COLLECTIONS = [...ENV_COLLECTIONS, ...KNOWN_COLLECTIONS];
|
|
1153
1183
|
|
|
1154
1184
|
// src/mock/mock.store.ts
|
|
1155
|
-
var
|
|
1185
|
+
var import_bom_core11 = require("@websolutespa/bom-core");
|
|
1156
1186
|
|
|
1157
1187
|
// src/fs/fs.service.ts
|
|
1158
1188
|
var fs = __toESM(require("fs"));
|
|
@@ -1180,8 +1210,8 @@ async function fsReadJson(pathname) {
|
|
|
1180
1210
|
}
|
|
1181
1211
|
|
|
1182
1212
|
// src/static/static.service.ts
|
|
1183
|
-
var
|
|
1184
|
-
var StaticService = class extends
|
|
1213
|
+
var import_bom_core10 = require("@websolutespa/bom-core");
|
|
1214
|
+
var StaticService = class extends import_bom_core10.JsonService {
|
|
1185
1215
|
constructor(config, store) {
|
|
1186
1216
|
super([]);
|
|
1187
1217
|
this.loaded = false;
|
|
@@ -1209,7 +1239,7 @@ var StaticService = class extends import_bom_core9.JsonService {
|
|
|
1209
1239
|
const config = this.config;
|
|
1210
1240
|
const src = config.src;
|
|
1211
1241
|
if (typeof src === "string") {
|
|
1212
|
-
const response = await (0,
|
|
1242
|
+
const response = await (0, import_bom_core10.httpGet)(src, {});
|
|
1213
1243
|
items = response.items;
|
|
1214
1244
|
} else if (typeof src === "function") {
|
|
1215
1245
|
if (src instanceof Promise) {
|
|
@@ -1325,7 +1355,7 @@ function getMockStoreFromData_(data) {
|
|
|
1325
1355
|
return store;
|
|
1326
1356
|
}
|
|
1327
1357
|
async function getStoreData_() {
|
|
1328
|
-
if (
|
|
1358
|
+
if (import_bom_core11.isDevelopment) {
|
|
1329
1359
|
const path = require("path");
|
|
1330
1360
|
const pathname = path.join(process.cwd(), ".mixer", "store.json");
|
|
1331
1361
|
return await fsReadJson(pathname) || {};
|
|
@@ -1341,17 +1371,17 @@ async function getMockStore_() {
|
|
|
1341
1371
|
}
|
|
1342
1372
|
async function getMockStore() {
|
|
1343
1373
|
return fromCache(async () => {
|
|
1344
|
-
if (!
|
|
1374
|
+
if (!import_bom_core11.isDevelopment) {
|
|
1345
1375
|
console.warn("getMockStore");
|
|
1346
1376
|
}
|
|
1347
1377
|
const store = await getMockStore_();
|
|
1348
1378
|
return store;
|
|
1349
|
-
},
|
|
1379
|
+
}, import_bom_core11.isDevelopment ? void 0 : "STORE_MOCK_");
|
|
1350
1380
|
}
|
|
1351
1381
|
|
|
1352
1382
|
// src/cached/cached.service.ts
|
|
1353
|
-
var
|
|
1354
|
-
var CachedService = class extends
|
|
1383
|
+
var import_bom_core12 = require("@websolutespa/bom-core");
|
|
1384
|
+
var CachedService = class extends import_bom_core12.JsonService {
|
|
1355
1385
|
constructor(service, options = {}) {
|
|
1356
1386
|
super([]);
|
|
1357
1387
|
this.cache_ = /* @__PURE__ */ new Map();
|
|
@@ -1390,7 +1420,7 @@ var CachedService = class extends import_bom_core11.JsonService {
|
|
|
1390
1420
|
});
|
|
1391
1421
|
}
|
|
1392
1422
|
async findOne(id, params = {}) {
|
|
1393
|
-
const serializedParams = (0,
|
|
1423
|
+
const serializedParams = (0, import_bom_core12.qsSerialize)(params);
|
|
1394
1424
|
const key = `findOne[${id}]?${serializedParams}`;
|
|
1395
1425
|
return await this.fromCache_(async () => {
|
|
1396
1426
|
return await this.service_.findOne(id, params);
|
|
@@ -1399,7 +1429,7 @@ var CachedService = class extends import_bom_core11.JsonService {
|
|
|
1399
1429
|
async findMany(params = {}) {
|
|
1400
1430
|
if (storeStrategy === "decoratedApi" /* DecoratedApi */) {
|
|
1401
1431
|
const { market, locale, ...otherParams } = params;
|
|
1402
|
-
const serializedParams = (0,
|
|
1432
|
+
const serializedParams = (0, import_bom_core12.qsSerialize)({ market, locale });
|
|
1403
1433
|
const key = `findMany?${serializedParams}`;
|
|
1404
1434
|
let items = await this.fromCache_(async () => {
|
|
1405
1435
|
return await this.service_.findMany({ market, locale });
|
|
@@ -1412,7 +1442,7 @@ var CachedService = class extends import_bom_core11.JsonService {
|
|
|
1412
1442
|
}
|
|
1413
1443
|
return items;
|
|
1414
1444
|
} else {
|
|
1415
|
-
const serializedParams = (0,
|
|
1445
|
+
const serializedParams = (0, import_bom_core12.qsSerialize)(params);
|
|
1416
1446
|
const key = `findMany?${serializedParams}`;
|
|
1417
1447
|
return await this.fromCache_(async () => {
|
|
1418
1448
|
return await this.service_.findMany(params);
|
|
@@ -1420,7 +1450,7 @@ var CachedService = class extends import_bom_core11.JsonService {
|
|
|
1420
1450
|
}
|
|
1421
1451
|
}
|
|
1422
1452
|
async findPaged(params = {}) {
|
|
1423
|
-
const serializedParams = (0,
|
|
1453
|
+
const serializedParams = (0, import_bom_core12.qsSerialize)(params);
|
|
1424
1454
|
const key = `findPaged?${serializedParams}`;
|
|
1425
1455
|
return await this.fromCache_(async () => {
|
|
1426
1456
|
return await this.service_.findPaged(params);
|
|
@@ -1451,25 +1481,25 @@ var CachedService = class extends import_bom_core11.JsonService {
|
|
|
1451
1481
|
};
|
|
1452
1482
|
|
|
1453
1483
|
// src/remote/remote.service.ts
|
|
1454
|
-
var
|
|
1484
|
+
var import_bom_core14 = require("@websolutespa/bom-core");
|
|
1455
1485
|
|
|
1456
1486
|
// src/remote/remote.api.ts
|
|
1457
|
-
var
|
|
1487
|
+
var import_bom_core13 = require("@websolutespa/bom-core");
|
|
1458
1488
|
var API_URL3 = process.env.STORE_URL;
|
|
1459
1489
|
var API_BASE4 = process.env.STORE_API || "/api";
|
|
1460
1490
|
function getRemoteApiUrl(pathname) {
|
|
1461
1491
|
let url = `${API_URL3}${API_BASE4}${pathname}`;
|
|
1462
|
-
url =
|
|
1492
|
+
url = import_bom_core13.isBrowser ? url : url.replace("://localhost", "://127.0.0.1");
|
|
1463
1493
|
return url;
|
|
1464
1494
|
}
|
|
1465
1495
|
async function remoteApiFetch(pathname, options = {}) {
|
|
1466
|
-
if (
|
|
1496
|
+
if (import_bom_core13.isBrowser) {
|
|
1467
1497
|
throw "Unauthorized: remote api cannot be fetched client side";
|
|
1468
1498
|
}
|
|
1469
1499
|
const url = getRemoteApiUrl(pathname);
|
|
1470
|
-
const apiOptions = (0,
|
|
1500
|
+
const apiOptions = (0, import_bom_core13.merge)({ ...getRemoteApiRequestOptions() }, options);
|
|
1471
1501
|
console.log("remoteApiFetch", apiOptions.method, url);
|
|
1472
|
-
const apiResponse = await (0,
|
|
1502
|
+
const apiResponse = await (0, import_bom_core13.httpFetch)(url, apiOptions);
|
|
1473
1503
|
return apiResponse;
|
|
1474
1504
|
}
|
|
1475
1505
|
async function remoteApiGet(url, options = {}) {
|
|
@@ -1506,7 +1536,7 @@ var RemoteService = class {
|
|
|
1506
1536
|
async findOne(id, params = {}, token) {
|
|
1507
1537
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1508
1538
|
params.draft = RemoteServiceStrategy.draft;
|
|
1509
|
-
const query = (0,
|
|
1539
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1510
1540
|
const search = query ? `?${query}` : "";
|
|
1511
1541
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1512
1542
|
try {
|
|
@@ -1523,7 +1553,7 @@ var RemoteService = class {
|
|
|
1523
1553
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1524
1554
|
params.draft = RemoteServiceStrategy.draft;
|
|
1525
1555
|
params.pagination = false;
|
|
1526
|
-
const query = (0,
|
|
1556
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1527
1557
|
const search = query ? `?${query}` : "";
|
|
1528
1558
|
const url = `/${this.key}${search}`;
|
|
1529
1559
|
const response = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
@@ -1534,7 +1564,7 @@ var RemoteService = class {
|
|
|
1534
1564
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1535
1565
|
params.draft = RemoteServiceStrategy.draft;
|
|
1536
1566
|
params.pagination = true;
|
|
1537
|
-
const query = (0,
|
|
1567
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1538
1568
|
const search = query ? `?${query}` : "";
|
|
1539
1569
|
const url = `/${this.key}${search}`;
|
|
1540
1570
|
const response = await remoteApiGet(url, getRemoteApiRequestOptions(token));
|
|
@@ -1545,7 +1575,7 @@ var RemoteService = class {
|
|
|
1545
1575
|
async create(payload, params = {}, token) {
|
|
1546
1576
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1547
1577
|
params.draft = RemoteServiceStrategy.draft;
|
|
1548
|
-
const query = (0,
|
|
1578
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1549
1579
|
const search = query ? `?${query}` : "";
|
|
1550
1580
|
const url = `/${this.key}${search}`;
|
|
1551
1581
|
const result = await remoteApiPost(url, payload, getRemoteApiRequestOptions(token));
|
|
@@ -1553,7 +1583,7 @@ var RemoteService = class {
|
|
|
1553
1583
|
return await this.decorateOne_(item, {});
|
|
1554
1584
|
}
|
|
1555
1585
|
async updateOne(id, payload, params = {}, token) {
|
|
1556
|
-
const query = (0,
|
|
1586
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1557
1587
|
const search = query ? `?${query}` : "";
|
|
1558
1588
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1559
1589
|
const result = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
@@ -1564,7 +1594,7 @@ var RemoteService = class {
|
|
|
1564
1594
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1565
1595
|
params.draft = RemoteServiceStrategy.draft;
|
|
1566
1596
|
params.pagination = false;
|
|
1567
|
-
const query = (0,
|
|
1597
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1568
1598
|
const search = query ? `?${query}` : "";
|
|
1569
1599
|
const url = `/${this.key}${search}`;
|
|
1570
1600
|
const response = await remoteApiPatch(url, payload, getRemoteApiRequestOptions(token));
|
|
@@ -1572,7 +1602,7 @@ var RemoteService = class {
|
|
|
1572
1602
|
return await this.decorateMany_(items, params);
|
|
1573
1603
|
}
|
|
1574
1604
|
async deleteOne(id, params = {}, token) {
|
|
1575
|
-
const query = (0,
|
|
1605
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1576
1606
|
const search = query ? `?${query}` : "";
|
|
1577
1607
|
const url = `/${this.key}/${encodeURIComponent(id)}${search}`;
|
|
1578
1608
|
const item = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
@@ -1582,7 +1612,7 @@ var RemoteService = class {
|
|
|
1582
1612
|
params.richText = String(params.richText) === "true" ? true : false;
|
|
1583
1613
|
params.draft = RemoteServiceStrategy.draft;
|
|
1584
1614
|
params.pagination = false;
|
|
1585
|
-
const query = (0,
|
|
1615
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1586
1616
|
const search = query ? `?${query}` : "";
|
|
1587
1617
|
const url = `/${this.key}${search}`;
|
|
1588
1618
|
const response = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
@@ -1604,7 +1634,7 @@ var RemoteService = class {
|
|
|
1604
1634
|
}
|
|
1605
1635
|
}
|
|
1606
1636
|
};
|
|
1607
|
-
const query = (0,
|
|
1637
|
+
const query = (0, import_bom_core14.qsSerialize)(params);
|
|
1608
1638
|
const search = query ? `?${query}` : "";
|
|
1609
1639
|
const url = `/${this.key}${search}`;
|
|
1610
1640
|
const item = await remoteApiDelete(url, getRemoteApiRequestOptions(token));
|
|
@@ -1780,7 +1810,7 @@ function collectionGet(storePromise) {
|
|
|
1780
1810
|
const collection = collections[0];
|
|
1781
1811
|
const id = collections[1];
|
|
1782
1812
|
const qs = Object.entries(query).map((kv) => kv.join("=")).join("&");
|
|
1783
|
-
const params = (0,
|
|
1813
|
+
const params = (0, import_bom_core15.qsDeserialize)(qs);
|
|
1784
1814
|
if (!COLLECTIONS.includes(collection)) {
|
|
1785
1815
|
console.error("storeHandler.collectionGet", "method not allowed", collection);
|
|
1786
1816
|
return response.status(405).send("method not allowed");
|
|
@@ -1791,18 +1821,19 @@ function collectionGet(storePromise) {
|
|
|
1791
1821
|
console.error("storeHandler.collectionGet", "method not allowed", collection);
|
|
1792
1822
|
return response.status(405).send("method not allowed");
|
|
1793
1823
|
}
|
|
1824
|
+
const token = await getSessionToken(request, response);
|
|
1794
1825
|
if (id) {
|
|
1795
|
-
const item = await service.findOne((0,
|
|
1826
|
+
const item = await service.findOne((0, import_bom_core15.asEquatable)(id), params, token);
|
|
1796
1827
|
if (item) {
|
|
1797
1828
|
return response.status(200).json(item);
|
|
1798
1829
|
} else {
|
|
1799
1830
|
return response.status(404).send("not found");
|
|
1800
1831
|
}
|
|
1801
1832
|
} else if (String(params.pagination) === "true") {
|
|
1802
|
-
const pagination = await service.findPaged(params);
|
|
1833
|
+
const pagination = await service.findPaged(params, token);
|
|
1803
1834
|
return response.status(200).json(pagination);
|
|
1804
1835
|
} else {
|
|
1805
|
-
const items = await service.findMany(params);
|
|
1836
|
+
const items = await service.findMany(params, token);
|
|
1806
1837
|
return response.status(200).json(items);
|
|
1807
1838
|
}
|
|
1808
1839
|
} catch (error) {
|
|
@@ -1820,6 +1851,13 @@ function collectionPost(storePromise) {
|
|
|
1820
1851
|
return response.status(405).send("method not allowed");
|
|
1821
1852
|
}
|
|
1822
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);
|
|
1823
1861
|
const store = await storePromise();
|
|
1824
1862
|
const service = store[collection];
|
|
1825
1863
|
if (!service) {
|
|
@@ -1829,6 +1867,10 @@ function collectionPost(storePromise) {
|
|
|
1829
1867
|
if (collection === "route") {
|
|
1830
1868
|
return routePost(store, request, response);
|
|
1831
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);
|
|
1832
1874
|
} catch (error) {
|
|
1833
1875
|
console.error("storeHandler.error.500", error);
|
|
1834
1876
|
throw error;
|
|
@@ -1850,7 +1892,7 @@ function collectionPatch(storePromise) {
|
|
|
1850
1892
|
const collection = collections[0];
|
|
1851
1893
|
const id = collections[1];
|
|
1852
1894
|
const qs = Object.entries(query).map((kv) => kv.join("=")).join("&");
|
|
1853
|
-
const params = (0,
|
|
1895
|
+
const params = (0, import_bom_core15.qsDeserialize)(qs);
|
|
1854
1896
|
if (!COLLECTIONS.includes(collection)) {
|
|
1855
1897
|
console.error("storeHandler.collectionUpdate", "method not allowed", collection);
|
|
1856
1898
|
return response.status(405).send("method not allowed");
|
|
@@ -1861,15 +1903,17 @@ function collectionPatch(storePromise) {
|
|
|
1861
1903
|
console.error("storeHandler.collectionUpdate", "method not allowed", collection);
|
|
1862
1904
|
return response.status(405).send("method not allowed");
|
|
1863
1905
|
}
|
|
1906
|
+
const token = await getSessionToken(request, response);
|
|
1907
|
+
const payload = request.body;
|
|
1864
1908
|
if (id) {
|
|
1865
|
-
const item = await service.updateOne((0,
|
|
1909
|
+
const item = await service.updateOne((0, import_bom_core15.asEquatable)(id), payload, params, token);
|
|
1866
1910
|
if (item) {
|
|
1867
1911
|
return response.status(200).json(item);
|
|
1868
1912
|
} else {
|
|
1869
1913
|
return response.status(404).send("not found");
|
|
1870
1914
|
}
|
|
1871
1915
|
} else {
|
|
1872
|
-
const items = await service.updateMany(params);
|
|
1916
|
+
const items = await service.updateMany(payload, params, token);
|
|
1873
1917
|
return response.status(200).json(items);
|
|
1874
1918
|
}
|
|
1875
1919
|
} catch (error) {
|
|
@@ -1893,7 +1937,7 @@ function collectionDelete(storePromise) {
|
|
|
1893
1937
|
const collection = collections[0];
|
|
1894
1938
|
const id = collections[1];
|
|
1895
1939
|
const qs = Object.entries(query).map((kv) => kv.join("=")).join("&");
|
|
1896
|
-
const params = (0,
|
|
1940
|
+
const params = (0, import_bom_core15.qsDeserialize)(qs);
|
|
1897
1941
|
if (!COLLECTIONS.includes(collection)) {
|
|
1898
1942
|
console.error("storeHandler.collectionDelete", "method not allowed", collection);
|
|
1899
1943
|
return response.status(405).send("method not allowed");
|
|
@@ -1904,15 +1948,16 @@ function collectionDelete(storePromise) {
|
|
|
1904
1948
|
console.error("storeHandler.collectionDelete", "method not allowed", collection);
|
|
1905
1949
|
return response.status(405).send("method not allowed");
|
|
1906
1950
|
}
|
|
1951
|
+
const token = await getSessionToken(request, response);
|
|
1907
1952
|
if (id) {
|
|
1908
|
-
const item = await service.deleteOne((0,
|
|
1953
|
+
const item = await service.deleteOne((0, import_bom_core15.asEquatable)(id), params, token);
|
|
1909
1954
|
if (item) {
|
|
1910
1955
|
return response.status(200).json(item);
|
|
1911
1956
|
} else {
|
|
1912
1957
|
return response.status(404).send("not found");
|
|
1913
1958
|
}
|
|
1914
1959
|
} else {
|
|
1915
|
-
const items = await service.deleteMany(params);
|
|
1960
|
+
const items = await service.deleteMany(params, token);
|
|
1916
1961
|
return response.status(200).json(items);
|
|
1917
1962
|
}
|
|
1918
1963
|
} catch (error) {
|
|
@@ -1953,8 +1998,8 @@ function storeIndexHandler() {
|
|
|
1953
1998
|
return apiHandler({
|
|
1954
1999
|
get: async (request, response) => {
|
|
1955
2000
|
try {
|
|
1956
|
-
const market =
|
|
1957
|
-
const locale =
|
|
2001
|
+
const market = import_bom_core15.defaultMarket;
|
|
2002
|
+
const locale = import_bom_core15.defaultLocale;
|
|
1958
2003
|
if (!Array.isArray(COLLECTIONS) || COLLECTIONS?.length === 0) {
|
|
1959
2004
|
console.error("apiHome.collectionGet", "collections not found", COLLECTIONS);
|
|
1960
2005
|
return response.status(405).send("method not allowed");
|
|
@@ -2221,7 +2266,7 @@ async function storeApiDelete(url, options = {}) {
|
|
|
2221
2266
|
}
|
|
2222
2267
|
|
|
2223
2268
|
// src/vars/vars.handler.ts
|
|
2224
|
-
var
|
|
2269
|
+
var import_bom_core17 = require("@websolutespa/bom-core");
|
|
2225
2270
|
|
|
2226
2271
|
// ../models/src/label/label.service.ts
|
|
2227
2272
|
async function getLabels(params = {}) {
|
|
@@ -2252,7 +2297,7 @@ async function getMenus(params = {}) {
|
|
|
2252
2297
|
}
|
|
2253
2298
|
|
|
2254
2299
|
// ../models/src/route/route.service.ts
|
|
2255
|
-
var
|
|
2300
|
+
var import_bom_core16 = require("@websolutespa/bom-core");
|
|
2256
2301
|
async function getRouteLinkTree(market, locale) {
|
|
2257
2302
|
const store = await getStore();
|
|
2258
2303
|
const routes = await store.route.findMany({
|
|
@@ -2266,7 +2311,7 @@ async function getRouteLinkTree(market, locale) {
|
|
|
2266
2311
|
}
|
|
2267
2312
|
});
|
|
2268
2313
|
const categories = await store.category.findMany({ market, locale });
|
|
2269
|
-
const rootCategory = (0,
|
|
2314
|
+
const rootCategory = (0, import_bom_core16.getRootCategory)(categories);
|
|
2270
2315
|
if (rootCategory) {
|
|
2271
2316
|
const root = categoryToRouteLink(routes, categories, rootCategory, locale);
|
|
2272
2317
|
return root;
|
|
@@ -2274,13 +2319,13 @@ async function getRouteLinkTree(market, locale) {
|
|
|
2274
2319
|
return void 0;
|
|
2275
2320
|
}
|
|
2276
2321
|
function categoryToRouteLink(routes, categories, category, locale) {
|
|
2277
|
-
const route = (0,
|
|
2322
|
+
const route = (0, import_bom_core16.findRouteOfCategory)(routes, category.id);
|
|
2278
2323
|
if (!route) {
|
|
2279
2324
|
return void 0;
|
|
2280
2325
|
}
|
|
2281
|
-
const rootCategory = (0,
|
|
2326
|
+
const rootCategory = (0, import_bom_core16.getRootCategory)(categories);
|
|
2282
2327
|
const childCategories = categories.filter((x) => {
|
|
2283
|
-
const parentId = (0,
|
|
2328
|
+
const parentId = (0, import_bom_core16.asCategoryId)(x.category);
|
|
2284
2329
|
return rootCategory && rootCategory.id === category.id ? x.id !== rootCategory.id && (parentId === category.id || !parentId) : parentId === category.id;
|
|
2285
2330
|
});
|
|
2286
2331
|
const routeLink = newRouteLink(category, route, locale);
|
|
@@ -2292,7 +2337,7 @@ function newRouteLink(category, route, locale) {
|
|
|
2292
2337
|
const id = route?.page || category.id;
|
|
2293
2338
|
const media = category.media || route?.media;
|
|
2294
2339
|
const schema = route?.template || route?.schema || category?.schema || "unknown";
|
|
2295
|
-
const title = (0,
|
|
2340
|
+
const title = (0, import_bom_core16.localize)(category.title || route?.title || "untitled", locale);
|
|
2296
2341
|
const routeLink = {
|
|
2297
2342
|
category: category.id,
|
|
2298
2343
|
href,
|
|
@@ -2369,10 +2414,10 @@ function varsHandler() {
|
|
|
2369
2414
|
const { locale = defaultLocale3, market = defaultMaket } = request.query || {};
|
|
2370
2415
|
const layout = typeof locale === "string" && typeof market === "string" ? await getLayout(market, locale) : void 0;
|
|
2371
2416
|
const info = {
|
|
2372
|
-
production:
|
|
2373
|
-
development:
|
|
2374
|
-
origin: (0,
|
|
2375
|
-
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),
|
|
2376
2421
|
headers: { ...sortKeys(request.headers) },
|
|
2377
2422
|
env: { ...getFilteredEnv(process.env) },
|
|
2378
2423
|
layout: { ...sortKeys(layout) }
|
package/dist/index.mjs
CHANGED
|
@@ -752,6 +752,36 @@ async function mockApiDelete(url, options = {}) {
|
|
|
752
752
|
// src/store/store.handler.ts
|
|
753
753
|
import { asEquatable, defaultLocale, defaultMarket, qsDeserialize } from "@websolutespa/bom-core";
|
|
754
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
|
+
|
|
755
785
|
// src/static/static.ts
|
|
756
786
|
var staticCollections = [{
|
|
757
787
|
src: "https://actarian.github.io/cms-i18n-react/api/continent.json",
|
|
@@ -1047,7 +1077,7 @@ var KNOWN_COLLECTIONS = [
|
|
|
1047
1077
|
var COLLECTIONS = [...ENV_COLLECTIONS, ...KNOWN_COLLECTIONS];
|
|
1048
1078
|
|
|
1049
1079
|
// src/mock/mock.store.ts
|
|
1050
|
-
import { isDevelopment } from "@websolutespa/bom-core";
|
|
1080
|
+
import { isDevelopment as isDevelopment2 } from "@websolutespa/bom-core";
|
|
1051
1081
|
|
|
1052
1082
|
// src/fs/fs.service.ts
|
|
1053
1083
|
import * as fs from "fs";
|
|
@@ -1220,7 +1250,7 @@ function getMockStoreFromData_(data) {
|
|
|
1220
1250
|
return store;
|
|
1221
1251
|
}
|
|
1222
1252
|
async function getStoreData_() {
|
|
1223
|
-
if (
|
|
1253
|
+
if (isDevelopment2) {
|
|
1224
1254
|
const path = __require("path");
|
|
1225
1255
|
const pathname = path.join(process.cwd(), ".mixer", "store.json");
|
|
1226
1256
|
return await fsReadJson(pathname) || {};
|
|
@@ -1236,12 +1266,12 @@ async function getMockStore_() {
|
|
|
1236
1266
|
}
|
|
1237
1267
|
async function getMockStore() {
|
|
1238
1268
|
return fromCache(async () => {
|
|
1239
|
-
if (!
|
|
1269
|
+
if (!isDevelopment2) {
|
|
1240
1270
|
console.warn("getMockStore");
|
|
1241
1271
|
}
|
|
1242
1272
|
const store = await getMockStore_();
|
|
1243
1273
|
return store;
|
|
1244
|
-
},
|
|
1274
|
+
}, isDevelopment2 ? void 0 : "STORE_MOCK_");
|
|
1245
1275
|
}
|
|
1246
1276
|
|
|
1247
1277
|
// src/cached/cached.service.ts
|
|
@@ -1686,18 +1716,19 @@ function collectionGet(storePromise) {
|
|
|
1686
1716
|
console.error("storeHandler.collectionGet", "method not allowed", collection);
|
|
1687
1717
|
return response.status(405).send("method not allowed");
|
|
1688
1718
|
}
|
|
1719
|
+
const token = await getSessionToken(request, response);
|
|
1689
1720
|
if (id) {
|
|
1690
|
-
const item = await service.findOne(asEquatable(id), params);
|
|
1721
|
+
const item = await service.findOne(asEquatable(id), params, token);
|
|
1691
1722
|
if (item) {
|
|
1692
1723
|
return response.status(200).json(item);
|
|
1693
1724
|
} else {
|
|
1694
1725
|
return response.status(404).send("not found");
|
|
1695
1726
|
}
|
|
1696
1727
|
} else if (String(params.pagination) === "true") {
|
|
1697
|
-
const pagination = await service.findPaged(params);
|
|
1728
|
+
const pagination = await service.findPaged(params, token);
|
|
1698
1729
|
return response.status(200).json(pagination);
|
|
1699
1730
|
} else {
|
|
1700
|
-
const items = await service.findMany(params);
|
|
1731
|
+
const items = await service.findMany(params, token);
|
|
1701
1732
|
return response.status(200).json(items);
|
|
1702
1733
|
}
|
|
1703
1734
|
} catch (error) {
|
|
@@ -1715,6 +1746,13 @@ function collectionPost(storePromise) {
|
|
|
1715
1746
|
return response.status(405).send("method not allowed");
|
|
1716
1747
|
}
|
|
1717
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);
|
|
1718
1756
|
const store = await storePromise();
|
|
1719
1757
|
const service = store[collection];
|
|
1720
1758
|
if (!service) {
|
|
@@ -1724,6 +1762,10 @@ function collectionPost(storePromise) {
|
|
|
1724
1762
|
if (collection === "route") {
|
|
1725
1763
|
return routePost(store, request, response);
|
|
1726
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);
|
|
1727
1769
|
} catch (error) {
|
|
1728
1770
|
console.error("storeHandler.error.500", error);
|
|
1729
1771
|
throw error;
|
|
@@ -1756,15 +1798,17 @@ function collectionPatch(storePromise) {
|
|
|
1756
1798
|
console.error("storeHandler.collectionUpdate", "method not allowed", collection);
|
|
1757
1799
|
return response.status(405).send("method not allowed");
|
|
1758
1800
|
}
|
|
1801
|
+
const token = await getSessionToken(request, response);
|
|
1802
|
+
const payload = request.body;
|
|
1759
1803
|
if (id) {
|
|
1760
|
-
const item = await service.updateOne(asEquatable(id), params);
|
|
1804
|
+
const item = await service.updateOne(asEquatable(id), payload, params, token);
|
|
1761
1805
|
if (item) {
|
|
1762
1806
|
return response.status(200).json(item);
|
|
1763
1807
|
} else {
|
|
1764
1808
|
return response.status(404).send("not found");
|
|
1765
1809
|
}
|
|
1766
1810
|
} else {
|
|
1767
|
-
const items = await service.updateMany(params);
|
|
1811
|
+
const items = await service.updateMany(payload, params, token);
|
|
1768
1812
|
return response.status(200).json(items);
|
|
1769
1813
|
}
|
|
1770
1814
|
} catch (error) {
|
|
@@ -1799,15 +1843,16 @@ function collectionDelete(storePromise) {
|
|
|
1799
1843
|
console.error("storeHandler.collectionDelete", "method not allowed", collection);
|
|
1800
1844
|
return response.status(405).send("method not allowed");
|
|
1801
1845
|
}
|
|
1846
|
+
const token = await getSessionToken(request, response);
|
|
1802
1847
|
if (id) {
|
|
1803
|
-
const item = await service.deleteOne(asEquatable(id), params);
|
|
1848
|
+
const item = await service.deleteOne(asEquatable(id), params, token);
|
|
1804
1849
|
if (item) {
|
|
1805
1850
|
return response.status(200).json(item);
|
|
1806
1851
|
} else {
|
|
1807
1852
|
return response.status(404).send("not found");
|
|
1808
1853
|
}
|
|
1809
1854
|
} else {
|
|
1810
|
-
const items = await service.deleteMany(params);
|
|
1855
|
+
const items = await service.deleteMany(params, token);
|
|
1811
1856
|
return response.status(200).json(items);
|
|
1812
1857
|
}
|
|
1813
1858
|
} catch (error) {
|
|
@@ -2116,7 +2161,7 @@ async function storeApiDelete(url, options = {}) {
|
|
|
2116
2161
|
}
|
|
2117
2162
|
|
|
2118
2163
|
// src/vars/vars.handler.ts
|
|
2119
|
-
import { getHost, getOrigin, isDevelopment as
|
|
2164
|
+
import { getHost, getOrigin, isDevelopment as isDevelopment3, isProduction } from "@websolutespa/bom-core";
|
|
2120
2165
|
|
|
2121
2166
|
// ../models/src/label/label.service.ts
|
|
2122
2167
|
async function getLabels(params = {}) {
|
|
@@ -2265,7 +2310,7 @@ function varsHandler() {
|
|
|
2265
2310
|
const layout = typeof locale === "string" && typeof market === "string" ? await getLayout(market, locale) : void 0;
|
|
2266
2311
|
const info = {
|
|
2267
2312
|
production: isProduction,
|
|
2268
|
-
development:
|
|
2313
|
+
development: isDevelopment3,
|
|
2269
2314
|
origin: getOrigin(request.headers),
|
|
2270
2315
|
host: getHost(request.headers),
|
|
2271
2316
|
headers: { ...sortKeys(request.headers) },
|
package/package.json
CHANGED
|
@@ -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) {
|