diff --git a/guide/configuration.md b/guide/configuration.md index 29d6c80..2cef56a 100644 --- a/guide/configuration.md +++ b/guide/configuration.md @@ -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` @@ -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 @@ -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" diff --git a/packages/api/src/create-api/index.ts b/packages/api/src/create-api/index.ts index 6eda5d9..7d71522 100644 --- a/packages/api/src/create-api/index.ts +++ b/packages/api/src/create-api/index.ts @@ -31,7 +31,7 @@ export function createApi< ): (req: ApiRequest & TArg) => Promise> { const { path: pathFn, method } = opts; return async req => { - const { params = {}, headers = {}, data, auth } = req; + const { params = {}, headers = {}, data, auth, timeout } = req; const baseUrl = getBaseUrl(req); const path = typeof pathFn === "string" ? pathFn : pathFn(req); const url = new URL(joinUrl(baseUrl, path)); @@ -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(); diff --git a/packages/api/src/create-api/type.ts b/packages/api/src/create-api/type.ts index f248516..a3d40da 100644 --- a/packages/api/src/create-api/type.ts +++ b/packages/api/src/create-api/type.ts @@ -26,6 +26,7 @@ export type ApiRequest = { headers?: Record; data?: Record; auth?: Auth; + timeout: number; } & (TParam extends undefined ? { params?: ListParameter & TParam } : { params: ListParameter & TParam }) & diff --git a/packages/app/src/api.ts b/packages/app/src/api.ts index d328390..2e4dd65 100644 --- a/packages/app/src/api.ts +++ b/packages/app/src/api.ts @@ -21,13 +21,14 @@ const getZoneId = async (ctx: Context, record: Record): Promise => { 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) { @@ -54,13 +55,14 @@ const getDNSRecord = async ( record: Record, zoneId: string ): Promise => { - 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) { @@ -75,7 +77,7 @@ const update = async ( zoneId: string, dnsRecord: DNSRecord ): Promise => { - 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({ @@ -83,7 +85,8 @@ const update = async ( data: { content: record.ip, name, type, proxied, ttl }, zoneId, recordId, - baseUrl + baseUrl, + timeout }); const { success, errors, result } = res; if (!success || !result) { @@ -97,13 +100,14 @@ const create = async ( record: Record, zoneId: string ): Promise => { - 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) { diff --git a/packages/config/src/schema/config.ts b/packages/config/src/schema/config.ts index 2daef81..245f8e9 100644 --- a/packages/config/src/schema/config.ts +++ b/packages/config/src/schema/config.ts @@ -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."