Socket
Book a DemoInstallSign in
Socket

cloudflare-basics

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cloudflare-basics

[![NPM version](https://img.shields.io/npm/v/cloudflare-basics?color=a1b858&label=)](https://www.npmjs.com/package/cloudflare-basics)

latest
Source
npmnpm
Version
0.0.9
Version published
Maintainers
1
Created
Source

cloudflare-basics

NPM version

Just the basics and nothing more.

  • Simple Router
  • Simple request body parsing
  • Simple zod validation

See also:

Requirements

  • Requires Node 18.x
  • Technically built for Cloudflare Workers, but you can use it in Node.js too

Usage

import { Router, json } from 'cloudflare-basics'

export default {
  async fetch(
    request: Request,
    env: Env,
    ctx: ExecutionContext
  ): Promise<Response> {
    const router = new Router<Env>()

    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?.id

      return json({ bookId })
    })

    return (
      router.handle(request, env, ctx) ??
      new Response('Not Found', { status: 404 })
    )
  },
}

Zod Validation

const schema = z.object({
  name: z.string(),
})

type schemaType = z.infer<typeof schema>

const MyRoute = withZod<Env, schemaType>(schema, async (options) => {
  console.log(options.data) //=> { name: 'test' }
  return new Response('ok')
})

router.post('/', MyRoute)

License

MIT

FAQs

Package last updated on 12 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts