-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.ts
92 lines (75 loc) · 2.74 KB
/
proxy.ts
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
// Deno proxy server for [NTB Mediebank API v1](https://api.ntb.no/portal/docs/media)
//
// Pre v1 examples:
// http://localhost:8000/preview/385801 alias for http://localhost:8000/api/v1/apps/asset/preview/preview/385801
// http://localhost:8000/albums/527 – alias for http://localhost:8000/api/v1/apps/assets?query&albums[]=527
const { serve, env } = Deno;
const ntb_mediebank_secret = env.get("ntb_mediebank_secret");
const baseURL = new URL("https://mediebank.ntb.no/api/v1");
// const errorResponse = (
// { status = 500, statusText = status }: ResponseInit = {},
// ) => Response.json(statusText, { status });
const preview = new URLPattern({
pathname: "/:variant(preview|thumbnail_big|preview_big|original|custom)/:id",
});
// original: 1024px (?)
// preview_big: 1024px
// preview: 512px
// thumbnail_big: 256px
const albums = new URLPattern({ pathname: "/album{s}?/:id" });
const patterns = [albums, preview];
const mediebankURL = (url: URL | string) => {
url = new URL(url);
url.protocol = "https:";
url.hostname = baseURL.hostname;
url.port = "443";
for (const pattern of patterns) {
const match = pattern.exec(url);
if (match) {
if (albums === pattern) {
const { id } = match.pathname.groups;
url.pathname = `${baseURL.pathname}/apps/assets`;
url.searchParams.set("albums[]", id as string);
} else {
const { id, variant } = match.pathname.groups;
url.pathname =
`${baseURL.pathname}/apps/asset/preview/${variant}/${id}`;
}
}
}
console.debug("url", url.href);
return url;
};
const corsHeaders = new Headers({
"access-control-allow-origin": "*",
"access-control-allow-methods": "GET, OPTIONS",
});
export const proxy = async (request: Request) => {
try {
if ("OPTIONS" === request.method) {
return new Response(undefined, { status: 204, headers: corsHeaders });
}
const url = mediebankURL(request.url);
const headers = new Headers(request.headers);
headers.append("x-api-secret", ntb_mediebank_secret as string);
headers.append("accept", "application/json,image/*,text/*");
// Uncomment&refactor to become a real proxy, for now only GET
// const { body, method } = request;
const body = undefined;
const method = "GET";
const response = await fetch(url.href, {
headers,
method,
body,
signal: AbortSignal.timeout(15000),
});
// Add CORS to response headers
// @todo Serve unmodified response on anything but JSON?
const responseHeaders = new Headers([...response.headers, ...corsHeaders]);
return new Response(response.body, { headers: responseHeaders });
} catch (e) {
console.error(e);
return Response.json({ error: String(e) }, { status: 500 });
}
};
serve(proxy);