-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
192 lines (173 loc) · 5.06 KB
/
index.js
File metadata and controls
192 lines (173 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { resolve } from 'node:path'
import { StatusCodes } from 'http-status-codes'
import Joi from 'joi'
import { getAccessToken } from '~/src/server/plugins/map/routes/get-os-token.js'
import { find, nearest } from '~/src/server/plugins/map/service.js'
import {
get,
request as httpRequest
} from '~/src/server/services/httpService.js'
/**
* Gets the map support routes
* @param {MapConfiguration} options - ordnance survey names api key
*/
export function getRoutes(options) {
return [
mapStyleResourceRoutes(),
mapProxyRoute(options),
tileProxyRoute(options),
geocodeProxyRoute(options),
reverseGeocodeProxyRoute(options)
]
}
/**
* Proxies ordnance survey requests from the front end to api.os.com
* Used for the VTS map source by forwarding on the request
* and adding the auth token and SRS (spatial reference system)
* @param {MapConfiguration} options - the map options
* @returns {ServerRoute<MapProxyGetRequestRefs>}
*/
function mapProxyRoute(options) {
return {
method: 'GET',
path: '/api/map-proxy',
handler: async (request, h) => {
const { query } = request
const targetUrl = new URL(decodeURIComponent(query.url))
const token = await getAccessToken(options)
targetUrl.searchParams.set('srs', '3857')
const proxyResponse = await httpRequest('get', targetUrl.toString(), {
headers: {
Authorization: `Bearer ${token}`
}
})
const buffer = proxyResponse.payload
const contentType = proxyResponse.res.headers['content-type']
const response = h.response(buffer)
if (contentType) {
response.type(contentType)
}
return response
},
options: {
validate: {
query: Joi.object()
.keys({
url: Joi.string().required()
})
.optional()
}
}
}
}
/**
* Proxies ordnance survey requests from the front end to api.os.uk
* Used for VTS map tiles forwarding on the request and adding the auth token
* @param {MapConfiguration} options - the map options
* @returns {ServerRoute<MapProxyGetRequestRefs>}
*/
function tileProxyRoute(options) {
return {
method: 'GET',
path: '/api/tile/{z}/{y}/{x}.pbf',
handler: async (request, h) => {
const { z, y, x } = request.params
const token = await getAccessToken(options)
const url = `https://api.os.uk/maps/vector/v1/vts/tile/${z}/${y}/${x}.pbf?srs=3857`
const { payload, res } = await get(url, {
headers: {
Authorization: `Bearer ${token}`,
Accept: 'application/x-protobuf'
},
json: false,
gunzip: true
})
if (res.statusCode && res.statusCode !== StatusCodes.OK.valueOf()) {
return h.response('Tile fetch failed').code(res.statusCode)
}
return h
.response(payload)
.type('application/x-protobuf')
.header('Cache-Control', 'public, max-age=86400')
}
}
}
/**
* Proxies ordnance survey geocode requests from the front end to api.os.uk
* Used for the gazzeteer address lookup to find name from query strings like postcode and place names
* @param {MapConfiguration} options - the map options
* @returns {ServerRoute<MapGeocodeGetRequestRefs>}
*/
function geocodeProxyRoute(options) {
return {
method: 'GET',
path: '/api/geocode-proxy',
async handler(request, _h) {
const { query } = request
const data = await find(query.query, options.ordnanceSurveyApiKey)
return data
},
options: {
validate: {
query: Joi.object()
.keys({
query: Joi.string().required()
})
.required()
}
}
}
}
/**
* Proxies ordnance survey reverse geocode requests from the front end to api.os.uk
* Used to find name from easting and northing points.
* N.B this endpoint is currently not used by the front end but will be soon in "maps V2"
* @param {MapConfiguration} options - the map options
* @returns {ServerRoute<MapReverseGeocodeGetRequestRefs>}
*/
function reverseGeocodeProxyRoute(options) {
return {
method: 'GET',
path: '/api/reverse-geocode-proxy',
async handler(request, _h) {
const { query } = request
const data = await nearest(
query.easting,
query.northing,
options.ordnanceSurveyApiKey
)
return data
},
options: {
validate: {
query: Joi.object()
.keys({
easting: Joi.number().required(),
northing: Joi.number().required()
})
.required()
}
}
}
}
/**
* Resource routes to return sprites and glyphs
* @returns {ServerRoute<MapProxyGetRequestRefs>}
*/
function mapStyleResourceRoutes() {
return {
method: 'GET',
path: '/api/maps/vts/{path*}',
options: {
handler: {
directory: {
path: resolve(import.meta.dirname, './vts')
}
}
}
}
}
/**
* @import { ServerRoute } from '@hapi/hapi'
* @import { MapConfiguration, MapProxyGetRequestRefs, MapGeocodeGetRequestRefs, MapReverseGeocodeGetRequestRefs } from '~/src/server/plugins/map/types.js'
*/