diff --git a/client/Dockerfile b/client/Dockerfile index 48ad2cc..2333ed5 100644 --- a/client/Dockerfile +++ b/client/Dockerfile @@ -4,10 +4,12 @@ FROM node:20.18.1-alpine AS build WORKDIR /app COPY ./package*.json ./ +COPY ./common/package*.json ./common/ COPY ./client/package*.json ./client/ RUN npm ci COPY ./tsconfig.json ./ +COPY ./common ./common COPY ./client ./client RUN npm run build @@ -17,9 +19,11 @@ WORKDIR /app RUN apk add --no-cache tini COPY ./package*.json ./ +COPY ./common/package*.json ./common/ COPY ./client/package*.json ./client/ RUN npm ci --omit=dev +COPY --from=build /app/common/dist ./common/dist COPY --from=build /app/client/dist ./client/dist USER node diff --git a/client/src/client.ts b/client/src/client.ts index 1e4380a..7e51938 100644 --- a/client/src/client.ts +++ b/client/src/client.ts @@ -1,14 +1,11 @@ +import type { UpdateResponse } from '@meyfa/ddns-common' + export interface UpdateOptions { url: URL secret: string signal?: AbortSignal } -export interface UpdateResponse { - ip: string - modified: boolean -} - export async function update(options: UpdateOptions): Promise { const res = await fetch(options.url, { method: 'PUT', diff --git a/common/package.json b/common/package.json new file mode 100644 index 0000000..c877bdc --- /dev/null +++ b/common/package.json @@ -0,0 +1,11 @@ +{ + "private": true, + "name": "@meyfa/ddns-common", + "type": "module", + "main": "dist/main.js", + "types": "dist/main.d.ts", + "scripts": { + "build": "tsc", + "lint": "tsc --noEmit" + } +} diff --git a/common/src/main.ts b/common/src/main.ts new file mode 100644 index 0000000..49ecc2c --- /dev/null +++ b/common/src/main.ts @@ -0,0 +1,4 @@ +export interface UpdateResponse { + ip: string + modified: boolean +} diff --git a/common/tsconfig.json b/common/tsconfig.json new file mode 100644 index 0000000..5f23914 --- /dev/null +++ b/common/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "outDir": "./dist", + "sourceMap": true, + "declaration": true, + "noEmit": false + }, + "include": [ + "src" + ] +} diff --git a/package-lock.json b/package-lock.json index 734dabd..8705d0d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,6 +6,7 @@ "": { "name": "@meyfa/ddns", "workspaces": [ + "common", "client", "worker" ], @@ -27,6 +28,9 @@ "npm": ">=9" } }, + "common": { + "name": "@meyfa/ddns-common" + }, "node_modules/@cloudflare/kv-asset-handler": { "version": "0.3.4", "resolved": "https://registry.npmjs.org/@cloudflare/kv-asset-handler/-/kv-asset-handler-0.3.4.tgz", @@ -603,6 +607,10 @@ "resolved": "worker", "link": true }, + "node_modules/@meyfa/ddns-common": { + "resolved": "common", + "link": true + }, "node_modules/@types/node": { "version": "20.17.9", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.17.9.tgz", diff --git a/package.json b/package.json index 4adc2c5..b6b68a9 100644 --- a/package.json +++ b/package.json @@ -2,11 +2,12 @@ "private": true, "name": "@meyfa/ddns", "scripts": { - "lint": "npm run lint --workspaces", - "build": "npm run build --workspace=client", - "deploy": "npm run deploy --workspace=worker" + "lint": "npm run build --workspace=common && npm run lint --workspaces", + "build": "npm run build --workspace=common && npm run build --workspace=client", + "deploy": "npm run build --workspace=common && npm run deploy --workspace=worker" }, "workspaces": [ + "common", "client", "worker" ], diff --git a/worker/src/main.ts b/worker/src/main.ts index 7f1a01b..d6aa318 100644 --- a/worker/src/main.ts +++ b/worker/src/main.ts @@ -1,3 +1,5 @@ +import type { UpdateResponse } from '@meyfa/ddns-common' + interface Env { DDNS_SECRET?: string CLOUDFLARE_API_TOKEN?: string @@ -5,11 +7,6 @@ interface Env { CLOUDFLARE_RECORD_NAME?: string } -interface UpdateResponse { - ip: string - modified: boolean -} - const API = new URL('https://api.cloudflare.com/client/v4/') const RATE_LIMIT_MS = 30_000