Skip to main content

@rtorcato/cf-common

Type Aliases​

ServiceTarget​

ServiceTarget = (new (...args) => Rpc.WorkerEntrypointBranded) | Rpc.WorkerEntrypointBranded | ExportedHandler<any, any, any> | undefined

Defined in: index.ts:8

What a service binding can point at: a WorkerEntrypoint class or instance type, or a plain export default { fetch } handler.

Functions​

getService()​

getService<T>(env, name): Service<T>

Defined in: index.ts:31

Read a service binding from env, typed by the callee's entrypoint. Name the entrypoint class (or instance type) and its public methods become awaitable RPC calls; fetch is there too. Throws a non-exposed 500 when the binding is missing (a deploy misconfiguration; see getBinding).

Import the callee's class as a type only, so its code isn't bundled into the caller:

Example​

import type { AuthService } from '@acme/auth-worker'

const auth = getService<typeof AuthService>(env, 'AUTH')
const user = await auth.verify(token)

Type Parameters​

T​

T extends ServiceTarget = undefined

Parameters​

env​

Env

name​

string

Returns​

Service<T>


callService()​

callService<R>(label, call): Promise<R>

Defined in: index.ts:51

Run an RPC call and map any failure (callee threw, binding unreachable, callee exceeded its limits) to a non-exposed 502 with code RPC_CALL_FAILED, keeping the original error as cause. label names the service in the message and logs.

A callee's own status doesn't cross the RPC boundary, so a callee that wants to report an expected failure (not found, invalid input) should return a result value rather than throw.

Example​

const user = await callService('AUTH', () => auth.verify(token))

Type Parameters​

R​

R

Parameters​

label​

string

call​

() => R | Promise<R>

Returns​

Promise<R>