@energica-city/shared-amplify-utils
Version:
Shared utilities for AWS Amplify projects
73 lines • 3.52 kB
TypeScript
import type { Middleware } from '../middlewareChain';
import type { RestResponse, RestInputWithModels, RestInputWithValidation, RestRequestValidationConfig } from './types';
import type { AmplifyModelType } from '../../queries/types';
/** Symbol key for storing validated request body data */
declare const VALIDATED_BODY_KEY: unique symbol;
/** Symbol key for storing validated query parameters */
declare const VALIDATED_QUERY_KEY: unique symbol;
/** Symbol key for storing validated path parameters */
declare const VALIDATED_PATH_KEY: unique symbol;
/** Symbol key for storing validated headers */
declare const VALIDATED_HEADERS_KEY: unique symbol;
export { VALIDATED_BODY_KEY, VALIDATED_QUERY_KEY, VALIDATED_PATH_KEY, VALIDATED_HEADERS_KEY, };
/**
* Retrieves validated request body from middleware chain
* @param input - REST input with validation data storage
* @returns Validated and typed request body
*
* @example
* ```typescript
* const userData = getValidatedBody<UserCreateInput>(input);
* console.log(userData.name); // Type-safe access
* ```
*/
export declare function getValidatedBody<T = unknown, TTypes extends Record<string, AmplifyModelType> = Record<string, AmplifyModelType>>(input: RestInputWithValidation<TTypes>): T;
/**
* Retrieves validated query parameters from middleware chain
* @param input - REST input with validation data storage
* @returns Validated and typed query parameters
*
* @example
* ```typescript
* const queryParams = getValidatedQuery<{ page: number; limit: number }>(input);
* ```
*/
export declare function getValidatedQuery<T = Record<string, unknown>, TTypes extends Record<string, AmplifyModelType> = Record<string, AmplifyModelType>>(input: RestInputWithValidation<TTypes>): T;
/**
* Retrieves validated path parameters from middleware chain
* @param input - REST input with validation data storage
* @returns Validated and typed path parameters
*/
export declare function getValidatedPath<T = Record<string, unknown>, TTypes extends Record<string, AmplifyModelType> = Record<string, AmplifyModelType>>(input: RestInputWithValidation<TTypes>): T;
/**
* Retrieves validated headers from middleware chain
* @param input - REST input with validation data storage
* @returns Validated and typed headers
*/
export declare function getValidatedHeaders<T = Record<string, unknown>, TTypes extends Record<string, AmplifyModelType> = Record<string, AmplifyModelType>>(input: RestInputWithValidation<TTypes>): T;
/**
* Creates REST request validator middleware with parallel validation
*
* Validates request components (body, query, path, headers) in parallel for optimal performance.
* Stores validated data using symbol keys for type-safe retrieval in handlers.
*
* @param validationConfig - Configuration specifying which parts to validate
* @returns Middleware function that validates requests and stores results
*
* @example
* ```typescript
* const validator = createRestRequestValidator({
* body: yup.object({
* name: yup.string().required(),
* email: yup.string().email().required()
* }),
* headers: yup.object({
* authorization: yup.string().required()
* })
* });
*
* chain.use('validator', validator);
* ```
*/
export declare function createRestRequestValidator<TTypes extends Record<string, AmplifyModelType> = Record<string, AmplifyModelType>>(validationConfig: RestRequestValidationConfig): Middleware<RestInputWithModels<TTypes>, RestResponse>;
//# sourceMappingURL=RestRequestValidator.d.ts.map