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
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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)