UNPKG

@energica-city/shared-amplify-utils

Version:

Shared utilities for AWS Amplify projects

50 lines 2.22 kB
import type { RestModelInitializerConfig, RestInputWithModels, RestHandlerReturn, RestResponse } from './types'; import type { Middleware } from '../middlewareChain'; import type { AmplifyModelType } from '../../queries/types'; /** * Creates REST model initializer middleware for Amplify database access * * This middleware initializes Amplify model instances and makes them available * to subsequent middleware and handlers through the input.models property. * * Features: * - Singleton pattern: models are initialized once and reused * - Timeout protection: prevents hanging on initialization failures * - Type safety: provides fully typed model instances * - Error handling: throws errors that are caught by error handler middleware * * @template TSchema - Amplify schema type with models property * @template TTypes - Record mapping model names to their Amplify types * @template TReturn - Return type of the middleware chain * * @param config - Configuration for model initialization * @returns Middleware function that initializes and provides model instances * * @example * ```typescript * // Define your schema and types * type MySchema = { models: { User: any; Post: any } }; * type MyTypes = { User: UserType; Post: PostType }; * * // Create the middleware * const modelInitializer = createRestModelInitializer<MySchema, MyTypes>({ * schema: myAmplifySchema, * amplifyOutputs: myAmplifyOutputs, * entities: ['User', 'Post'], // Optional: specific models only * timeout: 10000 // Optional: custom timeout * }); * * // Use in middleware chain * chain.use('models', modelInitializer); * * // Access in handler * const handler = async (input: RestInputWithModels<MyTypes>) => { * const user = await input.models.User.create({ input: userData }); * return createSuccessResponse(user); * }; * ``` */ export declare function createRestModelInitializer<TSchema extends { models: Record<string, unknown>; }, TTypes extends Record<string, AmplifyModelType>, TReturn extends RestHandlerReturn = RestResponse>(config: RestModelInitializerConfig<TSchema, TTypes>): Middleware<RestInputWithModels<TTypes>, TReturn>; //# sourceMappingURL=RestModelInitializer.d.ts.map