koas-serializer
Version:
Koas serializer converts a response body to the negotiated response format.
30 lines (29 loc) • 1.03 kB
TypeScript
/// <reference types="node" />
import { Context } from 'koa';
import { Plugin } from 'koas-core';
/**
* A function to serialize a response body.
*
* @param body - The body that was set on the Koa context.
* @param ctx - The Koa context.
* @returns The serialized body.
*/
export declare type Serializer = (body: unknown, ctx: Context) => Buffer | string;
export interface SerializeOptions {
/**
* A mapping content types to serializer functions.
*
* The key represents a content type. An asterisk is allowed to use it for multiple similar
* content types.
*/
serializers?: Record<string, Serializer>;
}
/**
* Serialize a response body based on the accept header.
*
* Default serializers have been registered for `application/json` and `text/*`.
*
* @param options - The options for serializeing the response body.
* @returns A Koas plugin which serializes the response body bases on the accept header.
*/
export declare function serializer({ serializers }?: SerializeOptions): Plugin;