-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
figma-bot
committed
Oct 23, 2024
1 parent
659795a
commit 60b9be4
Showing
45 changed files
with
437 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
|
||
import { ProxyAgent, setGlobalDispatcher } from 'undici' | ||
|
||
type Method = 'POST' | 'GET' | 'PUT' | 'DELETE' | 'post' | 'get' | 'put' | 'delete' | ||
|
||
type Options<OptionsT extends RequestInit = RequestInit> = OptionsT & { | ||
/* Set the query string of the request from an object */ | ||
query?: Record<string, any> | ||
} | ||
|
||
class FetchError extends Error { | ||
constructor( | ||
public response: Response, | ||
public data: Record<any, any> | undefined, | ||
) { | ||
super() | ||
} | ||
} | ||
|
||
export const isFetchError = (error: unknown): error is FetchError => { | ||
return error instanceof FetchError | ||
} | ||
|
||
export function getProxyUrl() { | ||
return ( | ||
process.env.HTTPS_PROXY || | ||
process.env.HTTP_PROXY || | ||
process.env.https_proxy || | ||
process.env.http_proxy | ||
) | ||
} | ||
|
||
/** | ||
* Creates a ProxyAgent and sets it as the global dispatcher via unidici (which | ||
* affects fetch calls) if a proxy is set either in VS Code settings or as an | ||
* environment variable. | ||
*/ | ||
const proxyUrl = getProxyUrl() | ||
const agent = proxyUrl ? new ProxyAgent({ uri: proxyUrl }) : undefined | ||
if (agent) { | ||
setGlobalDispatcher(agent) | ||
} | ||
|
||
/** | ||
* Makes a request to the Figma API. This is used by other functions to make | ||
* various types of requests. We return both the response object, and the data | ||
* parsed as JSON, to make it easier to work with the response. | ||
*/ | ||
async function makeRequestInternal<ResponseT = unknown>( | ||
url: string, | ||
method: Method, | ||
options: Options = {}, | ||
body?: Record<any, any>, | ||
) { | ||
const urlObj = new URL(url) | ||
if (options?.query) { | ||
Object.entries(options.query).forEach(([key, value]) => { | ||
urlObj.searchParams.append(key, value as string) | ||
}) | ||
} | ||
url = urlObj.toString() | ||
|
||
if (body) { | ||
options.body = JSON.stringify(body) | ||
} | ||
|
||
const response = await fetch(url, { ...options, method }) | ||
if (!response.ok) { | ||
let data | ||
try { | ||
data = await response.json() | ||
} catch (e) { | ||
data = undefined | ||
} | ||
throw new FetchError(response, data) | ||
} | ||
|
||
const text = await response.text() | ||
const data = text ? (JSON.parse(text) as ResponseT) : ({} as ResponseT) | ||
|
||
return { response, data } | ||
} | ||
|
||
export const request = { | ||
get: <MetaT>(url: string, options: Options = {}) => { | ||
return makeRequestInternal<MetaT>(url, 'GET', options) | ||
}, | ||
post: <MetaT>(url: string, body: Record<any, any>, options: Options = {}) => { | ||
return makeRequestInternal<MetaT>(url, 'POST', options, body) | ||
}, | ||
put: <MetaT>(url: string, body: Record<any, any>, options: Options = {}) => { | ||
return makeRequestInternal<MetaT>(url, 'PUT', options, body) | ||
}, | ||
delete: <MetaT>(url: string, body?: Record<any, any>, options: Options = {}) => { | ||
return makeRequestInternal<MetaT>(url, 'DELETE', options, body) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.