Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tenapato/cfw-ts-apiserver-template
Cloudflare workers TS api boilerplate using subrouting
https://github.com/tenapato/cfw-ts-apiserver-template
api boilerplate cloudflare cloudflare-workers rest-api router routing subrouting template templates typescript
Last synced: 3 days ago
JSON representation
Cloudflare workers TS api boilerplate using subrouting
- Host: GitHub
- URL: https://github.com/tenapato/cfw-ts-apiserver-template
- Owner: tenapato
- License: mit
- Created: 2022-09-12T01:38:30.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-09-12T01:44:10.000Z (over 2 years ago)
- Last Synced: 2023-03-10T07:22:22.150Z (almost 2 years ago)
- Topics: api, boilerplate, cloudflare, cloudflare-workers, rest-api, router, routing, subrouting, template, templates, typescript
- Language: TypeScript
- Homepage:
- Size: 113 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CloudFlare Workers Typescript REST API Boilerplate
> Author: Patricio TenaThis is a REST API boilerplate for CloudFlare Workers using Typescript. This template uses subrouting to give a better organization for each hanlder and its respective functions.
## Parent Router
First you create a `parent` router as follows:
```ts
const parent = Router()parent
.all('/v1/handler*', handlerRoutes.handle)
.all('*', () => missing('Root API could not find that endpoint.'))
```As you can see in the example above, the parent router has a handler for each hanlder subrouter.
I personally create each subrouter routes in the `/routes` folder, then export it to the `handler.ts`.## Child Routers
Each child router must have a base defined:
```ts
const v1 = Router({ base: '/v1' })
```After that you can use it as a regular router:
```ts
import {handleFetch} from '../handlers/handler'
v1
.get('/handler/test', () => new Response("Testing!", { status: 200 }))
.get('/handler',handleFetch)
.all('*', () => missing('API v1/handler could not find that endpoint.'))export default v1
```
> The handleFecth function is imported from the `./handlers` folder.## Handlers
A basic structure for a function hanlder can look something like this:
```ts
export async function handleFetch(event: Event){
try{
return new Response(JSON.stringify({
status: 200,
message: 'Ok',}), {
status: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
}
})
}catch(err){
console.log(err);
}
}
```