@golemio/core 4.0.0-dev.2649848072 → 4.0.0-dev.2672402710
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/dist/helpers/routing/AbstractGeoJsonRouter.d.ts +2 -1
- package/dist/helpers/routing/AbstractGeoJsonRouter.js +10 -98
- package/dist/helpers/routing/AbstractGeoJsonRouter.js.map +1 -1
- package/dist/output-gateway/routes/GeoJsonRouteHandlers.d.ts +26 -0
- package/dist/output-gateway/routes/GeoJsonRouteHandlers.js +97 -0
- package/dist/output-gateway/routes/GeoJsonRouteHandlers.js.map +1 -0
- package/dist/output-gateway/routes/GeoJsonRouter.d.ts +1 -0
- package/dist/output-gateway/routes/GeoJsonRouter.js +8 -94
- package/dist/output-gateway/routes/GeoJsonRouter.js.map +1 -1
- package/dist/output-gateway/routes/interfaces/IGeoJsonAllFilterParameters.d.ts +0 -2
- package/package.json +1 -1
|
@@ -6,7 +6,7 @@ import { NextFunction, Request, Response } from "express";
|
|
|
6
6
|
import { ValidationChain } from "express-validator";
|
|
7
7
|
import QueryString from "qs";
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* Versioned router for data in GeoJSON format -
|
|
10
10
|
* binds / and /:id to GetAll and GetOne methods of GeoJsonModel.
|
|
11
11
|
*
|
|
12
12
|
* Router /WEB LAYER/: maps routes to specific model functions, passes request parameters and handles responses.
|
|
@@ -16,6 +16,7 @@ export declare abstract class AbstractGeoJsonRouter extends AbstractRouter {
|
|
|
16
16
|
protected model: IGeoJsonModel;
|
|
17
17
|
protected cacheHeaderMiddleware: CacheHeaderMiddleware;
|
|
18
18
|
protected standardParams: ValidationChain[];
|
|
19
|
+
private readonly handlers;
|
|
19
20
|
protected constructor(version: string, path: string, inModel: IGeoJsonModel);
|
|
20
21
|
/**
|
|
21
22
|
* Converts a single value of `any` type to an array containing this element
|
|
@@ -2,15 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AbstractGeoJsonRouter = void 0;
|
|
4
4
|
const AbstractRouter_1 = require("./AbstractRouter");
|
|
5
|
-
const
|
|
6
|
-
const ContainerToken_1 = require("../../output-gateway/ioc/ContainerToken");
|
|
7
|
-
const Di_1 = require("../../output-gateway/ioc/Di");
|
|
8
|
-
const Logger_1 = require("../../output-gateway/Logger");
|
|
5
|
+
const GeoJsonRouteHandlers_1 = require("../../output-gateway/routes/GeoJsonRouteHandlers");
|
|
9
6
|
const Validation_1 = require("../../output-gateway/Validation");
|
|
10
|
-
const express_validator_1 = require("express-validator");
|
|
11
|
-
const errors_1 = require("@golemio/errors");
|
|
12
7
|
/**
|
|
13
|
-
*
|
|
8
|
+
* Versioned router for data in GeoJSON format -
|
|
14
9
|
* binds / and /:id to GetAll and GetOne methods of GeoJsonModel.
|
|
15
10
|
*
|
|
16
11
|
* Router /WEB LAYER/: maps routes to specific model functions, passes request parameters and handles responses.
|
|
@@ -19,31 +14,10 @@ const errors_1 = require("@golemio/errors");
|
|
|
19
14
|
class AbstractGeoJsonRouter extends AbstractRouter_1.AbstractRouter {
|
|
20
15
|
constructor(version, path, inModel) {
|
|
21
16
|
super(version, path);
|
|
22
|
-
this.standardParams = [
|
|
23
|
-
(0, express_validator_1.query)("updatedSince").optional().isISO8601().not().isArray(),
|
|
24
|
-
(0, express_validator_1.query)("districts").optional().not().isEmpty({ ignore_whitespace: true }),
|
|
25
|
-
(0, express_validator_1.query)("ids")
|
|
26
|
-
.optional()
|
|
27
|
-
.custom((value) => {
|
|
28
|
-
if (Array.isArray(value)) {
|
|
29
|
-
if (!value.every((el) => Number.isInteger(+el))) {
|
|
30
|
-
throw new Error("Array contains a non-numeric id");
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
else if (!Number.isInteger(+value)) {
|
|
34
|
-
throw new Error("Non-numeric id");
|
|
35
|
-
}
|
|
36
|
-
return true;
|
|
37
|
-
}),
|
|
38
|
-
(0, express_validator_1.query)("latlng").optional().isString().isLatLong().not().isArray(),
|
|
39
|
-
(0, express_validator_1.query)("range").optional().isNumeric().not().isArray(),
|
|
40
|
-
];
|
|
41
17
|
/**
|
|
42
18
|
* Converts a single value of `any` type to an array containing this element
|
|
43
19
|
*/
|
|
44
|
-
this.ConvertToArray = (toBeArray) =>
|
|
45
|
-
return toBeArray instanceof Array ? toBeArray : [toBeArray];
|
|
46
|
-
};
|
|
20
|
+
this.ConvertToArray = (toBeArray) => this.handlers.ConvertToArray(toBeArray);
|
|
47
21
|
/**
|
|
48
22
|
* Initiates all routes. Should respond with correct data to a HTTP requests to all routes.
|
|
49
23
|
* @param {number|string} expire TTL for the caching middleware
|
|
@@ -56,76 +30,14 @@ class AbstractGeoJsonRouter extends AbstractRouter_1.AbstractRouter {
|
|
|
56
30
|
idParam,
|
|
57
31
|
], Validation_1.checkErrors, this.cacheHeaderMiddleware.getMiddleware(expire?.maxAge, expire?.staleWhileRevalidate), this.GetOne);
|
|
58
32
|
};
|
|
59
|
-
this.GetAll =
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
let districts = queryData.districts;
|
|
64
|
-
if (districts) {
|
|
65
|
-
districts = this.ConvertToArray(districts);
|
|
66
|
-
}
|
|
67
|
-
if (ids) {
|
|
68
|
-
ids = this.ConvertToArray(ids);
|
|
69
|
-
}
|
|
70
|
-
try {
|
|
71
|
-
const coords = await (0, Geo_1.parseCoordinates)(queryData.latlng, queryData.range);
|
|
72
|
-
const data = await this.model.GetAll({
|
|
73
|
-
districts,
|
|
74
|
-
ids: ids,
|
|
75
|
-
lat: coords.lat,
|
|
76
|
-
limit: queryData.limit,
|
|
77
|
-
lng: coords.lng,
|
|
78
|
-
offset: Number(queryData.offset),
|
|
79
|
-
range: coords.range,
|
|
80
|
-
updatedSince: queryData.updatedSince,
|
|
81
|
-
additionalFilters: this.GetAdditionalFilters(queryData),
|
|
82
|
-
});
|
|
83
|
-
res.status(200).send(data);
|
|
84
|
-
}
|
|
85
|
-
catch (err) {
|
|
86
|
-
next(err);
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
this.GetOne = async (req, res, next) => {
|
|
90
|
-
const id = req.params.id;
|
|
91
|
-
try {
|
|
92
|
-
const result = await this.model.GetOne(id);
|
|
93
|
-
if (result === undefined) {
|
|
94
|
-
throw new errors_1.GeneralError("not_found", this.model.GetName(), undefined, 404);
|
|
95
|
-
}
|
|
96
|
-
res.status(200).send(result);
|
|
97
|
-
}
|
|
98
|
-
catch (err) {
|
|
99
|
-
next(err);
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
this.GetIdQueryParamWithCorrectType = async () => {
|
|
103
|
-
let idParam;
|
|
104
|
-
// Get the primary ID of the schema (the attribute name)
|
|
105
|
-
const idKey = Object.keys(this.model.PrimaryIdentifierSelection("0"))[0];
|
|
106
|
-
let message = "Created model " + this.model.GetName() + " has ID `" + idKey + "` of type ";
|
|
107
|
-
const isPrimaryIdNumber = await this.model.IsPrimaryIdNumber(idKey);
|
|
108
|
-
if (isPrimaryIdNumber) {
|
|
109
|
-
message += "number.";
|
|
110
|
-
// Create a url parameter for detail route with type number
|
|
111
|
-
idParam = (0, express_validator_1.param)("id").exists().isNumeric();
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
message += "string.";
|
|
115
|
-
// Create a url parameter for detail route with type string
|
|
116
|
-
idParam = (0, express_validator_1.param)("id").exists().isString();
|
|
117
|
-
}
|
|
118
|
-
Logger_1.log.silly(message);
|
|
119
|
-
return idParam;
|
|
120
|
-
};
|
|
121
|
-
this.GetAdditionalFilters = (query) => {
|
|
122
|
-
const { districts, ids, latlng, limit, offset, range, updatedSince, ...rest } = query;
|
|
123
|
-
return {
|
|
124
|
-
...rest,
|
|
125
|
-
};
|
|
126
|
-
};
|
|
33
|
+
this.GetAll = (req, res, next) => this.handlers.GetAll(req, res, next);
|
|
34
|
+
this.GetOne = (req, res, next) => this.handlers.GetOne(req, res, next);
|
|
35
|
+
this.GetIdQueryParamWithCorrectType = () => this.handlers.GetIdQueryParamWithCorrectType();
|
|
36
|
+
this.GetAdditionalFilters = (query) => this.handlers.GetAdditionalFilters(query);
|
|
127
37
|
this.model = inModel;
|
|
128
|
-
this.
|
|
38
|
+
this.handlers = new GeoJsonRouteHandlers_1.GeoJsonRouteHandlers(inModel);
|
|
39
|
+
this.cacheHeaderMiddleware = this.handlers.cacheHeaderMiddleware;
|
|
40
|
+
this.standardParams = this.handlers.standardParams;
|
|
129
41
|
}
|
|
130
42
|
}
|
|
131
43
|
exports.AbstractGeoJsonRouter = AbstractGeoJsonRouter;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbstractGeoJsonRouter.js","sourceRoot":"","sources":["../../../src/helpers/routing/AbstractGeoJsonRouter.ts"],"names":[],"mappings":";;;AAAA,qDAAiE;AAGjE,
|
|
1
|
+
{"version":3,"file":"AbstractGeoJsonRouter.js","sourceRoot":"","sources":["../../../src/helpers/routing/AbstractGeoJsonRouter.ts"],"names":[],"mappings":";;;AAAA,qDAAiE;AAGjE,2FAAuE;AAEvE,gEAAwE;AAKxE;;;;;;GAMG;AACH,MAAsB,qBAAsB,SAAQ,+BAAc;IAO9D,YAAsB,OAAe,EAAE,IAAY,EAAE,OAAsB;QACvE,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAOzB;;WAEG;QACI,mBAAc,GAAG,CAAC,SAAc,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC;QAEpF;;;WAGG;QACO,eAAU,GAAG,KAAK,EAAE,SAAkC,EAAE,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,oBAAoB,EAAE,CAAC,EAAE,EAAE,EAAE;YAC3G,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,8BAA8B,EAAE,CAAC;YAE5D,IAAI,CAAC,MAAM,CAAC,GAAG,CACX,GAAG,EACH,IAAI,CAAC,cAAc,EACnB,IAAA,sCAAyB,EAA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+
import { CacheHeaderMiddleware } from "../CacheHeaderMiddleware";
|
|
2
|
+
import { IGeoJsonModel } from "../models/interfaces/IGeoJsonModel";
|
|
3
|
+
import { NextFunction, Request, Response } from "express";
|
|
4
|
+
import { ValidationChain } from "express-validator";
|
|
5
|
+
import QueryString from "qs";
|
|
6
|
+
/**
|
|
7
|
+
* Holds the request handlers and validators for a GeoJSON model's `/` and `/:id` endpoints.
|
|
8
|
+
* Has no knowledge of any specific express.Router - callers register the exposed
|
|
9
|
+
* handlers/validators on whichever router (versioned or not) they own.
|
|
10
|
+
*/
|
|
11
|
+
export declare class GeoJsonRouteHandlers {
|
|
12
|
+
readonly cacheHeaderMiddleware: CacheHeaderMiddleware;
|
|
13
|
+
readonly standardParams: ValidationChain[];
|
|
14
|
+
private readonly model;
|
|
15
|
+
constructor(model: IGeoJsonModel);
|
|
16
|
+
/**
|
|
17
|
+
* Converts a single value of `any` type to an array containing this element
|
|
18
|
+
*/
|
|
19
|
+
ConvertToArray: (toBeArray: any) => any[];
|
|
20
|
+
GetAll: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
21
|
+
GetOne: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
22
|
+
GetIdQueryParamWithCorrectType: () => Promise<ValidationChain>;
|
|
23
|
+
GetAdditionalFilters: (query: QueryString.ParsedQs) => {
|
|
24
|
+
[key: string]: string | QueryString.ParsedQs | (string | QueryString.ParsedQs)[] | undefined;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GeoJsonRouteHandlers = void 0;
|
|
4
|
+
const Geo_1 = require("../Geo");
|
|
5
|
+
const Logger_1 = require("../Logger");
|
|
6
|
+
const ioc_1 = require("../ioc");
|
|
7
|
+
const errors_1 = require("@golemio/errors");
|
|
8
|
+
const express_validator_1 = require("express-validator");
|
|
9
|
+
/**
|
|
10
|
+
* Holds the request handlers and validators for a GeoJSON model's `/` and `/:id` endpoints.
|
|
11
|
+
* Has no knowledge of any specific express.Router - callers register the exposed
|
|
12
|
+
* handlers/validators on whichever router (versioned or not) they own.
|
|
13
|
+
*/
|
|
14
|
+
class GeoJsonRouteHandlers {
|
|
15
|
+
constructor(model) {
|
|
16
|
+
this.standardParams = [
|
|
17
|
+
(0, express_validator_1.query)("updatedSince").optional().isISO8601().not().isArray(),
|
|
18
|
+
(0, express_validator_1.query)("districts").optional().not().isEmpty({ ignore_whitespace: true }),
|
|
19
|
+
(0, express_validator_1.query)("latlng").optional().isString().isLatLong().not().isArray(),
|
|
20
|
+
(0, express_validator_1.query)("range").optional().isNumeric().not().isArray(),
|
|
21
|
+
(0, express_validator_1.query)("type").optional().isString().not().isArray(),
|
|
22
|
+
];
|
|
23
|
+
/**
|
|
24
|
+
* Converts a single value of `any` type to an array containing this element
|
|
25
|
+
*/
|
|
26
|
+
this.ConvertToArray = (toBeArray) => {
|
|
27
|
+
return toBeArray instanceof Array ? toBeArray : [toBeArray];
|
|
28
|
+
};
|
|
29
|
+
this.GetAll = async (req, res, next) => {
|
|
30
|
+
const queryData = (0, express_validator_1.matchedData)(req, { includeOptionals: true });
|
|
31
|
+
// Parsing parameters
|
|
32
|
+
let districts = queryData.districts;
|
|
33
|
+
if (districts) {
|
|
34
|
+
districts = this.ConvertToArray(districts);
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const coords = await (0, Geo_1.parseCoordinates)(queryData.latlng, queryData.range);
|
|
38
|
+
const data = await this.model.GetAll({
|
|
39
|
+
districts,
|
|
40
|
+
lat: coords.lat,
|
|
41
|
+
limit: queryData.limit,
|
|
42
|
+
lng: coords.lng,
|
|
43
|
+
offset: Number(queryData.offset),
|
|
44
|
+
range: coords.range,
|
|
45
|
+
updatedSince: queryData.updatedSince,
|
|
46
|
+
additionalFilters: this.GetAdditionalFilters(queryData),
|
|
47
|
+
});
|
|
48
|
+
res.status(200).send(data);
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
next(err);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
this.GetOne = async (req, res, next) => {
|
|
55
|
+
const id = req.params.id;
|
|
56
|
+
try {
|
|
57
|
+
const result = await this.model.GetOne(id);
|
|
58
|
+
if (result === undefined) {
|
|
59
|
+
throw new errors_1.GeneralError("not_found", this.model.GetName(), undefined, 404);
|
|
60
|
+
}
|
|
61
|
+
res.status(200).send(result);
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
next(err);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
this.GetIdQueryParamWithCorrectType = async () => {
|
|
68
|
+
let idParam;
|
|
69
|
+
// Get the primary ID of the schema (the attribute name)
|
|
70
|
+
const idKey = Object.keys(this.model.PrimaryIdentifierSelection("0"))[0];
|
|
71
|
+
let message = "Created model " + this.model.GetName() + " has ID `" + idKey + "` of type ";
|
|
72
|
+
const isPrimaryIdNumber = await this.model.IsPrimaryIdNumber(idKey);
|
|
73
|
+
if (isPrimaryIdNumber) {
|
|
74
|
+
message += "number.";
|
|
75
|
+
// Create a url parameter for detail route with type number
|
|
76
|
+
idParam = (0, express_validator_1.param)("id").exists().isNumeric();
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
message += "string.";
|
|
80
|
+
// Create a url parameter for detail route with type string
|
|
81
|
+
idParam = (0, express_validator_1.param)("id").exists().isString();
|
|
82
|
+
}
|
|
83
|
+
Logger_1.log.silly(message);
|
|
84
|
+
return idParam;
|
|
85
|
+
};
|
|
86
|
+
this.GetAdditionalFilters = (query) => {
|
|
87
|
+
const { districts, latlng, limit, offset, range, updatedSince, ...rest } = query;
|
|
88
|
+
return {
|
|
89
|
+
...rest,
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
this.model = model;
|
|
93
|
+
this.cacheHeaderMiddleware = ioc_1.OutputGatewayContainer.resolve(ioc_1.ContainerToken.CacheHeaderMiddleware);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.GeoJsonRouteHandlers = GeoJsonRouteHandlers;
|
|
97
|
+
//# sourceMappingURL=GeoJsonRouteHandlers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GeoJsonRouteHandlers.js","sourceRoot":"","sources":["../../../src/output-gateway/routes/GeoJsonRouteHandlers.ts"],"names":[],"mappings":";;;AACA,gCAA2C;AAC3C,sCAAiC;AACjC,gCAAiE;AAEjE,4CAA+C;AAE/C,yDAA+E;AAI/E;;;;GAIG;AACH,MAAa,oBAAoB;IAa7B,YAAmB,KAAoB;QAVvB,mBAAc,GAAG;YAC7B,IAAA,yBAAK,EAAC,cAAc,CAAC,CAAC,QAAQ,EAAE,CAAC,SAAS,EAAE,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE;YAC5D,IAAA,yBAAK,EAAC,WAAW,CAAC,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,EAAE,iBAAiB,EAAE,IAAI,EAAE,CAAC;YACxE,IAAA,yBAAK,EAAC,QAAQ,CAAC,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,CAAC,SAAS,EAAE,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE;YACjE,IAAA,yBAAK,EAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,CAAC,SAAS,EAAE,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE;YACrD,IAAA,yBAAK,EAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE;SACtD,CAAC;QASF;;WAEG;QACI,mBAAc,GAAG,CAAC,SAAc,EAAE,EAAE;YACvC,OAAO,SAAS,YAAY,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;QAChE,CAAC,CAAC;QAEK,WAAM,GAAG,KAAK,EAAE,GAA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nB,CAAC,CAAC;QAEK,yBAAoB,GAAG,CAAC,KAA2B,EAAE,EAAE;YAC1D,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,GAAG,IAAI,EAAE,GAAG,KAAK,CAAC;YACjF,OAAO;gBACH,GAAG,IAAI;aACV,CAAC;QACN,CAAC,CAAC;QAjFE,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,qBAAqB,GAAG,4BAAsB,CAAC,OAAO,CAAwB,oBAAc,CAAC,qBAAqB,CAAC,CAAC;IAC7H,CAAC;CAgFJ;AAhGD,oDAgGC"}
|
|
@@ -17,6 +17,7 @@ export declare class GeoJsonRouter extends BaseRouter {
|
|
|
17
17
|
protected model: IGeoJsonModel;
|
|
18
18
|
protected cacheHeaderMiddleware: CacheHeaderMiddleware;
|
|
19
19
|
protected standardParams: ValidationChain[];
|
|
20
|
+
private readonly handlers;
|
|
20
21
|
constructor(inModel: IGeoJsonModel);
|
|
21
22
|
/**
|
|
22
23
|
* Initiates all routes. Should respond with correct data to a HTTP requests to all routes.
|
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.GeoJsonRouter = void 0;
|
|
4
|
-
const Geo_1 = require("../Geo");
|
|
5
|
-
const Logger_1 = require("../Logger");
|
|
6
4
|
const Validation_1 = require("../Validation");
|
|
7
|
-
const ioc_1 = require("../ioc");
|
|
8
5
|
const BaseRouter_1 = require("./BaseRouter");
|
|
6
|
+
const GeoJsonRouteHandlers_1 = require("./GeoJsonRouteHandlers");
|
|
9
7
|
const express_1 = require("express");
|
|
10
|
-
const express_validator_1 = require("express-validator");
|
|
11
|
-
const errors_1 = require("@golemio/errors");
|
|
12
8
|
/**
|
|
13
9
|
* Router for data in GeoJSON format using GeoJSON model -
|
|
14
10
|
* binds / and /:id to GetAll and GetOne methods of GeoJsonModel.
|
|
@@ -21,26 +17,6 @@ class GeoJsonRouter extends BaseRouter_1.BaseRouter {
|
|
|
21
17
|
super();
|
|
22
18
|
// Assign router to the express.Router() instance
|
|
23
19
|
this.router = (0, express_1.Router)();
|
|
24
|
-
this.standardParams = [
|
|
25
|
-
(0, express_validator_1.query)("updatedSince").optional().isISO8601().not().isArray(),
|
|
26
|
-
(0, express_validator_1.query)("districts").optional().not().isEmpty({ ignore_whitespace: true }),
|
|
27
|
-
(0, express_validator_1.query)("ids")
|
|
28
|
-
.optional()
|
|
29
|
-
.custom((value) => {
|
|
30
|
-
if (Array.isArray(value)) {
|
|
31
|
-
if (!value.every((el) => Number.isInteger(+el))) {
|
|
32
|
-
throw new Error("Array contains a non-numeric id");
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
else if (!Number.isInteger(+value)) {
|
|
36
|
-
throw new Error("Non-numeric id");
|
|
37
|
-
}
|
|
38
|
-
return true;
|
|
39
|
-
}),
|
|
40
|
-
(0, express_validator_1.query)("latlng").optional().isString().isLatLong().not().isArray(),
|
|
41
|
-
(0, express_validator_1.query)("range").optional().isNumeric().not().isArray(),
|
|
42
|
-
(0, express_validator_1.query)("type").optional().isString().not().isArray(),
|
|
43
|
-
];
|
|
44
20
|
/**
|
|
45
21
|
* Initiates all routes. Should respond with correct data to a HTTP requests to all routes.
|
|
46
22
|
* @param {IRouterExpireParameters|number|string} expire TTL for the caching middleware
|
|
@@ -53,76 +29,14 @@ class GeoJsonRouter extends BaseRouter_1.BaseRouter {
|
|
|
53
29
|
idParam,
|
|
54
30
|
], Validation_1.checkErrors, this.cacheHeaderMiddleware.getMiddleware(expire.maxAge, expire.staleWhileRevalidate), this.GetOne);
|
|
55
31
|
};
|
|
56
|
-
this.GetAll =
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
let districts = queryData.districts;
|
|
61
|
-
if (districts) {
|
|
62
|
-
districts = this.ConvertToArray(districts);
|
|
63
|
-
}
|
|
64
|
-
if (ids) {
|
|
65
|
-
ids = this.ConvertToArray(ids);
|
|
66
|
-
}
|
|
67
|
-
try {
|
|
68
|
-
const coords = await (0, Geo_1.parseCoordinates)(queryData.latlng, queryData.range);
|
|
69
|
-
const data = await this.model.GetAll({
|
|
70
|
-
districts,
|
|
71
|
-
ids: ids,
|
|
72
|
-
lat: coords.lat,
|
|
73
|
-
limit: queryData.limit,
|
|
74
|
-
lng: coords.lng,
|
|
75
|
-
offset: Number(queryData.offset),
|
|
76
|
-
range: coords.range,
|
|
77
|
-
updatedSince: queryData.updatedSince,
|
|
78
|
-
additionalFilters: this.GetAdditionalFilters(queryData),
|
|
79
|
-
});
|
|
80
|
-
res.status(200).send(data);
|
|
81
|
-
}
|
|
82
|
-
catch (err) {
|
|
83
|
-
next(err);
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
this.GetOne = async (req, res, next) => {
|
|
87
|
-
const id = req.params.id;
|
|
88
|
-
try {
|
|
89
|
-
const result = await this.model.GetOne(id);
|
|
90
|
-
if (result === undefined) {
|
|
91
|
-
throw new errors_1.GeneralError("not_found", this.model.GetName(), undefined, 404);
|
|
92
|
-
}
|
|
93
|
-
res.status(200).send(result);
|
|
94
|
-
}
|
|
95
|
-
catch (err) {
|
|
96
|
-
next(err);
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
this.GetIdQueryParamWithCorrectType = async () => {
|
|
100
|
-
let idParam;
|
|
101
|
-
// Get the primary ID of the schema (the attribute name)
|
|
102
|
-
const idKey = Object.keys(this.model.PrimaryIdentifierSelection("0"))[0];
|
|
103
|
-
let message = "Created model " + this.model.GetName() + " has ID `" + idKey + "` of type ";
|
|
104
|
-
const isPrimaryIdNumber = await this.model.IsPrimaryIdNumber(idKey);
|
|
105
|
-
if (isPrimaryIdNumber) {
|
|
106
|
-
message += "number.";
|
|
107
|
-
// Create a url parameter for detail route with type number
|
|
108
|
-
idParam = (0, express_validator_1.param)("id").exists().isNumeric();
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
message += "string.";
|
|
112
|
-
// Create a url parameter for detail route with type string
|
|
113
|
-
idParam = (0, express_validator_1.param)("id").exists().isString();
|
|
114
|
-
}
|
|
115
|
-
Logger_1.log.silly(message);
|
|
116
|
-
return idParam;
|
|
117
|
-
};
|
|
118
|
-
this.GetAdditionalFilters = (query) => {
|
|
119
|
-
const { districts, ids, latlng, limit, offset, range, updatedSince, ...rest } = query;
|
|
120
|
-
return {
|
|
121
|
-
...rest,
|
|
122
|
-
};
|
|
123
|
-
};
|
|
32
|
+
this.GetAll = (req, res, next) => this.handlers.GetAll(req, res, next);
|
|
33
|
+
this.GetOne = (req, res, next) => this.handlers.GetOne(req, res, next);
|
|
34
|
+
this.GetIdQueryParamWithCorrectType = () => this.handlers.GetIdQueryParamWithCorrectType();
|
|
35
|
+
this.GetAdditionalFilters = (query) => this.handlers.GetAdditionalFilters(query);
|
|
124
36
|
this.model = inModel;
|
|
125
|
-
this.
|
|
37
|
+
this.handlers = new GeoJsonRouteHandlers_1.GeoJsonRouteHandlers(inModel);
|
|
38
|
+
this.cacheHeaderMiddleware = this.handlers.cacheHeaderMiddleware;
|
|
39
|
+
this.standardParams = this.handlers.standardParams;
|
|
126
40
|
}
|
|
127
41
|
}
|
|
128
42
|
exports.GeoJsonRouter = GeoJsonRouter;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GeoJsonRouter.js","sourceRoot":"","sources":["../../../src/output-gateway/routes/GeoJsonRouter.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"GeoJsonRouter.js","sourceRoot":"","sources":["../../../src/output-gateway/routes/GeoJsonRouter.ts"],"names":[],"mappings":";;;AACA,8CAAwE;AAExE,6CAAmD;AACnD,iEAAuE;AACvE,qCAAkE;AAKlE;;;;;;GAMG;AACH,MAAa,aAAc,SAAQ,uBAAU;IAUzC,YAAmB,OAAsB;QACrC,KAAK,EAAE,CAAC;QAVZ,iDAAiD;QA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AAC;QAvCrG,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC;QACrB,IAAI,CAAC,QAAQ,GAAG,IAAI,2CAAoB,CAAC,OAAO,CAAC,CAAC;QAClD,IAAI,CAAC,qBAAqB,GAAG,IAAI,CAAC,QAAQ,CAAC,qBAAqB,CAAC;QACjE,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC;IACvD,CAAC;CAoCJ;AApDD,sCAoDC"}
|
|
@@ -15,8 +15,6 @@ export interface IGeoJsonAllFilterParameters {
|
|
|
15
15
|
updatedSince?: string;
|
|
16
16
|
/** Filters the data to include only these with one of the specified "district" value */
|
|
17
17
|
districts?: string[];
|
|
18
|
-
/** Filters the data to include only specified IDs */
|
|
19
|
-
ids?: number[];
|
|
20
18
|
/** Object with additional filter conditions to be added to the selection */
|
|
21
19
|
additionalFilters?: object;
|
|
22
20
|
}
|