Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Add "common" library for shared code #12

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
7 changes: 2 additions & 5 deletions client/src/client.ts
Original file line number Diff line number Diff line change
@@ -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<UpdateResponse> {
const res = await fetch(options.url, {
method: 'PUT',
Expand Down
11 changes: 11 additions & 0 deletions common/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
4 changes: 4 additions & 0 deletions common/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface UpdateResponse {
ip: string
modified: boolean
}
12 changes: 12 additions & 0 deletions common/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"sourceMap": true,
"declaration": true,
"noEmit": false
},
"include": [
"src"
]
}
8 changes: 8 additions & 0 deletions package-lock.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand Down
7 changes: 2 additions & 5 deletions worker/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type { UpdateResponse } from '@meyfa/ddns-common'

interface Env {
DDNS_SECRET?: string
CLOUDFLARE_API_TOKEN?: string
CLOUDFLARE_ZONE_ID?: string
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

Expand Down