Skip to main content

@rtorcato/cf-common

Interfaces​

CorsOptions​

Defined in: index.ts:31

CORS configuration. Defaults are permissive (*) — tighten origin in prod.

Properties​

origin?​

optional origin?: string

Defined in: index.ts:33

Access-Control-Allow-Origin. Default *.

methods?​

optional methods?: string

Defined in: index.ts:35

Access-Control-Allow-Methods. Default GET,POST,OPTIONS.

headers?​

optional headers?: string

Defined in: index.ts:37

Access-Control-Allow-Headers. Default Content-Type.

maxAge?​

optional maxAge?: number

Defined in: index.ts:39

Access-Control-Max-Age in seconds, for caching the preflight.

Functions​

error()​

error(status, message): Response

Defined in: index.ts:13

JSON error response with the shape { error }, matching errorResponse.

Parameters​

status​

number

message​

string

Returns​

Response


errorResponse()​

errorResponse(err): Response

Defined in: index.ts:22

Turn any thrown value into a Response. CloudflareError keeps its status/code; a 5xx (or non-exposed error) hides its message so internals don't leak. This is the catch block every Worker would otherwise copy.

Parameters​

err​

unknown

Returns​

Response


corsHeaders()​

corsHeaders(options?): Record<string, string>

Defined in: index.ts:43

Build the Access-Control-* headers for CorsOptions.

Parameters​

options?​

CorsOptions = {}

Returns​

Record<string, string>


withCors()​

withCors(res, options?): Response

Defined in: index.ts:54

Copy res with CORS headers added.

Parameters​

res​

Response

options?​

CorsOptions

Returns​

Response


preflight()​

preflight(options?): Response

Defined in: index.ts:61

A 204 preflight response for OPTIONS requests.

Parameters​

options?​

CorsOptions

Returns​

Response


defineFetch()​

defineFetch<Env>(handler): ExportedHandler<Env>

Defined in: index.ts:75

Wrap a fetch handler so any thrown error becomes a Response via errorResponse. Returns an ExportedHandler you can export default.

Example​

export default defineFetch((req, env) => {
const id = requireEnv(env, 'ID') // throws CloudflareError -> mapped to a Response
return Response.json({ id })
})

Type Parameters​

Env​

Env = unknown

Parameters​

handler​

(request, env, ctx) => Response | Promise<Response>

Returns​

ExportedHandler<Env>