@worker-tools/middleware
Version:
A suite of standalone HTTP server middlewares for Worker Runtimes.
58 lines (57 loc) • 2.97 kB
TypeScript
import { Awaitable } from './utils/common-types.js';
import { Context } from './index.js';
export interface ContentType<T> {
/** The best content type _acceptable to the client_. */
type: T;
}
export interface ContentLanguage<T> {
/** The best language _acceptable to the client_. */
language: T;
}
export interface ContentEncoding<T> {
/** The best encoding _acceptable to the client_. */
encoding: T;
}
export interface Accepted<T> {
/** The request's `Content-Type` header iff acceptable to this endpoint */
accepted: T;
}
export interface AcceptedLanguage<T> {
/** The request's `Language` header if (and only if) accepted by this endpoint */
acceptedLanguage: T;
}
export interface AcceptedEncoding<T> {
/** The request's `Encoding` header if (and only if) accepted by this endpoint */
acceptedEncoding: T;
}
/**
* Performs content negotiation over the content type of the response.
* @param types The content types _provided_ by this endpoint.
*/
export declare function contentTypes<T extends string, TS extends readonly T[]>(types: TS): <X extends Context>(ax: Awaitable<X>) => Promise<X & ContentType<TS[number]>>;
/**
* Performs content negotiation over the content language of the response.
* @param languages The languages _provided_ by this endpoint.
*/
export declare function contentLanguages<T extends string, TS extends readonly T[]>(languages: TS): <X extends Context>(ax: Awaitable<X>) => Promise<X & ContentLanguage<TS[number]>>;
/**
* Performs content negotiation over the content encoding of the response.
* @param encodings The encodings _provided_ by this endpoint.
*/
export declare function contentEncodings<T extends string, TS extends readonly T[]>(encodings: TS): <X extends Context>(ax: Awaitable<X>) => Promise<X & ContentEncoding<TS[number]>>;
export { contentTypes as provides, contentLanguages as providesLanguages, contentEncodings as providesEncodings, };
/**
* Determines if a request body content type is _acceptable_ to this endpoint.
* @param types The content types _acceptable_ to this endpoint.
*/
export declare function accepts<T extends string, TS extends readonly T[]>(types: TS): <X extends Context>(ax: Awaitable<X>) => Promise<X & Accepted<TS[number]>>;
/**
* Determines if a request body content language is _acceptable_ to this endpoint.
* @param languages The languages (of the request body) _acceptable_ to this endpoint.
*/
export declare function acceptsLanguages<T extends string, TS extends readonly T[]>(languages: TS): <X extends Context>(ax: Awaitable<X>) => Promise<X & AcceptedLanguage<TS[number]>>;
/**
* Determines if a request body content encoding is _acceptable_ to this endpoint.
* @param encodings The body encodings _acceptable_ to this endpoint.
*/
export declare function acceptsEncodings<T extends string, TS extends readonly T[]>(encodings: TS): <X extends Context>(ax: Awaitable<X>) => Promise<X & AcceptedEncoding<TS[number]>>;