Skip to main content

Plugins

Plugins are a powerful feature of AxiSpark.js that allow you to extend the functionality of your application. They can be used to add new features, integrate with third-party services, or modify the behavior of existing components.

Creating a Plugin​

In AxiSpark.js, a plugin is a class that implements the Plugin interface. This interface defines the methods that a plugin must implement to be compatible with the AxiSpark.js application. Also, a plugin needs to be decorated with the @Injectable() decorator to be recognized by the dependency injection system.

plugin.ts
import { Plugin } from '@axisparkjs/core';
import { Injectable } from '@axisparkjs/di';
import { Logger } from '@axisparkjs/logger';

@Injectable()
export class ExamplePlugin extends Plugin {
private context: AxiSparkContext;
protected options: HttpPluginOptions;

constructor(
private readonly logger: Logger,
private readonly injector: Injector
) {
super();
}

async onRegister() {
if (!options) throw new PluginNotConfiguredError(ExamplePlugin.name);
this.context = context;
this.options = options;
this.logger = this.logger.child('ExamplePlugin');

await this.logger.info('onRegister called');
}

async onStart() {
await this.logger.info('onStart called');
}

async onStop() {
await this.logger.info('onStop called');
}
}

The onRegister, onStart, and onStop methods are lifecycle hooks that allow you to perform actions at different stages of the plugin's lifecycle. You can use these methods to initialize resources, start background tasks, or clean up resources when the plugin is stopped.

Adding a Plugin to Your Application​

You can add a plugin to your AxiSpark.js application by using the app.use() method. This method takes a plugin class as a parameter and registers it with the application.

app.ts
app.use(ExamplePlugin);

If the plugin requires configuration options, you can pass them as a second parameter to the app.use() method:

app.ts
app.use(ExamplePlugin, { option1: 'value1', option2: 'value2' });

Implemented Plugins​

The are some plugins already implemented in AxiSpark.js, which you can use in your application. These plugins provide common functionality that can be useful in many applications.

PluginPackageDescription
HttpPlugin@axisparkjs/http

Provides HTTP server functionality, allowing you to handle HTTP requests and responses in your application. For this plugin, you need to use an http server provider, like ExpressServerProvider or FastifyServerProvider, which are implemented in the @axisparkjs/http-express and @axisparkjs/http-fastify packages.

DataPlugin@axisparkjs/data

Provides TypeORM data-source management and generated entity repositories for SQL databases and MongoDB. See the Data Plugin documentation for setup, repository injection, and derived queries.

OpenApiPlugin@axisparkjs/openapi

Provides OpenAPI support, allowing you to generate OpenAPI documentation for your application and validate requests and responses against the OpenAPI specification. It requires the HttpPlugin to be registered in your application.

SchedulePlugin@axisparkjs/schedule

Provides scheduling functionality, allowing you to schedule tasks to run at specific intervals or times.

You can find more information about the implemented plugins in the section of the documentation dedicated to each plugin. Each plugin has its own configuration options and usage instructions, which you can follow to integrate them into your application.