1.0.0 • Published 1 year ago

@blissdivorce/metadata v1.0.0

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

@blissdivorce/metadata

Bliss metadata manager

Install

npm i @blissdivorce/metadata

Configuration

In order for this library to work properly it expects to have a configuration based on @blissdivorce/config.

Metadata Manager

The MetadataManager handles the metadata. It provides methods to init/update this data into our sessions. It needs a Metadata Provider to work.

Metadata Providers

Providers allows to store and retrieve the metadata.

Each provider must ensure a way to add/remove/update metadata property/values.

Go to /providers to see the available providers we have.

Examples

Important: error handling must be implemented by consumers

Init metadata

import { MetadataManager, MetadataManagerRequest, Providers } from '@blissdivorce/metadata';

const initMetadataMiddleware = async (req: MetadataManagerRequest, res: Response, next: NextFunction) => {

  try {
    const metadataManager = new MetadataManager(new Providers.Auth0Provider(), req);
    await metadataManager.init();
  } catch (error) {
    logger.error(`error trying to initialize metadata`, { data: { userId: req.user.id, error } });
  }
  
}

Updating metadata

import { MetadataManager, MetadataManagerRequest, Providers } from '@blissdivorce/metadata';

const checkIntroViewedMiddleware = async (req: MetadataManagerRequest, res: Response, next: NextFunction) => {

  try {
    if (req.query.view !== undefined) {
      const metadataManager = new MetadataManager(new Providers.Auth0Provider(), req);
      await metadataManager.setAppMetadata({ modules: { finances: { intro: { done: true } } } });
      return res.redirect('/accounts');
    }

    next();

  } catch (error) {
    logger.error(`error checking metadata`, { data: { queryParams: req.query, error } });
    return res.redirect('/accounts?skipIntro');
  }

};