@energica-city/shared-amplify-utils
Version:
Shared utilities for AWS Amplify projects
67 lines • 3.27 kB
TypeScript
import type { RestInputWithModels, RestHandlerReturn, RestMiddlewareChain } from './types';
import type { AmplifyModelType, QueryFactoryResult } from '../../queries/types';
/**
* Creates a REST middleware chain for composing middleware functions
* @param config - Configuration options for the middleware chain
* @param config.enableDebugLogging - Whether to enable debug logging for middleware execution
* @param config.onError - Error handler callback for middleware errors
* @returns Configured REST middleware chain instance
*
* @example
* ```typescript
* const chain = createRestChain<{ User: UserType }>({
* enableDebugLogging: true,
* onError: (error, middlewareName) => console.error(`Error in ${middlewareName}:`, error)
* });
* ```
*/
export declare function createRestChain<TTypes extends Record<string, AmplifyModelType>, TReturn = RestHandlerReturn>(config?: {
enableDebugLogging?: boolean;
onError?: (error: unknown, middlewareName: string) => void;
}): RestMiddlewareChain<TTypes, TReturn>;
/**
* Wraps a REST handler function with a middleware chain for AWS Lambda
* @param chain - The middleware chain to execute before the handler
* @param handler - The main handler function to wrap
* @returns Lambda-compatible function that executes middleware chain then handler
*
* @example
* ```typescript
* const handler = async (input: RestInputWithModels<MyTypes>) => {
* return createSuccessResponse({ message: 'Hello World' });
* };
*
* export const lambdaHandler = wrapRestHandler(chain, handler);
* ```
*/
export declare function wrapRestHandler<TTypes extends Record<string, AmplifyModelType>, TReturn extends RestHandlerReturn = RestHandlerReturn>(chain: RestMiddlewareChain<TTypes, TReturn>, handler: (input: RestInputWithModels<TTypes>) => Promise<TReturn>): (event: RestInputWithModels<TTypes>['event'], context: RestInputWithModels<TTypes>['context']) => Promise<TReturn>;
/**
* Retrieves all initialized models from the middleware input
* @param input - REST input containing initialized models
* @returns Object containing all available model instances
* @throws Error if models are not available (ModelInitializer middleware not used)
*
* @example
* ```typescript
* const models = getModelsFromInput(input);
* const user = await models.User.create({ input: userData });
* ```
*/
export declare function getModelsFromInput<TTypes extends Record<string, AmplifyModelType>>(input: RestInputWithModels<TTypes>): {
[K in keyof TTypes]: QueryFactoryResult<K & string, TTypes>;
};
/**
* Retrieves a specific model instance from the middleware input
* @param input - REST input containing initialized models
* @param modelName - Name of the model to retrieve
* @returns The requested model instance
* @throws Error if models are not available or the specific model is not found
*
* @example
* ```typescript
* const UserModel = getModelFromInput(input, 'User');
* const user = await UserModel.create({ input: userData });
* ```
*/
export declare function getModelFromInput<T extends keyof TTypes, TTypes extends Record<string, AmplifyModelType>>(input: RestInputWithModels<TTypes>, modelName: T): QueryFactoryResult<T & string, TTypes>;
//# sourceMappingURL=RestMiddlewareChain.d.ts.map