https://github.com/yusukebe/my-first-js-rpc
My first JS RPC for Cloudflare Workers
https://github.com/yusukebe/my-first-js-rpc
Last synced: 9 months ago
JSON representation
My first JS RPC for Cloudflare Workers
- Host: GitHub
- URL: https://github.com/yusukebe/my-first-js-rpc
- Owner: yusukebe
- Created: 2024-04-08T11:04:08.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-08T11:34:23.000Z (about 2 years ago)
- Last Synced: 2025-07-31T19:19:49.833Z (11 months ago)
- Language: TypeScript
- Homepage:
- Size: 965 KB
- Stars: 11
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# My first JS RPC for Cloudflare Workers
This is my first project using [the PRC feature](https://developers.cloudflare.com/workers/runtime-apis/rpc/) for Cloudflare Workers.
## Features
- Minimal
- Monorepo with Yarn
- TypeScript Support
## Structure
```txt
.
├── package.json
├── tsconfig.json
└── workers
├── calc // A service to provide functions of a calculator
│ ├── package.json
│ ├── src
│ │ └── index.ts
│ ├── tsconfig.json
│ └── wrangler.toml
└── server // A server to use the calc service
├── package.json
├── src
│ └── index.ts
├── tsconfig.json
└── wrangler.toml
```
## Codes
`workers/calc/src/index.ts`:
```ts
import { WorkerEntrypoint } from 'cloudflare:workers'
export default class Calc extends WorkerEntrypoint {
add(a: number, b: number) {
return a + b
}
}
```
`workers/server/src/index.ts`:
```ts
import type Calc from 'calc'
import { Hono } from 'hono'
type Bindings = {
CALC: Service
}
const app = new Hono<{ Bindings: Bindings }>()
app.get('/', async (c) => {
const result = await c.env.CALC.add(1, 2)
return c.body(result.toString())
})
export default app
```
## How to try
```sh
yarn install
yarn calc:build
yarn dev
```
## References
- https://blog.cloudflare.com/javascript-native-rpc
- https://github.com/sor4chi/minimun-workers-service-bindings-rpc
## Author
Yusuke Wada
## License
MIT