mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-02 07:01:30 -08:00
65 lines
1.5 KiB
TypeScript
65 lines
1.5 KiB
TypeScript
|
async function getAccessToken() {
|
||
|
return '';
|
||
|
}
|
||
|
|
||
|
export async function authenticatedFetch<T>(...args: Parameters<typeof fetch>): Promise<T> {
|
||
|
const accessToken = await getAccessToken();
|
||
|
|
||
|
const response = await fetch(args[0], {
|
||
|
...args[1],
|
||
|
mode: 'cors',
|
||
|
cache: 'no-cache',
|
||
|
headers: {
|
||
|
...(accessToken ? { authorization: `Bearer ${accessToken}` } : {}),
|
||
|
...args[1]?.headers,
|
||
|
},
|
||
|
});
|
||
|
|
||
|
return (await response.json()) as Promise<T>;
|
||
|
}
|
||
|
|
||
|
export async function get<T>(
|
||
|
url: string,
|
||
|
query: Record<string, string> = {},
|
||
|
options: RequestInit = {},
|
||
|
) {
|
||
|
let resolvedUrl = url;
|
||
|
if (Object.keys(query).length > 0) {
|
||
|
resolvedUrl = `${resolvedUrl}?${new URLSearchParams(query).toString()}`;
|
||
|
}
|
||
|
|
||
|
return authenticatedFetch<T>(resolvedUrl, { ...options, method: 'GET' });
|
||
|
}
|
||
|
|
||
|
export async function post<T>(url: string, body: object = {}, options: RequestInit = {}) {
|
||
|
return authenticatedFetch<T>(url, {
|
||
|
...options,
|
||
|
method: 'POST',
|
||
|
body: JSON.stringify(body),
|
||
|
});
|
||
|
}
|
||
|
|
||
|
export async function put<T>(url: string, body: object = {}, options: RequestInit = {}) {
|
||
|
return authenticatedFetch<T>(url, {
|
||
|
...options,
|
||
|
method: 'PUT',
|
||
|
body: JSON.stringify(body),
|
||
|
});
|
||
|
}
|
||
|
|
||
|
export async function patch<T>(url: string, body: object = {}, options: RequestInit = {}) {
|
||
|
return authenticatedFetch<T>(url, {
|
||
|
...options,
|
||
|
method: 'PATCH',
|
||
|
body: JSON.stringify(body),
|
||
|
});
|
||
|
}
|
||
|
|
||
|
export async function del<T>(url: string, body: object = {}, options: RequestInit = {}) {
|
||
|
return authenticatedFetch<T>(url, {
|
||
|
...options,
|
||
|
method: 'DELETE',
|
||
|
body: JSON.stringify(body),
|
||
|
});
|
||
|
}
|