@withstudiocms/effect
Version:
Effect-TS Utilities for Astro
35 lines (34 loc) • 2.25 kB
TypeScript
import type { APIContext } from 'astro';
import type { EffectRouteOptions } from '../types.js';
/**
* Generates CORS (Cross-Origin Resource Sharing) headers for an API response based on the provided configuration.
*
* @param context - The API context containing the incoming request, used to extract the `Origin` header.
* @param corsConfig - Optional CORS configuration specifying allowed origins, methods, headers, and credentials.
* @returns An object containing the appropriate CORS headers to be set on the response.
*
* @remarks
* - If `corsConfig.origin` is `true`, allows all origins (`*`).
* - If `corsConfig.origin` is a string, allows only the specified origin.
* - If `corsConfig.origin` is an array, allows only origins included in the array.
* - If `corsConfig.methods` is provided, sets the allowed HTTP methods.
* - If `corsConfig.headers` is provided, sets the allowed request headers.
* - If `corsConfig.credentials` is `true`, allows credentials to be included in requests.
* - When credentials are enabled, the wildcard origin '*' is not permitted by browsers; the origin will be reflected and 'Vary: Origin' will be set.
*/
export declare function getCorsHeaders(context: APIContext, corsConfig?: EffectRouteOptions['cors']): Record<string, string>;
/**
* Validates the incoming API request based on the provided validation options.
*
* @param context - The API context containing request details such as params, URL, and request object.
* @param validate - An object specifying validation functions for params, query, and body.
* @returns A promise that resolves to a string describing the validation error, or `null` if the request is valid.
*
* @remarks
* - Validates route parameters if a `params` validator is provided.
* - Validates query parameters if a `query` validator is provided.
* - Validates the request body (for non-GET/HEAD requests) if a `body` validator is provided.
* - Attempts to parse the request body as JSON, form data, or text based on the `Content-Type` header.
* - Returns a specific error message for each validation failure or parsing error.
*/
export declare function validateRequest(context: APIContext, validate: EffectRouteOptions['validate']): Promise<string | null>;