Skip to content

Commit 03ec6de

Browse files
wip
1 parent 12fd131 commit 03ec6de

File tree

4 files changed

+160
-71
lines changed

4 files changed

+160
-71
lines changed

server/schema.js

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.tileSchema = exports.tripSchema = exports.matchSchema = exports.tableSchema = exports.nearestSchema = exports.routeSchema = void 0;
3+
exports.parseQueryString = exports.tileSchema = exports.tripSchema = exports.matchSchema = exports.tableSchema = exports.nearestSchema = exports.routeSchema = void 0;
4+
const querystring = require('querystring');
45
const queryStringJsonSchemaGeneral = {
56
type: 'object',
67
properties: {
8+
// TODO: check numbers of elements is the same in bearings and radiuses
79
bearings: {
8-
type: 'string',
9-
// TODO: pattern
10+
type: 'array',
11+
items: {
12+
type: 'array',
13+
// TODO: check [min;max] + numbers of elements should be exactly 2
14+
items: {
15+
type: 'number'
16+
}
17+
}
1018
},
1119
radiuses: {
1220
type: 'string',
1321
// TODO: pattern
1422
},
1523
generate_hints: { type: 'boolean', default: true },
1624
hints: {
17-
type: 'string',
18-
// TODO: pattern
25+
type: 'array',
26+
items: {
27+
type: 'string'
28+
}
1929
},
2030
approaches: {
2131
type: 'string',
@@ -55,17 +65,18 @@ const queryStringJsonSchemaRoute = {
5565
default: 'default'
5666
},
5767
waypoints: {
58-
type: 'string',
59-
// list of numbers separated by semicolon
60-
pattern: '^(\\d+(;\\d+)*)?$'
68+
type: 'array',
69+
items: {
70+
type: 'integer'
71+
}
6172
}
6273
}
6374
};
6475
const queryStringJsonSchemaNearest = {
6576
type: 'object',
6677
properties: {
6778
...queryStringJsonSchemaGeneral.properties,
68-
alternatives: { type: ['integer'], default: 1 }
79+
number: { type: ['integer'], default: 1 }
6980
}
7081
};
7182
const queryStringJsonSchemaTable = {
@@ -116,19 +127,21 @@ const queryStringJsonSchemaMatch = {
116127
default: 'simplified'
117128
},
118129
timestamps: {
119-
type: 'string',
120-
// list of numbers separated by semicolon
121-
pattern: '^(\\d+(;\\d+)*)?$'
130+
type: 'array',
131+
items: {
132+
type: 'integer'
133+
}
122134
},
123135
gaps: {
124136
enum: ['split', 'ignore'],
125137
default: 'split'
126138
},
127139
tidy: { type: 'boolean', default: false },
128140
waypoints: {
129-
type: 'string',
130-
// list of numbers separated by semicolon
131-
pattern: '^(\\d+(;\\d+)*)?$'
141+
type: 'array',
142+
items: {
143+
type: 'integer'
144+
}
132145
},
133146
}
134147
};
@@ -186,3 +199,32 @@ const paramsJsonSchemaTile = {
186199
exports.tileSchema = {
187200
params: paramsJsonSchemaTile,
188201
};
202+
function parseArray(listString, separator) {
203+
// `querystring` parses `foo=1&foo=2` as `{ foo: ['1', '2'] }`
204+
if (Array.isArray(listString)) {
205+
return listString;
206+
}
207+
return listString.split(separator);
208+
}
209+
function parseQueryString(queryString) {
210+
const parsed = querystring.parse(queryString, '&', '=', {
211+
maxKeys: 0
212+
});
213+
if ('timestamps' in parsed) {
214+
parsed['timestamps'] = parseArray(parsed['timestamps'], ';');
215+
}
216+
if ('waypoints' in parsed) {
217+
parsed['waypoints'] = parseArray(parsed['waypoints'], ';');
218+
}
219+
if ('hints' in parsed) {
220+
parsed['hints'] = parseArray(parsed['hints'], ';');
221+
}
222+
if ('exclude' in parsed) {
223+
parsed['exclude'] = parseArray(parsed['exclude'], ',');
224+
}
225+
if ('bearings' in parsed) {
226+
parsed['bearings'] = parseArray(parsed['bearings'], ';').map(bearingWithRange => { parseArray(bearingWithRange, ','); });
227+
}
228+
return parsed;
229+
}
230+
exports.parseQueryString = parseQueryString;

server/schema.ts

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
1+
const querystring = require('querystring');
2+
13
const queryStringJsonSchemaGeneral = {
24
type: 'object',
35
properties: {
6+
// TODO: check numbers of elements is the same in bearings and radiuses
47
bearings: {
5-
type: 'string',
6-
// TODO: pattern
8+
type: 'array',
9+
items: {
10+
type: 'array',
11+
// TODO: check [min;max] + numbers of elements should be exactly 2
12+
items: {
13+
type: 'number'
14+
}
15+
}
716
},
817
radiuses: {
918
type: 'string',
1019
// TODO: pattern
1120
},
1221
generate_hints: { type: 'boolean', default: true },
1322
hints: {
14-
type: 'string',
15-
// TODO: pattern
23+
type: 'array',
24+
items: {
25+
type: 'string'
26+
}
1627
},
1728
approaches: {
1829
type: 'string',
@@ -53,9 +64,10 @@ const queryStringJsonSchemaRoute = {
5364
default: 'default'
5465
},
5566
waypoints: {
56-
type: 'string',
57-
// list of numbers separated by semicolon
58-
pattern: '^(\\d+(;\\d+)*)?$'
67+
type: 'array',
68+
items: {
69+
type: 'integer'
70+
}
5971
}
6072
}
6173
}
@@ -64,7 +76,7 @@ const queryStringJsonSchemaNearest = {
6476
type: 'object',
6577
properties: {
6678
...queryStringJsonSchemaGeneral.properties,
67-
alternatives: { type: ['integer'], default: 1 }
79+
number: { type: ['integer'], default: 1 }
6880
}
6981
}
7082

@@ -119,9 +131,10 @@ const queryStringJsonSchemaMatch = {
119131
default: 'simplified'
120132
},
121133
timestamps: {
122-
type: 'string',
123-
// list of numbers separated by semicolon
124-
pattern: '^(\\d+(;\\d+)*)?$'
134+
type: 'array',
135+
items: {
136+
type: 'integer'
137+
}
125138
},
126139

127140
gaps: {
@@ -130,9 +143,10 @@ const queryStringJsonSchemaMatch = {
130143
},
131144
tidy: { type: 'boolean', default: false },
132145
waypoints: {
133-
type: 'string',
134-
// list of numbers separated by semicolon
135-
pattern: '^(\\d+(;\\d+)*)?$'
146+
type: 'array',
147+
items: {
148+
type: 'integer'
149+
}
136150
},
137151
}
138152
}
@@ -198,4 +212,34 @@ const paramsJsonSchemaTile = {
198212

199213
export const tileSchema = {
200214
params: paramsJsonSchemaTile,
201-
};
215+
};
216+
217+
function parseArray(listString: string | string[], separator: string): string[] {
218+
// `querystring` parses `foo=1&foo=2` as `{ foo: ['1', '2'] }`
219+
if (Array.isArray(listString)) {
220+
return listString;
221+
}
222+
return listString.split(separator);
223+
}
224+
225+
export function parseQueryString(queryString: string): any {
226+
const parsed = querystring.parse(queryString, '&', '=', {
227+
maxKeys: 0
228+
});
229+
if ('timestamps' in parsed) {
230+
parsed['timestamps'] = parseArray(parsed['timestamps'], ';');
231+
}
232+
if ('waypoints' in parsed) {
233+
parsed['waypoints'] = parseArray(parsed['waypoints'], ';');
234+
}
235+
if ('hints' in parsed) {
236+
parsed['hints'] = parseArray(parsed['hints'], ';');
237+
}
238+
if ('exclude' in parsed) {
239+
parsed['exclude'] = parseArray(parsed['exclude'], ',');
240+
}
241+
if ('bearings' in parsed) {
242+
parsed['bearings'] = parseArray(parsed['bearings'], ';').map(bearingWithRange => { parseArray(bearingWithRange, ',') });
243+
}
244+
return parsed;
245+
}

server/server.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ async function handleNearest(osrm, coordinates, query, format) {
9797
coordinates: coordinates
9898
};
9999
handleCommonParams(query, options, format);
100+
if (query.number !== undefined) {
101+
options.number = query.number;
102+
}
100103
const res = await nearest(osrm, options);
101-
// TODO: number?
102104
return res;
103105
}
104106
async function handleTable(osrm, coordinates, query, format) {
@@ -129,15 +131,9 @@ function handleCommonParams(query, options, format) {
129131
if (query.overview) {
130132
options.overview = query.overview;
131133
}
132-
if (query.timestamps) {
133-
options.timestamps = query.timestamps.split(';').map((t) => parseInt(t));
134-
}
135134
if (query.geometries) {
136135
options.geometries = query.geometries;
137136
}
138-
if (query.waypoints) {
139-
options.waypoints = query.waypoints.split(';').map((t) => parseInt(t));
140-
}
141137
if (query.steps) {
142138
options.steps = query.steps;
143139
}
@@ -152,7 +148,7 @@ function handleCommonParams(query, options, format) {
152148
options.annotations = annotations;
153149
}
154150
if (query.exclude) {
155-
options.exclude = query.exclude.split(',');
151+
options.exclude = query.exclude;
156152
}
157153
if (query.snapping) {
158154
options.snapping = query.snapping;
@@ -166,15 +162,10 @@ function handleCommonParams(query, options, format) {
166162
});
167163
}
168164
if (query.bearings) {
169-
options.bearings = query.bearings.split(';').map((bearingWithRange) => {
170-
if (bearingWithRange === '') {
171-
return null;
172-
}
173-
return bearingWithRange.split(',').map((t) => parseFloat(t));
174-
});
165+
options.bearings = query.bearings;
175166
}
176167
if (query.hints) {
177-
options.hints = query.hints.split(';');
168+
options.hints = query.hints;
178169
}
179170
if (query.generate_hints) {
180171
options.generate_hints = query.generate_hints;
@@ -191,6 +182,12 @@ async function handleMatch(osrm, coordinates, query, format) {
191182
coordinates: coordinates
192183
};
193184
handleCommonParams(query, options, format);
185+
if (query.timestamps) {
186+
options.timestamps = query.timestamps;
187+
}
188+
if (query.waypoints) {
189+
options.waypoints = query.waypoints;
190+
}
194191
if (query.gaps) {
195192
options.gaps = query.gaps;
196193
}
@@ -223,12 +220,14 @@ async function handleRoute(osrm, coordinates, query, format) {
223220
};
224221
handleCommonParams(query, options, format);
225222
if (query.alternatives) {
226-
// TODO: number ?
227223
options.alternatives = query.alternatives;
228224
}
229225
if (query.approaches) {
230226
options.approaches = query.approaches.split(';');
231227
}
228+
if (query.waypoints) {
229+
options.waypoints = query.waypoints;
230+
}
232231
const res = await route(osrm, options);
233232
return res;
234233
}
@@ -277,7 +276,8 @@ async function main() {
277276
rewriteUrl: (req) => {
278277
// https://github.com/fastify/fastify/issues/2487
279278
return req.url.replaceAll(';', '%3B');
280-
}
279+
},
280+
querystringParser: schema_1.parseQueryString
281281
});
282282
function parseCoordinatesAndFormat(coordinatesAndFormat) {
283283
const lastDotIndex = coordinatesAndFormat.lastIndexOf('.');
@@ -291,7 +291,7 @@ async function main() {
291291
return { coordinates, format };
292292
}
293293
async function processRequest(handler, request, reply) {
294-
const { service, profile, coordinatesAndFormat } = request.params;
294+
const { coordinatesAndFormat } = request.params;
295295
const query = request.query;
296296
const { format, coordinates } = parseCoordinatesAndFormat(coordinatesAndFormat);
297297
switch (format) {

0 commit comments

Comments
 (0)