@worker-tools/middleware
Version:
A suite of standalone HTTP server middlewares for Worker Runtimes.
53 lines (52 loc) • 2.2 kB
TypeScript
import type { Awaitable } from "./utils/common-types.js";
import { Context } from "./context.js";
import { Accepted } from './content-negotiation.js';
export declare const JSON = "application/json";
export declare const FORM = "application/x-www-form-urlencoded";
export declare const FORM_DATA = "multipart/form-data";
export declare const TEXT_HTML = "text/html";
export declare const TEXT_PLAIN = "text/plain";
/** Standard MIME type for binary data */
export declare const BINARY = "application/octet-stream";
/** Non-standard MIME type for binary data. Sometimes used, so included anyway. */
export declare const X_BINARY = "application/x-binary";
export interface BodyParserOptions<J> {
defaultJSON?: J;
maxSize?: number;
}
export declare type BodyParsable = typeof FORM | typeof FORM_DATA | typeof JSON | typeof BINARY | typeof X_BINARY | `application/${string}+json` | `text/${string}`;
export interface BodyJSONContext<J = any> {
accepted: typeof JSON;
body: J;
json: J;
}
export interface BodyVendorJSONContext<J = any> {
accepted: `application/${string}+json`;
body: J;
json: J;
}
export interface BodyFormContext {
accepted: typeof FORM;
body: URLSearchParams;
form: URLSearchParams;
}
export interface BodyFormDataContext {
accepted: typeof FORM_DATA;
body: FormData;
formData: FormData;
}
export interface BodyBinaryContext {
accepted: typeof BINARY | typeof X_BINARY;
body: ArrayBuffer;
arrayBuffer: ArrayBuffer;
blob: Blob;
}
export interface BodyTextContext {
accepted: `text/${string}`;
body: string;
text: string;
}
export declare type BodyContext<J> = BodyJSONContext<J> | BodyBinaryContext | BodyFormContext | BodyFormDataContext | BodyTextContext | BodyVendorJSONContext<J>;
export declare type BodyParserDeps = Context & Accepted<BodyParsable>;
export declare const bodyParser: <J = any>(opts?: BodyParserOptions<J>) => <X extends BodyParserDeps>(ax: Awaitable<X>) => Promise<X & BodyContext<J>>;
export declare const defaultBodyParser: <J = any>(options?: BodyParserOptions<J> | undefined) => <X extends Context>(ax: Awaitable<X>) => Promise<X & Accepted<BodyParsable> & BodyContext<J>>;