@websolutespa/bom-mixer-store 0.3.1 → 1.0.0
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 +12 -0
- package/dist/index.d.ts +8 -3
- package/dist/index.js +271 -189
- package/dist/index.mjs +266 -189
- package/package.json +1 -1
- package/src/api/api.service.ts +10 -0
- package/src/fs/fs.service.ts +7 -7
- package/src/mock/mock-menu.service.ts +42 -32
- package/src/mock/mock.store.ts +28 -24
- package/src/store/store.handler.ts +240 -156
- package/src/store/store.service.ts +1 -0
- package/src/storeApi/storeApi.service.ts +1 -1
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -50,7 +50,7 @@ declare function isRelation(item: unknown): item is IPageRelation;
|
|
|
50
50
|
|
|
51
51
|
declare function getMockStoreServer<T extends IStore>(): Promise<T>;
|
|
52
52
|
declare function getMockStore<T extends IStore>(getJson: () => Promise<string>): Promise<T>;
|
|
53
|
-
declare function getMockStoreFromData<T extends IStore>(data
|
|
53
|
+
declare function getMockStoreFromData<T extends IStore>(data?: IMemoryStore): T;
|
|
54
54
|
|
|
55
55
|
type IField = {
|
|
56
56
|
name: string;
|
|
@@ -89,7 +89,12 @@ declare enum StoreStrategy {
|
|
|
89
89
|
}
|
|
90
90
|
declare const storeStrategy: StoreStrategy;
|
|
91
91
|
|
|
92
|
-
declare function storeHandler<T extends IStore>(storePromise?: () => Promise<T>): any;
|
|
92
|
+
declare function storeHandler<T extends IStore>(storePromise?: () => Promise<T>): any;
|
|
93
|
+
declare function collectionGet<T extends IStore>(storePromise: () => Promise<T>): (request: NextApiRequest, response: NextApiResponse) => Promise<void>;
|
|
94
|
+
declare function collectionPost<T extends IStore>(storePromise: () => Promise<T>): (request: NextApiRequest, response: NextApiResponse) => Promise<void>;
|
|
95
|
+
declare function collectionPatch<T extends IStore>(storePromise: () => Promise<T>): (request: NextApiRequest, response: NextApiResponse) => Promise<void>;
|
|
96
|
+
declare function collectionDelete<T extends IStore>(storePromise: () => Promise<T>): (request: NextApiRequest, response: NextApiResponse) => Promise<void>;
|
|
97
|
+
declare function routePost(store: IStore, request: NextApiRequest, response: NextApiResponse): Promise<void>;
|
|
93
98
|
|
|
94
99
|
declare const MOCK_COLLECTIONS: string[];
|
|
95
100
|
declare const STATIC_COLLECTIONS: string[];
|
|
@@ -122,4 +127,4 @@ declare function getApiStore<T extends IStore>(collections: string[]): Promise<T
|
|
|
122
127
|
declare function getApiRuntimeStore<T extends IStore>(collections: string[]): Promise<T>;
|
|
123
128
|
declare function getApiBuildStore<T extends IStore>(): Promise<T>;
|
|
124
129
|
|
|
125
|
-
export { ApiRequestOptions, COLLECTIONS, IApiHandler, ICrudMethod, IField, IHandler, KNOWN_COLLECTIONS, MOCK_COLLECTIONS, MapTo, MockService, STATIC_COLLECTIONS, StaticService, StaticServiceConfig, StaticServiceSrc, StoreApiService, StoreStrategy, apiDelete, apiFetch, apiGet, apiHandler, apiPatch, apiPost, apiPut, errorHandler, getApiBuildStore, getApiRuntimeStore, getApiStore, getApiUrl, getMockStore, getMockStoreFromData, getMockStoreServer, getStore, isApiRequest, isNextRequest, isObject, isRelation, isStaticRequest, mockHandler, staticCollections, storeApiDelete, storeApiFetch, storeApiGet, storeApiPatch, storeApiPost, storeApiPut, storeHandler, storeStrategy };
|
|
130
|
+
export { ApiRequestOptions, COLLECTIONS, IApiHandler, ICrudMethod, IField, IHandler, KNOWN_COLLECTIONS, MOCK_COLLECTIONS, MapTo, MockService, STATIC_COLLECTIONS, StaticService, StaticServiceConfig, StaticServiceSrc, StoreApiService, StoreStrategy, apiDelete, apiFetch, apiGet, apiHandler, apiPatch, apiPost, apiPut, collectionDelete, collectionGet, collectionPatch, collectionPost, errorHandler, getApiBuildStore, getApiRuntimeStore, getApiStore, getApiUrl, getMockStore, getMockStoreFromData, getMockStoreServer, getStore, isApiRequest, isNextRequest, isObject, isRelation, isStaticRequest, mockHandler, routePost, staticCollections, storeApiDelete, storeApiFetch, storeApiGet, storeApiPatch, storeApiPost, storeApiPut, storeHandler, storeStrategy };
|
package/dist/index.js
CHANGED
|
@@ -35,6 +35,10 @@ __export(src_exports, {
|
|
|
35
35
|
apiPatch: () => apiPatch,
|
|
36
36
|
apiPost: () => apiPost,
|
|
37
37
|
apiPut: () => apiPut,
|
|
38
|
+
collectionDelete: () => collectionDelete,
|
|
39
|
+
collectionGet: () => collectionGet,
|
|
40
|
+
collectionPatch: () => collectionPatch,
|
|
41
|
+
collectionPost: () => collectionPost,
|
|
38
42
|
errorHandler: () => errorHandler,
|
|
39
43
|
getApiBuildStore: () => getApiBuildStore,
|
|
40
44
|
getApiRuntimeStore: () => getApiRuntimeStore,
|
|
@@ -50,6 +54,7 @@ __export(src_exports, {
|
|
|
50
54
|
isRelation: () => isRelation,
|
|
51
55
|
isStaticRequest: () => isStaticRequest,
|
|
52
56
|
mockHandler: () => mockHandler,
|
|
57
|
+
routePost: () => routePost,
|
|
53
58
|
staticCollections: () => staticCollections,
|
|
54
59
|
storeApiDelete: () => storeApiDelete,
|
|
55
60
|
storeApiFetch: () => storeApiFetch,
|
|
@@ -159,10 +164,10 @@ var fs = require("fs");
|
|
|
159
164
|
async function fsRead(pathname, encoding = "utf8") {
|
|
160
165
|
try {
|
|
161
166
|
const data = await fs.promises.readFile(pathname, encoding);
|
|
162
|
-
return data ||
|
|
167
|
+
return data || void 0;
|
|
163
168
|
} catch (error) {
|
|
164
169
|
console.log("fsRead", error, pathname);
|
|
165
|
-
return
|
|
170
|
+
return;
|
|
166
171
|
}
|
|
167
172
|
}
|
|
168
173
|
async function fsReadJson(pathname) {
|
|
@@ -171,11 +176,11 @@ async function fsReadJson(pathname) {
|
|
|
171
176
|
if (data) {
|
|
172
177
|
return JSON.parse(data);
|
|
173
178
|
} else {
|
|
174
|
-
return
|
|
179
|
+
return;
|
|
175
180
|
}
|
|
176
181
|
} catch (error) {
|
|
177
182
|
console.log("fsReadJson", error, pathname);
|
|
178
|
-
return
|
|
183
|
+
return;
|
|
179
184
|
}
|
|
180
185
|
}
|
|
181
186
|
|
|
@@ -573,6 +578,26 @@ var MockMenuService = class extends MockService {
|
|
|
573
578
|
}
|
|
574
579
|
}
|
|
575
580
|
};
|
|
581
|
+
async function collectPages(store, items) {
|
|
582
|
+
if (!items) {
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
for (const item of items) {
|
|
586
|
+
if (item.type === "page" && typeof item.page.value === "number" && store[item.page.relationTo] !== void 0) {
|
|
587
|
+
const value = await store[item.page.relationTo].findOne(item.page.value);
|
|
588
|
+
if (value) {
|
|
589
|
+
item.page.value = value;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
switch (item.type) {
|
|
593
|
+
case "group":
|
|
594
|
+
case "link":
|
|
595
|
+
case "page":
|
|
596
|
+
await collectPages(store, item.items);
|
|
597
|
+
break;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
576
601
|
async function decorateMenu(store, item, market = "ww", locale = "en") {
|
|
577
602
|
const items = item.items || [];
|
|
578
603
|
const decoratedItem = {
|
|
@@ -590,27 +615,7 @@ async function decorateMenu(store, item, market = "ww", locale = "en") {
|
|
|
590
615
|
}
|
|
591
616
|
}
|
|
592
617
|
});
|
|
593
|
-
|
|
594
|
-
if (!items2) {
|
|
595
|
-
return;
|
|
596
|
-
}
|
|
597
|
-
for (const item2 of items2) {
|
|
598
|
-
if (item2.type === "page" && typeof item2.page.value === "number" && store[item2.page.relationTo] !== void 0) {
|
|
599
|
-
const value = await store[item2.page.relationTo].findOne(item2.page.value);
|
|
600
|
-
if (value) {
|
|
601
|
-
item2.page.value = value;
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
switch (item2.type) {
|
|
605
|
-
case "group":
|
|
606
|
-
case "link":
|
|
607
|
-
case "page":
|
|
608
|
-
await collectPages(item2.items);
|
|
609
|
-
break;
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
await collectPages(items);
|
|
618
|
+
await collectPages(store, items);
|
|
614
619
|
decoratedItem.items = items.filter((x) => hasMarket(x, market)).map((x) => decorateMenuItem(x, categories, routes, market));
|
|
615
620
|
return decoratedItem;
|
|
616
621
|
}
|
|
@@ -619,12 +624,14 @@ async function decorateMenus(store, items, market = "ww", locale = "en") {
|
|
|
619
624
|
}
|
|
620
625
|
function decorateMenuCategory(item, categories, routes, market, depth = 0) {
|
|
621
626
|
const decoratedItem = {
|
|
622
|
-
|
|
623
|
-
type: item.type,
|
|
624
|
-
title: "",
|
|
627
|
+
abstract: item.abstract,
|
|
625
628
|
category: "",
|
|
629
|
+
customClass: item.customClass,
|
|
626
630
|
href: "",
|
|
627
|
-
|
|
631
|
+
id: item.id || "",
|
|
632
|
+
items: [],
|
|
633
|
+
title: "",
|
|
634
|
+
type: item.type
|
|
628
635
|
};
|
|
629
636
|
const category = typeof item.category === "string" ? categories.find((x) => x.id === item.category) : item.category;
|
|
630
637
|
if (category) {
|
|
@@ -680,9 +687,11 @@ function decorateMenuRoute(item) {
|
|
|
680
687
|
function decorateMenuGroup(item, categories, routes, market) {
|
|
681
688
|
const items = item.items || [];
|
|
682
689
|
const decoratedItem = {
|
|
690
|
+
abstract: item.abstract,
|
|
691
|
+
customClass: item.customClass,
|
|
683
692
|
id: item.id || "",
|
|
684
|
-
type: item.type,
|
|
685
693
|
title: item.title,
|
|
694
|
+
type: item.type,
|
|
686
695
|
items: items.filter((x) => hasMarket(x, market)).map((x) => decorateMenuItem(x, categories, routes, market))
|
|
687
696
|
};
|
|
688
697
|
return decoratedItem;
|
|
@@ -690,11 +699,13 @@ function decorateMenuGroup(item, categories, routes, market) {
|
|
|
690
699
|
function decorateMenuLink(item, categories, routes, market) {
|
|
691
700
|
const items = item.items || [];
|
|
692
701
|
const decoratedItem = {
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
title: item.title,
|
|
702
|
+
abstract: item.abstract,
|
|
703
|
+
customClass: item.customClass,
|
|
696
704
|
href: item.href,
|
|
705
|
+
id: item.id || "",
|
|
697
706
|
target: item.target || "",
|
|
707
|
+
title: item.title,
|
|
708
|
+
type: item.type,
|
|
698
709
|
items: items.filter((x) => hasMarket(x, market)).map((x) => decorateMenuItem(x, categories, routes, market))
|
|
699
710
|
};
|
|
700
711
|
return decoratedItem;
|
|
@@ -706,6 +717,8 @@ function decorateMenuPage(item, categories, routes, market) {
|
|
|
706
717
|
id: item.id || "",
|
|
707
718
|
type: item.type,
|
|
708
719
|
title: item.title || item.page.value.title || "",
|
|
720
|
+
abstract: item.abstract || item.page.value.abstract || "",
|
|
721
|
+
customClass: item.customClass,
|
|
709
722
|
href: route?.id || "",
|
|
710
723
|
media: item.page.value.media,
|
|
711
724
|
page: route?.page || "",
|
|
@@ -836,28 +849,32 @@ async function getMockStore(getJson) {
|
|
|
836
849
|
}
|
|
837
850
|
function getMockStoreFromData(data) {
|
|
838
851
|
const store = {};
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
852
|
+
if (data) {
|
|
853
|
+
for (const key in data) {
|
|
854
|
+
if (!PAGES.includes(key)) {
|
|
855
|
+
const service = new MockService(data[key], store);
|
|
856
|
+
store[key] = service;
|
|
857
|
+
}
|
|
843
858
|
}
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
859
|
+
const category = store.category;
|
|
860
|
+
const route = store.route;
|
|
861
|
+
const menu = data.menu;
|
|
862
|
+
if (category && route && menu) {
|
|
863
|
+
store.menu = new MockMenuService(menu, store);
|
|
864
|
+
}
|
|
865
|
+
for (const key in data) {
|
|
866
|
+
if (PAGES.includes(key)) {
|
|
867
|
+
let service;
|
|
868
|
+
if (route) {
|
|
869
|
+
service = new MockPageService(data[key], store);
|
|
870
|
+
} else {
|
|
871
|
+
service = new MockService(data[key], store);
|
|
872
|
+
}
|
|
873
|
+
store[key] = service;
|
|
858
874
|
}
|
|
859
|
-
store[key] = service;
|
|
860
875
|
}
|
|
876
|
+
} else {
|
|
877
|
+
console.log("mock store not found");
|
|
861
878
|
}
|
|
862
879
|
staticCollections.forEach((config) => {
|
|
863
880
|
store[config.slug] = new StaticService(config, store);
|
|
@@ -1107,6 +1124,7 @@ var KNOWN_COLLECTIONS = [
|
|
|
1107
1124
|
"menu",
|
|
1108
1125
|
"template",
|
|
1109
1126
|
"page",
|
|
1127
|
+
"redirect",
|
|
1110
1128
|
"route",
|
|
1111
1129
|
...STATIC_COLLECTIONS
|
|
1112
1130
|
];
|
|
@@ -1127,157 +1145,216 @@ async function getStore() {
|
|
|
1127
1145
|
// src/store/store.handler.ts
|
|
1128
1146
|
function storeHandler(storePromise = async () => await getStore()) {
|
|
1129
1147
|
return apiHandler({
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
return response.status(200).json(pagination);
|
|
1148
|
+
get: collectionGet(storePromise),
|
|
1149
|
+
post: collectionPost(storePromise),
|
|
1150
|
+
patch: collectionPatch(storePromise),
|
|
1151
|
+
delete: collectionDelete(storePromise)
|
|
1152
|
+
});
|
|
1153
|
+
}
|
|
1154
|
+
function collectionGet(storePromise) {
|
|
1155
|
+
return async (request, response) => {
|
|
1156
|
+
try {
|
|
1157
|
+
const { collections, ...query } = request.query;
|
|
1158
|
+
if (!Array.isArray(collections) || collections?.length === 0) {
|
|
1159
|
+
console.error("storeHandler.collectionGet", "method not allowed", collections);
|
|
1160
|
+
return response.status(405).send("method not allowed");
|
|
1161
|
+
}
|
|
1162
|
+
const collection = collections[0];
|
|
1163
|
+
const id = collections[1];
|
|
1164
|
+
const qs = Object.entries(query).map((kv) => kv.join("=")).join("&");
|
|
1165
|
+
const params = (0, import_bom_core9.qsDeserialize)(qs);
|
|
1166
|
+
if (!COLLECTIONS.includes(collection)) {
|
|
1167
|
+
console.error("storeHandler.collectionGet", "method not allowed", collection);
|
|
1168
|
+
return response.status(405).send("method not allowed");
|
|
1169
|
+
}
|
|
1170
|
+
const store = await storePromise();
|
|
1171
|
+
const service = store[collection];
|
|
1172
|
+
if (!service) {
|
|
1173
|
+
console.error("storeHandler.collectionGet", "method not allowed", collection);
|
|
1174
|
+
return response.status(405).send("method not allowed");
|
|
1175
|
+
}
|
|
1176
|
+
if (id) {
|
|
1177
|
+
const item = await service.findOne((0, import_bom_core9.asEquatable)(id), params);
|
|
1178
|
+
if (item) {
|
|
1179
|
+
return response.status(200).json(item);
|
|
1163
1180
|
} else {
|
|
1164
|
-
|
|
1165
|
-
return response.status(200).json(items);
|
|
1181
|
+
return response.status(404).send("not found");
|
|
1166
1182
|
}
|
|
1167
|
-
}
|
|
1168
|
-
|
|
1169
|
-
|
|
1183
|
+
} else if (String(params.pagination) === "true") {
|
|
1184
|
+
const pagination = await service.findPaged(params);
|
|
1185
|
+
return response.status(200).json(pagination);
|
|
1186
|
+
} else {
|
|
1187
|
+
const items = await service.findMany(params);
|
|
1188
|
+
return response.status(200).json(items);
|
|
1170
1189
|
}
|
|
1171
|
-
}
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1190
|
+
} catch (error) {
|
|
1191
|
+
console.error("storeHandler.collectionGet.error", "500", error);
|
|
1192
|
+
throw error;
|
|
1193
|
+
}
|
|
1194
|
+
};
|
|
1195
|
+
}
|
|
1196
|
+
function collectionPost(storePromise) {
|
|
1197
|
+
return async (request, response) => {
|
|
1198
|
+
try {
|
|
1199
|
+
const { collections, ...query } = request.query;
|
|
1200
|
+
if (!Array.isArray(collections) || collections.length !== 1) {
|
|
1201
|
+
console.error("method not allowed", collections);
|
|
1202
|
+
return response.status(405).send("method not allowed");
|
|
1203
|
+
}
|
|
1204
|
+
const collection = collections[0];
|
|
1205
|
+
const store = await storePromise();
|
|
1206
|
+
const service = store[collection];
|
|
1207
|
+
if (!service) {
|
|
1208
|
+
console.error("method not allowed", collection);
|
|
1209
|
+
return response.status(405).send("method not allowed");
|
|
1210
|
+
}
|
|
1211
|
+
if (collection === "route") {
|
|
1212
|
+
return routePost(store, request, response);
|
|
1213
|
+
}
|
|
1214
|
+
} catch (error) {
|
|
1215
|
+
console.error("storeHandler.error.500", error);
|
|
1216
|
+
throw error;
|
|
1217
|
+
}
|
|
1218
|
+
};
|
|
1219
|
+
}
|
|
1220
|
+
function collectionPatch(storePromise) {
|
|
1221
|
+
return async (request, response) => {
|
|
1222
|
+
try {
|
|
1223
|
+
const params = request.query;
|
|
1224
|
+
const { collections, ...query } = params;
|
|
1225
|
+
if (!Array.isArray(collections) || collections.length !== 2) {
|
|
1226
|
+
console.error("method not allowed", collections);
|
|
1227
|
+
return response.status(405).send("method not allowed");
|
|
1228
|
+
}
|
|
1229
|
+
const collection = collections[0];
|
|
1230
|
+
const id = collections[1];
|
|
1231
|
+
const store = await storePromise();
|
|
1232
|
+
const service = store[collection];
|
|
1233
|
+
if (!service) {
|
|
1234
|
+
console.error("method not allowed", collection);
|
|
1235
|
+
return response.status(405).send("method not allowed");
|
|
1236
|
+
}
|
|
1237
|
+
} catch (error) {
|
|
1238
|
+
console.error("storeHandler.error.500", error);
|
|
1239
|
+
throw error;
|
|
1240
|
+
}
|
|
1241
|
+
};
|
|
1242
|
+
}
|
|
1243
|
+
function collectionDelete(storePromise) {
|
|
1244
|
+
return async (request, response) => {
|
|
1245
|
+
try {
|
|
1246
|
+
const params = request.query;
|
|
1247
|
+
const { collections, ...query } = params;
|
|
1248
|
+
if (!Array.isArray(collections) || collections.length !== 2) {
|
|
1249
|
+
console.error("method not allowed", collections);
|
|
1250
|
+
return response.status(405).send("method not allowed");
|
|
1251
|
+
}
|
|
1252
|
+
const collection = collections[0];
|
|
1253
|
+
const id = collections[1];
|
|
1254
|
+
const store = await storePromise();
|
|
1255
|
+
const service = store[collection];
|
|
1256
|
+
if (!service) {
|
|
1257
|
+
console.error("method not allowed", collection);
|
|
1258
|
+
return response.status(405).send("method not allowed");
|
|
1259
|
+
}
|
|
1260
|
+
} catch (error) {
|
|
1261
|
+
console.error("storeHandler.error.500", error);
|
|
1262
|
+
throw error;
|
|
1263
|
+
}
|
|
1264
|
+
};
|
|
1265
|
+
}
|
|
1266
|
+
async function routePost(store, request, response) {
|
|
1267
|
+
if (request.body.pathname && request.body.href) {
|
|
1268
|
+
try {
|
|
1269
|
+
const { pathname, href } = request.body;
|
|
1270
|
+
const routes = await store.route.findMany({
|
|
1271
|
+
where: {
|
|
1272
|
+
id: {
|
|
1273
|
+
equals: pathname
|
|
1274
|
+
}
|
|
1186
1275
|
}
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
}
|
|
1196
|
-
});
|
|
1197
|
-
if (routes.length === 0) {
|
|
1198
|
-
const splatRoutes = await store.route.findMany({
|
|
1199
|
-
where: {
|
|
1200
|
-
useSplat: {
|
|
1201
|
-
equals: true
|
|
1202
|
-
}
|
|
1203
|
-
}
|
|
1204
|
-
});
|
|
1205
|
-
const route = splatRoutes.reduce((p, c) => {
|
|
1206
|
-
const match = pathname.indexOf(c.id) === 0;
|
|
1207
|
-
if (match && (!p || p.id.length < c.id.length)) {
|
|
1208
|
-
return { ...c, splat: pathname.substring(c.id.length, pathname.length) };
|
|
1209
|
-
} else {
|
|
1210
|
-
return p;
|
|
1211
|
-
}
|
|
1212
|
-
}, void 0);
|
|
1213
|
-
if (route) {
|
|
1214
|
-
return response.status(200).json(route);
|
|
1215
|
-
}
|
|
1216
|
-
}
|
|
1217
|
-
if (routes.length > 0) {
|
|
1218
|
-
return response.status(200).json(routes[0]);
|
|
1219
|
-
} else {
|
|
1220
|
-
return response.status(404).send("not found");
|
|
1221
|
-
}
|
|
1222
|
-
} catch (error) {
|
|
1223
|
-
if (error.status === 404) {
|
|
1224
|
-
return response.status(404).send("not found");
|
|
1225
|
-
} else {
|
|
1226
|
-
console.error("storeHandler.error.500", error);
|
|
1227
|
-
throw error;
|
|
1276
|
+
});
|
|
1277
|
+
if (routes.length > 0) {
|
|
1278
|
+
return response.status(200).json(routes[0]);
|
|
1279
|
+
} else {
|
|
1280
|
+
const splatRoutes = await store.route.findMany({
|
|
1281
|
+
where: {
|
|
1282
|
+
useSplat: {
|
|
1283
|
+
equals: true
|
|
1228
1284
|
}
|
|
1229
1285
|
}
|
|
1286
|
+
});
|
|
1287
|
+
const splatRoute = splatRoutes.reduce((p, c) => {
|
|
1288
|
+
const match = pathname.indexOf(c.id) === 0;
|
|
1289
|
+
if (match && (!p || p.id.length < c.id.length)) {
|
|
1290
|
+
return { ...c, splat: pathname.substring(c.id.length, pathname.length) };
|
|
1291
|
+
} else {
|
|
1292
|
+
return p;
|
|
1293
|
+
}
|
|
1294
|
+
}, void 0);
|
|
1295
|
+
if (splatRoute) {
|
|
1296
|
+
return response.status(200).json(splatRoute);
|
|
1230
1297
|
}
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1298
|
+
const redirects = await store.redirect.findMany({
|
|
1299
|
+
where: {
|
|
1300
|
+
isActive: {
|
|
1301
|
+
equals: true
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
});
|
|
1305
|
+
const exactMatchResolver = (redirect) => {
|
|
1306
|
+
const status = parseInt(redirect.status) || 307;
|
|
1307
|
+
if (status < 400) {
|
|
1308
|
+
const redirectUrl = redirect.to;
|
|
1309
|
+
return response.status(status).send({
|
|
1310
|
+
redirectUrl
|
|
1311
|
+
});
|
|
1312
|
+
} else {
|
|
1313
|
+
return response.status(status).send("gone");
|
|
1314
|
+
}
|
|
1315
|
+
};
|
|
1316
|
+
const exactMatchRedirect = redirects.find((x) => x.from === href);
|
|
1317
|
+
if (exactMatchRedirect !== void 0) {
|
|
1318
|
+
return exactMatchResolver(exactMatchRedirect);
|
|
1244
1319
|
}
|
|
1245
|
-
const
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1320
|
+
const patternMatchResolver = (redirect) => {
|
|
1321
|
+
const status = parseInt(redirect.status) || 307;
|
|
1322
|
+
if (status < 400) {
|
|
1323
|
+
const regexp = new RegExp(redirect.from);
|
|
1324
|
+
let i = 0;
|
|
1325
|
+
let redirectUrl = redirect.to.replace(/\*/g, (...rest) => {
|
|
1326
|
+
return `$${++i}`;
|
|
1327
|
+
});
|
|
1328
|
+
redirectUrl = href.replace(regexp, redirectUrl);
|
|
1329
|
+
return response.status(status).send({
|
|
1330
|
+
redirectUrl
|
|
1331
|
+
});
|
|
1332
|
+
} else {
|
|
1333
|
+
return response.status(status).send("gone");
|
|
1334
|
+
}
|
|
1335
|
+
};
|
|
1336
|
+
redirects.forEach((x) => {
|
|
1337
|
+
x.from = x.from.replace(/\*/g, (...rest) => {
|
|
1338
|
+
return "(.*)";
|
|
1339
|
+
});
|
|
1340
|
+
});
|
|
1341
|
+
const firstMatch = redirects.find((x) => href.match(x.from));
|
|
1342
|
+
if (firstMatch) {
|
|
1343
|
+
return patternMatchResolver(firstMatch);
|
|
1252
1344
|
}
|
|
1253
|
-
|
|
1254
|
-
console.error("storeHandler.error.500", error);
|
|
1255
|
-
throw error;
|
|
1345
|
+
return response.status(404).send("not found");
|
|
1256
1346
|
}
|
|
1257
|
-
}
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
const params = request.query;
|
|
1262
|
-
const { collections, ...query } = params;
|
|
1263
|
-
if (!Array.isArray(collections) || collections.length !== 2) {
|
|
1264
|
-
console.error("method not allowed", collections);
|
|
1265
|
-
return response.status(405).send("method not allowed");
|
|
1266
|
-
}
|
|
1267
|
-
const collection = collections[0];
|
|
1268
|
-
const id = collections[1];
|
|
1269
|
-
const store = await storePromise();
|
|
1270
|
-
const service = store[collection];
|
|
1271
|
-
if (!service) {
|
|
1272
|
-
console.error("method not allowed", collection);
|
|
1273
|
-
return response.status(405).send("method not allowed");
|
|
1274
|
-
}
|
|
1275
|
-
} catch (error) {
|
|
1347
|
+
} catch (error) {
|
|
1348
|
+
if (error.status === 404) {
|
|
1349
|
+
return response.status(404).send("not found");
|
|
1350
|
+
} else {
|
|
1276
1351
|
console.error("storeHandler.error.500", error);
|
|
1277
1352
|
throw error;
|
|
1278
1353
|
}
|
|
1279
1354
|
}
|
|
1280
|
-
}
|
|
1355
|
+
} else {
|
|
1356
|
+
return response.status(400).send("bad request");
|
|
1357
|
+
}
|
|
1281
1358
|
}
|
|
1282
1359
|
|
|
1283
1360
|
// src/mock/mock.handler.ts
|
|
@@ -1301,6 +1378,10 @@ function mockHandler(getJson) {
|
|
|
1301
1378
|
apiPatch,
|
|
1302
1379
|
apiPost,
|
|
1303
1380
|
apiPut,
|
|
1381
|
+
collectionDelete,
|
|
1382
|
+
collectionGet,
|
|
1383
|
+
collectionPatch,
|
|
1384
|
+
collectionPost,
|
|
1304
1385
|
errorHandler,
|
|
1305
1386
|
getApiBuildStore,
|
|
1306
1387
|
getApiRuntimeStore,
|
|
@@ -1316,6 +1397,7 @@ function mockHandler(getJson) {
|
|
|
1316
1397
|
isRelation,
|
|
1317
1398
|
isStaticRequest,
|
|
1318
1399
|
mockHandler,
|
|
1400
|
+
routePost,
|
|
1319
1401
|
staticCollections,
|
|
1320
1402
|
storeApiDelete,
|
|
1321
1403
|
storeApiFetch,
|