https://github.com/msisdev/cf-worker-starter
A template for cloudflare worker
https://github.com/msisdev/cf-worker-starter
Last synced: 8 months ago
JSON representation
A template for cloudflare worker
- Host: GitHub
- URL: https://github.com/msisdev/cf-worker-starter
- Owner: msisdev
- License: mit
- Created: 2024-05-04T02:47:01.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-06-17T10:36:16.000Z (about 2 years ago)
- Last Synced: 2024-12-24T15:28:27.573Z (over 1 year ago)
- Language: TypeScript
- Size: 88.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cf-worker-starter
A simple opinionated template for cloudflare worker project.
Feel free to copy & modify.
## How to use
1. Download
```
$ gh repo clone boxcolli/cf-worker-starter
$ npm i
```
2. Update gitignore list (those are written as comment initially):
- `.dev.vars`
- `.env.vitest`
## Demo
- https://cf-worker-demo.boxcolli.com
- https://cf-worker-demo.boxcolli.com/v1/hello
- https://cf-worker-demo.boxcolli.com/v1/hello/John
## Notes
### Staging
There are two environments: default and 'pro'. Each time you add an element in `wrangler.toml`, you should configure both, like below:
```toml
# /wrangler.toml
...
[vars]
WHICH_ENV = "dev"
[env.pro.vars]
WHICH_ENV = "pro"
```
If you want to deploy, you run `$ npm run deploy`, which actually is:
```json
// package.json
{
"scripts": {
"deploy": "wrangler deploy -e pro",
},
}
```
About `Secrets`, configure:
- `.dev.vars` file for development
- `$ npx wrangler types` will detect it as well.
- `$ npx wrangler secret put -e pro` for production.
### CORS
I made two same router instances to deal with different stages. And they are statically configured. It won't affect the performance too much, probably.
```ts
// /src/router.ts
...
export default {
dev: getRouter('*'),
pro: getRouter('https://some.domain')
}
```
### Vitest
Since `@cloudflare/vitest-pool-workers` [cannot run test remotely](https://developers.cloudflare.com/workers/testing/vitest-integration/get-started/), I adopted just `vitest`. So you should `$ npm run dev` before `$ npm run test`.
I put an env file to detect the local test url.
```
// /.env.vitest
LOCAL_URL="http://localhost:8787"
```
This value is loaded dynamically with `vitest.setup.ts`.
```ts
// /vitest.setup.ts
export interface Config {
LOCAL_URL: string
}
const envFile = './.env.vitest'
dotenv.config({ path: envFile })
```
So I recommend you to put `.env.vitest` into gitignore list and modify it freely.
### Dependencies
```json
{
"devDependencies": {
"@cloudflare/workers-types": "^4.20240502.0",
"dotenv": "^16.4.5",
"typescript": "^5.0.4",
"vitest": "1.3.0",
"wrangler": "^3.0.0"
},
"dependencies": {
"http-status-codes": "^2.3.0",
"itty-router": "^5.0.17",
"pino": "^9.0.0"
}
}
```