This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Replace axios with fetch and add timeout (#85)
Replace axios with fetch and add timeout
- Loading branch information
1 parent
20df5c2
commit 83c9ae2
Showing
18 changed files
with
194 additions
and
325 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,10 @@ | ||
import type { ApiError } from "./type.js"; | ||
|
||
export class CloudflareApiError extends Error { | ||
public errors: ApiError[]; | ||
public constructor(errors: ApiError[]) { | ||
const messages = errors.map(error => JSON.stringify(error)).join("\n"); | ||
super(`Api Error from Cloudflare. Errors: \n${messages}`); | ||
this.errors = errors; | ||
} | ||
} |
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,58 @@ | ||
import { URL, URLSearchParams } from "node:url"; | ||
import { CloudflareApiError } from "./error.js"; | ||
|
||
import type { ApiRequest, ApiResponse } from "./type.js"; | ||
|
||
const defaultBaseUrl = "https://api.cloudflare.com/client/v4/"; | ||
const defaultHeaders: Record<string, string> = { | ||
"Content-Type": "application/json" | ||
}; | ||
|
||
function getBaseUrl<TParam, TData, TArg>( | ||
req: ApiRequest<TParam, TData> & TArg | ||
): string { | ||
const { baseUrl } = req; | ||
return baseUrl ?? defaultBaseUrl; | ||
} | ||
|
||
export interface CreateApiOptions<TParam, TData, TArg> { | ||
path: string | ((req: ApiRequest<TParam, TData> & TArg) => string); | ||
method: string; | ||
} | ||
|
||
export function createApi< | ||
TResult, | ||
TParam = undefined, | ||
TData = undefined, | ||
TArg = Record<string, unknown> | ||
>( | ||
opts: CreateApiOptions<TParam, TData, TArg> | ||
): (req: ApiRequest<TParam, TData> & TArg) => Promise<ApiResponse<TResult>> { | ||
const { path: pathFn, method } = opts; | ||
return async req => { | ||
const { params = {}, headers = {}, data, auth } = req; | ||
const baseUrl = getBaseUrl<TParam, TData, TArg>(req); | ||
const path = typeof pathFn === "string" ? pathFn : pathFn(req); | ||
const url = new URL(path, baseUrl); | ||
url.search = new URLSearchParams(params).toString(); | ||
const authHeaders = { ...defaultHeaders }; | ||
if (auth) { | ||
authHeaders.Authorization = `Bearer ${auth.scopedToken}`; | ||
} | ||
const res = await fetch(url.toString(), { | ||
method, | ||
headers: { | ||
...authHeaders, | ||
...headers | ||
}, | ||
body: data ? JSON.stringify(data) : undefined, | ||
signal: AbortSignal.timeout(5000) | ||
}); | ||
if (res.status !== 200) { | ||
throw new CloudflareApiError(await res.json()); | ||
} | ||
return res.json(); | ||
}; | ||
} | ||
|
||
export * from "./type.js"; |
Oops, something went wrong.