defineService(definition) API Reference

Module: webspresso
Signature: defineService(definition: ServiceDefinition): ServiceDefinition
TypeScript Definition: index.d.ts


1. Parameters (ServiceDefinition) #

PropertyTypeDefaultDescription
handler (required)(input: TInput, ctx: ServiceContext) => Promise<TOutput>—The execution logic of the service.
schemaZodType | (({ z }) => ZodType) | ObjectnullDeclarative input validation schema.
authboolean | string | string[] | PredicatefalseAccess control guard (true, role array, or (user, ctx) => boolean).
transactionbooleanfalseWhen true, automatically opens or inherits an ambient ACID Knex transaction.
cacheboolean | { ttl?: string | number, key?: (input) => string }falseService-level result memoization and caching.
timeoutnumber | stringnullMaximum duration allowed for handler execution (e.g. 5000 or '5s').

2. ServiceContext Object #

Every service handler receives a ctx object as its second argument:

PropertyTypeDescription
ctx.dbDatabaseInstanceScoped Knex database instance auto-bound to ambient transaction.
ctx.reqRequestExpress request object (if called from an HTTP flow).
ctx.resResponseExpress response object (if called from an HTTP flow).
ctx.userUser | nullAuthenticated user record.
ctx.service(name, input, opts) => Promise<any>Invoke sibling services with ambient context propagation.
ctx.localestringActive locale code (e.g. 'en', 'tr').
ctx.t(key, params) => stringTranslation function.
ctx.fsyTemplateHelpersFull template helper catalog.
Created · Updated