Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/team-openpm/cloudflare-basics
Just the basics and nothing more.
https://github.com/team-openpm/cloudflare-basics
Last synced: 3 months ago
JSON representation
Just the basics and nothing more.
- Host: GitHub
- URL: https://github.com/team-openpm/cloudflare-basics
- Owner: team-openpm
- License: mit
- Created: 2023-05-20T00:08:42.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-05-12T20:41:37.000Z (6 months ago)
- Last Synced: 2024-07-13T07:47:42.318Z (4 months ago)
- Language: TypeScript
- Homepage:
- Size: 177 KB
- Stars: 29
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cloudflare-basics
[![NPM version](https://img.shields.io/npm/v/cloudflare-basics?color=a1b858&label=)](https://www.npmjs.com/package/cloudflare-basics)
Just the basics and nothing more.
- Simple Router
- Simple request body parsing
- Simple zod validationSee also:
- [cloudflare-basics-ai-plugin](https://github.com/maccman/cloudflare-basics-ai-plugin) for ChatGPT plugins.
# Requirements
- Requires Node 18.x
- Technically built for Cloudflare Workers, but you can use it in Node.js too## Usage
```ts
import { Router, json } from 'cloudflare-basics'export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise {
const router = new Router()router.get('/', async ({ request }) => {
return new Response('Hello World!')
})router.post('/books', async ({ request }) => {
const data = await request.body<{ foo: string }>()return json({ data })
})router.get('/books/:id', async ({ params }) => {
const bookId = params?.idreturn json({ bookId })
})return (
router.handle(request, env, ctx) ??
new Response('Not Found', { status: 404 })
)
},
}
```## Zod Validation
```ts
const schema = z.object({
name: z.string(),
})type schemaType = z.infer
const MyRoute = withZod(schema, async (options) => {
console.log(options.data) //=> { name: 'test' }
return new Response('ok')
})router.post('/', MyRoute)
```## License
MIT