Skip to main content

@rtorcato/cf-common

Interfaces​

SchemaLike​

Defined in: index.ts:28

The minimal shape parseJson needs from a schema: a parse that returns the validated value or throws. A Zod schema (z.object({...})) satisfies this as is, as does any validator exposing the same method.

Type Parameters​

T​

T

Methods​

parse()​

parse(input): T

Defined in: index.ts:29

Parameters​
input​

unknown

Returns​

T

Functions​

cf()​

cf(request): IncomingRequestCfProperties<unknown> | undefined

Defined in: index.ts:17

Cloudflare's per-request metadata (country, colo, tlsVersion, …). undefined when unavailable — e.g. wrangler dev without --remote, or a synthetic Request in a test.

Example​

const country = cf(request)?.country ?? 'unknown'

Parameters​

request​

Request

Returns​

IncomingRequestCfProperties<unknown> | undefined


parseJson()​

parseJson<T>(request, schema): Promise<T>

Defined in: index.ts:42

Read and validate a JSON request body. Reuses the caller's schema (bring your own Zod schema) and turns both malformed JSON and a failed validation into an exposed 400 CloudflareError, so defineFetch maps it to a clean 400 without leaking internals.

Example​

const body = await parseJson(request, z.object({ email: z.string().email() }))
// ^? { email: string }

Type Parameters​

T​

T

Parameters​

request​

Request

schema​

SchemaLike<T>

Returns​

Promise<T>


bearerToken()​

bearerToken(request): string | null

Defined in: index.ts:66

Extract the token from an Authorization: Bearer <token> header, or null if the header is absent or not a well-formed bearer credential. Case-insensitive on the Bearer scheme, per RFC 6750.

Example​

const token = bearerToken(request)
if (!token) throw new CloudflareError('Missing token', { status: 401 })

Parameters​

request​

Request

Returns​

string | null


clientIp()​

clientIp(request): string | null

Defined in: index.ts:80

The client's IP from the CF-Connecting-IP header Cloudflare sets on every request, or null when absent (e.g. a test without the header).

Example​

const ip = clientIp(request) ?? 'unknown'

Parameters​

request​

Request

Returns​

string | null