| super(message || statusText) |
| 9 | this.status = status |
| 10 | this.statusText = statusText |
| 11 | } |
| 12 | } |
| 13 |
| 14 | export interface V2Envelope<T> { |
| 15 | code: number |
| 16 | msg: string |
| 17 | data: T |
| 18 | } |
| 19 |
| 20 | export interface V2Page<T> { |
| 21 | total: number |
| 22 | page: number |
| 23 | pageSize: number |
| 24 | items: T[] |
| 25 | } |
| 26 |
| 27 | type QueryParamValue = string | number | boolean | null | undefined |
| 28 |
| 29 | async function request<T>(url: string, options: RequestInit = {}): Promise<T> { |
| 30 | const defaultOptions: RequestInit = { |
| 31 | credentials: 'include', |
| 32 | } |
| 33 |
| 34 | const response = await fetch(url, { ...defaultOptions, ...options }) |
| 35 |
| 36 | if (!response.ok) { |
| 37 | throw new HTTPError( |
| 38 | response.status, |
| 39 | response.statusText, |
| 40 | `HTTP ${response.status}`, |
| 41 | ) |
| 42 | } |
| 43 |
| 44 | // Handle empty response (e.g. 204 No Content) |
| 45 | if (response.status === 204) { |
| 46 | return {} as T |
| 47 | } |
| 48 |
| 49 | return response.json() |
| 50 | } |
| 51 |
| 52 | async function requestV2<T>( |
| 53 | url: string, |
| 54 | options: RequestInit = {}, |
| 55 | ): Promise<T> { |
| 56 | const defaultOptions: RequestInit = { |
| 57 | credentials: 'include', |
| 58 | } |
| 59 |
| 60 | const response = await fetch(url, { ...defaultOptions, ...options }) |
| 61 | const envelope = (await response.json().catch(() => null)) as |
| 62 | | V2Envelope<T> |
| 63 | | null |
| 64 |
| 65 | if (!response.ok) { |
| 66 | throw new HTTPError( |
| 67 | response.status, |
| 68 | response.statusText, |
| 69 | envelope?.msg || `HTTP ${response.status}`, |
| 70 | ) |
| 71 | } |
| 72 |
| 73 | if (!envelope || typeof envelope.code !== 'number') { |
| 74 | throw new Error('Invalid API v2 response') |
| 75 | } |
| 76 |
| 77 | if (envelope.code >= 400) { |
| 78 | throw new HTTPError(envelope.code, envelope.msg, envelope.msg) |
| 79 | } |
| 80 |
| 81 | return envelope.data |
| 82 | } |
| 83 |
| 84 | export const buildQueryString = ( |
| 85 | params: Record<string, QueryParamValue>, |
| 86 | ): string => { |
| 87 | const query = new URLSearchParams() |
| 88 | for (const [key, value] of Object.entries(params)) { |
| 89 | if (value === null || value === undefined) continue |
| 90 | query.append(key, String(value)) |
| 91 | } |
| 92 | const text = query.toString() |
| 93 | return text ? `?${text}` : '' |
| 94 | } |
| 95 |
| 96 | export const http = { |
| 97 | get: <T>(url: string, options?: RequestInit) => |
| 98 | request<T>(url, { ...options, method: 'GET' }), |
| 99 | getV2: <T>(url: string, options?: RequestInit) => |
| 100 | requestV2<T>(url, { ...options, method: 'GET' }), |
| 101 | postV2: <T>(url: string, body?: any, options?: RequestInit) => |
| 102 | requestV2<T>(url, { |
| 103 | ...options, |
| 104 | method: 'POST', |
| 105 | headers: { 'Content-Type': 'application/json', ...options?.headers }, |
| 106 | body: JSON.stringify(body), |
| 107 | }), |
| 108 | post: <T>(url: string, body?: any, options?: RequestInit) => |
| 109 | request<T>(url, { |
| 110 | ...options, |
| 111 | method: 'POST', |
| 112 | headers: { 'Content-Type': 'application/json', ...options?.headers }, |
| 113 | body: JSON.stringify(body), |
| 114 | }), |
| 115 | put: <T>(url: string, body?: any, options?: RequestInit) => |
| 116 | request<T>(url, { |
| 117 | ...options, |
| 118 | method: 'PUT', |
| 119 | headers: { 'Content-Type': 'application/json', ...options?.headers }, |
| 120 | body: JSON.stringify(body), |
| 121 | }), |
| 122 | delete: <T>(url: string, options?: RequestInit) => |
| 123 | request<T>(url, { ...options, method: 'DELETE' }), |
| 124 | } |