@defra/forms-engine-plugin 4.0.41 → 4.0.43
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/.public/javascripts/application.min.js +1 -1
- package/.public/javascripts/application.min.js.map +1 -1
- package/.public/javascripts/shared.min.js +1 -1
- package/.public/javascripts/shared.min.js.map +1 -1
- package/.public/stylesheets/application.min.css +2 -2
- package/.public/stylesheets/application.min.css.map +1 -1
- package/.server/client/javascripts/location-map.d.ts +93 -0
- package/.server/client/javascripts/location-map.js +749 -0
- package/.server/client/javascripts/location-map.js.map +1 -0
- package/.server/client/javascripts/shared.d.ts +4 -0
- package/.server/client/javascripts/shared.js +5 -0
- package/.server/client/javascripts/shared.js.map +1 -1
- package/.server/client/stylesheets/_location-fields.scss +11 -0
- package/.server/client/stylesheets/application.scss +0 -1
- package/.server/client/stylesheets/shared.scss +1 -0
- package/.server/config/index.js +1 -1
- package/.server/config/index.js.map +1 -1
- package/.server/server/plugins/engine/configureEnginePlugin.d.ts +1 -1
- package/.server/server/plugins/engine/configureEnginePlugin.js +4 -2
- package/.server/server/plugins/engine/configureEnginePlugin.js.map +1 -1
- package/.server/server/plugins/engine/options.js +2 -1
- package/.server/server/plugins/engine/options.js.map +1 -1
- package/.server/server/plugins/engine/plugin.js +14 -1
- package/.server/server/plugins/engine/plugin.js.map +1 -1
- package/.server/server/plugins/engine/types.d.ts +1 -0
- package/.server/server/plugins/engine/types.js.map +1 -1
- package/.server/server/plugins/engine/views/components/_location-field-base.html +1 -1
- package/.server/server/plugins/engine/views/components/osgridreffield.html +4 -2
- package/.server/server/plugins/map/index.d.ts +7 -0
- package/.server/server/plugins/map/index.js +20 -0
- package/.server/server/plugins/map/index.js.map +1 -0
- package/.server/server/plugins/map/routes/get-os-token.d.ts +6 -0
- package/.server/server/plugins/map/routes/get-os-token.js +41 -0
- package/.server/server/plugins/map/routes/get-os-token.js.map +1 -0
- package/.server/server/plugins/map/routes/get-os-token.test.js +49 -0
- package/.server/server/plugins/map/routes/get-os-token.test.js.map +1 -0
- package/.server/server/plugins/map/routes/index.d.ts +10 -0
- package/.server/server/plugins/map/routes/index.js +172 -0
- package/.server/server/plugins/map/routes/index.js.map +1 -0
- package/.server/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/dark.json +690 -0
- package/.server/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/dark.png +0 -0
- package/.server/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/dark@2x.json +690 -0
- package/.server/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/dark@2x.png +0 -0
- package/.server/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/greyscale.json +690 -0
- package/.server/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/greyscale.png +0 -0
- package/.server/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/greyscale@2x.json +690 -0
- package/.server/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/greyscale@2x.png +0 -0
- package/.server/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/sprite.json +690 -0
- package/.server/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/sprite.png +0 -0
- package/.server/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/sprite@2x.json +690 -0
- package/.server/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/sprite@2x.png +0 -0
- package/.server/server/plugins/map/routes/vts/OS_VTS_3857_Black_and_White.json +7858 -0
- package/.server/server/plugins/map/routes/vts/OS_VTS_3857_Dark.json +7669 -0
- package/.server/server/plugins/map/routes/vts/OS_VTS_3857_Outdoor.json +7653 -0
- package/.server/server/plugins/map/routes/vts/README.md +5 -0
- package/.server/server/plugins/map/service.d.ts +14 -0
- package/.server/server/plugins/map/service.js +76 -0
- package/.server/server/plugins/map/service.js.map +1 -0
- package/.server/server/plugins/map/service.test.js +120 -0
- package/.server/server/plugins/map/service.test.js.map +1 -0
- package/.server/server/plugins/map/test/__stubs__/find.d.ts +3 -0
- package/.server/server/plugins/map/test/__stubs__/find.js +216 -0
- package/.server/server/plugins/map/test/__stubs__/find.js.map +1 -0
- package/.server/server/plugins/map/test/__stubs__/nearest.d.ts +37 -0
- package/.server/server/plugins/map/test/__stubs__/nearest.js +38 -0
- package/.server/server/plugins/map/test/__stubs__/nearest.js.map +1 -0
- package/.server/server/plugins/map/types.d.ts +233 -0
- package/.server/server/plugins/map/types.js +121 -0
- package/.server/server/plugins/map/types.js.map +1 -0
- package/.server/server/types.d.ts +1 -0
- package/.server/server/types.js.map +1 -1
- package/package.json +3 -1
- package/src/client/javascripts/location-map.js +774 -0
- package/src/client/javascripts/shared.js +4 -0
- package/src/client/stylesheets/_location-fields.scss +11 -0
- package/src/client/stylesheets/application.scss +0 -1
- package/src/client/stylesheets/shared.scss +1 -0
- package/src/config/index.ts +1 -1
- package/src/server/plugins/engine/configureEnginePlugin.ts +4 -2
- package/src/server/plugins/engine/options.js +2 -1
- package/src/server/plugins/engine/plugin.ts +14 -1
- package/src/server/plugins/engine/types.ts +1 -0
- package/src/server/plugins/engine/views/components/_location-field-base.html +1 -1
- package/src/server/plugins/engine/views/components/osgridreffield.html +4 -2
- package/src/server/plugins/map/index.js +19 -0
- package/src/server/plugins/map/routes/get-os-token.js +41 -0
- package/src/server/plugins/map/routes/get-os-token.test.js +55 -0
- package/src/server/plugins/map/routes/index.js +192 -0
- package/src/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/dark.json +690 -0
- package/src/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/dark.png +0 -0
- package/src/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/dark@2x.json +690 -0
- package/src/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/dark@2x.png +0 -0
- package/src/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/greyscale.json +690 -0
- package/src/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/greyscale.png +0 -0
- package/src/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/greyscale@2x.json +690 -0
- package/src/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/greyscale@2x.png +0 -0
- package/src/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/sprite.json +690 -0
- package/src/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/sprite.png +0 -0
- package/src/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/sprite@2x.json +690 -0
- package/src/server/plugins/map/routes/vts/OS_VTS_3857/resources/sprites/sprite@2x.png +0 -0
- package/src/server/plugins/map/routes/vts/OS_VTS_3857_Black_and_White.json +7858 -0
- package/src/server/plugins/map/routes/vts/OS_VTS_3857_Dark.json +7669 -0
- package/src/server/plugins/map/routes/vts/OS_VTS_3857_Outdoor.json +7653 -0
- package/src/server/plugins/map/routes/vts/README.md +5 -0
- package/src/server/plugins/map/service.js +84 -0
- package/src/server/plugins/map/service.test.js +144 -0
- package/src/server/plugins/map/test/__stubs__/find.js +271 -0
- package/src/server/plugins/map/test/__stubs__/nearest.js +46 -0
- package/src/server/plugins/map/types.js +120 -0
- package/src/server/types.ts +1 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { post } from "../../../services/httpService.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @type {string}
|
|
5
|
+
*/
|
|
6
|
+
let cachedToken;
|
|
7
|
+
let tokenExpiry = 0;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Get Ordnance Survey OAuth token
|
|
11
|
+
* @param {MapConfiguration} options - Ordnance survey map options
|
|
12
|
+
*/
|
|
13
|
+
export async function getAccessToken(options) {
|
|
14
|
+
const {
|
|
15
|
+
ordnanceSurveyApiKey: key,
|
|
16
|
+
ordnanceSurveyApiSecret: secret
|
|
17
|
+
} = options;
|
|
18
|
+
const now = Date.now();
|
|
19
|
+
if (cachedToken && now < tokenExpiry) {
|
|
20
|
+
return cachedToken;
|
|
21
|
+
}
|
|
22
|
+
const creds = `${key}:${secret}`;
|
|
23
|
+
const result = await post('https://api.os.uk/oauth2/token/v1', {
|
|
24
|
+
headers: {
|
|
25
|
+
Authorization: `Basic ${btoa(creds)}`,
|
|
26
|
+
'Content-Type': 'application/x-www-form-urlencoded'
|
|
27
|
+
},
|
|
28
|
+
payload: 'grant_type=client_credentials',
|
|
29
|
+
json: true
|
|
30
|
+
});
|
|
31
|
+
const data = result.payload;
|
|
32
|
+
cachedToken = data.access_token;
|
|
33
|
+
tokenExpiry = now + (data.expires_in - 60) * 1000; // refresh early
|
|
34
|
+
|
|
35
|
+
return cachedToken;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @import { MapConfiguration } from '~/src/server/plugins/map/types.js'
|
|
40
|
+
*/
|
|
41
|
+
//# sourceMappingURL=get-os-token.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-os-token.js","names":["post","cachedToken","tokenExpiry","getAccessToken","options","ordnanceSurveyApiKey","key","ordnanceSurveyApiSecret","secret","now","Date","creds","result","headers","Authorization","btoa","payload","json","data","access_token","expires_in"],"sources":["../../../../../src/server/plugins/map/routes/get-os-token.js"],"sourcesContent":["import { post } from '~/src/server/services/httpService.js'\n\n/**\n * @type {string}\n */\nlet cachedToken\nlet tokenExpiry = 0\n\n/**\n * Get Ordnance Survey OAuth token\n * @param {MapConfiguration} options - Ordnance survey map options\n */\nexport async function getAccessToken(options) {\n const { ordnanceSurveyApiKey: key, ordnanceSurveyApiSecret: secret } = options\n const now = Date.now()\n\n if (cachedToken && now < tokenExpiry) {\n return cachedToken\n }\n\n const creds = `${key}:${secret}`\n const result = await post('https://api.os.uk/oauth2/token/v1', {\n headers: {\n Authorization: `Basic ${btoa(creds)}`,\n 'Content-Type': 'application/x-www-form-urlencoded'\n },\n payload: 'grant_type=client_credentials',\n json: true\n })\n\n const data = result.payload\n\n cachedToken = data.access_token\n tokenExpiry = now + (data.expires_in - 60) * 1000 // refresh early\n\n return cachedToken\n}\n\n/**\n * @import { MapConfiguration } from '~/src/server/plugins/map/types.js'\n */\n"],"mappings":"AAAA,SAASA,IAAI;;AAEb;AACA;AACA;AACA,IAAIC,WAAW;AACf,IAAIC,WAAW,GAAG,CAAC;;AAEnB;AACA;AACA;AACA;AACA,OAAO,eAAeC,cAAcA,CAACC,OAAO,EAAE;EAC5C,MAAM;IAAEC,oBAAoB,EAAEC,GAAG;IAAEC,uBAAuB,EAAEC;EAAO,CAAC,GAAGJ,OAAO;EAC9E,MAAMK,GAAG,GAAGC,IAAI,CAACD,GAAG,CAAC,CAAC;EAEtB,IAAIR,WAAW,IAAIQ,GAAG,GAAGP,WAAW,EAAE;IACpC,OAAOD,WAAW;EACpB;EAEA,MAAMU,KAAK,GAAG,GAAGL,GAAG,IAAIE,MAAM,EAAE;EAChC,MAAMI,MAAM,GAAG,MAAMZ,IAAI,CAAC,mCAAmC,EAAE;IAC7Da,OAAO,EAAE;MACPC,aAAa,EAAE,SAASC,IAAI,CAACJ,KAAK,CAAC,EAAE;MACrC,cAAc,EAAE;IAClB,CAAC;IACDK,OAAO,EAAE,+BAA+B;IACxCC,IAAI,EAAE;EACR,CAAC,CAAC;EAEF,MAAMC,IAAI,GAAGN,MAAM,CAACI,OAAO;EAE3Bf,WAAW,GAAGiB,IAAI,CAACC,YAAY;EAC/BjB,WAAW,GAAGO,GAAG,GAAG,CAACS,IAAI,CAACE,UAAU,GAAG,EAAE,IAAI,IAAI,EAAC;;EAElD,OAAOnB,WAAW;AACpB;;AAEA;AACA;AACA","ignoreList":[]}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { getAccessToken } from "./get-os-token.js";
|
|
2
|
+
import { post } from "../../../services/httpService.js";
|
|
3
|
+
jest.mock("../../../services/httpService.ts");
|
|
4
|
+
describe('OS OAuth token', () => {
|
|
5
|
+
describe('getAccessToken', () => {
|
|
6
|
+
it('should get access token', async () => {
|
|
7
|
+
jest.mocked(post).mockResolvedValueOnce({
|
|
8
|
+
res: (/** @type {IncomingMessage} */{
|
|
9
|
+
statusCode: 200,
|
|
10
|
+
headers: {}
|
|
11
|
+
}),
|
|
12
|
+
payload: {
|
|
13
|
+
access_token: 'access_token',
|
|
14
|
+
expires_in: '299',
|
|
15
|
+
issued_at: '1770036762387',
|
|
16
|
+
token_type: 'Bearer'
|
|
17
|
+
},
|
|
18
|
+
error: undefined
|
|
19
|
+
});
|
|
20
|
+
const token = await getAccessToken({
|
|
21
|
+
ordnanceSurveyApiKey: 'apikey',
|
|
22
|
+
ordnanceSurveyApiSecret: 'apisecret'
|
|
23
|
+
});
|
|
24
|
+
expect(token).toBe('access_token');
|
|
25
|
+
expect(post).toHaveBeenCalledWith('https://api.os.uk/oauth2/token/v1', {
|
|
26
|
+
headers: {
|
|
27
|
+
Authorization: `Basic ${btoa('apikey:apisecret')}`,
|
|
28
|
+
'Content-Type': 'application/x-www-form-urlencoded'
|
|
29
|
+
},
|
|
30
|
+
payload: 'grant_type=client_credentials',
|
|
31
|
+
json: true
|
|
32
|
+
});
|
|
33
|
+
expect(post).toHaveBeenCalledTimes(1);
|
|
34
|
+
});
|
|
35
|
+
it('should return an cached token', async () => {
|
|
36
|
+
const token = await getAccessToken({
|
|
37
|
+
ordnanceSurveyApiKey: 'apikey',
|
|
38
|
+
ordnanceSurveyApiSecret: 'apisecret'
|
|
39
|
+
});
|
|
40
|
+
expect(token).toBe('access_token');
|
|
41
|
+
expect(post).toHaveBeenCalledTimes(0);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @import { IncomingMessage } from 'node:http'
|
|
48
|
+
*/
|
|
49
|
+
//# sourceMappingURL=get-os-token.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-os-token.test.js","names":["getAccessToken","post","jest","mock","describe","it","mocked","mockResolvedValueOnce","res","statusCode","headers","payload","access_token","expires_in","issued_at","token_type","error","undefined","token","ordnanceSurveyApiKey","ordnanceSurveyApiSecret","expect","toBe","toHaveBeenCalledWith","Authorization","btoa","json","toHaveBeenCalledTimes"],"sources":["../../../../../src/server/plugins/map/routes/get-os-token.test.js"],"sourcesContent":["import { getAccessToken } from '~/src/server/plugins/map/routes/get-os-token.js'\nimport { post } from '~/src/server/services/httpService.js'\n\njest.mock('~/src/server/services/httpService.ts')\n\ndescribe('OS OAuth token', () => {\n describe('getAccessToken', () => {\n it('should get access token', async () => {\n jest.mocked(post).mockResolvedValueOnce({\n res: /** @type {IncomingMessage} */ ({\n statusCode: 200,\n headers: {}\n }),\n payload: {\n access_token: 'access_token',\n expires_in: '299',\n issued_at: '1770036762387',\n token_type: 'Bearer'\n },\n error: undefined\n })\n\n const token = await getAccessToken({\n ordnanceSurveyApiKey: 'apikey',\n ordnanceSurveyApiSecret: 'apisecret'\n })\n\n expect(token).toBe('access_token')\n\n expect(post).toHaveBeenCalledWith('https://api.os.uk/oauth2/token/v1', {\n headers: {\n Authorization: `Basic ${btoa('apikey:apisecret')}`,\n 'Content-Type': 'application/x-www-form-urlencoded'\n },\n payload: 'grant_type=client_credentials',\n json: true\n })\n expect(post).toHaveBeenCalledTimes(1)\n })\n\n it('should return an cached token', async () => {\n const token = await getAccessToken({\n ordnanceSurveyApiKey: 'apikey',\n ordnanceSurveyApiSecret: 'apisecret'\n })\n\n expect(token).toBe('access_token')\n expect(post).toHaveBeenCalledTimes(0)\n })\n })\n})\n\n/**\n * @import { IncomingMessage } from 'node:http'\n */\n"],"mappings":"AAAA,SAASA,cAAc;AACvB,SAASC,IAAI;AAEbC,IAAI,CAACC,IAAI,mCAAuC,CAAC;AAEjDC,QAAQ,CAAC,gBAAgB,EAAE,MAAM;EAC/BA,QAAQ,CAAC,gBAAgB,EAAE,MAAM;IAC/BC,EAAE,CAAC,yBAAyB,EAAE,YAAY;MACxCH,IAAI,CAACI,MAAM,CAACL,IAAI,CAAC,CAACM,qBAAqB,CAAC;QACtCC,GAAG,GAAE,8BAAgC;UACnCC,UAAU,EAAE,GAAG;UACfC,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC;QACFC,OAAO,EAAE;UACPC,YAAY,EAAE,cAAc;UAC5BC,UAAU,EAAE,KAAK;UACjBC,SAAS,EAAE,eAAe;UAC1BC,UAAU,EAAE;QACd,CAAC;QACDC,KAAK,EAAEC;MACT,CAAC,CAAC;MAEF,MAAMC,KAAK,GAAG,MAAMlB,cAAc,CAAC;QACjCmB,oBAAoB,EAAE,QAAQ;QAC9BC,uBAAuB,EAAE;MAC3B,CAAC,CAAC;MAEFC,MAAM,CAACH,KAAK,CAAC,CAACI,IAAI,CAAC,cAAc,CAAC;MAElCD,MAAM,CAACpB,IAAI,CAAC,CAACsB,oBAAoB,CAAC,mCAAmC,EAAE;QACrEb,OAAO,EAAE;UACPc,aAAa,EAAE,SAASC,IAAI,CAAC,kBAAkB,CAAC,EAAE;UAClD,cAAc,EAAE;QAClB,CAAC;QACDd,OAAO,EAAE,+BAA+B;QACxCe,IAAI,EAAE;MACR,CAAC,CAAC;MACFL,MAAM,CAACpB,IAAI,CAAC,CAAC0B,qBAAqB,CAAC,CAAC,CAAC;IACvC,CAAC,CAAC;IAEFtB,EAAE,CAAC,+BAA+B,EAAE,YAAY;MAC9C,MAAMa,KAAK,GAAG,MAAMlB,cAAc,CAAC;QACjCmB,oBAAoB,EAAE,QAAQ;QAC9BC,uBAAuB,EAAE;MAC3B,CAAC,CAAC;MAEFC,MAAM,CAACH,KAAK,CAAC,CAACI,IAAI,CAAC,cAAc,CAAC;MAClCD,MAAM,CAACpB,IAAI,CAAC,CAAC0B,qBAAqB,CAAC,CAAC,CAAC;IACvC,CAAC,CAAC;EACJ,CAAC,CAAC;AACJ,CAAC,CAAC;;AAEF;AACA;AACA","ignoreList":[]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets the map support routes
|
|
3
|
+
* @param {MapConfiguration} options - ordnance survey names api key
|
|
4
|
+
*/
|
|
5
|
+
export function getRoutes(options: MapConfiguration): (ServerRoute<MapProxyGetRequestRefs> | ServerRoute<MapGeocodeGetRequestRefs> | ServerRoute<MapReverseGeocodeGetRequestRefs>)[];
|
|
6
|
+
import type { MapConfiguration } from '~/src/server/plugins/map/types.js';
|
|
7
|
+
import type { MapProxyGetRequestRefs } from '~/src/server/plugins/map/types.js';
|
|
8
|
+
import type { ServerRoute } from '@hapi/hapi';
|
|
9
|
+
import type { MapGeocodeGetRequestRefs } from '~/src/server/plugins/map/types.js';
|
|
10
|
+
import type { MapReverseGeocodeGetRequestRefs } from '~/src/server/plugins/map/types.js';
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
import { StatusCodes } from 'http-status-codes';
|
|
3
|
+
import Joi from 'joi';
|
|
4
|
+
import { getAccessToken } from "./get-os-token.js";
|
|
5
|
+
import { find, nearest } from "../service.js";
|
|
6
|
+
import { get, request as httpRequest } from "../../../services/httpService.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Gets the map support routes
|
|
10
|
+
* @param {MapConfiguration} options - ordnance survey names api key
|
|
11
|
+
*/
|
|
12
|
+
export function getRoutes(options) {
|
|
13
|
+
return [mapStyleResourceRoutes(), mapProxyRoute(options), tileProxyRoute(options), geocodeProxyRoute(options), reverseGeocodeProxyRoute(options)];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Proxies ordnance survey requests from the front end to api.os.com
|
|
18
|
+
* Used for the VTS map source by forwarding on the request
|
|
19
|
+
* and adding the auth token and SRS (spatial reference system)
|
|
20
|
+
* @param {MapConfiguration} options - the map options
|
|
21
|
+
* @returns {ServerRoute<MapProxyGetRequestRefs>}
|
|
22
|
+
*/
|
|
23
|
+
function mapProxyRoute(options) {
|
|
24
|
+
return {
|
|
25
|
+
method: 'GET',
|
|
26
|
+
path: '/api/map-proxy',
|
|
27
|
+
handler: async (request, h) => {
|
|
28
|
+
const {
|
|
29
|
+
query
|
|
30
|
+
} = request;
|
|
31
|
+
const targetUrl = new URL(decodeURIComponent(query.url));
|
|
32
|
+
const token = await getAccessToken(options);
|
|
33
|
+
targetUrl.searchParams.set('srs', '3857');
|
|
34
|
+
const proxyResponse = await httpRequest('get', targetUrl.toString(), {
|
|
35
|
+
headers: {
|
|
36
|
+
Authorization: `Bearer ${token}`
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
const buffer = proxyResponse.payload;
|
|
40
|
+
const contentType = proxyResponse.res.headers['content-type'];
|
|
41
|
+
const response = h.response(buffer);
|
|
42
|
+
if (contentType) {
|
|
43
|
+
response.type(contentType);
|
|
44
|
+
}
|
|
45
|
+
return response;
|
|
46
|
+
},
|
|
47
|
+
options: {
|
|
48
|
+
validate: {
|
|
49
|
+
query: Joi.object().keys({
|
|
50
|
+
url: Joi.string().required()
|
|
51
|
+
}).optional()
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Proxies ordnance survey requests from the front end to api.os.uk
|
|
59
|
+
* Used for VTS map tiles forwarding on the request and adding the auth token
|
|
60
|
+
* @param {MapConfiguration} options - the map options
|
|
61
|
+
* @returns {ServerRoute<MapProxyGetRequestRefs>}
|
|
62
|
+
*/
|
|
63
|
+
function tileProxyRoute(options) {
|
|
64
|
+
return {
|
|
65
|
+
method: 'GET',
|
|
66
|
+
path: '/api/tile/{z}/{y}/{x}.pbf',
|
|
67
|
+
handler: async (request, h) => {
|
|
68
|
+
const {
|
|
69
|
+
z,
|
|
70
|
+
y,
|
|
71
|
+
x
|
|
72
|
+
} = request.params;
|
|
73
|
+
const token = await getAccessToken(options);
|
|
74
|
+
const url = `https://api.os.uk/maps/vector/v1/vts/tile/${z}/${y}/${x}.pbf?srs=3857`;
|
|
75
|
+
const {
|
|
76
|
+
payload,
|
|
77
|
+
res
|
|
78
|
+
} = await get(url, {
|
|
79
|
+
headers: {
|
|
80
|
+
Authorization: `Bearer ${token}`,
|
|
81
|
+
Accept: 'application/x-protobuf'
|
|
82
|
+
},
|
|
83
|
+
json: false,
|
|
84
|
+
gunzip: true
|
|
85
|
+
});
|
|
86
|
+
if (res.statusCode && res.statusCode !== StatusCodes.OK.valueOf()) {
|
|
87
|
+
return h.response('Tile fetch failed').code(res.statusCode);
|
|
88
|
+
}
|
|
89
|
+
return h.response(payload).type('application/x-protobuf').header('Cache-Control', 'public, max-age=86400');
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Proxies ordnance survey geocode requests from the front end to api.os.uk
|
|
96
|
+
* Used for the gazzeteer address lookup to find name from query strings like postcode and place names
|
|
97
|
+
* @param {MapConfiguration} options - the map options
|
|
98
|
+
* @returns {ServerRoute<MapGeocodeGetRequestRefs>}
|
|
99
|
+
*/
|
|
100
|
+
function geocodeProxyRoute(options) {
|
|
101
|
+
return {
|
|
102
|
+
method: 'GET',
|
|
103
|
+
path: '/api/geocode-proxy',
|
|
104
|
+
async handler(request, _h) {
|
|
105
|
+
const {
|
|
106
|
+
query
|
|
107
|
+
} = request;
|
|
108
|
+
const data = await find(query.query, options.ordnanceSurveyApiKey);
|
|
109
|
+
return data;
|
|
110
|
+
},
|
|
111
|
+
options: {
|
|
112
|
+
validate: {
|
|
113
|
+
query: Joi.object().keys({
|
|
114
|
+
query: Joi.string().required()
|
|
115
|
+
}).required()
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Proxies ordnance survey reverse geocode requests from the front end to api.os.uk
|
|
123
|
+
* Used to find name from easting and northing points.
|
|
124
|
+
* N.B this endpoint is currently not used by the front end but will be soon in "maps V2"
|
|
125
|
+
* @param {MapConfiguration} options - the map options
|
|
126
|
+
* @returns {ServerRoute<MapReverseGeocodeGetRequestRefs>}
|
|
127
|
+
*/
|
|
128
|
+
function reverseGeocodeProxyRoute(options) {
|
|
129
|
+
return {
|
|
130
|
+
method: 'GET',
|
|
131
|
+
path: '/api/reverse-geocode-proxy',
|
|
132
|
+
async handler(request, _h) {
|
|
133
|
+
const {
|
|
134
|
+
query
|
|
135
|
+
} = request;
|
|
136
|
+
const data = await nearest(query.easting, query.northing, options.ordnanceSurveyApiKey);
|
|
137
|
+
return data;
|
|
138
|
+
},
|
|
139
|
+
options: {
|
|
140
|
+
validate: {
|
|
141
|
+
query: Joi.object().keys({
|
|
142
|
+
easting: Joi.number().required(),
|
|
143
|
+
northing: Joi.number().required()
|
|
144
|
+
}).required()
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Resource routes to return sprites and glyphs
|
|
152
|
+
* @returns {ServerRoute<MapProxyGetRequestRefs>}
|
|
153
|
+
*/
|
|
154
|
+
function mapStyleResourceRoutes() {
|
|
155
|
+
return {
|
|
156
|
+
method: 'GET',
|
|
157
|
+
path: '/api/maps/vts/{path*}',
|
|
158
|
+
options: {
|
|
159
|
+
handler: {
|
|
160
|
+
directory: {
|
|
161
|
+
path: resolve(import.meta.dirname, './vts')
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @import { ServerRoute } from '@hapi/hapi'
|
|
170
|
+
* @import { MapConfiguration, MapProxyGetRequestRefs, MapGeocodeGetRequestRefs, MapReverseGeocodeGetRequestRefs } from '~/src/server/plugins/map/types.js'
|
|
171
|
+
*/
|
|
172
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["resolve","StatusCodes","Joi","getAccessToken","find","nearest","get","request","httpRequest","getRoutes","options","mapStyleResourceRoutes","mapProxyRoute","tileProxyRoute","geocodeProxyRoute","reverseGeocodeProxyRoute","method","path","handler","h","query","targetUrl","URL","decodeURIComponent","url","token","searchParams","set","proxyResponse","toString","headers","Authorization","buffer","payload","contentType","res","response","type","validate","object","keys","string","required","optional","z","y","x","params","Accept","json","gunzip","statusCode","OK","valueOf","code","header","_h","data","ordnanceSurveyApiKey","easting","northing","number","directory","import","meta","dirname"],"sources":["../../../../../src/server/plugins/map/routes/index.js"],"sourcesContent":["import { resolve } from 'node:path'\n\nimport { StatusCodes } from 'http-status-codes'\nimport Joi from 'joi'\n\nimport { getAccessToken } from '~/src/server/plugins/map/routes/get-os-token.js'\nimport { find, nearest } from '~/src/server/plugins/map/service.js'\nimport {\n get,\n request as httpRequest\n} from '~/src/server/services/httpService.js'\n\n/**\n * Gets the map support routes\n * @param {MapConfiguration} options - ordnance survey names api key\n */\nexport function getRoutes(options) {\n return [\n mapStyleResourceRoutes(),\n mapProxyRoute(options),\n tileProxyRoute(options),\n geocodeProxyRoute(options),\n reverseGeocodeProxyRoute(options)\n ]\n}\n\n/**\n * Proxies ordnance survey requests from the front end to api.os.com\n * Used for the VTS map source by forwarding on the request\n * and adding the auth token and SRS (spatial reference system)\n * @param {MapConfiguration} options - the map options\n * @returns {ServerRoute<MapProxyGetRequestRefs>}\n */\nfunction mapProxyRoute(options) {\n return {\n method: 'GET',\n path: '/api/map-proxy',\n handler: async (request, h) => {\n const { query } = request\n const targetUrl = new URL(decodeURIComponent(query.url))\n const token = await getAccessToken(options)\n\n targetUrl.searchParams.set('srs', '3857')\n\n const proxyResponse = await httpRequest('get', targetUrl.toString(), {\n headers: {\n Authorization: `Bearer ${token}`\n }\n })\n const buffer = proxyResponse.payload\n const contentType = proxyResponse.res.headers['content-type']\n const response = h.response(buffer)\n\n if (contentType) {\n response.type(contentType)\n }\n\n return response\n },\n options: {\n validate: {\n query: Joi.object()\n .keys({\n url: Joi.string().required()\n })\n .optional()\n }\n }\n }\n}\n\n/**\n * Proxies ordnance survey requests from the front end to api.os.uk\n * Used for VTS map tiles forwarding on the request and adding the auth token\n * @param {MapConfiguration} options - the map options\n * @returns {ServerRoute<MapProxyGetRequestRefs>}\n */\nfunction tileProxyRoute(options) {\n return {\n method: 'GET',\n path: '/api/tile/{z}/{y}/{x}.pbf',\n handler: async (request, h) => {\n const { z, y, x } = request.params\n const token = await getAccessToken(options)\n\n const url = `https://api.os.uk/maps/vector/v1/vts/tile/${z}/${y}/${x}.pbf?srs=3857`\n\n const { payload, res } = await get(url, {\n headers: {\n Authorization: `Bearer ${token}`,\n Accept: 'application/x-protobuf'\n },\n json: false,\n gunzip: true\n })\n\n if (res.statusCode && res.statusCode !== StatusCodes.OK.valueOf()) {\n return h.response('Tile fetch failed').code(res.statusCode)\n }\n\n return h\n .response(payload)\n .type('application/x-protobuf')\n .header('Cache-Control', 'public, max-age=86400')\n }\n }\n}\n\n/**\n * Proxies ordnance survey geocode requests from the front end to api.os.uk\n * Used for the gazzeteer address lookup to find name from query strings like postcode and place names\n * @param {MapConfiguration} options - the map options\n * @returns {ServerRoute<MapGeocodeGetRequestRefs>}\n */\nfunction geocodeProxyRoute(options) {\n return {\n method: 'GET',\n path: '/api/geocode-proxy',\n async handler(request, _h) {\n const { query } = request\n const data = await find(query.query, options.ordnanceSurveyApiKey)\n\n return data\n },\n options: {\n validate: {\n query: Joi.object()\n .keys({\n query: Joi.string().required()\n })\n .required()\n }\n }\n }\n}\n\n/**\n * Proxies ordnance survey reverse geocode requests from the front end to api.os.uk\n * Used to find name from easting and northing points.\n * N.B this endpoint is currently not used by the front end but will be soon in \"maps V2\"\n * @param {MapConfiguration} options - the map options\n * @returns {ServerRoute<MapReverseGeocodeGetRequestRefs>}\n */\nfunction reverseGeocodeProxyRoute(options) {\n return {\n method: 'GET',\n path: '/api/reverse-geocode-proxy',\n async handler(request, _h) {\n const { query } = request\n const data = await nearest(\n query.easting,\n query.northing,\n options.ordnanceSurveyApiKey\n )\n\n return data\n },\n options: {\n validate: {\n query: Joi.object()\n .keys({\n easting: Joi.number().required(),\n northing: Joi.number().required()\n })\n .required()\n }\n }\n }\n}\n\n/**\n * Resource routes to return sprites and glyphs\n * @returns {ServerRoute<MapProxyGetRequestRefs>}\n */\nfunction mapStyleResourceRoutes() {\n return {\n method: 'GET',\n path: '/api/maps/vts/{path*}',\n options: {\n handler: {\n directory: {\n path: resolve(import.meta.dirname, './vts')\n }\n }\n }\n }\n}\n\n/**\n * @import { ServerRoute } from '@hapi/hapi'\n * @import { MapConfiguration, MapProxyGetRequestRefs, MapGeocodeGetRequestRefs, MapReverseGeocodeGetRequestRefs } from '~/src/server/plugins/map/types.js'\n */\n"],"mappings":"AAAA,SAASA,OAAO,QAAQ,WAAW;AAEnC,SAASC,WAAW,QAAQ,mBAAmB;AAC/C,OAAOC,GAAG,MAAM,KAAK;AAErB,SAASC,cAAc;AACvB,SAASC,IAAI,EAAEC,OAAO;AACtB,SACEC,GAAG,EACHC,OAAO,IAAIC,WAAW;;AAGxB;AACA;AACA;AACA;AACA,OAAO,SAASC,SAASA,CAACC,OAAO,EAAE;EACjC,OAAO,CACLC,sBAAsB,CAAC,CAAC,EACxBC,aAAa,CAACF,OAAO,CAAC,EACtBG,cAAc,CAACH,OAAO,CAAC,EACvBI,iBAAiB,CAACJ,OAAO,CAAC,EAC1BK,wBAAwB,CAACL,OAAO,CAAC,CAClC;AACH;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASE,aAAaA,CAACF,OAAO,EAAE;EAC9B,OAAO;IACLM,MAAM,EAAE,KAAK;IACbC,IAAI,EAAE,gBAAgB;IACtBC,OAAO,EAAE,MAAAA,CAAOX,OAAO,EAAEY,CAAC,KAAK;MAC7B,MAAM;QAAEC;MAAM,CAAC,GAAGb,OAAO;MACzB,MAAMc,SAAS,GAAG,IAAIC,GAAG,CAACC,kBAAkB,CAACH,KAAK,CAACI,GAAG,CAAC,CAAC;MACxD,MAAMC,KAAK,GAAG,MAAMtB,cAAc,CAACO,OAAO,CAAC;MAE3CW,SAAS,CAACK,YAAY,CAACC,GAAG,CAAC,KAAK,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