Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@cloudflare/workers-types

Package Overview
Dependencies
Maintainers
35
Versions
129
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cloudflare/workers-types

TypeScript typings for Cloudflare Workers

  • 4.20241205.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
643K
increased by11.04%
Maintainers
35
Weekly downloads
 
Created

What is @cloudflare/workers-types?

@cloudflare/workers-types provides TypeScript type definitions for Cloudflare Workers, enabling developers to write type-safe code for Cloudflare's serverless platform.

What are @cloudflare/workers-types's main functionalities?

FetchEvent

The FetchEvent interface represents the event that is fired when a Fetch request is made. This allows you to intercept and handle HTTP requests.

addEventListener('fetch', (event) => {
  event.respondWith(handleRequest(event.request));
});

async function handleRequest(request: Request): Promise<Response> {
  return new Response('Hello, world!');
}

KVNamespace

KVNamespace provides type definitions for Cloudflare Workers KV, a key-value storage system. This allows you to interact with KV storage in a type-safe manner.

declare const MY_KV_NAMESPACE: KVNamespace;

async function handleRequest(request: Request): Promise<Response> {
  const value = await MY_KV_NAMESPACE.get('my-key');
  return new Response(value || 'Key not found');
}

DurableObjectNamespace

DurableObjectNamespace provides type definitions for Durable Objects, which are stateful objects that can be used to manage state across multiple requests.

declare const MY_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace;

class MyDurableObject {
  state: DurableObjectState;
  constructor(state: DurableObjectState) {
    this.state = state;
  }

  async fetch(request: Request): Promise<Response> {
    return new Response('Durable Object response');
  }
}

addEventListener('fetch', (event) => {
  event.respondWith(handleRequest(event.request));
});

async function handleRequest(request: Request): Promise<Response> {
  const id = MY_DURABLE_OBJECT_NAMESPACE.idFromName('object-name');
  const stub = MY_DURABLE_OBJECT_NAMESPACE.get(id);
  return stub.fetch(request);
}

Other packages similar to @cloudflare/workers-types

FAQs

Package last updated on 05 Dec 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc