WithPlugin: <TInstance>(
    instance: TInstance,
    plugins: OneOrMore<Plugin<TInstance>>,
) => TInstance

Applies one or more plugins to a target instance.

Each plugin is invoked in the order provided, receiving the instance and an Enhance utility to optionally wrap its methods with middleware. Plugins are run against the same instance reference, allowing them to collectively configure the object.

Type declaration

class UserService {
async getUser(id: string): Promise<{ name: string }> {
// retrieval logic
}

async deleteUser(id: string): Promise<void> {
// deletion logic
}
}

// Function-based plugin: adds logging to all methods
const loggingPlugin: PluginFn<UserService> = (service, enhance) => {
enhance(service, "getUser", [
(next) => async (...args) => {
console.log(`${method} called with:`, args);
const result = await next(...args);
console.log(`${method} returned:`, result);
return result;
},
]);

enhance(service, "deleteUser", [
(next) => async (...args) => {
console.log(`${method} called with:`, args);
const result = await next(...args);
console.log(`${method} returned:`, result);
return result;
},
]);
};

function main(withPlugin: WithPlugin): void {
const service = new UserService();
const enhancedService = withPlugin(service, loggingPlugin);
await enhancedService.getUser("123");
// Logs:
// getUser called with: ["123"]
// getUser returned: { name: "Alice" }
}

IMPORT_PATH: @daiso-tech/core/middleware