Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@gasket/core

Package Overview
Dependencies
Maintainers
8
Versions
95
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@gasket/core

Entry point to setting up Gasket instances

  • 7.0.0-next.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
131
increased by162%
Maintainers
8
Weekly downloads
 
Created
Source

@gasket/core

Installation

Existing apps
npm install @gasket/core

Add a gasket.mjs file to the root of your project. This can be a .js extension if your package.json has the type field set to module. It is also possible to use with a .ts extension if you have TypeScript configured.

// gasket.mjs
import { makeGasket } from '@gasket/core';
import LoggerPlugin from '@gasket/plugin-logger';
import MyPlugin from './my-plugin';

export default makeGasket({
  plugins: [
    LoggerPlugin,
    MyPlugin
  ]
});

You can now import the Gasket instance from your gasket.mjs file into your application code. With a Gasket, you can fire actions that will trigger lifecycles hooked by plugins which encapsulate functionality allowing reuse across many applications.

Registered plugins

A plugin is a module that exports a name and hooks object (See Plugins Guide). In your gasket.mjs file, you can import plugins and add them to the plugins array of the Gasket configuration.

Lifecycles

When a new Gasket is created, there are three lifecycles executed in the following order:

  1. init
  2. actions
  3. configure

init

The init lifecycle allows the earliest entry to setting up a Gasket instance. It can be used for setting up an initial state.

// gasket-plugin-example.mjs

export const name = 'gasket-plugin-example';

let _initializedTime;

export const hooks = {
  init(gasket) {
    _initializedTime = Date.now();
  }
};

While it is possible to attach properties to the gasket instance, it is not recommended. If a plugin needs to make properties available to other plugins, it should register an action that can be executed to retrieve the value.

// gasket-plugin-example.mjs

export const name = 'gasket-plugin-example';

let _initializedTime;

export const hooks = {
  init(gasket) {
-    gasket.initializedTime = Date.now();
+    _initializedTime = Date.now();
  },
+  actions() {
+    return {
+      getInitializedTime() {
+        return _initializedTime;
+      }
+    }
+  },
  configure(gasket) {
-    const time = gasket.initializedTime;
+    const time = gasket.actions.getInitializedTime();
  }
};

actions

The actions lifecycle is the second lifecycle executed when a Gasket is created. This will let plugins register actions that can be fired by the application code where the Gasket is imported, or in other plugins.

// gasket-plugin-example.mjs

export const name = 'gasket-plugin-example';

export const hooks = {
  actions(gasket) {
    return {
      async getDoodads() {
        if(gasket.config.example) {
          const dodaads = await gasket.exec('dodaads');
          return dodaads.flat()
        }
      }
    }
  }
};

configure

The configure lifecycle is the first lifecycle executed when a Gasket is instantiated. This allows any registered plugins to adjust the configuration before further lifecycles are executed.

// gasket-plugin-example.mjs

export const name = 'gasket-plugin-example';

export const hooks = {
  configure(gasket, gasketConfig) {
    // Modify the configuration
    return {
      ...gasketConfig,
      example: true
    };
  }
};

In this example, we register an action getDoodads that will only execute if the example configuration is set to true. It will then execute the doodads lifecycle, allowing any registered plugin to provide doodads.

Keywords

FAQs

Package last updated on 29 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc