|
| 1 | +import { PUBLIC_BACKEND_API_DOMAIN } from '$env/static/public'; |
| 2 | +import { ResponseError } from './ResponseError'; |
| 3 | + |
| 4 | +const BaseUrl = `https://${PUBLIC_BACKEND_API_DOMAIN}`; |
| 5 | + |
| 6 | +type ApiVersion = 1 | 2; |
| 7 | +export type Path = `/${ApiVersion}/${string}`; |
| 8 | + |
| 9 | +export function GetBackendUrl(path: Path) { |
| 10 | + return BaseUrl + path; |
| 11 | +} |
| 12 | + |
| 13 | +export async function GetJson<T>( |
| 14 | + path: Path, |
| 15 | + expectedStatus = 200, |
| 16 | + transformer: (data: unknown) => T |
| 17 | +): Promise<T> { |
| 18 | + const res = await fetch(BaseUrl + path, { |
| 19 | + headers: { accept: 'application/json' }, |
| 20 | + method: 'GET', |
| 21 | + redirect: 'error', |
| 22 | + }); |
| 23 | + |
| 24 | + if (res.status !== expectedStatus) { |
| 25 | + throw new ResponseError(res, `Unexpected status ${res.status}`); |
| 26 | + } |
| 27 | + |
| 28 | + const contentType = res.headers.get('content-type') ?? ''; |
| 29 | + if (!contentType.includes('application/json')) { |
| 30 | + throw new ResponseError(res, `Expected JSON but got ${contentType}`); |
| 31 | + } |
| 32 | + |
| 33 | + const data = await res.json(); |
| 34 | + |
| 35 | + return transformer(data); |
| 36 | +} |
| 37 | + |
| 38 | +export async function PostJson<T>( |
| 39 | + path: Path, |
| 40 | + body: unknown, |
| 41 | + expectedStatus = 200, |
| 42 | + transformer: (data: unknown) => T |
| 43 | +): Promise<T> { |
| 44 | + const url = GetBackendUrl(path); |
| 45 | + |
| 46 | + const res = await fetch(url, { |
| 47 | + method: 'POST', |
| 48 | + headers: { |
| 49 | + 'content-type': 'application/json', |
| 50 | + accept: 'application/json', |
| 51 | + }, |
| 52 | + body: JSON.stringify(body), |
| 53 | + redirect: 'error', |
| 54 | + }); |
| 55 | + |
| 56 | + if (res.status !== expectedStatus) { |
| 57 | + throw new ResponseError(res, `Unexpected status ${res.status} for POST ${url}`); |
| 58 | + } |
| 59 | + |
| 60 | + const contentType = res.headers.get('content-type') ?? ''; |
| 61 | + if (!contentType.includes('application/json')) { |
| 62 | + throw new ResponseError(res, `Expected JSON but got ${contentType}`); |
| 63 | + } |
| 64 | + |
| 65 | + const data = await res.json(); |
| 66 | + |
| 67 | + return transformer(data); |
| 68 | +} |
| 69 | + |
| 70 | +export async function PostRedirect( |
| 71 | + path: Path, |
| 72 | + expectedStatus: 302 | 303 | 307 | 308 = 302 |
| 73 | +): Promise<void> { |
| 74 | + const url = BaseUrl + path; |
| 75 | + const res = await fetch(url, { method: 'POST', redirect: 'manual' }); |
| 76 | + |
| 77 | + if (res.status !== expectedStatus) { |
| 78 | + throw new ResponseError(res, `Unexpected status ${res.status} for POST ${url}`); |
| 79 | + } |
| 80 | + |
| 81 | + const loc = res.headers.get('Location'); |
| 82 | + if (!loc) throw new ResponseError(res, 'Missing Location header'); |
| 83 | + |
| 84 | + const target = new URL(loc, url); // handles absolute or relative |
| 85 | + |
| 86 | + // Restrict which domains we can be redirected to |
| 87 | + const allowedHosts = new Set([PUBLIC_BACKEND_API_DOMAIN, 'accounts.google.com', 'github.com']); |
| 88 | + |
| 89 | + if (!allowedHosts.has(target.hostname)) { |
| 90 | + throw new ResponseError(res, `Blocked redirect to disallowed host ${target.href}`); |
| 91 | + } |
| 92 | + |
| 93 | + if (target.protocol !== 'https:') { |
| 94 | + throw new ResponseError(res, `Blocked non-HTTPS redirect to ${target.href}`); |
| 95 | + } |
| 96 | + |
| 97 | + window.location.assign(target.toString()); |
| 98 | +} |
0 commit comments