diff --git a/package-lock.json b/package-lock.json index 4be1c53..b077d3a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,7 +28,7 @@ "mocha": "^10.2.0", "prettier": "^2.8.4", "rimraf": "^4.4.0", - "typescript": "^5.0.2" + "typescript": "^5.1.6" } }, "node_modules/@babel/code-frame": { @@ -638,11 +638,6 @@ "node": "*" } }, - "node_modules/asynckit": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" - }, "node_modules/atomic-sleep": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/atomic-sleep/-/atomic-sleep-1.0.0.tgz", @@ -651,16 +646,6 @@ "node": ">=8.0.0" } }, - "node_modules/axios": { - "version": "1.3.4", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.3.4.tgz", - "integrity": "sha512-toYm+Bsyl6VC5wSkfkbbNB6ROv7KY93PEBBL6xyDczaIHasAiv4wPqQ/c4RjoQzipxRD2W5g21cOqQulZ7rHwQ==", - "dependencies": { - "follow-redirects": "^1.15.0", - "form-data": "^4.0.0", - "proxy-from-env": "^1.1.0" - } - }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -881,17 +866,6 @@ "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.19.tgz", "integrity": "sha512-3tlv/dIP7FWvj3BsbHrGLJ6l/oKh1O3TcgBqMn+yyCagOxc23fyzDS6HypQbgxWbkpDnf52p1LuR4eWDQ/K9WQ==" }, - "node_modules/combined-stream": { - "version": "1.0.8", - "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", - "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "dependencies": { - "delayed-stream": "~1.0.0" - }, - "engines": { - "node": ">= 0.8" - } - }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -984,14 +958,6 @@ "integrity": "sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==", "dev": true }, - "node_modules/delayed-stream": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", - "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", - "engines": { - "node": ">=0.4.0" - } - }, "node_modules/diff": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz", @@ -1465,38 +1431,6 @@ "integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==", "dev": true }, - "node_modules/follow-redirects": { - "version": "1.15.2", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", - "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==", - "funding": [ - { - "type": "individual", - "url": "https://github.com/sponsors/RubenVerborgh" - } - ], - "engines": { - "node": ">=4.0" - }, - "peerDependenciesMeta": { - "debug": { - "optional": true - } - } - }, - "node_modules/form-data": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", - "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", - "dependencies": { - "asynckit": "^0.4.0", - "combined-stream": "^1.0.8", - "mime-types": "^2.1.12" - }, - "engines": { - "node": ">= 6" - } - }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -1996,25 +1930,6 @@ "node": ">=8.6" } }, - "node_modules/mime-db": { - "version": "1.52.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", - "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", - "engines": { - "node": ">= 0.6" - } - }, - "node_modules/mime-types": { - "version": "2.1.35", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", - "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", - "dependencies": { - "mime-db": "1.52.0" - }, - "engines": { - "node": ">= 0.6" - } - }, "node_modules/minimatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", @@ -2335,9 +2250,9 @@ } }, "node_modules/pino": { - "version": "8.11.0", - "resolved": "https://registry.npmjs.org/pino/-/pino-8.11.0.tgz", - "integrity": "sha512-Z2eKSvlrl2rH8p5eveNUnTdd4AjJk8tAsLkHYZQKGHP4WTh2Gi1cOSOs3eWPqaj+niS3gj4UkoreoaWgF3ZWYg==", + "version": "8.14.0", + "resolved": "https://registry.npmjs.org/pino/-/pino-8.14.0.tgz", + "integrity": "sha512-Nxt00zDlFm7r3P9LprR6w7+b2OpjmLDwKbqLmFYcQNTLMaHvvTklTvL4gyZAXSN3PgXaLUpmc0Q5QyNir/LX3g==", "dependencies": { "atomic-sleep": "^1.0.0", "fast-redact": "^3.1.1", @@ -2442,11 +2357,6 @@ "resolved": "https://registry.npmjs.org/process-warning/-/process-warning-2.1.0.tgz", "integrity": "sha512-9C20RLxrZU/rFnxWncDkuF6O999NdIf3E1ws4B0ZeY3sRVPzWBMsYDE2lxjxhiXxg464cQTgKUGm8/i6y2YGXg==" }, - "node_modules/proxy-from-env": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", - "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" - }, "node_modules/pump": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", @@ -2903,16 +2813,16 @@ } }, "node_modules/typescript": { - "version": "5.0.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.2.tgz", - "integrity": "sha512-wVORMBGO/FAs/++blGNeAVdbNKtIh1rbBL2EyQ1+J9lClJ93KiiKe8PmFIVdXhHcyv44SL9oglmfeSsndo0jRw==", + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz", + "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==", "dev": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" }, "engines": { - "node": ">=12.20" + "node": ">=14.17" } }, "node_modules/uri-js": { @@ -3053,10 +2963,7 @@ "packages/api": { "name": "@cloudflare-ddns/api", "version": "1.0.0", - "license": "MIT", - "dependencies": { - "axios": "^1.3.4" - } + "license": "MIT" }, "packages/app": { "name": "@cloudflare-ddns/app", @@ -3070,9 +2977,8 @@ "@cloudflare-ddns/ip-echo-parser-json": "^1.0.0", "@cloudflare-ddns/ip-echo-parser-text": "^1.0.0", "@cloudflare-ddns/schema": "^1.0.0", - "axios": "^1.3.4", "lodash-es": "4.17.21", - "pino": "8.11.0" + "pino": "8.14.0" }, "devDependencies": { "@types/lodash-es": "^4.17.7" @@ -3087,7 +2993,7 @@ "@sinclair/typebox": "0.25.24", "cosmiconfig": "8.1.3", "lodash-es": "4.17.21", - "pino": "8.11.0", + "pino": "8.14.0", "read-env-vars": "1.0.0" }, "devDependencies": { diff --git a/package.json b/package.json index e76e0c0..bf17600 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/api/package.json b/packages/api/package.json index 9ada29c..32b0d92 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -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" } diff --git a/packages/api/src/base.ts b/packages/api/src/base.ts deleted file mode 100644 index 15296e0..0000000 --- a/packages/api/src/base.ts +++ /dev/null @@ -1,66 +0,0 @@ -import axios from "axios"; - -import type { AxiosRequestConfig, AxiosResponse } from "axios"; -import type { ApiResponse, ListParameter } from "./model.js"; - -export const axiosInstance = axios.create({ - baseURL: "https://api.cloudflare.com/client/v4/", - headers: { - "Content-Type": "application/json" - } -}); - -export interface GlobalAuth { - /** - * Cloudflare account email - */ - email: string; - /** - * Cloudflare API Key - */ - globalToken: string; -} - -export interface ScopedAuth { - /** - * Cloudflare API token - */ - scopedToken: string; -} - -export type Auth = GlobalAuth | ScopedAuth; - -const createAuthHeaders = (auth: Auth): Record => - "scopedToken" in auth - ? { Authorization: `Bearer ${auth.scopedToken}` } - : { "X-Auth-Email": auth.email, "X-Auth-Key": auth.globalToken }; - -export type ApiRequest = { - baseURL?: string; - auth: Auth; -} & (TParam extends undefined - ? { params?: ListParameter & TParam } - : { params: ListParameter & TParam }) & - (TData extends undefined ? { data?: TData } : { data: TData }); - -export const createRequestConfig = ( - req: ApiRequest -): AxiosRequestConfig => { - const { baseURL, params, auth } = req; - return { - baseURL, - params, - headers: createAuthHeaders(auth) - }; -}; - -export interface Api< - TResult, - TParam = undefined, - TData = undefined, - TArg = Record -> { - (req: ApiRequest & TArg): Promise< - AxiosResponse> - >; -} diff --git a/packages/api/src/create-api/error.ts b/packages/api/src/create-api/error.ts new file mode 100644 index 0000000..8104e13 --- /dev/null +++ b/packages/api/src/create-api/error.ts @@ -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; + } +} diff --git a/packages/api/src/create-api/index.ts b/packages/api/src/create-api/index.ts new file mode 100644 index 0000000..71d2d02 --- /dev/null +++ b/packages/api/src/create-api/index.ts @@ -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 = { + "Content-Type": "application/json" +}; + +function getBaseUrl( + req: ApiRequest & TArg +): string { + const { baseUrl } = req; + return baseUrl ?? defaultBaseUrl; +} + +export interface CreateApiOptions { + path: string | ((req: ApiRequest & TArg) => string); + method: string; +} + +export function createApi< + TResult, + TParam = undefined, + TData = undefined, + TArg = Record +>( + opts: CreateApiOptions +): (req: ApiRequest & TArg) => Promise> { + const { path: pathFn, method } = opts; + return async req => { + const { params = {}, headers = {}, data, auth } = req; + const baseUrl = getBaseUrl(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"; diff --git a/packages/api/src/create-api/type.ts b/packages/api/src/create-api/type.ts new file mode 100644 index 0000000..f248516 --- /dev/null +++ b/packages/api/src/create-api/type.ts @@ -0,0 +1,39 @@ +export interface ApiResponse { + success: boolean; + errors: ApiError[]; + messages: unknown[]; + result: T | null; +} + +export interface ApiError { + code: number; + message: string; +} + +export interface ListParameter { + match?: string; + name?: string; + order?: string; + page?: number; + // eslint-disable-next-line @typescript-eslint/naming-convention + per_page?: number; + direction?: "asc" | "desc"; +} + +export type ApiRequest = { + baseUrl?: string; + params?: Record; + headers?: Record; + data?: Record; + auth?: Auth; +} & (TParam extends undefined + ? { params?: ListParameter & TParam } + : { params: ListParameter & TParam }) & + (TData extends undefined ? { data?: TData } : { data: TData }); + +export interface Auth { + /** + * Cloudflare API token + */ + scopedToken: string; +} diff --git a/packages/api/src/create-dns-record.ts b/packages/api/src/create-dns-record.ts index 1510b39..9aebcf1 100644 --- a/packages/api/src/create-dns-record.ts +++ b/packages/api/src/create-dns-record.ts @@ -1,6 +1,4 @@ -import { axiosInstance, createRequestConfig } from "./base.js"; - -import type { Api } from "./base.js"; +import { createApi } from "./create-api/index.js"; interface Argument { zoneId: string; @@ -30,11 +28,7 @@ interface Result { modified_on: string; } -export const createDNSRecord: Api = req => { - const { zoneId, data, ...others } = req; - return axiosInstance.post( - `/zones/${zoneId}/dns_records`, - data, - createRequestConfig(others) - ); -}; +export const createDNSRecord = createApi({ + path: req => `/zones/${req.zoneId}/dns_records`, + method: "POST" +}); diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts index 06d540a..10fa226 100644 --- a/packages/api/src/index.ts +++ b/packages/api/src/index.ts @@ -2,5 +2,4 @@ export * from "./list-zones.js"; export * from "./list-dns-records.js"; export * from "./update-dns-records.js"; export * from "./create-dns-record.js"; -export * from "./model.js"; -export * from "./base.js"; +export * from "./create-api/index.js"; diff --git a/packages/api/src/list-dns-records.ts b/packages/api/src/list-dns-records.ts index 0df0b72..8f4bc57 100644 --- a/packages/api/src/list-dns-records.ts +++ b/packages/api/src/list-dns-records.ts @@ -1,6 +1,4 @@ -import { axiosInstance, createRequestConfig } from "./base.js"; - -import type { Api } from "./base.js"; +import { createApi } from "./create-api/index.js"; interface Argument { zoneId: string; @@ -33,15 +31,12 @@ interface Result { modified_on: string; } -export const listDNSRecords: Api< +export const listDNSRecords = createApi< Result[], Parameter | undefined, undefined, Argument -> = req => { - const { zoneId, ...others } = req; - return axiosInstance.get( - `/zones/${zoneId}/dns_records`, - createRequestConfig(others) - ); -}; +>({ + path: req => `/zones/${req.zoneId}/dns_records`, + method: "GET" +}); diff --git a/packages/api/src/list-zones.ts b/packages/api/src/list-zones.ts index c3efffd..a29d8be 100644 --- a/packages/api/src/list-zones.ts +++ b/packages/api/src/list-zones.ts @@ -1,6 +1,4 @@ -import { axiosInstance, createRequestConfig } from "./base.js"; - -import type { Api } from "./base.js"; +import { createApi } from "./create-api/index.js"; interface Parameter { match?: "any" | "all"; @@ -51,5 +49,7 @@ interface Result { name_servers: string[]; } -export const listZones: Api = req => - axiosInstance.get("/zones", createRequestConfig(req)); +export const listZones = createApi({ + path: "/zones", + method: "GET" +}); diff --git a/packages/api/src/model.ts b/packages/api/src/model.ts deleted file mode 100644 index 597da81..0000000 --- a/packages/api/src/model.ts +++ /dev/null @@ -1,20 +0,0 @@ -export interface ApiResponse { - success: boolean; - errors: ApiError[]; - messages: unknown[]; - result: T | null; -} - -export interface ApiError { - code: number; - message: string; -} - -export interface ListParameter { - match?: string; - name?: string; - order?: string; - page?: number; - per_page?: number; - direction?: "asc" | "desc"; -} diff --git a/packages/api/src/update-dns-records.ts b/packages/api/src/update-dns-records.ts index 34b3fa0..0b3d97c 100644 --- a/packages/api/src/update-dns-records.ts +++ b/packages/api/src/update-dns-records.ts @@ -1,6 +1,4 @@ -import { axiosInstance, createRequestConfig } from "./base.js"; - -import type { Api } from "./base.js"; +import { createApi } from "./create-api/index.js"; interface Argument { zoneId: string; @@ -30,11 +28,7 @@ interface Result { modified_on: string; } -export const updateDNSRecords: Api = req => { - const { zoneId, recordId, data, ...others } = req; - return axiosInstance.put( - `/zones/${zoneId}/dns_records/${recordId}`, - data, - createRequestConfig(others) - ); -}; +export const updateDNSRecords = createApi({ + path: req => `/zones/${req.zoneId}/dns_records/${req.recordId}`, + method: "POST" +}); diff --git a/packages/app/package.json b/packages/app/package.json index 78d0fd4..41ca1dc 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -37,9 +37,8 @@ "@cloudflare-ddns/ip-echo-parser-json": "^1.0.0", "@cloudflare-ddns/ip-echo-parser-text": "^1.0.0", "@cloudflare-ddns/schema": "^1.0.0", - "axios": "^1.3.4", "lodash-es": "4.17.21", - "pino": "8.11.0" + "pino": "8.14.0" }, "devDependencies": { "@types/lodash-es": "^4.17.7" diff --git a/packages/app/src/api.ts b/packages/app/src/api.ts index 9eed9ab..832251f 100644 --- a/packages/app/src/api.ts +++ b/packages/app/src/api.ts @@ -6,7 +6,7 @@ import { } from "@cloudflare-ddns/api"; import { first } from "lodash-es"; import { parseZoneName } from "@cloudflare-ddns/config"; -import { CloudflareApiError, CloudflareError, wrapError } from "./error.js"; +import { CloudflareApiError, CloudflareError } from "./error.js"; import type { Domain } from "@cloudflare-ddns/config"; import type { Context } from "./context.js"; @@ -24,24 +24,20 @@ const getZoneId = async (ctx: Context, record: Record): Promise => { const { auth, api: baseURL } = ctx.config; const { zoneName } = domain; const name = zoneName ? zoneName : parseZoneName(domain.name); - try { - const res = await listZones({ - auth, - params: { name }, - baseURL - }); - const { success, errors, result } = res.data; - if (!success || !result) { - throw new CloudflareApiError(errors); - } - const zoneId = first(result)?.id; - if (!zoneId) { - throw new CloudflareError("No match zone found"); - } - return zoneId; - } catch (e) { - throw wrapError(e); + const res = await listZones({ + auth, + params: { name }, + baseURL + }); + const { success, errors, result } = res; + if (!success || !result) { + throw new CloudflareApiError(errors); + } + const zoneId = first(result)?.id; + if (!zoneId) { + throw new CloudflareError("No match zone found"); } + return zoneId; }; interface DNSRecord { @@ -58,23 +54,19 @@ const getDNSRecord = async ( record: Record, zoneId: string ): Promise => { - const { auth, api: baseURL } = ctx.config; + const { auth, api: baseUrl } = ctx.config; const { name, type } = record.domain; - try { - const res = await listDNSRecords({ - auth, - params: { name, type }, - zoneId, - baseURL - }); - const { success, errors, result } = res.data; - if (!success || !result) { - throw new CloudflareApiError(errors); - } - return first(result); - } catch (e) { - throw wrapError(e); + const res = await listDNSRecords({ + auth, + params: { name, type }, + zoneId, + baseUrl + }); + const { success, errors, result } = res; + if (!success || !result) { + throw new CloudflareApiError(errors); } + return first(result); }; const update = async ( @@ -83,25 +75,21 @@ const update = async ( zoneId: string, dnsRecord: DNSRecord ): Promise => { - const { auth, api: baseURL } = ctx.config; + const { auth, api: baseUrl } = ctx.config; const { name, type, proxied } = record.domain; const { ttl, id: recordId } = dnsRecord; - try { - const res = await updateDNSRecords({ - auth, - data: { content: record.ip, name, type, proxied, ttl }, - zoneId, - recordId, - baseURL - }); - const { success, errors, result } = res.data; - if (!success || !result) { - throw new CloudflareApiError(errors); - } - return result; - } catch (e) { - throw wrapError(e); + const res = await updateDNSRecords({ + auth, + data: { content: record.ip, name, type, proxied, ttl }, + zoneId, + recordId, + baseUrl + }); + const { success, errors, result } = res; + if (!success || !result) { + throw new CloudflareApiError(errors); } + return result; }; const create = async ( @@ -109,23 +97,19 @@ const create = async ( record: Record, zoneId: string ): Promise => { - const { auth, api: baseURL } = ctx.config; + const { auth, api: baseUrl } = ctx.config; const { name, type, proxied } = record.domain; - try { - const res = await createDNSRecord({ - auth, - data: { content: record.ip, name, type, proxied, ttl: 1 }, - zoneId, - baseURL - }); - const { success, errors, result } = res.data; - if (!success || !result) { - throw new CloudflareApiError(errors); - } - return result; - } catch (e) { - throw wrapError(e); + const res = await createDNSRecord({ + auth, + data: { content: record.ip, name, type, proxied, ttl: 1 }, + zoneId, + baseUrl + }); + const { success, errors, result } = res; + if (!success || !result) { + throw new CloudflareApiError(errors); } + return result; }; const updateOrCreate = async ( diff --git a/packages/app/src/error.ts b/packages/app/src/error.ts index 788719d..32699cd 100644 --- a/packages/app/src/error.ts +++ b/packages/app/src/error.ts @@ -1,8 +1,4 @@ import type { ApiError } from "@cloudflare-ddns/api"; -import type { AxiosError } from "axios"; - -const isAxiosError = (error: any): error is AxiosError => - error.isAxiosError === true; export class CloudflareError extends Error {} @@ -14,18 +10,3 @@ export class CloudflareApiError extends CloudflareError { this.errors = errors; } } - -export const wrapError = (e: unknown): unknown => { - if (!isAxiosError(e)) { - return e; - } - const res = e.response; - if (!res) { - return e; - } - const { success, errors, result } = res.data as any; - if (!success || !result) { - return new CloudflareApiError(errors); - } - return e; -}; diff --git a/packages/app/src/index.ts b/packages/app/src/index.ts index 5e01a59..7604fc2 100644 --- a/packages/app/src/index.ts +++ b/packages/app/src/index.ts @@ -1,5 +1,4 @@ import { get, omit } from "lodash-es"; -import axios from "axios"; import pino from "pino"; import { readConfig } from "@cloudflare-ddns/config"; import { registerParser } from "@cloudflare-ddns/ip-echo-parser"; @@ -33,9 +32,9 @@ const requestWebhook = async ( const { logger } = ctx; try { if (data) { - await axios.post(url, data); + await fetch(url, { method: "POST", body: JSON.stringify(data) }); } else { - await axios.get(url); + await fetch(url, { method: "GET" }); } } catch (e) { logger.warn(`Fail to fetch ${url}.\n${get(e, "message", e)}`); diff --git a/packages/config/package.json b/packages/config/package.json index c21b0ac..c44f0d6 100644 --- a/packages/config/package.json +++ b/packages/config/package.json @@ -50,7 +50,7 @@ "@sinclair/typebox": "0.25.24", "cosmiconfig": "8.1.3", "lodash-es": "4.17.21", - "pino": "8.11.0", + "pino": "8.14.0", "read-env-vars": "1.0.0" } }