npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2022 – Pkg Stats / Ryan Hefner

@mia-platform/custom-plugin-lib

v5.0.0

Published

Library that allows you to define Mia-Platform custom plugins easily

Downloads

9,701

Readme

Mia service Node.js Library

Build Status javascript style guide Coverage Status
NPM version

This library is intended to ease the creation of new services to deploy on Mia-Platform.
Built on Fastify, it takes advantage of Mia-Platform Node.js service launcher lc39.

Getting Started

You can use this module in your projects or, from the DevOps Console, get started quickly and easily with a ready-to-use microservice template. Even in the Mia-Platform Marketplace repository, you can find some examples and templates that using this library.

Setup the local development environment

To develop the service locally you need:

  • Node.js v12 or later.

To setup node.js, we suggest using nvm, so you can manage multiple versions easily. Once you have installed nvm, you can go inside the directory of the project and simply run nvm install, the .nvmrc file will install and select the correct version if you don’t already have it.

Once you have all the dependency in place, you can launch:

npm i
npm run coverage

These two commands, will install the dependencies and run the tests with the coverage report that you can view as an HTML page in coverage/lcov-report/index.html.

Install module

To install the package with npm:

npm i @mia-platform/custom-plugin-lib --save

To install with Yarn:

yarn add @mia-platform/custom-plugin-lib

Define a Custom Service

You can define a new Custom Service that integrates with the platform simply writing this:

const customService = require('@mia-platform/custom-plugin-lib')()

module.exports = customService(async function helloWorldService(service) {
  service.addRawCustomPlugin('GET', '/hello', function handler(request, reply) {
    request.log.trace('requested myuser')
    // if the user is not logged, this method returns a falsy value
    const user = request.getUserId() || 'World'
    reply.send({
      hello: `${user}!`,
    })
  })
}) 
  • The library exports a function which creates the infrastructure ready to accept the definition of routes and decorators. Optionally can take a schema of the required environment variables, you can find the reference here. The function returned, customService, expects an async function to initialize and configure the service.

  • service is a Fastify instance, that is decorated by the library to help you interact with Mia-Platform resources. You can use service to register any Fastify routes, custom decorations and plugin, see here for a list of currently available plugins.

  • addRawCustomPlugin is a function that requires the HTTP method, the path of the route and a handler. The handler can also be an async function.
    Optionally you can indicate the JSONSchemas to validate the querystring, the parameters, the payload and the response.

To get more info about Custom Services can you look at the related section.

Environment Variables configuration

To works correctly, this library needs some specific environment variables:

  • USERID_HEADER_KEY
  • USER_PROPERTIES_HEADER_KEY
  • GROUPS_HEADER_KEY
  • CLIENTTYPE_HEADER_KEY
  • BACKOFFICE_HEADER_KEY
  • MICROSERVICE_GATEWAY_SERVICE_NAME

When creating a new service from Mia-Platform DevOps Console, they come already defined but you can always change or add them anytime as described in the DevOps console documentation.
In local, the environment variables are defined in this file.

Other variables can be specified by setting your envSchema when calling the plugin.

Examples

You can see an advanced example to see different use cases of the library.

To see other examples of library use you can visit GitHub dependant repository graph and check all packages that depends on it.

To run the examples directly you can move to specific example folder and run:

npm run start:local

This command will launch the service on localhost:3000 with the environment variables defined in this file. Now you can consult the swagger documentation of the service at http://localhost:3000/documentation/.

How to

  • Create a Custom Service
  • Declare routes
  • Add decorators
  • Call the other services on the Platform project
  • API documentation
  • Testing
  • Logging