fetch routes
Make sure to create a api
directory for all your route files.
e.g. /api/hello.ts
import { Get } from 'darkflare'
Get({}, c => {
// environment variables
const variables = c.env
// https://developers.cloudflare.com/workers/runtime-apis/scheduled-event/#methods
c.waitUntil(async () => {})
// pass data between hooks by storing data in the c.data object
const data = c.data
// get a json object or string for a error message or code based on the accept header of the incoming request
return c.error('Access Denied') // { code: 403, message: 'Access Denied' } | 'Access Denied'
return c.error(403) // { code: 403, message: 'Access Denied' } | 'Access Denied'
// send a email using mailchannels
await c.mail(c, {
subject: 'Hello',
message: 'Hello',
from: {
name: 'John Doe',
email: 'john@doma.in',
},
to: 'jane@doma.in',
wait: true // wait until it's sent - disabled by default
})
c.req.raw() // untouched Request object
c.req.body // parsed request body, validated by your specified schema
c.req.query // parsed request query parameters, validated by your specified schema
c.req.headers // parsed request headers, validated by your specified schema
c.req.cookies // parsed request cookies, validated by your specified schema
c.req.parameters // request parameters, validated by your specified schema
// geolocation data
console.log(c.req.geo)
/*
{
ip
city
region
country
continent
regionCode
latitude
longitude
postalCode
timezone
datacenter
}
*/
// parse the request body (without validation)
const arrayBuffer = await c.req.buffer()
const blob = await c.req.blob()
const formData = await c.req.formData()
const readableStream = c.req.stream()
// parse the user agent from the fetch context
const userAgent = c.req.userAgent()
// set the status code of the response
c.res.code(200)
// attach a cookie to the response
c.res.cookie('name', 'value')
// add a header to the response
c.res.header('name', 'value')
// set the payload of the response to a HTML string
c.res.html('<h1>Hello World</h1>')
// set the payload of the response to a JSON object
c.res.json({})
return {} // alternatively, you can just return it
// set the payload of the response to a string
c.res.text('Hello World')
return 'Hello World' // alternatively, you can just return it
// set the payload of the response to a FormData
c.res.formData(new FormData())
return new FormData() // alternatively, you can just return it
// set the payload of the response to a Blob
c.res.blob(new Blob())
return new Blob() // alternatively, you can just return it
// set the payload of the response to a Buffer
c.res.buffer(arrayBuffer)
return arrayBuffer // alternatively, you can just return it
// set the payload of the response to a ReadableStream
c.res.stream(readableStream)
// redirect the incoming request
c.res.redirect('https://google.com', 301) // 307 (temporary) redirect by default
})
Types
Basic Routes
/api/example.ts
→/example
/api/a.ts
→/a
/api/b.ts
→/b
Index Routes
/api/mod.ts
→/
/api/example/mod.ts
→/example
Nested Routes
/api/a/b.ts
→/a/b
/api/a/b/c/d.ts
→/a/b/c/d
Dynamic Routes
/api/[example].ts
→/:example
Methods
Get
Post
Put
Patch
Delete
Head