Skip to content

Latest commit

 

History

History
156 lines (114 loc) · 3.77 KB

4.Extensions.md

File metadata and controls

156 lines (114 loc) · 3.77 KB

Extensions

The extensions are used to add features and reuse a maximum of code. You can find the list of offical extensions in Introduction page.

Official Klient extensions are based on open-stack template. If you want to submit your own extension, your repository must be duplicated from open-stack repository.

Create an extension

An extension can be easily registered by using push method of extensions registry. When new klient instance is built, by default all registered extensions are loaded, else it's possible to define specific extensions to load.

import Klient, { Extensions } from '@klient/core';

//
// An extension must define a name (should be uniq)
// & an initializer (a method called one time at Klient instanciation)
//
const extension = {
  name: '@klient/example',
  initialize: klient => {
    // Use parameters, add services or some listeners, ...
    klient.services.set('test', {
      customMethod: true
    });
  }
}

//
// Register your extension in the global registry
//
Extensions.push(extension);


//
// Build Klient instance
//
const klient = new Klient();


//
// At this point the extension has been automatically loaded
// when klient object has been constructed
//
console.log(klient.services.test.customMethod()); // Print "true"

Share an extension

We recommand you to create a repository based on open-stack template for publishing it quickly and take advantages of an easy maintainability.

//
// In your main file "index.js"
//

import { Extensions } from '@klient/core';

// Create you extension logic
const extension = {
  name: '@klient/rest',
  initialize: klient => {
    // ...
  }
};

// Register your extension in global registry
Extensions.push(extension);

// This line is optional but recommanded for manual usage
export default extension;

Use a shared extension

The following code explain how to load an extension build like in previous section :

import Klient from '@klient/core';

//
// Auto registration of extension by importing main file
//
import '@klient/rest';


//
// Build a new Klient instance
//
const klient = new Klient();


//
// See loaded extensions
//
console.log(klient.extensions); // Print ['@klient/rest']


//
// Use extension features
//
klient.register('User', '/users');

Specify extensions to load

The parameter "extensions" allows you to specify extensions you want to load :

  • If you set an empty array, no extension will be loaded
  • If you do not provide any value, all registered extensions will be loaded
  • Else you can specify each extension you need
import Klient from '@klient/core';

//
// Auto registration of extension in registry
//
import '@klient/rest';
import '@klient/jwt';


//
// Build a new Klient instance 
//
const klient = new Klient({
  extensions: ['@klient/rest'], // Specify extensions to load
});


//
// Load extension manually after instanciation
//
klient.load('@klient/jwt');

TOC   >   Introduction   >   Usage   >   Events   >   Request   >   Extensions   >   Utilities   >   API