UNPKG

webserv

Version:

a quick, flexible, fully typed development server

32 lines (31 loc) 1.35 kB
import { Connection, ConnectionMethods, RealtimeUpgradeProperties } from '../upgrades/realtime.upgrade'; import { Service } from '../app'; export interface Action<T = any> { type: string; payload: T; } export declare type ActionMiddleware<T extends Action = Action> = (data: T, con: Connection, methods: ConnectionMethods) => void | Promise<void>; export interface ActionHandlerDefinition { type: string; handler: ActionMiddleware<any>; } export interface ActionServiceProperties extends Omit<RealtimeUpgradeProperties, 'onMessage'> { handlers: ActionHandlerDefinition[]; defaultHandler?: ActionMiddleware; } interface ErrorPayload { message: string; } /** * Helper function to send an error response to an action request */ export declare function sendError<T extends object, U extends any>(con: Connection | Connection[], source: Action<T>, payload: ErrorPayload & U): void; /** * Helper function to send a serialized action to a connection */ export declare function sendResponse<T extends object>(con: Connection | Connection[], action: Action<T>): void; /** * This is a realtime message-passing service using Websockets built on top of realtime.upgrade. */ export declare function actionService({ handlers, defaultHandler, ...rest }: ActionServiceProperties): Service; export {};