11import { resolve } from 'node:path'
22
3+ import { StatusCodes } from 'http-status-codes'
34import Joi from 'joi'
45
6+ import { getAccessToken } from '~/src/server/plugins/map/routes/get-os-token.js'
57import { find , nearest } from '~/src/server/plugins/map/service.js'
6- import { request as httpRequest } from '~/src/server/services/httpService.js'
8+ import {
9+ get ,
10+ request as httpRequest
11+ } from '~/src/server/services/httpService.js'
712
813/**
914 * Gets the map support routes
1015 * @param {MapConfiguration } options - ordnance survey names api key
1116 */
1217export function getRoutes ( options ) {
1318 return [
19+ mapStyleResourceRoutes ( ) ,
1420 mapProxyRoute ( options ) ,
21+ tileProxyRoute ( options ) ,
1522 geocodeProxyRoute ( options ) ,
16- reverseGeocodeProxyRoute ( options ) ,
17- ...tileRoutes ( )
23+ reverseGeocodeProxyRoute ( options )
1824 ]
1925}
2026
2127/**
2228 * Proxies ordnance survey requests from the front end to api.os.com
23- * Used for VTS map tiles, sprites and fonts by forwarding on the request
24- * and adding the apikey and optionally an SRS (spatial reference system)
29+ * Used for the VTS map source by forwarding on the request
30+ * and adding the auth token and SRS (spatial reference system)
2531 * @param {MapConfiguration } options - the map options
2632 * @returns {ServerRoute<MapProxyGetRequestRefs> }
2733 */
@@ -32,14 +38,15 @@ function mapProxyRoute(options) {
3238 handler : async ( request , h ) => {
3339 const { query } = request
3440 const targetUrl = new URL ( decodeURIComponent ( query . url ) )
41+ const token = await getAccessToken ( options )
3542
36- // Add API key server-side and set SRS
37- targetUrl . searchParams . set ( 'key' , options . ordnanceSurveyApiKey )
38- if ( ! targetUrl . searchParams . has ( 'srs' ) ) {
39- targetUrl . searchParams . set ( 'srs' , '3857' )
40- }
43+ targetUrl . searchParams . set ( 'srs' , '3857' )
4144
42- const proxyResponse = await httpRequest ( 'get' , targetUrl . toString ( ) )
45+ const proxyResponse = await httpRequest ( 'get' , targetUrl . toString ( ) , {
46+ headers : {
47+ Authorization : `Bearer ${ token } `
48+ }
49+ } )
4350 const buffer = proxyResponse . payload
4451 const contentType = proxyResponse . res . headers [ 'content-type' ]
4552 const response = h . response ( buffer )
@@ -63,7 +70,44 @@ function mapProxyRoute(options) {
6370}
6471
6572/**
66- * Proxies ordnance survey geocode requests from the front end to api.os.com
73+ * Proxies ordnance survey requests from the front end to api.os.uk
74+ * Used for VTS map tiles forwarding on the request and adding the auth token
75+ * @param {MapConfiguration } options - the map options
76+ * @returns {ServerRoute<MapProxyGetRequestRefs> }
77+ */
78+ function tileProxyRoute ( options ) {
79+ return {
80+ method : 'GET' ,
81+ path : '/api/tile/{z}/{y}/{x}.pbf' ,
82+ handler : async ( request , h ) => {
83+ const { z, y, x } = request . params
84+ const token = await getAccessToken ( options )
85+
86+ const url = `https://api.os.uk/maps/vector/v1/vts/tile/${ z } /${ y } /${ x } .pbf?srs=3857`
87+
88+ const { payload, res } = await get ( url , {
89+ headers : {
90+ Authorization : `Bearer ${ token } ` ,
91+ Accept : 'application/x-protobuf'
92+ } ,
93+ json : false ,
94+ gunzip : true
95+ } )
96+
97+ if ( res . statusCode && res . statusCode !== StatusCodes . OK . valueOf ( ) ) {
98+ return h . response ( 'Tile fetch failed' ) . code ( res . statusCode )
99+ }
100+
101+ return h
102+ . response ( payload )
103+ . type ( 'application/x-protobuf' )
104+ . header ( 'Cache-Control' , 'public, max-age=86400' )
105+ }
106+ }
107+ }
108+
109+ /**
110+ * Proxies ordnance survey geocode requests from the front end to api.os.uk
67111 * Used for the gazzeteer address lookup to find name from query strings like postcode and place names
68112 * @param {MapConfiguration } options - the map options
69113 * @returns {ServerRoute<MapGeocodeGetRequestRefs> }
@@ -91,7 +135,7 @@ function geocodeProxyRoute(options) {
91135}
92136
93137/**
94- * Proxies ordnance survey reverse geocode requests from the front end to api.os.com
138+ * Proxies ordnance survey reverse geocode requests from the front end to api.os.uk
95139 * Used to find name from easting and northing points.
96140 * N.B this endpoint is currently not used by the front end but will be soon in "maps V2"
97141 * @param {MapConfiguration } options - the map options
@@ -124,20 +168,22 @@ function reverseGeocodeProxyRoute(options) {
124168 }
125169}
126170
127- function tileRoutes ( ) {
128- return [
129- {
130- method : 'GET' ,
131- path : '/api/maps/vts/{path*}' ,
132- options : {
133- handler : {
134- directory : {
135- path : resolve ( import . meta. dirname , './vts' )
136- }
171+ /**
172+ * Resource routes to return sprites and glyphs
173+ * @returns {ServerRoute<MapProxyGetRequestRefs> }
174+ */
175+ function mapStyleResourceRoutes ( ) {
176+ return {
177+ method : 'GET' ,
178+ path : '/api/maps/vts/{path*}' ,
179+ options : {
180+ handler : {
181+ directory : {
182+ path : resolve ( import . meta. dirname , './vts' )
137183 }
138184 }
139185 }
140- ]
186+ }
141187}
142188
143189/**
0 commit comments