@websolutespa/bom-mixer-store 0.3.2 → 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 +49 -43
- package/dist/index.d.ts +8 -3
- package/dist/index.js +234 -160
- package/dist/index.mjs +229 -160
- 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.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
|
@@ -1,43 +1,49 @@
|
|
|
1
|
-
# @websolutespa/bom-mixer-store
|
|
2
|
-
|
|
3
|
-
## 0.
|
|
4
|
-
|
|
5
|
-
###
|
|
6
|
-
|
|
7
|
-
- Modified
|
|
8
|
-
|
|
9
|
-
## 0.3.
|
|
10
|
-
|
|
11
|
-
### Patch Changes
|
|
12
|
-
|
|
13
|
-
-
|
|
14
|
-
|
|
15
|
-
## 0.3.
|
|
16
|
-
|
|
17
|
-
###
|
|
18
|
-
|
|
19
|
-
- Added:
|
|
20
|
-
|
|
21
|
-
## 0.
|
|
22
|
-
|
|
23
|
-
### Minor Changes
|
|
24
|
-
|
|
25
|
-
- Added:
|
|
26
|
-
|
|
27
|
-
## 0.
|
|
28
|
-
|
|
29
|
-
###
|
|
30
|
-
|
|
31
|
-
-
|
|
32
|
-
|
|
33
|
-
## 0.1.
|
|
34
|
-
|
|
35
|
-
###
|
|
36
|
-
|
|
37
|
-
-
|
|
38
|
-
|
|
39
|
-
## 0.0
|
|
40
|
-
|
|
41
|
-
###
|
|
42
|
-
|
|
43
|
-
-
|
|
1
|
+
# @websolutespa/bom-mixer-store
|
|
2
|
+
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Modified routes, added redirects.
|
|
8
|
+
|
|
9
|
+
## 0.3.2
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Modified: MockMenuService.
|
|
14
|
+
|
|
15
|
+
## 0.3.1
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Added: source files.
|
|
20
|
+
|
|
21
|
+
## 0.3.0
|
|
22
|
+
|
|
23
|
+
### Minor Changes
|
|
24
|
+
|
|
25
|
+
- Added: splat routes.
|
|
26
|
+
|
|
27
|
+
## 0.2.0
|
|
28
|
+
|
|
29
|
+
### Minor Changes
|
|
30
|
+
|
|
31
|
+
- Added: @websolutespa/bom-core.
|
|
32
|
+
|
|
33
|
+
## 0.1.1
|
|
34
|
+
|
|
35
|
+
### Patch Changes
|
|
36
|
+
|
|
37
|
+
- Modified: store service.
|
|
38
|
+
|
|
39
|
+
## 0.1.0
|
|
40
|
+
|
|
41
|
+
### Minor Changes
|
|
42
|
+
|
|
43
|
+
- mock refactor.
|
|
44
|
+
|
|
45
|
+
## 0.0.2
|
|
46
|
+
|
|
47
|
+
### Patch Changes
|
|
48
|
+
|
|
49
|
+
- Added: first release.
|
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
|
|
|
@@ -844,28 +849,32 @@ async function getMockStore(getJson) {
|
|
|
844
849
|
}
|
|
845
850
|
function getMockStoreFromData(data) {
|
|
846
851
|
const store = {};
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
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
|
+
}
|
|
851
858
|
}
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
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;
|
|
866
874
|
}
|
|
867
|
-
store[key] = service;
|
|
868
875
|
}
|
|
876
|
+
} else {
|
|
877
|
+
console.log("mock store not found");
|
|
869
878
|
}
|
|
870
879
|
staticCollections.forEach((config) => {
|
|
871
880
|
store[config.slug] = new StaticService(config, store);
|
|
@@ -1115,6 +1124,7 @@ var KNOWN_COLLECTIONS = [
|
|
|
1115
1124
|
"menu",
|
|
1116
1125
|
"template",
|
|
1117
1126
|
"page",
|
|
1127
|
+
"redirect",
|
|
1118
1128
|
"route",
|
|
1119
1129
|
...STATIC_COLLECTIONS
|
|
1120
1130
|
];
|
|
@@ -1135,157 +1145,216 @@ async function getStore() {
|
|
|
1135
1145
|
// src/store/store.handler.ts
|
|
1136
1146
|
function storeHandler(storePromise = async () => await getStore()) {
|
|
1137
1147
|
return apiHandler({
|
|
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
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
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);
|
|
1171
1180
|
} else {
|
|
1172
|
-
|
|
1173
|
-
return response.status(200).json(items);
|
|
1181
|
+
return response.status(404).send("not found");
|
|
1174
1182
|
}
|
|
1175
|
-
}
|
|
1176
|
-
|
|
1177
|
-
|
|
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);
|
|
1178
1189
|
}
|
|
1179
|
-
}
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
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
|
+
}
|
|
1194
1275
|
}
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
}
|
|
1204
|
-
});
|
|
1205
|
-
if (routes.length === 0) {
|
|
1206
|
-
const splatRoutes = await store.route.findMany({
|
|
1207
|
-
where: {
|
|
1208
|
-
useSplat: {
|
|
1209
|
-
equals: true
|
|
1210
|
-
}
|
|
1211
|
-
}
|
|
1212
|
-
});
|
|
1213
|
-
const route = splatRoutes.reduce((p, c) => {
|
|
1214
|
-
const match = pathname.indexOf(c.id) === 0;
|
|
1215
|
-
if (match && (!p || p.id.length < c.id.length)) {
|
|
1216
|
-
return { ...c, splat: pathname.substring(c.id.length, pathname.length) };
|
|
1217
|
-
} else {
|
|
1218
|
-
return p;
|
|
1219
|
-
}
|
|
1220
|
-
}, void 0);
|
|
1221
|
-
if (route) {
|
|
1222
|
-
return response.status(200).json(route);
|
|
1223
|
-
}
|
|
1224
|
-
}
|
|
1225
|
-
if (routes.length > 0) {
|
|
1226
|
-
return response.status(200).json(routes[0]);
|
|
1227
|
-
} else {
|
|
1228
|
-
return response.status(404).send("not found");
|
|
1229
|
-
}
|
|
1230
|
-
} catch (error) {
|
|
1231
|
-
if (error.status === 404) {
|
|
1232
|
-
return response.status(404).send("not found");
|
|
1233
|
-
} else {
|
|
1234
|
-
console.error("storeHandler.error.500", error);
|
|
1235
|
-
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
|
|
1236
1284
|
}
|
|
1237
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);
|
|
1238
1297
|
}
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
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);
|
|
1252
1319
|
}
|
|
1253
|
-
const
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
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);
|
|
1260
1344
|
}
|
|
1261
|
-
|
|
1262
|
-
console.error("storeHandler.error.500", error);
|
|
1263
|
-
throw error;
|
|
1345
|
+
return response.status(404).send("not found");
|
|
1264
1346
|
}
|
|
1265
|
-
}
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
const params = request.query;
|
|
1270
|
-
const { collections, ...query } = params;
|
|
1271
|
-
if (!Array.isArray(collections) || collections.length !== 2) {
|
|
1272
|
-
console.error("method not allowed", collections);
|
|
1273
|
-
return response.status(405).send("method not allowed");
|
|
1274
|
-
}
|
|
1275
|
-
const collection = collections[0];
|
|
1276
|
-
const id = collections[1];
|
|
1277
|
-
const store = await storePromise();
|
|
1278
|
-
const service = store[collection];
|
|
1279
|
-
if (!service) {
|
|
1280
|
-
console.error("method not allowed", collection);
|
|
1281
|
-
return response.status(405).send("method not allowed");
|
|
1282
|
-
}
|
|
1283
|
-
} catch (error) {
|
|
1347
|
+
} catch (error) {
|
|
1348
|
+
if (error.status === 404) {
|
|
1349
|
+
return response.status(404).send("not found");
|
|
1350
|
+
} else {
|
|
1284
1351
|
console.error("storeHandler.error.500", error);
|
|
1285
1352
|
throw error;
|
|
1286
1353
|
}
|
|
1287
1354
|
}
|
|
1288
|
-
}
|
|
1355
|
+
} else {
|
|
1356
|
+
return response.status(400).send("bad request");
|
|
1357
|
+
}
|
|
1289
1358
|
}
|
|
1290
1359
|
|
|
1291
1360
|
// src/mock/mock.handler.ts
|
|
@@ -1309,6 +1378,10 @@ function mockHandler(getJson) {
|
|
|
1309
1378
|
apiPatch,
|
|
1310
1379
|
apiPost,
|
|
1311
1380
|
apiPut,
|
|
1381
|
+
collectionDelete,
|
|
1382
|
+
collectionGet,
|
|
1383
|
+
collectionPatch,
|
|
1384
|
+
collectionPost,
|
|
1312
1385
|
errorHandler,
|
|
1313
1386
|
getApiBuildStore,
|
|
1314
1387
|
getApiRuntimeStore,
|
|
@@ -1324,6 +1397,7 @@ function mockHandler(getJson) {
|
|
|
1324
1397
|
isRelation,
|
|
1325
1398
|
isStaticRequest,
|
|
1326
1399
|
mockHandler,
|
|
1400
|
+
routePost,
|
|
1327
1401
|
staticCollections,
|
|
1328
1402
|
storeApiDelete,
|
|
1329
1403
|
storeApiFetch,
|