import { Config, Model, Models, NamedModel, RematchBag } from './types' import { validateModel } from './validate' /** * Creates and returns a 'Rematch Bag', which is a set of configuration options * used by the Rematch library in various functions. */ export default function createRematchBag< TModels extends Models, TExtraModels extends Models >(config: Config): RematchBag { return { models: createNamedModels(config.models), reduxConfig: config.redux, forEachPlugin(method, fn): void { config.plugins.forEach((plugin) => { if (plugin[method]) { fn(plugin[method]!) } }) }, effects: {}, } } /** * Transforms mapping from a model name to a model object, into an array of * 'named' models - models with embedded name and default value for reducers * if user didn't provide any. */ function createNamedModels>( models: TModels | Partial ): NamedModel[] { return Object.keys(models).map((modelName: string) => { const model = createNamedModel(modelName, (models as TModels)[modelName]) validateModel(model) return model }) } /** * Transforms a model into 'named' model - model which contains 'name' and * 'reducers' properties if user didn't provide any. */ function createNamedModel>( name: string, model: Model ): NamedModel { return { name, reducers: {}, ...model, } }