astro-ghostcms/.pnpm-store/v3/files/ca/fe065ce697bf543fb5c9f4dc880...

37 lines
1.3 KiB
Plaintext
Raw Normal View History

2024-02-14 14:10:47 +00:00
import type { APIContext, MiddlewareHandler } from '../../@types/astro.js';
/**
* Utility function that is in charge of calling the middleware.
*
* It accepts a `R` generic, which usually is the `Response` returned.
* It is a generic because endpoints can return a different payload.
*
* When calling a middleware, we provide a `next` function, this function might or
* might not be called.
*
* A middleware, to behave correctly, can:
* - return a `Response`;
* - call `next`;
*
* Failing doing so will result an error. A middleware can call `next` and do not return a
* response. A middleware can not call `next` and return a new `Response` from scratch (maybe with a redirect).
*
* ```js
* const onRequest = async (context, next) => {
* const response = await next(context);
* return response;
* }
* ```
*
* ```js
* const onRequest = async (context, next) => {
* context.locals = "foo";
* next();
* }
* ```
*
* @param onRequest The function called which accepts a `context` and a `resolve` function
* @param apiContext The API context
* @param responseFunction A callback function that should return a promise with the response
*/
export declare function callMiddleware(onRequest: MiddlewareHandler, apiContext: APIContext, responseFunction: () => Promise<Response>): Promise<Response>;