/// import type { $PromiseMaybe } from "@dweb-browser/helper/$PromiseMaybe.ts"; import { Ipc, IpcBody, IpcHeaders, IpcResponse, IpcServerRequest } from "../index.js"; import { PureRequest } from "../ipc-message/IpcRequest.js"; export type $OnFetchReturn = Response | IpcResponse | $FetchResponse | void; /** * fetch 处理函数 * 如果标记成 中间件 模式,那么该函数会执行之前函数 */ export type $OnFetch = (event: IpcFetchEvent) => $PromiseMaybe<$OnFetchReturn>; export type $OnFetchMid = (respose: IpcResponse, event: IpcFetchEvent) => $PromiseMaybe<$OnFetchReturn>; /** * 对即将要进行的响应内容,作出额外的处理 */ export declare const fetchMid: (handler: $OnFetchMid) => $OnFetchMid & { readonly [FETCH_MID_SYMBOL]: true; }; export declare const FETCH_MID_SYMBOL: unique symbol; export type $OnFetchEnd = (event: IpcFetchEvent, respose: IpcResponse | undefined) => $PromiseMaybe<$OnFetchReturn>; /** * 对即将要进行的响应内容,做出最后的处理 * * 如果没有返回值,那么就不会执行 ipc.postMessage */ export declare const fetchEnd: (handler: $OnFetchEnd) => $OnFetchEnd & { readonly [FETCH_END_SYMBOL]: true; }; export declare const FETCH_END_SYMBOL: unique symbol; export type $OnWebSocket = $OnFetch; /** * 响应 WebSocket * @param handler * @returns */ export declare const fetchWs: (handler: $OnWebSocket) => ((event: IpcFetchEvent) => $PromiseMaybe<$OnFetchReturn>) & { readonly [FETCH_WS_SYMBOL]: true; }; export declare const FETCH_WS_SYMBOL: unique symbol; /** * 目前对于响应,有三种角色 * * 响应器:在前面没有做出响应内容的情况下,会执行响应器来获取响应内容。直到有一个响应器做出响应,那么后续的响应器就不会被执行。 * 中间件:在前面拥有响应内容的情况下,基于该响应 做出修改原有的响应内容 或者 替换新的响应内容 * 终止符:不论前面有无响应内容,都会执行它,它既可以像响应器和中间件一样去创建修改响应内容,也可以取消内容。(如果取消,那么后面的响应器就会因为 当前没有响应内容 而继续运作。所以你可以用它来切分组合你的响应流) */ export type $AnyFetchHanlder = $OnFetch | ReturnType | ReturnType | ReturnType; export declare const fetchHanlderFactory: { NoFound: () => $OnFetchEnd & { readonly [FETCH_END_SYMBOL]: true; }; Forbidden: () => $OnFetchEnd & { readonly [FETCH_END_SYMBOL]: true; }; BadRequest: () => $OnFetchEnd & { readonly [FETCH_END_SYMBOL]: true; }; InternalServerError: (message?: any) => $OnFetchEnd & { readonly [FETCH_END_SYMBOL]: true; }; }; /** * 一个通用的 ipcRequest 处理器 * 开发不需要面对 ipcRequest,而是面对 web 标准的 Request、Response 即可 */ export declare const createFetchHandler: (onFetchs?: Iterable<$OnFetch>) => { onFetch: (...handler: $OnFetch[]) => any & ((request: IpcServerRequest) => Promise); onWebSocket: (hanlder: $OnWebSocket) => any & ((request: IpcServerRequest) => Promise); mid: (handler: $OnFetchMid) => any & ((request: IpcServerRequest) => Promise); end: (handler: $OnFetchEnd) => any & ((request: IpcServerRequest) => Promise); /** * 配置跨域,一般是最后调用 * @param config */ cors: (config?: { origin?: string | undefined; headers?: string | undefined; methods?: string | undefined; }) => any & ((request: IpcServerRequest) => Promise); noFound: () => any & ((request: IpcServerRequest) => Promise); forbidden: () => any & ((request: IpcServerRequest) => Promise); badRequest: () => any & ((request: IpcServerRequest) => Promise); internalServerError: (message?: any) => any & ((request: IpcServerRequest) => Promise); extendsTo: (_to: T) => { onFetch: (...handler: $OnFetch[]) => any & T; onWebSocket: (hanlder: $OnWebSocket) => any & T; mid: (handler: $OnFetchMid) => any & T; end: (handler: $OnFetchEnd) => any & T; /** * 配置跨域,一般是最后调用 * @param config */ cors: (config?: { origin?: string; headers?: string; methods?: string; }) => any & T; noFound: () => any & T; forbidden: () => any & T; badRequest: () => any & T; internalServerError: (message?: any) => any & T; extendsTo: any; } & T; } & ((request: IpcServerRequest) => Promise); export type $FetchHandler = ReturnType; export interface $FetchResponse extends ResponseInit { body?: BodyInit | null | IpcBody; } export declare class IpcFetchEvent { #private; readonly ipcRequest: IpcServerRequest; readonly ipc: Ipc; constructor(ipcRequest: IpcServerRequest, ipc: Ipc); get url(): URL; get pathname(): string; get search(): string; get searchParams(): URLSearchParams; get request(): PureRequest; /** A simple getter used to expose a `ReadableStream` of the body contents. */ get body(): ReadableStream | null; /** Stores a `Boolean` that declares whether the body has been used in a * response yet. */ get bodyUsed(): boolean; /** Takes a `Response` stream and reads it to completion. It returns a promise * that resolves with an `ArrayBuffer`. */ arrayBuffer(): Promise; typedArray(): Promise; /** Takes a `Response` stream and reads it to completion. It returns a promise * that resolves with a `Blob`. */ blob(): Promise; /** Takes a `Response` stream and reads it to completion. It returns a promise * that resolves with a `FormData` object. */ formData(): Promise; /** Takes a `Response` stream and reads it to completion. It returns a promise * that resolves with the result of parsing the body text as JSON. */ json(): Promise; /** Takes a `Response` stream and reads it to completion. It returns a promise * that resolves with a `USVString` (text). */ text(): Promise; /** Returns a Headers object consisting of the headers associated with request. Note that headers added in the network layer by the user agent will not be accounted for in this object, e.g., the "Host" header. */ get headers(): IpcHeaders; /** Returns request's HTTP method, which is "GET" by default. */ get method(): import("./PureMethod.js").PURE_METHOD; /** Returns the URL of request as a string. */ get href(): string; get reqId(): number; } export declare class IpcFetchError extends Error { constructor(message: string, options?: $FetchErrorOptions); readonly code: number; } export interface $FetchErrorOptions { status?: number; cause?: unknown; } //# sourceMappingURL=ipcFetchHelper.d.ts.map