Skip to content

API Reference ​

Complete reference for all exports in the capix package.

See API stability for what counts as public API and the compatibility policy per release stage.

For transport-specific APIs, see:


capability(resolver) ​

capability(inputSchema, resolver) ​

capability(inputSchema, resolver, intent) ​

Creates a capability.

ParameterTypeDescription
inputSchemaZodSchema | nullOptional Zod schema for input validation
resolver(input, ctx) => output | Promise<output>The resolver function
intentIntentExplicit intent: 'query' | 'mutation' | 'update' | 'replace' | 'delete'

Returns Capability<TInput, TOutput, BaseContext>.


capability.withContext<TContext>() ​

Returns a factory function identical to capability() but with TContext pre-bound as the context type.

ts
const cap = capability.withContext<AppContext>();
const getUser = cap(schema, async (input, ctx) => {
  // ctx is AppContext
});

defineContext(fn) ​

Defines a context builder. fn receives a RawRequest and returns the context object.

The returned function is a ContextBuilder — pass it to createServer({ context: buildContext }).


defineGuard(fn) ​

Defines a guard. fn receives the context and should throw a FrameworkError to reject.

ts
const mustBeUser = defineGuard((ctx) => {
  if (!ctx.user) throw errors.Unauthorized();
});

defineGuardFor<T>() ​

Returns a guard factory for a narrowing guard — one that asserts the context is subtype T.

ts
type AuthCtx = AppContext & { user: User };

const mustBeUser = defineGuardFor<AuthCtx>()((ctx) => {
  if (!ctx.user) throw errors.Unauthorized();
});

defineInputGuard(fn) ​

Defines an input guard — a guard that runs after input validation and receives (input, ctx).


defineError(status, message, code?) ​

Creates a typed error factory.

ParameterTypeDescription
statusnumberHTTP status code
messagestringHuman-readable message; also used to derive code
codestring | undefinedMachine-readable error code (optional; derived from message if omitted)

Returns ErrorFactory: a callable (meta?) => FrameworkError.


defineEnhancer(fn) ​

Type-safe pass-through for enhancer functions. Ensures the function matches the Enhancer signature.


definePlugin(plugin) ​

Creates a plugin from a plain object with optional capabilities and context fields.


defineConfig(config) ​

Pass-through for type inference on server config objects. Use to get TypeScript autocomplete on config literals.


createServer(config) ​

Creates a server. Does not start transports.

Config fieldTypeDescription
contextContextBuilderRequired if any capability uses context
capabilitiesGroupTreeDefault capabilities for all transports
pluginsPlugin[]Plugin array
transportsTransport[]Transport array
isDevelopmentbooleanEnable output validation; defaults to NODE_ENV !== 'production'

Returns { start(), stop(), invoke() }.


createEventBus<TEvents>() ​

Creates a typed event bus. TEvents maps event names to their payload types.

Returns { publish(event, data), subscribe(event, handler, options?) }.


isCapability(v) ​

Type guard: returns true if v is a Capix capability.


isFrameworkError(v) ​

Type guard: returns true if v is a FrameworkError created by defineError.


inferIntent(key) ​

Infers capability intent from a key name using the prefix tables. Returns Intent.


compileRegistry(groupTree) ​

Compiles a capability group tree into a flat CapabilityRegistry (a Map<string, AnyCapability>).


getHeader(req, name) ​

Safe header access from RawRequest. Returns string | undefined.


defaultErrors ​

Pre-defined error factories:

NameStatus
BadRequest400
Unauthorized401
Forbidden403
NotFound404
Conflict409
TooManyRequests429
Internal500
Timeout504

Enhancers ​

See guide/enhancers.md for usage.

ExportSignature
withCache(ttlSeconds: number) => Enhancer
withRateLimit(opts: RateLimitOptions) => Enhancer
withCircuitBreaker(opts: CircuitBreakerOptions) => Enhancer
withTimeout(ms: number) => Enhancer
withRetry(maxAttempts: number, delayMs?: number) => Enhancer
withRollbackEnhancer
withMetrics(collector: MetricsCollector) => Enhancer
withLoggingEnhancer
consoleMetricsCollectorMetricsCollector

Types ​

TypeDescription
Capability<I, O, C>A capability with typed input, output, and context
AnyCapabilityCapability<unknown, unknown, BaseContext>
BaseContext{ requestId: string }
ContextBuilder(req: RawRequest) => Context | Promise<Context>
RawRequest{ headers: Record<string, string | string[] | undefined> }
Guard(ctx: C) => void | Promise<void>
NarrowingGuard<C, N extends C>Guard that asserts ctx is N
InputGuard(input: I, ctx: C) => void | Promise<void>
Enhancer(cap: Capability) => Capability
Resolver<I, O, C>(input: I, ctx: C) => O | Promise<O>
Plugin{ capabilities?: GroupTree; context?: (base: C) => C }
Intent'query' | 'mutation' | 'update' | 'replace' | 'delete'
GroupTreeRecursive object of capabilities
CapabilityRegistryMap<string, AnyCapability>
InferInput<Cap>Extract input type from a capability
InferOutput<Cap>Extract output type from a capability
InferContext<Cap>Extract context type from a capability
FrameworkErrorTyped error from defineError
ErrorFactory(meta?: Record<string, unknown>) => FrameworkError
WithRollback<T>Context extended with onRollback(fn)
MetricsCollector{ increment(name, tags?), histogram(name, value, tags?) }
CircuitBreakerOptions{ failureThreshold, successThreshold, timeoutMs }
RateLimitOptions{ max, windowMs }
Transport{ mount(invoke, options): Promise<() => Promise<void>> }
TransportWithCapabilitiesTransport & { _capabilities?: GroupTree }
MountOptions{ registry: CapabilityRegistry; invoke: InvokeFn }
ServerConfigFull config type for createServer
Server{ start(), stop(), invoke() }
EventBus<TEvents>Typed event bus
EventMapBase type for event maps
InvokeFn(req: CapabilityRequest) => Promise<CapabilityResponse>
CapabilityRequest{ capability, input, headers? }
CapabilityResponse{ ok, status, data? } | { ok: false, status, error, message }

Released under the MIT License.