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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sbesson/express-openapi-backend

v2.1.1

Published

Modèle de développement d'un service REST Api

Downloads

12

Readme

express-openapi-backend

const { Server } = require("@sbesson/express-openapi-backend");

const server = new Serveur({
    host, // host for listen server
    port, // port for listen server
    ssl: {
      enable, // if set to true use of https (default false)
      keyFile, // key file for https
      certFile, // certificat file for https
    },
    swaggerUiOption, // see https://www.npmjs.com/package/swagger-ui-express
    validateResponses, // allows to control the responses by openapi validator (default true)
  })

const context = {logger}
// server.initializeSite can be run multiple times to, for example, manage different versions of the backend
await server.initializeSite({
  apiSpecFile, // openapi file global
  modulesPaths, // string|array path to modules
  context, // inject services or informations to modules
})
await server.start();

Dans le répertoire modulesPath :

module1/
  openapi/
    openapi.yml
  index.js

Exemple d'un module root

Ces modules sont les 1er a être initialisé et donc les middleware seront les 1er a être exécuté avant même la mise en place de openapi

structure du fichier index.js

const { RootModuleInterface } = require("@sbesson/express-openapi-backend");

class Module1 extends RootModuleInterface {
  constructor({ context }) {} // facultatif
  async initialize({ modules }) {} // facultatif, peut ne pas être async !
  async updateRootRouter({ router }) { // peut ne pas être async
    router.use(function middleware(req, res, next) {}) // for exemple
    ...
  }
}

Exemple d'un module public

Ces modules sont initialisés après la mise en place de openapi et donc les routes devront faire partie de l'API, mais elles seront avant les modules d'authentification permettant donc d'être accédé sans authentification préalable.

structure du fichier index.js

const { PublicModuleInterface } = require("@sbesson/express-openapi-backend");

class Module1 extends PublicModuleInterface {
  constructor({ context }) {} // facultatif
  async initialize({ modules }) {} // facultatif, peut ne pas être async !
  async routerFactory() { // peut ne pas être async
    const router = express.Router();
    ...
    return router
  }
}

Exemple d'un module authentification

Ces modules ont pour but de gérer l'authentification (exemple création de token et validation du token), ils seront donc initialisés après les modules public

structure du fichier index.js

const { AuthentificationModuleInterface } = require("@sbesson/express-openapi-backend");

class Module1 extends AuthentificationModuleInterface {
  constructor({ context }) {} // facultatif
  async initialize({ modules }) {} // facultatif, peut ne pas être async !
  async updateRootRouter({ router }) {} // peut ne pas être async
    router.use(function middleware(req, res, next) {/* check authentification */}) // for example
    router.post(`/${this.apiPath}/token`, function middleware(req, res, next) {/* create token */})
    ...
  }
}

Exemple d'un module private

Ces modules seront accessible après validation de l'authentification. Ils seront donc initialisés après les modules d'authentification

Module accessible si identifié structure du fichier index.js

const { PrivateModuleInterface } = require("@sbesson/express-openapi-backend");

class Module1 extends PrivateModuleInterface {
  constructor({ context }) {} // facultatif
  async initialize({ modules }) {} // facultatif, peut ne pas être async !
  async routerFactory() { // peut ne pas être async
    const router = express.Router();
    router.get("/", function middleware(req, res, next)); // for example
    ...
    return router
  }
}

Exemple d'un module error

Ces modules ont pour but gérer les erreurs et seront donc initialisé en dernier.

error non géré par openapi structure du fichier index.js

const { ErrorModuleInterface } = require("@sbesson/express-openapi-backend");

class Module1 extends ErrorModuleInterface {
  constructor({ context }) {} // facultatif
  async initialize({ modules }) {} // facultatif, peut ne pas être async !
  async updateRootRouter({ router }) {} // peut ne pas être async
    // error global
    router.use(function middleware(error, req, res, next) {/* manage error */})
    // error module
    router.use(`/${this.apiPath}`, function errorMiddleware(error, req, res, next) {/* manage error */})
    ...
  }
}

TODO

  • [ ] readme.md