UNPKG

@thisismissem/adonisjs-respond-with

Version:

A small plugin for Adonis.js to make responding with different content-types easier.

30 lines (29 loc) 918 B
import type { AcceptNegotiator } from './acceptor.js'; import type Negotiator from 'negotiator'; export interface RespondWithConfig { defaultHandler: string | 'error'; mappings: { [contentType: string]: string; }; } export type Handler = (matchedType?: string) => any; export type ResponseMatchers = Record<string, Handler> & { error?: never; }; export interface NegotiateOptions<MatcherNames> { defaultHandler?: (MatcherNames & string) | 'error'; } declare module '@adonisjs/core/http' { interface Request { negotiator: Negotiator; } interface Response { negotiate<T extends ResponseMatchers>(matchers: T): any; negotiate<T extends ResponseMatchers>(matchers: T, options: NegotiateOptions<keyof T>): any; } } declare module '@adonisjs/core/types' { interface ContainerBindings { 'respondWith.acceptNegotiator': AcceptNegotiator; } }