Skip to content

Commit

Permalink
try adding proxyRes headers too
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicamcinchak committed Jan 18, 2024
1 parent e126a9f commit d939407
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions api.planx.uk/modules/ordnanceSurvey/controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useProxy } from "../../shared/middleware/proxy";
import { NextFunction, Request, Response } from "express";
import { IncomingMessage } from "http";
import { request } from "https";

export const OS_DOMAIN = "https://api.os.uk";

Expand Down Expand Up @@ -28,16 +29,18 @@ export const useOrdnanceSurveyProxy = async (

return useProxy({
target: OS_DOMAIN,
onProxyRes: (proxyRes) => setCORPHeaders(proxyRes),
onProxyRes: (proxyRes) => setCORPHeaders(proxyRes, req),
pathRewrite: (fullPath, req) => appendAPIKey(fullPath, req),
})(req, res, next);
};

const isValid = (req: Request): boolean =>
MAP_ALLOWLIST.some((re) => re.test(req.headers?.referer as string));

const setCORPHeaders = (proxyRes: IncomingMessage): void => {
const setCORPHeaders = (proxyRes: IncomingMessage, req: Request): void => {
proxyRes.headers["Cross-Origin-Resource-Policy"] = "cross-origin";
proxyRes.headers["Access-Control-Allow-Origin"] = req.headers.origin;
proxyRes.headers["Access-Control-Allow-Headers"] = "Origin, X-Requested-With, Content-Type, Accept";
};

export const appendAPIKey = (fullPath: string, req: Request): string => {
Expand Down

0 comments on commit d939407

Please sign in to comment.