Skip to content
This repository was archived by the owner on Jan 29, 2024. It is now read-only.

Commit 83c9ae2

Browse files
authored
fix: Replace axios with fetch and add timeout (#85)
Replace axios with fetch and add timeout
1 parent 20df5c2 commit 83c9ae2

18 files changed

+194
-325
lines changed

package-lock.json

Lines changed: 11 additions & 105 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"mocha": "^10.2.0",
4343
"prettier": "^2.8.4",
4444
"rimraf": "^4.4.0",
45-
"typescript": "^5.0.2"
45+
"typescript": "^5.1.6"
4646
},
4747
"dependencies": {
4848
"dotenv": "16.0.3",

packages/api/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,5 @@
2828
"bugs": {
2929
"url": "https://github.com/joshuaavalon/docker-cloudflare/issues"
3030
},
31-
"homepage": "https://github.com/joshuaavalon/docker-cloudflare/packages/api#readme",
32-
"dependencies": {
33-
"axios": "^1.3.4"
34-
}
31+
"homepage": "https://github.com/joshuaavalon/docker-cloudflare/packages/api#readme"
3532
}

packages/api/src/base.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

packages/api/src/create-api/error.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { ApiError } from "./type.js";
2+
3+
export class CloudflareApiError extends Error {
4+
public errors: ApiError[];
5+
public constructor(errors: ApiError[]) {
6+
const messages = errors.map(error => JSON.stringify(error)).join("\n");
7+
super(`Api Error from Cloudflare. Errors: \n${messages}`);
8+
this.errors = errors;
9+
}
10+
}

packages/api/src/create-api/index.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { URL, URLSearchParams } from "node:url";
2+
import { CloudflareApiError } from "./error.js";
3+
4+
import type { ApiRequest, ApiResponse } from "./type.js";
5+
6+
const defaultBaseUrl = "https://api.cloudflare.com/client/v4/";
7+
const defaultHeaders: Record<string, string> = {
8+
"Content-Type": "application/json"
9+
};
10+
11+
function getBaseUrl<TParam, TData, TArg>(
12+
req: ApiRequest<TParam, TData> & TArg
13+
): string {
14+
const { baseUrl } = req;
15+
return baseUrl ?? defaultBaseUrl;
16+
}
17+
18+
export interface CreateApiOptions<TParam, TData, TArg> {
19+
path: string | ((req: ApiRequest<TParam, TData> & TArg) => string);
20+
method: string;
21+
}
22+
23+
export function createApi<
24+
TResult,
25+
TParam = undefined,
26+
TData = undefined,
27+
TArg = Record<string, unknown>
28+
>(
29+
opts: CreateApiOptions<TParam, TData, TArg>
30+
): (req: ApiRequest<TParam, TData> & TArg) => Promise<ApiResponse<TResult>> {
31+
const { path: pathFn, method } = opts;
32+
return async req => {
33+
const { params = {}, headers = {}, data, auth } = req;
34+
const baseUrl = getBaseUrl<TParam, TData, TArg>(req);
35+
const path = typeof pathFn === "string" ? pathFn : pathFn(req);
36+
const url = new URL(path, baseUrl);
37+
url.search = new URLSearchParams(params).toString();
38+
const authHeaders = { ...defaultHeaders };
39+
if (auth) {
40+
authHeaders.Authorization = `Bearer ${auth.scopedToken}`;
41+
}
42+
const res = await fetch(url.toString(), {
43+
method,
44+
headers: {
45+
...authHeaders,
46+
...headers
47+
},
48+
body: data ? JSON.stringify(data) : undefined,
49+
signal: AbortSignal.timeout(5000)
50+
});
51+
if (res.status !== 200) {
52+
throw new CloudflareApiError(await res.json());
53+
}
54+
return res.json();
55+
};
56+
}
57+
58+
export * from "./type.js";

0 commit comments

Comments
 (0)