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

Commit

Permalink
fix: Replace axios with fetch and add timeout (#85)
Browse files Browse the repository at this point in the history
Replace axios with fetch and add timeout
  • Loading branch information
joshuaavalon authored Jul 22, 2023
1 parent 20df5c2 commit 83c9ae2
Show file tree
Hide file tree
Showing 18 changed files with 194 additions and 325 deletions.
116 changes: 11 additions & 105 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"mocha": "^10.2.0",
"prettier": "^2.8.4",
"rimraf": "^4.4.0",
"typescript": "^5.0.2"
"typescript": "^5.1.6"
},
"dependencies": {
"dotenv": "16.0.3",
Expand Down
5 changes: 1 addition & 4 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,5 @@
"bugs": {
"url": "https://github.com/joshuaavalon/docker-cloudflare/issues"
},
"homepage": "https://github.com/joshuaavalon/docker-cloudflare/packages/api#readme",
"dependencies": {
"axios": "^1.3.4"
}
"homepage": "https://github.com/joshuaavalon/docker-cloudflare/packages/api#readme"
}
66 changes: 0 additions & 66 deletions packages/api/src/base.ts

This file was deleted.

10 changes: 10 additions & 0 deletions packages/api/src/create-api/error.ts
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;
}
}
58 changes: 58 additions & 0 deletions packages/api/src/create-api/index.ts
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";
Loading

0 comments on commit 83c9ae2

Please sign in to comment.