|
| 1 | + |
| 2 | +import { ProxyAgent, setGlobalDispatcher } from 'undici' |
| 3 | + |
| 4 | +type Method = 'POST' | 'GET' | 'PUT' | 'DELETE' | 'post' | 'get' | 'put' | 'delete' |
| 5 | + |
| 6 | +type Options<OptionsT extends RequestInit = RequestInit> = OptionsT & { |
| 7 | + /* Set the query string of the request from an object */ |
| 8 | + query?: Record<string, any> |
| 9 | +} |
| 10 | + |
| 11 | +class FetchError extends Error { |
| 12 | + constructor( |
| 13 | + public response: Response, |
| 14 | + public data: Record<any, any> | undefined, |
| 15 | + ) { |
| 16 | + super() |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +export const isFetchError = (error: unknown): error is FetchError => { |
| 21 | + return error instanceof FetchError |
| 22 | +} |
| 23 | + |
| 24 | +export function getProxyUrl() { |
| 25 | + return ( |
| 26 | + process.env.HTTPS_PROXY || |
| 27 | + process.env.HTTP_PROXY || |
| 28 | + process.env.https_proxy || |
| 29 | + process.env.http_proxy |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Creates a ProxyAgent and sets it as the global dispatcher via unidici (which |
| 35 | + * affects fetch calls) if a proxy is set either in VS Code settings or as an |
| 36 | + * environment variable. |
| 37 | + */ |
| 38 | +const proxyUrl = getProxyUrl() |
| 39 | +const agent = proxyUrl ? new ProxyAgent({ uri: proxyUrl }) : undefined |
| 40 | +if (agent) { |
| 41 | + setGlobalDispatcher(agent) |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Makes a request to the Figma API. This is used by other functions to make |
| 46 | + * various types of requests. We return both the response object, and the data |
| 47 | + * parsed as JSON, to make it easier to work with the response. |
| 48 | + */ |
| 49 | +async function makeRequestInternal<ResponseT = unknown>( |
| 50 | + url: string, |
| 51 | + method: Method, |
| 52 | + options: Options = {}, |
| 53 | + body?: Record<any, any>, |
| 54 | +) { |
| 55 | + const urlObj = new URL(url) |
| 56 | + if (options?.query) { |
| 57 | + Object.entries(options.query).forEach(([key, value]) => { |
| 58 | + urlObj.searchParams.append(key, value as string) |
| 59 | + }) |
| 60 | + } |
| 61 | + url = urlObj.toString() |
| 62 | + |
| 63 | + if (body) { |
| 64 | + options.body = JSON.stringify(body) |
| 65 | + } |
| 66 | + |
| 67 | + const response = await fetch(url, { ...options, method }) |
| 68 | + if (!response.ok) { |
| 69 | + let data |
| 70 | + try { |
| 71 | + data = await response.json() |
| 72 | + } catch (e) { |
| 73 | + data = undefined |
| 74 | + } |
| 75 | + throw new FetchError(response, data) |
| 76 | + } |
| 77 | + |
| 78 | + const text = await response.text() |
| 79 | + const data = text ? (JSON.parse(text) as ResponseT) : ({} as ResponseT) |
| 80 | + |
| 81 | + return { response, data } |
| 82 | +} |
| 83 | + |
| 84 | +export const request = { |
| 85 | + get: <MetaT>(url: string, options: Options = {}) => { |
| 86 | + return makeRequestInternal<MetaT>(url, 'GET', options) |
| 87 | + }, |
| 88 | + post: <MetaT>(url: string, body: Record<any, any>, options: Options = {}) => { |
| 89 | + return makeRequestInternal<MetaT>(url, 'POST', options, body) |
| 90 | + }, |
| 91 | + put: <MetaT>(url: string, body: Record<any, any>, options: Options = {}) => { |
| 92 | + return makeRequestInternal<MetaT>(url, 'PUT', options, body) |
| 93 | + }, |
| 94 | + delete: <MetaT>(url: string, body?: Record<any, any>, options: Options = {}) => { |
| 95 | + return makeRequestInternal<MetaT>(url, 'DELETE', options, body) |
| 96 | + }, |
| 97 | +} |
0 commit comments