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

Commit

Permalink
feat: Add timeout to configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaavalon committed Jul 27, 2023
1 parent 5e1f999 commit e549f87
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
10 changes: 10 additions & 0 deletions guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ Set to `json` for JSON log output.

Cloudflare v4 API endpoint.

### Timeout

- Env: `CF_DNS__TIMEOUT`
- File: `timeout`
- Default: 5000

Timeout for network request. Disable if timeout <= 0.

### Log Level

- Env: `CF_DNS__LOG_LEVEL`
Expand Down Expand Up @@ -174,6 +182,7 @@ See [echo parser](./echo-parser.md) for details.

```yaml
api: https://api.cloudflare.com/client/v4/
timeout: 5000
logLevel: info
auth:
scopedToken: QPExdfoNLwndJPDbt4nK1-yF1z_srC8D0m6-Gv_h
Expand Down Expand Up @@ -225,6 +234,7 @@ const formatter = (status, data) => {
module.exports = {
api: "https://api.cloudflare.com/client/v4/",
timeout: 5000,
logLevel: "info",
auth: {
scopedToken: "QPExdfoNLwndJPDbt4nK1-yF1z_srC8D0m6-Gv_h"
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/create-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function createApi<
): (req: ApiRequest<TParam, TData> & TArg) => Promise<ApiResponse<TResult>> {
const { path: pathFn, method } = opts;
return async req => {
const { params = {}, headers = {}, data, auth } = req;
const { params = {}, headers = {}, data, auth, timeout } = req;
const baseUrl = getBaseUrl<TParam, TData, TArg>(req);
const path = typeof pathFn === "string" ? pathFn : pathFn(req);
const url = new URL(joinUrl(baseUrl, path));
Expand All @@ -47,7 +47,7 @@ export function createApi<
...headers
},
body: data ? JSON.stringify(data) : undefined,
signal: AbortSignal.timeout(5000)
signal: timeout > 0 ? AbortSignal.timeout(timeout) : undefined
});
if (res.status !== 200) {
const json = await res.json();
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/create-api/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type ApiRequest<TParam, TData> = {
headers?: Record<string, string>;
data?: Record<string, unknown>;
auth?: Auth;
timeout: number;
} & (TParam extends undefined
? { params?: ListParameter & TParam }
: { params: ListParameter & TParam }) &
Expand Down
20 changes: 12 additions & 8 deletions packages/app/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ const getZoneId = async (ctx: Context, record: Record): Promise<string> => {
if (domain.zoneId) {
return domain.zoneId;
}
const { auth, api: baseURL } = ctx.config;
const { auth, api: baseURL, timeout } = ctx.config;
const { zoneName } = domain;
const name = zoneName ? zoneName : parseZoneName(domain.name);
const res = await listZones({
auth,
params: { name },
baseURL
baseURL,
timeout
});
const { success, errors, result } = res;
if (!success || !result) {
Expand All @@ -54,13 +55,14 @@ const getDNSRecord = async (
record: Record,
zoneId: string
): Promise<DNSRecord | undefined> => {
const { auth, api: baseUrl } = ctx.config;
const { auth, api: baseUrl, timeout } = ctx.config;
const { name, type } = record.domain;
const res = await listDNSRecords({
auth,
params: { name, type },
zoneId,
baseUrl
baseUrl,
timeout
});
const { success, errors, result } = res;
if (!success || !result) {
Expand All @@ -75,15 +77,16 @@ const update = async (
zoneId: string,
dnsRecord: DNSRecord
): Promise<DNSRecord> => {
const { auth, api: baseUrl } = ctx.config;
const { auth, api: baseUrl, timeout } = ctx.config;
const { name, type, proxied } = record.domain;
const { ttl, id: recordId } = dnsRecord;
const res = await updateDNSRecords({
auth,
data: { content: record.ip, name, type, proxied, ttl },
zoneId,
recordId,
baseUrl
baseUrl,
timeout
});
const { success, errors, result } = res;
if (!success || !result) {
Expand All @@ -97,13 +100,14 @@ const create = async (
record: Record,
zoneId: string
): Promise<DNSRecord> => {
const { auth, api: baseUrl } = ctx.config;
const { auth, api: baseUrl, timeout } = ctx.config;
const { name, type, proxied } = record.domain;
const res = await createDNSRecord({
auth,
data: { content: record.ip, name, type, proxied, ttl: 1 },
zoneId,
baseUrl
baseUrl,
timeout
});
const { success, errors, result } = res;
if (!success || !result) {
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/schema/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const configSchema = Type.Object(
'Cloudflare V4 API url. Default to "https://api.cloudflare.com/client/v4/".',
default: "https://api.cloudflare.com/client/v4/"
}),
timeout: Type.Number({ default: 5000 }),
auth: scopedAuthSchema,
domains: Type.Array(domainSchema, {
description: "List of domains to be updated."
Expand Down

0 comments on commit e549f87

Please sign in to comment.