Skip to content

Commit a93892a

Browse files
authored
Implementation (#1)
* feat: implementation * fix: bundling * chore: staged changes * chore: staged changes * chore: metadata * docs: guide to use with frontend * chore: staged changes * feat: changeset * docs: readme
1 parent e55b64a commit a93892a

File tree

20 files changed

+901
-32
lines changed

20 files changed

+901
-32
lines changed

.changeset/chubby-poets-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"elysia-remote-dts": major
3+
---
4+
5+
Initial release

.github/workflows/pr.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: PR Checks
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build:
10+
name: Build
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: checkout
14+
uses: actions/checkout@v4
15+
- name: bun
16+
uses: oven-sh/setup-bun@v2
17+
with:
18+
bun-version: latest
19+
- name: install
20+
run: bun i
21+
- name: build
22+
run: bun run build
23+
- name: publish
24+
run: bunx pkg-pr-new publish

README.md

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,60 @@
1-
# quick-npm
1+
# Elysia remote DTS
22

3-
Quick start template to rapidly develop NPM packages with speed, and automations.
3+
A plugin that provide `.d.ts` types remotely for Eden Treaty to consume.
4+
5+
[![NPM Version](https://img.shields.io/npm/v/elysia-remote-dts)](https://www.npmjs.com/package/elysia-remote-dts)
6+
[![NPM Downloads](https://img.shields.io/npm/dw/elysia-remote-dts)](https://www.npmjs.com/package/elysia-remote-dts)
7+
[![NPM License](https://img.shields.io/npm/l/elysia-remote-dts)](https://www.npmjs.com/package/elysia-remote-dts)
8+
9+
Imagine in this scenario, you deploy an Elysia server remotely somewhere. And you also want to provide the benefit of end-to-end type safety by using [Eden Treaty](https://elysiajs.com/eden/overview#eden-treaty-recommended). But external developer may not have a direct access to source code to pull `typeof app` types out from your server maybe because.
10+
11+
- Your server is closed-source.
12+
- Frontend locate somewhere else that make types inaccessible.
13+
14+
This plugin will attempt to expose types remotely, and provide remote type to Eden Treaty to consume somehow.
15+
16+
> [!NOTE]
17+
> Part of the code that responsible for runtime type-generation is copied from project [rolldown-plugin-dts](https://github.com/sxzz/rolldown-plugin-dts), what difference is this `generateDts` utility is completely dependent, and decoupled from rolldown lifecycle. Full credit should go to them, I just port some functionality that hoped to be cool stuff on Elysia ecosystem.
418
519
## Install
620

7-
```bash
8-
bun add @rayriffy/quick-npm
21+
```
22+
bun add elysia-remote-dts
23+
```
24+
25+
## Usage
26+
27+
```ts
28+
import { Elysia } from 'elysia'
29+
import { dts } from 'elysia-remote-dts'
30+
31+
const app = new Elysia().use(dts('./src/index.ts')).listen(3000)
32+
33+
// Be sure to export type for plugin to consume as well.
34+
export type App = typeof app;
35+
```
36+
37+
Then types should be available at `/server.d.ts`.
38+
39+
Due to limitations with [Triple-Slash Directives](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html), types cannot be directly consumed from a remote URL ([tracking issue](https://github.com/microsoft/TypeScript/issues/28985)). For frontend projects, you'll need to first download the type declaration file from this path before using it with Eden.
40+
41+
```
42+
curl -o server.ts https://<remote-url>/server.d.ts
43+
```
44+
45+
```ts
46+
import { treaty } from '@elysiajs/eden'
47+
import type { App } from './server'
48+
49+
// frontend project should already have both elysia, and @elysiajs/eden installed
50+
export const app = treaty<App>('https://<remote-url>')
951
```
1052

11-
## Setting up
53+
## Configuration
1254

13-
1. Allow GitHub Actions to create pull request
14-
2. Generate NPM authotization token into `NPM_TOKEN` secret
55+
To be documented
1556

16-
## Publishing
57+
## Known Limitations
1758

18-
This repository has been configured to automatically publish NPM packages by [Changesets](https://github.com/changesets/changesets). Run `bun changeset` command to publishing your changes before commit.
59+
1. Sometimes emitting types can be `null`, this happens only in some runtime environment (So far, Distroless). I would recommended `oven/bun`, or `oven/bun:alpine` as base image.
60+
2. Be sure that `typescript` package is available when running. It's no longer `devDependencies`.

bun.lock

Lines changed: 90 additions & 2 deletions
Large diffs are not rendered by default.

example/.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env.local
29+
.env.development.local
30+
.env.test.local
31+
.env.production.local
32+
33+
# vercel
34+
.vercel
35+
36+
**/*.trace
37+
**/*.zip
38+
**/*.tar.gz
39+
**/*.tgz
40+
**/*.log
41+
package-lock.json
42+
**/*.bun

example/Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM oven/bun:alpine
2+
3+
WORKDIR /app
4+
5+
COPY package.json .
6+
COPY bun.lock .
7+
8+
RUN bun install
9+
10+
COPY src src
11+
COPY tsconfig.json .
12+
13+
ENV NODE_ENV production
14+
CMD ["bun", "src/index.ts"]
15+
16+
EXPOSE 3000

example/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Elysia with Bun runtime
2+
3+
## Getting Started
4+
To get started with this template, simply paste this command into your terminal:
5+
```bash
6+
bun create elysia ./elysia-example
7+
```
8+
9+
## Development
10+
To start the development server run:
11+
```bash
12+
bun run dev
13+
```
14+
15+
Open http://localhost:3000/ with your browser to see the result.

example/bun.lock

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"lockfileVersion": 1,
3+
"workspaces": {
4+
"": {
5+
"name": "example",
6+
"dependencies": {
7+
"elysia": "latest",
8+
"elysia-remote-dts": "https://pkg.pr.new/rayriffy/elysia-remote-dts@ff9f948",
9+
"typescript": "^5.8.3",
10+
},
11+
"devDependencies": {
12+
"bun-types": "latest",
13+
},
14+
},
15+
},
16+
"packages": {
17+
"@emnapi/core": ["@emnapi/[email protected]", "", { "dependencies": { "@emnapi/wasi-threads": "1.0.2", "tslib": "^2.4.0" } }, "sha512-4m62DuCE07lw01soJwPiBGC0nAww0Q+RY70VZ+n49yDIO13yyinhbWCeNnaob0lakDtWQzSdtNWzJeOJt2ma+g=="],
18+
19+
"@emnapi/runtime": ["@emnapi/[email protected]", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-pBPWdu6MLKROBX05wSNKcNb++m5Er+KQ9QkB+WVM+pW2Kx9hoSrVTnu3BdkI5eBLZoKu/J6mW/B6i6bJB2ytXQ=="],
20+
21+
"@emnapi/wasi-threads": ["@emnapi/[email protected]", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-5n3nTJblwRi8LlXkJ9eBzu+kZR8Yxcc7ubakyQTFzPMtIhFpUBRbsnc2Dv88IZDIbCDlBiWrknhB4Lsz7mg6BA=="],
22+
23+
"@napi-rs/wasm-runtime": ["@napi-rs/[email protected]", "", { "dependencies": { "@emnapi/core": "^1.4.0", "@emnapi/runtime": "^1.4.0", "@tybys/wasm-util": "^0.9.0" } }, "sha512-OKRBiajrrxB9ATokgEQoG87Z25c67pCpYcCwmXYX8PBftC9pBfN18gnm/fh1wurSLEKIAt+QRFLFCQISrb66Jg=="],
24+
25+
"@oxc-transform/binding-darwin-arm64": ["@oxc-transform/[email protected]", "", { "os": "darwin", "cpu": "arm64" }, "sha512-P3zBMhpOQceNSys3/ZqvrjuRvcIbVzfGFN/tH34HlVkOjOmfGK1mOWjORsGAZtbgh1muXrF6mQETLzFjfYndXQ=="],
26+
27+
"@oxc-transform/binding-darwin-x64": ["@oxc-transform/[email protected]", "", { "os": "darwin", "cpu": "x64" }, "sha512-B52aeo/C3spYHcwFQ4nAbDkwbMKf0K6ncWM8GrVUgGu8PPECLBhjPCW11kPW/lt9FxwrdgVYVzPYlZ6wmJmpEA=="],
28+
29+
"@oxc-transform/binding-linux-arm-gnueabihf": ["@oxc-transform/[email protected]", "", { "os": "linux", "cpu": "arm" }, "sha512-5Ir1eQrC9lvj/rR1TJVGwOR4yLgXTLmfKHIfpVH7GGSQrzK7VMUfHWX+dAsX1VutaeE8puXIqtYvf9cHLw78dw=="],
30+
31+
"@oxc-transform/binding-linux-arm64-gnu": ["@oxc-transform/[email protected]", "", { "os": "linux", "cpu": "arm64" }, "sha512-zTqfPET5+hZfJ3/dMqJboKxrpXMXk+j2HVdvX0wVhW2MI7n7hwELl+In6Yu20nXuEyJkNQlWHbNPCUfpM+cBWw=="],
32+
33+
"@oxc-transform/binding-linux-arm64-musl": ["@oxc-transform/[email protected]", "", { "os": "linux", "cpu": "arm64" }, "sha512-jzz/ATUhZ8wetb4gm5GwzheZns3Qj1CZ+DIMmD8nBxQXszmTS/fqnAPpgzruyLqkXBUuUfF3pHv5f/UmuHReuQ=="],
34+
35+
"@oxc-transform/binding-linux-x64-gnu": ["@oxc-transform/[email protected]", "", { "os": "linux", "cpu": "x64" }, "sha512-Qy2+tfglJ8yX6guC1EDAnuuzRZIXciXO9UwOewxyiahLxwuTpj/wvvZN3Cb1SA3c14zrwb2TNMZvaXS1/OS5Pg=="],
36+
37+
"@oxc-transform/binding-linux-x64-musl": ["@oxc-transform/[email protected]", "", { "os": "linux", "cpu": "x64" }, "sha512-tHoYgDIRhgvh+/wIrzAk3cUoj/LSSoJAdsZW9XRlaixFW/TF2puxRyaS1hRco0bcKTwotXl/eDYqZmhIfUyGRQ=="],
38+
39+
"@oxc-transform/binding-wasm32-wasi": ["@oxc-transform/[email protected]", "", { "dependencies": { "@napi-rs/wasm-runtime": "^0.2.9" }, "cpu": "none" }, "sha512-ZPT+1HECf7WUnotodIuS8tvSkwaiCdC2DDw8HVRmlerbS6iPYIPKyBCvkSM4RyUx0kljZtB9AciLCkEbwy5/zA=="],
40+
41+
"@oxc-transform/binding-win32-arm64-msvc": ["@oxc-transform/[email protected]", "", { "os": "win32", "cpu": "arm64" }, "sha512-+E3lOHCk4EuIk6IjshBAARknAUpgH+gHTtZxCPqK4AWYA+Tls2J6C0FVM48uZ4m3rZpAq8ZszM9JZVAkOaynBQ=="],
42+
43+
"@oxc-transform/binding-win32-x64-msvc": ["@oxc-transform/[email protected]", "", { "os": "win32", "cpu": "x64" }, "sha512-3pIIFb9g5aFrAODTQVJYitq+ONHgDJ4IYk/7pk+jsG6JpKUkURd0auUlxvriO11fFit5hdwy+wIbU4kBvyRUkg=="],
44+
45+
"@sinclair/typebox": ["@sinclair/[email protected]", "", {}, "sha512-5HAV9exOMcXRUxo+9iYB5n09XxzCXnfy4VTNW4xnDv+FgjzAGY989C28BIdljKqmF+ZltUwujE3aossvcVtq6g=="],
46+
47+
"@tybys/wasm-util": ["@tybys/[email protected]", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-6+7nlbMVX/PVDCwaIQ8nTOPveOcFLSt8GcXdx8hD0bt39uWxYT88uXzqTd4fTvqta7oeUJqudepapKNt2DYJFw=="],
48+
49+
"@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "~6.21.0" } }, "sha512-lX7HFZeHf4QG/J7tBZqrCAXwz9J5RD56Y6MpP0eJkka8p+K0RY/yBTW7CYFJ4VGCclxqOLKmiGP5juQc6MKgcw=="],
50+
51+
"bun-types": ["[email protected]", "", { "dependencies": { "@types/node": "*" } }, "sha512-dbkp5Lo8HDrXkLrONm6bk+yiiYQSntvFUzQp0v3pzTAsXk6FtgVMjdQ+lzFNVAmQFUkPQZ3WMZqH5tTo+Dp/IA=="],
52+
53+
"cookie": ["[email protected]", "", {}, "sha512-9Kr/j4O16ISv8zBBhJoi4bXOYNTkFLOqSL3UDB0njXxCXNezjeyVrJyGOWtgfs/q2km1gwBcfH8q1yEGoMYunA=="],
54+
55+
"debug": ["[email protected]", "", { "dependencies": { "ms": "^2.1.3" } }, "sha512-6WTZ/IxCY/T6BALoZHaE4ctp9xm+Z5kY/pzYaCHRFeyVhojxlrm+46y68HA6hr0TcwEssoxNiDEUJQjfPZ/RYA=="],
56+
57+
"elysia": ["[email protected]", "", { "dependencies": { "@sinclair/typebox": "^0.34.27", "cookie": "^1.0.2", "memoirist": "^0.3.0", "openapi-types": "^12.1.3" }, "peerDependencies": { "typescript": ">= 5.0.0" }, "optionalPeers": ["typescript"] }, "sha512-WsdQpORJvb4uszzeqYT0lg97knw1iBW1NTzJ1Jm57tiHg+DfAotlWXYbjmvQ039ssV0fYELDHinLLoUazZkEHg=="],
58+
59+
"elysia-remote-dts": ["elysia-remote-dts@https://pkg.pr.new/rayriffy/elysia-remote-dts@ff9f948", { "dependencies": { "debug": "^4.4.0", "get-tsconfig": "^4.10.0", "oxc-transform": "^0.67.0" } }],
60+
61+
"get-tsconfig": ["[email protected]", "", { "dependencies": { "resolve-pkg-maps": "^1.0.0" } }, "sha512-kGzZ3LWWQcGIAmg6iWvXn0ei6WDtV26wzHRMwDSzmAbcXrTEXxHy6IehI6/4eT6VRKyMP1eF1VqwrVUmE/LR7A=="],
62+
63+
"memoirist": ["[email protected]", "", {}, "sha512-wR+4chMgVPq+T6OOsk40u9Wlpw1Pjx66NMNiYxCQQ4EUJ7jDs3D9kTCeKdBOkvAiqXlHLVJlvYL01PvIJ1MPNg=="],
64+
65+
"ms": ["[email protected]", "", {}, "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="],
66+
67+
"openapi-types": ["[email protected]", "", {}, "sha512-N4YtSYJqghVu4iek2ZUvcN/0aqH1kRDuNqzcycDxhOUpg7GdvLa2F3DgS6yBNhInhv2r/6I0Flkn7CqL8+nIcw=="],
68+
69+
"oxc-transform": ["[email protected]", "", { "optionalDependencies": { "@oxc-transform/binding-darwin-arm64": "0.67.0", "@oxc-transform/binding-darwin-x64": "0.67.0", "@oxc-transform/binding-linux-arm-gnueabihf": "0.67.0", "@oxc-transform/binding-linux-arm64-gnu": "0.67.0", "@oxc-transform/binding-linux-arm64-musl": "0.67.0", "@oxc-transform/binding-linux-x64-gnu": "0.67.0", "@oxc-transform/binding-linux-x64-musl": "0.67.0", "@oxc-transform/binding-wasm32-wasi": "0.67.0", "@oxc-transform/binding-win32-arm64-msvc": "0.67.0", "@oxc-transform/binding-win32-x64-msvc": "0.67.0" } }, "sha512-QXwmpLfNrXZoHgIjEtDEf6lhwmvHouNtstNgg/UveczVIjo8VSzd5h25Ea96PoX9KzReJUY/qYa4QSNkJpZGfA=="],
70+
71+
"resolve-pkg-maps": ["[email protected]", "", {}, "sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw=="],
72+
73+
"tslib": ["[email protected]", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="],
74+
75+
"typescript": ["[email protected]", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ=="],
76+
77+
"undici-types": ["[email protected]", "", {}, "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ=="],
78+
}
79+
}

example/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "example",
3+
"version": "1.0.50",
4+
"scripts": {
5+
"test": "echo \"Error: no test specified\" && exit 1",
6+
"dev": "bun run --watch src/index.ts"
7+
},
8+
"dependencies": {
9+
"elysia": "latest",
10+
"elysia-remote-dts": "https://pkg.pr.new/rayriffy/elysia-remote-dts@ff9f948",
11+
"typescript": "^5.8.3"
12+
},
13+
"devDependencies": {
14+
"bun-types": "latest"
15+
},
16+
"module": "src/index.js"
17+
}

example/src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Elysia, t } from 'elysia'
2+
import { dts } from 'elysia-remote-dts'
3+
4+
const app = new Elysia()
5+
.use(dts('./src/index.ts'))
6+
.get('/', () => 'Hello Elysia')
7+
.post('/cow', ({ body }) => {
8+
return body.message
9+
}, {
10+
body: t.Object({
11+
message: t.String()
12+
})
13+
})
14+
.listen(3000)
15+
16+
export type App = typeof app
17+
18+
console.log(
19+
`🦊 Elysia is running at http://${app.server?.hostname}:${app.server?.port}`
20+
)

0 commit comments

Comments
 (0)