{"id":14384644,"url":"https://github.com/nfroidure/swagger-http-router","last_synced_at":"2025-08-23T17:33:08.513Z","repository":{"id":57375841,"uuid":"91805338","full_name":"nfroidure/swagger-http-router","owner":"nfroidure","description":"A HTTP router based on your Swagger definition.","archived":true,"fork":false,"pushed_at":"2019-02-02T11:01:00.000Z","size":660,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-12-19T09:37:05.659Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nfroidure.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-19T12:50:20.000Z","updated_at":"2023-01-28T11:46:17.000Z","dependencies_parsed_at":"2022-09-02T16:29:14.176Z","dependency_job_id":null,"html_url":"https://github.com/nfroidure/swagger-http-router","commit_stats":null,"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nfroidure%2Fswagger-http-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nfroidure%2Fswagger-http-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nfroidure%2Fswagger-http-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nfroidure%2Fswagger-http-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nfroidure","download_url":"https://codeload.github.com/nfroidure/swagger-http-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230716599,"owners_count":18269801,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-28T18:01:32.494Z","updated_at":"2024-12-21T12:30:47.927Z","avatar_url":"https://github.com/nfroidure.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"[//]: # ( )\n[//]: # (This file is automatically generated by a `metapak`)\n[//]: # (module. Do not change it  except between the)\n[//]: # (`content:start/end` flags, your changes would)\n[//]: # (be overridden.)\n[//]: # ( )\n# swagger-http-router\n\u003e A HTTP router based on your Swagger/OpenAPI definition.\n\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nfroidure/swagger-http-router/blob/master/LICENSE)\n[![Build status](https://secure.travis-ci.org/nfroidure/swagger-http-router.svg)](https://travis-ci.org/nfroidure/swagger-http-router)\n[![Coverage Status](https://coveralls.io/repos/nfroidure/swagger-http-router/badge.svg?branch=master)](https://coveralls.io/r/nfroidure/swagger-http-router?branch=master)\n[![NPM version](https://badge.fury.io/js/swagger-http-router.svg)](https://npmjs.org/package/swagger-http-router)\n[![Dependency Status](https://david-dm.org/nfroidure/swagger-http-router.svg)](https://david-dm.org/nfroidure/swagger-http-router)\n[![devDependency Status](https://david-dm.org/nfroidure/swagger-http-router/dev-status.svg)](https://david-dm.org/nfroidure/swagger-http-router#info=devDependencies)\n[![Package Quality](http://npm.packagequality.com/shield/swagger-http-router.svg)](http://packagequality.com/#?package=swagger-http-router)\n[![Code Climate](https://codeclimate.com/github/nfroidure/swagger-http-router.svg)](https://codeclimate.com/github/nfroidure/swagger-http-router)\n\n\n[//]: # (::contents:start)\n\nWhy write code when you have a Swagger/OpenAPI definition?\n\nBy taking part of the Swagger/OpenAPI standard and\n dependency injection  patterns, `swagger-http-router`\n provides a convenient, highly modular and easily\n testable REST tool.\n\n## Usage\n```js\nimport { constant } from 'knifecycle';\nimport initDB from './services/db';\nimport {\n  initWepApplication\n} from 'swagger-http-router';\n\nimport API from './swagger.api.json';\nimport * as HANDLERS from './handlers';\n\nrun();\n\nasync function run() {\n  try {\n    // STEP 1: Spawn a Knifecycle instance and attach\n    // it the API definition and its handlers\n    const $ = initWepApplication(API, HANDLERS);\n\n    // STEP 2: Register additional services\n    // Override the build in `uniqueId` service\n    // with the UUID v4 function\n    $.register(constant('uniqueId', uuid.v4))\n    // Provide the process environment\n    .register(constant('ENV', process.env))\n    // Register the database initializer\n    .register(initDB);\n\n    // STEP 3: Run your app!\n    // Run the execution silo that encapsulates the app\n    // Note that the `httpServer` and `process` services\n    // are injected for their respective side effects:\n    // creating the server and managing the process\n    // lifecycle\n    const { ENV, log, $destroy } = await $.run(['ENV', 'log', 'httpServer', 'process', '$destroy']);\n\n    log('info', `On air 🚀🌕`);\n\n    if(ENV.DRY_RUN) {\n      await $destroy();\n    }\n  } catch(err) {\n    console.error('💀 - Cannot launch the process:', err.stack);\n    process.exit(1);\n  }\n)\n```\n\nIn order to work, your Swagger definition endpoints\nmust provide an\n [`operationId`](http://swagger.io/specification/#operationObject).\n This is how the router figures out which handler\n to run. Those ids have to be unique. Here is\n a sample Swagger definition you could use as is:\n```js\n// file: ./my-swagger.json\n{\n  \"host\": \"localhost:1337\",\n  \"basePath\": \"/v1\",\n  \"schemes\": [\"http\"],\n  // (...)\n  \"paths\": {\n    \"GET\": {\n      \"/users/{userId}\": {\n        \"operationId\": \"getUser\",\n        \"summary\": \"Retrieve a user.\",\n        \"produces\": [\n          \"application/json\"\n        ],\n        \"parameters\": [{\n          \"in\": \"path\",\n          \"name\": \"userId\",\n          \"type\": \"number\",\n          \"pattern\": \"^[0-9]+$\"\n        }, {\n          \"in\": \"query\",\n          \"name\": \"extended\",\n          \"type\": \"boolean\"\n        }, {\n          \"in\": \"header\",\n          \"name\": \"Content-Type\",\n          \"type\": \"string\"\n        }],\n        \"responses\": {\n          \"200\": {\n            \"description\": \"User found\",\n            \"schema\": {\n              \"type\": \"object\",\n              \"properties\": {\n                \"id\": { \"type\": \"number\" },\n                \"name\": { \"type\": \"string\" }\n              }\n            }\n          },\n          \"404\": {\n            \"description\": \"User not found\"\n          }\n        }\n      }\n    }\n  }\n}\n```\n\nTo bring to the router the logic that each\n endpoint implements, you have to create\n handlers for each `operationId`:\n```js\n// file: ./handlers.js\n\n// Knifecycle is the dependency injection tool\n// we use. It provides decorators to declare\n// which dependencies to inject in your handlers\nimport { initializer } from 'knifecycle/dist/util';\n\nexport default initializer(\n  {\n    name: 'getUser',\n    type: 'service',\n    inject: ['db'],\n  },\n  getUser\n);\n\nasync function getUser({ db }) {\n  return async ({ userId }) =\u003e {\n    const user = await db.query('users', {\n      id: userId,\n    });\n\n    return {\n      status: 200,\n      headers: {},\n      body: {\n        id: userId,\n        name: user.name,\n      }\n    };\n  }\n}\n```\n\nAs you can see, handlers are just asynchronous functions\nthat takes the request parameters in input and provide\na JSON serializable response in output.\n\nThis router is designed to be used with a DI system and\n is particularly useful with\n [`knifecycle`](https://github.com/nfroidure/knifecycle).\n\nThat said, you could completely avoid using a DI system\n by simply using the initializers as functions and handle\n their initialization manually. See this\n  [alternate example](https://gist.github.com/nfroidure/647189bdeffef33bced3a3b6d924640e).\n\n## Goal\n\nThis router is just my way to do things. It is nice\n if you use it and I'd be happy if you improve it.\n\nTo be honest, I think this is a better approach but I do\n not want to spend energy and fight with giants to make\n this a standard approach. It means that it will probably\n never be the next hype and if you use it you must feel\n confident with forking and maintaining it yourself.\n That said, the code is well documented and not that hard.\n Also, the handlers you will end with will be easily\n reusable in any framework with little to no changes.\n\nYou may wonder why I found that I'd better write\n my own router instead of using current solutions\n like `ExpressJS` or `HapiJS`:\n- I want documentation first APIs. No documentation, no\n web service.\n- I want my code to be clear and descriptive instead of\n binded to some cryptic middleware or plugin defined\n elsewhere. Here are some\n [thoughts on middlewares](http://insertafter.com/en/blog/no_more_middlewares.html)\n that explain this statement in more depth.\n - I want easily testable and reusable handlers just\n  returning plain JSON. To be able to reuse it in\n  multiple situations: a lambda/serverless back-end,\n  when rendering server side React views or in my\n  GraphQL server resolvers.\n- I prefer functional programming: it just makes my code\n better. There are too many encapsulated states in existing\n frameworks. I just want my handlers to be pure and\n composable. For example, why adding a CORS middleware or\n plugin when you can just compose handlers?\n```js\nimport { reuseSpecialProps } from 'knifecycle/dist/util';\n\nconst CORS = {\n  'Access-Control-Allow-Origin': '*',\n  'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',\n  'Access-Control-Allow-Headers': 'Keep-Alive,User-Agent',\n};\n\nexport function wrapWithCORS(initHandler) {\n  // `reuseSpecialProps` create a new initializer\n  // with the original initializer properties\n  // applyed on it.\n  return reuseSpecialProps(\n    initHandler,\n    initHandlerWithCORS.bind(null, initHandler)\n  );\n}\n\n// This function is the actual initializer that\n// wraps the handler initializer. It is executed\n// once at startup.\nasync function initHandlerWithCORS(initHandler, services) =\u003e {\n  const handler = await initHandler(services);\n\n  return handleWithCors.bind(null, handler);\n}\n\n// And finally this one applies CORS to the\n// response\nasync function handleWithCors(handler, parameters) {\n  const response = await handler(parameters);\n\n  return  {\n    ...response,\n    headers: {\n      ...response.headers,\n      ...CORS,\n    }\n  };\n}\n\n```\n- and finally, I want to be able to instrument my code\n without having to do ugly hacks. This is why DI and\n Inversion of Control are at the core of my way to\n handle web services.\n\nYou may want to have a look at the\n [architecture notes](./ARCHITECTURE.md) of this module\n to better grasp how it is built.\n\n## Recommendations\n\nThe above usage section shows you a very basic\n usage of this router. For larger apps:\n- you may want to build you Swagger file to avoid\n repeating yourself. It won't change anything for\n `swagger-http-router` since it just assumes a\n Swagger file.\n- you will probably end up by automating the\n handlers loading with a configuration file.\n At that point, the DI system will become very\n handy.\n- you will certainly need some more services\n to make your app work. Please double check if\n one exists before creating your own. Also,\n handlers can be reused so feel free to\n publish yours and add your Swagger path\n objects to them in order for your users to\n add them to their own Swagger build.\n\n# API\n## Functions\n\n\u003cdl\u003e\n\u003cdt\u003e\u003ca href=\"#initErrorHandler\"\u003einitErrorHandler(services)\u003c/a\u003e ⇒ \u003ccode\u003ePromise\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eInitialize an error handler for the\nHTTP router\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#initWepApplication\"\u003einitWepApplication(API, HANDLERS, [$])\u003c/a\u003e ⇒ \u003ccode\u003eKnifecycle\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eInitialize a web application\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#registerHandlers\"\u003eregisterHandlers($, HANDLERS)\u003c/a\u003e ⇒ \u003ccode\u003evoid\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eRegister the handlers hash into the given Knifecycle\n instance\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#initHTTPRouter\"\u003einitHTTPRouter(services)\u003c/a\u003e ⇒ \u003ccode\u003ePromise\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eInitialize an HTTP router\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#initHTTPServer\"\u003einitHTTPServer(services)\u003c/a\u003e ⇒ \u003ccode\u003ePromise\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eInitialize an HTTP server\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#initHTTPTransaction\"\u003einitHTTPTransaction(services)\u003c/a\u003e ⇒ \u003ccode\u003ePromise.\u0026lt;function()\u0026gt;\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eInstantiate the httpTransaction service\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#flattenSwagger\"\u003eflattenSwagger(API)\u003c/a\u003e ⇒ \u003ccode\u003eObject\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eFlatten the inputed Swagger file\n object\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#getSwaggerOperations\"\u003egetSwaggerOperations(API)\u003c/a\u003e ⇒ \u003ccode\u003eArray\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eReturn a Swagger operation in a more\n convenient way to iterate onto its\n operations\u003c/p\u003e\n\u003c/dd\u003e\n\u003c/dl\u003e\n\n\u003ca name=\"initErrorHandler\"\u003e\u003c/a\u003e\n\n## initErrorHandler(services) ⇒ \u003ccode\u003ePromise\u003c/code\u003e\nInitialize an error handler for the\nHTTP router\n\n**Kind**: global function  \n**Returns**: \u003ccode\u003ePromise\u003c/code\u003e - A promise of a function to handle errors  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| services | \u003ccode\u003eObject\u003c/code\u003e | The services the server depends on |\n| [services.ENV] | \u003ccode\u003eObject\u003c/code\u003e | The services the server depends on |\n| [services.DEBUG_NODE_ENVS] | \u003ccode\u003eArray\u003c/code\u003e | The environnement that activate debugging  (prints stack trace in HTTP errors responses) |\n| [services.STRINGIFYERS] | \u003ccode\u003eObject\u003c/code\u003e | The synchronous body stringifyers |\n\n\u003ca name=\"initErrorHandler..errorHandler\"\u003e\u003c/a\u003e\n\n### initErrorHandler~errorHandler(transactionId, responseSpec, err) ⇒ \u003ccode\u003ePromise\u003c/code\u003e\nHandle an HTTP transaction error an\nmap it to a serializable response\n\n**Kind**: inner method of [\u003ccode\u003einitErrorHandler\u003c/code\u003e](#initErrorHandler)  \n**Returns**: \u003ccode\u003ePromise\u003c/code\u003e - A promise resolving when the operation\n completes  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| transactionId | \u003ccode\u003eString\u003c/code\u003e | A raw NodeJS HTTP incoming message |\n| responseSpec | \u003ccode\u003eObject\u003c/code\u003e | The response specification |\n| err | \u003ccode\u003eHTTPError\u003c/code\u003e | The encountered error |\n\n\u003ca name=\"initWepApplication\"\u003e\u003c/a\u003e\n\n## initWepApplication(API, HANDLERS, [$]) ⇒ \u003ccode\u003eKnifecycle\u003c/code\u003e\nInitialize a web application\n\n**Kind**: global function  \n**Returns**: \u003ccode\u003eKnifecycle\u003c/code\u003e - The passed in Knifecycle instance or the one created\n by default.  \n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| API | \u003ccode\u003eObject\u003c/code\u003e |  | The Swagger definition of the we application |\n| HANDLERS | \u003ccode\u003eObject\u003c/code\u003e |  | The handlers for each operations defined by the  Swagger definition. |\n| [$] | \u003ccode\u003eKnifecycle\u003c/code\u003e | \u003ccode\u003egetInstance(\u003c/code\u003e | A Knifecycle instance on which to set the application  up. |\n\n\u003ca name=\"registerHandlers\"\u003e\u003c/a\u003e\n\n## registerHandlers($, HANDLERS) ⇒ \u003ccode\u003evoid\u003c/code\u003e\nRegister the handlers hash into the given Knifecycle\n instance\n\n**Kind**: global function  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| $ | \u003ccode\u003eKnifecycle\u003c/code\u003e | The Knifecycle instance on which to set up the handlers |\n| HANDLERS | \u003ccode\u003eObject\u003c/code\u003e | The handlers hash |\n\n\u003ca name=\"initHTTPRouter\"\u003e\u003c/a\u003e\n\n## initHTTPRouter(services) ⇒ \u003ccode\u003ePromise\u003c/code\u003e\nInitialize an HTTP router\n\n**Kind**: global function  \n**Returns**: \u003ccode\u003ePromise\u003c/code\u003e - A promise of a function to handle HTTP requests.  \n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| services | \u003ccode\u003eObject\u003c/code\u003e |  | The services the server depends on |\n| services.API | \u003ccode\u003eObject\u003c/code\u003e |  | The Swagger definition of the API |\n| services.HANDLERS | \u003ccode\u003eObject\u003c/code\u003e |  | The handlers for the operations decribe  by the Swagger API definition |\n| [services.ENV] | \u003ccode\u003eObject\u003c/code\u003e |  | The services the server depends on |\n| [services.DEBUG_NODE_ENVS] | \u003ccode\u003eArray\u003c/code\u003e |  | The environnement that activate debugging  (prints stack trace in HTTP errors responses) |\n| [services.BUFFER_LIMIT] | \u003ccode\u003eString\u003c/code\u003e |  | The maximum bufferisation before parsing the  request body |\n| [services.PARSERS] | \u003ccode\u003eObject\u003c/code\u003e |  | The synchronous body parsers (for operations  that defines a request body schema) |\n| [services.STRINGIFYERS] | \u003ccode\u003eObject\u003c/code\u003e |  | The synchronous body stringifyers (for  operations that defines a response body  schema) |\n| [services.log] | \u003ccode\u003efunction\u003c/code\u003e | \u003ccode\u003enoop\u003c/code\u003e | A logging function |\n| services.httpTransaction | \u003ccode\u003efunction\u003c/code\u003e |  | A function to create a new HTTP transaction |\n\n\u003ca name=\"initHTTPServer\"\u003e\u003c/a\u003e\n\n## initHTTPServer(services) ⇒ \u003ccode\u003ePromise\u003c/code\u003e\nInitialize an HTTP server\n\n**Kind**: global function  \n**Returns**: \u003ccode\u003ePromise\u003c/code\u003e - A promise of an object with a NodeJS HTTP server\n in its `service` property.  \n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| services | \u003ccode\u003eObject\u003c/code\u003e |  | The services the server depends on |\n| services.ENV | \u003ccode\u003eObject\u003c/code\u003e |  | The process environment variables |\n| services.httpRouter | \u003ccode\u003efunction\u003c/code\u003e |  | The function to run with the req/res tuple |\n| [services.log] | \u003ccode\u003efunction\u003c/code\u003e | \u003ccode\u003enoop\u003c/code\u003e | A logging function |\n\n\u003ca name=\"initHTTPTransaction\"\u003e\u003c/a\u003e\n\n## initHTTPTransaction(services) ⇒ \u003ccode\u003ePromise.\u0026lt;function()\u0026gt;\u003c/code\u003e\nInstantiate the httpTransaction service\n\n**Kind**: global function  \n**Returns**: \u003ccode\u003ePromise.\u0026lt;function()\u0026gt;\u003c/code\u003e - A promise of the httpTransaction function  \n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| services | \u003ccode\u003eObject\u003c/code\u003e |  | The services to inject |\n| [services.TIMEOUT] | \u003ccode\u003eNumber\u003c/code\u003e | \u003ccode\u003e30000\u003c/code\u003e | A number indicating how many ms the transaction  should take to complete before being cancelled. |\n| [services.TRANSACTIONS] | \u003ccode\u003eObject\u003c/code\u003e | \u003ccode\u003e{}\u003c/code\u003e | A hash of every current transactions |\n| services.time | \u003ccode\u003efunction\u003c/code\u003e |  | A timing function |\n| services.delay | \u003ccode\u003eObject\u003c/code\u003e |  | A delaying service |\n| [services.log] | \u003ccode\u003efunction\u003c/code\u003e |  | A logging function |\n| [services.uniqueId] | \u003ccode\u003efunction\u003c/code\u003e |  | A function returning unique identifiers |\n\n**Example**  \n```js\nimport { initHTTPTransaction } from 'swagger-http-router';\n\nconst httpTransaction = await initHTTPTransaction({\n  log: console.log.bind(console),\n  time: Date.now.bind(Date),\n});\n```\n\u003ca name=\"initHTTPTransaction..httpTransaction\"\u003e\u003c/a\u003e\n\n### initHTTPTransaction~httpTransaction(req, res) ⇒ \u003ccode\u003eArray\u003c/code\u003e\nCreate a new HTTP transaction\n\n**Kind**: inner method of [\u003ccode\u003einitHTTPTransaction\u003c/code\u003e](#initHTTPTransaction)  \n**Returns**: \u003ccode\u003eArray\u003c/code\u003e - The normalized request and the HTTP\ntransaction created in an array.  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| req | \u003ccode\u003eHTTPRequest\u003c/code\u003e | A raw NodeJS HTTP incoming message |\n| res | \u003ccode\u003eHTTPResponse\u003c/code\u003e | A raw NodeJS HTTP response |\n\n\u003ca name=\"flattenSwagger\"\u003e\u003c/a\u003e\n\n## flattenSwagger(API) ⇒ \u003ccode\u003eObject\u003c/code\u003e\nFlatten the inputed Swagger file\n object\n\n**Kind**: global function  \n**Returns**: \u003ccode\u003eObject\u003c/code\u003e - The flattened Swagger definition  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| API | \u003ccode\u003eObject\u003c/code\u003e | An Object containing a parser Swagger JSON |\n\n\u003ca name=\"getSwaggerOperations\"\u003e\u003c/a\u003e\n\n## getSwaggerOperations(API) ⇒ \u003ccode\u003eArray\u003c/code\u003e\nReturn a Swagger operation in a more\n convenient way to iterate onto its\n operations\n\n**Kind**: global function  \n**Returns**: \u003ccode\u003eArray\u003c/code\u003e - An array of all the Swagger operations  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| API | \u003ccode\u003eObject\u003c/code\u003e | The flattened Swagger defition |\n\n**Example**  \n```js\ngetSwaggerOperations(API)\n.map((operation) =\u003e {\n   const { path, method, operationId, parameters } = operation;\n\n  // Do something with that operation\n});\n```\n\n[//]: # (::contents:end)\n\n# API\n## Functions\n\n\u003cdl\u003e\n\u003cdt\u003e\u003ca href=\"#initErrorHandler\"\u003einitErrorHandler(services)\u003c/a\u003e ⇒ \u003ccode\u003ePromise\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eInitialize an error handler for the\nHTTP router\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#initWepApplication\"\u003einitWepApplication(API, HANDLERS, [$])\u003c/a\u003e ⇒ \u003ccode\u003eKnifecycle\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eInitialize a web application\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#initHTTPRouter\"\u003einitHTTPRouter(services)\u003c/a\u003e ⇒ \u003ccode\u003ePromise\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eInitialize an HTTP router\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#initHTTPServer\"\u003einitHTTPServer(services)\u003c/a\u003e ⇒ \u003ccode\u003ePromise\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eInitialize an HTTP server\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#initHTTPTransaction\"\u003einitHTTPTransaction(services)\u003c/a\u003e ⇒ \u003ccode\u003ePromise.\u0026lt;function()\u0026gt;\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eInstantiate the httpTransaction service\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#flattenSwagger\"\u003eflattenSwagger(API)\u003c/a\u003e ⇒ \u003ccode\u003eObject\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eFlatten the inputed Swagger file\n object\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#getSwaggerOperations\"\u003egetSwaggerOperations(API)\u003c/a\u003e ⇒ \u003ccode\u003eArray\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eReturn a Swagger operation in a more\n convenient way to iterate onto its\n operations\u003c/p\u003e\n\u003c/dd\u003e\n\u003c/dl\u003e\n\n\u003ca name=\"initErrorHandler\"\u003e\u003c/a\u003e\n\n## initErrorHandler(services) ⇒ \u003ccode\u003ePromise\u003c/code\u003e\nInitialize an error handler for the\nHTTP router\n\n**Kind**: global function  \n**Returns**: \u003ccode\u003ePromise\u003c/code\u003e - A promise of a function to handle errors  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| services | \u003ccode\u003eObject\u003c/code\u003e | The services the server depends on |\n| [services.ENV] | \u003ccode\u003eObject\u003c/code\u003e | The services the server depends on |\n| [services.DEBUG_NODE_ENVS] | \u003ccode\u003eArray\u003c/code\u003e | The environnement that activate debugging  (prints stack trace in HTTP errors responses) |\n| [services.STRINGIFYERS] | \u003ccode\u003eObject\u003c/code\u003e | The synchronous body stringifyers |\n\n\u003ca name=\"initErrorHandler..errorHandler\"\u003e\u003c/a\u003e\n\n### initErrorHandler~errorHandler(transactionId, responseSpec, err) ⇒ \u003ccode\u003ePromise\u003c/code\u003e\nHandle an HTTP transaction error and\nmap it to a serializable response\n\n**Kind**: inner method of [\u003ccode\u003einitErrorHandler\u003c/code\u003e](#initErrorHandler)  \n**Returns**: \u003ccode\u003ePromise\u003c/code\u003e - A promise resolving when the operation\n completes  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| transactionId | \u003ccode\u003eString\u003c/code\u003e | A raw NodeJS HTTP incoming message |\n| responseSpec | \u003ccode\u003eObject\u003c/code\u003e | The response specification |\n| err | \u003ccode\u003eHTTPError\u003c/code\u003e | The encountered error |\n\n\u003ca name=\"initWepApplication\"\u003e\u003c/a\u003e\n\n## initWepApplication(API, HANDLERS, [$]) ⇒ \u003ccode\u003eKnifecycle\u003c/code\u003e\nInitialize a web application\n\n**Kind**: global function  \n**Returns**: \u003ccode\u003eKnifecycle\u003c/code\u003e - The passed in Knifecycle instance or the one created\n by default.  \n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| API | \u003ccode\u003eObject\u003c/code\u003e |  | The Swagger definition of the we application |\n| HANDLERS | \u003ccode\u003eObject\u003c/code\u003e |  | The handlers for each operations defined by the  Swagger definition. |\n| [$] | \u003ccode\u003eKnifecycle\u003c/code\u003e | \u003ccode\u003egetInstance(\u003c/code\u003e | A Knifecycle instance on which to set the application  up. |\n\n\u003ca name=\"initHTTPRouter\"\u003e\u003c/a\u003e\n\n## initHTTPRouter(services) ⇒ \u003ccode\u003ePromise\u003c/code\u003e\nInitialize an HTTP router\n\n**Kind**: global function  \n**Returns**: \u003ccode\u003ePromise\u003c/code\u003e - A promise of a function to handle HTTP requests.  \n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| services | \u003ccode\u003eObject\u003c/code\u003e |  | The services the server depends on |\n| services.API | \u003ccode\u003eObject\u003c/code\u003e |  | The Swagger definition of the API |\n| services.HANDLERS | \u003ccode\u003eObject\u003c/code\u003e |  | The handlers for the operations decribe  by the Swagger API definition |\n| [services.ENV] | \u003ccode\u003eObject\u003c/code\u003e |  | The services the server depends on |\n| [services.DEBUG_NODE_ENVS] | \u003ccode\u003eArray\u003c/code\u003e |  | The environnement that activate debugging  (prints stack trace in HTTP errors responses) |\n| [services.BUFFER_LIMIT] | \u003ccode\u003eString\u003c/code\u003e |  | The maximum bufferisation before parsing the  request body |\n| [services.PARSERS] | \u003ccode\u003eObject\u003c/code\u003e |  | The synchronous body parsers (for operations  that defines a request body schema) |\n| [services.STRINGIFYERS] | \u003ccode\u003eObject\u003c/code\u003e |  | The synchronous body stringifyers (for  operations that defines a response body  schema) |\n| [services.ENCODERS] | \u003ccode\u003eObject\u003c/code\u003e |  | A map of encoder stream constructors |\n| [services.DECODERS] | \u003ccode\u003eObject\u003c/code\u003e |  | A map of decoder stream constructors |\n| [services.QUERY_PARSER] | \u003ccode\u003eObject\u003c/code\u003e |  | A query parser with the `strict-qs` signature |\n| [services.log] | \u003ccode\u003efunction\u003c/code\u003e | \u003ccode\u003enoop\u003c/code\u003e | A logging function |\n| services.httpTransaction | \u003ccode\u003efunction\u003c/code\u003e |  | A function to create a new HTTP transaction |\n\n\u003ca name=\"initHTTPServer\"\u003e\u003c/a\u003e\n\n## initHTTPServer(services) ⇒ \u003ccode\u003ePromise\u003c/code\u003e\nInitialize an HTTP server\n\n**Kind**: global function  \n**Returns**: \u003ccode\u003ePromise\u003c/code\u003e - A promise of an object with a NodeJS HTTP server\n in its `service` property.  \n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| services | \u003ccode\u003eObject\u003c/code\u003e |  | The services the server depends on |\n| services.ENV | \u003ccode\u003eObject\u003c/code\u003e |  | The process environment variables |\n| services.HOST | \u003ccode\u003eObject\u003c/code\u003e |  | The server host |\n| services.PORT | \u003ccode\u003eObject\u003c/code\u003e |  | The server port |\n| services.MAX_HEADERS_COUNT | \u003ccode\u003eObject\u003c/code\u003e |  | The https://nodejs.org/api/http.html#http_server_maxheaderscount |\n| services.KEEP_ALIVE_TIMEOUT | \u003ccode\u003eObject\u003c/code\u003e |  | See https://nodejs.org/api/http.html#http_server_keepalivetimeout |\n| services.MAX_CONNECTIONS | \u003ccode\u003eObject\u003c/code\u003e |  | See https://nodejs.org/api/net.html#net_server_maxconnections |\n| services.TIMEOUT | \u003ccode\u003eObject\u003c/code\u003e |  | See https://nodejs.org/api/http.html#http_server_timeout |\n| services.httpRouter | \u003ccode\u003efunction\u003c/code\u003e |  | The function to run with the req/res tuple |\n| [services.log] | \u003ccode\u003efunction\u003c/code\u003e | \u003ccode\u003enoop\u003c/code\u003e | A logging function |\n\n\u003ca name=\"initHTTPTransaction\"\u003e\u003c/a\u003e\n\n## initHTTPTransaction(services) ⇒ \u003ccode\u003ePromise.\u0026lt;function()\u0026gt;\u003c/code\u003e\nInstantiate the httpTransaction service\n\n**Kind**: global function  \n**Returns**: \u003ccode\u003ePromise.\u0026lt;function()\u0026gt;\u003c/code\u003e - A promise of the httpTransaction function  \n\n| Param | Type | Default | Description |\n| --- | --- | --- | --- |\n| services | \u003ccode\u003eObject\u003c/code\u003e |  | The services to inject |\n| [services.TIMEOUT] | \u003ccode\u003eNumber\u003c/code\u003e | \u003ccode\u003e30000\u003c/code\u003e | A number indicating how many ms the transaction  should take to complete before being cancelled. |\n| [services.TRANSACTIONS] | \u003ccode\u003eObject\u003c/code\u003e | \u003ccode\u003e{}\u003c/code\u003e | A hash of every current transactions |\n| services.time | \u003ccode\u003efunction\u003c/code\u003e |  | A timing function |\n| services.delay | \u003ccode\u003eObject\u003c/code\u003e |  | A delaying service |\n| [services.log] | \u003ccode\u003efunction\u003c/code\u003e |  | A logging function |\n| [services.uniqueId] | \u003ccode\u003efunction\u003c/code\u003e |  | A function returning unique identifiers |\n\n**Example**  \n```js\nimport { initHTTPTransaction } from 'swagger-http-router';\n\nconst httpTransaction = await initHTTPTransaction({\n  log: console.log.bind(console),\n  time: Date.now.bind(Date),\n});\n```\n\u003ca name=\"initHTTPTransaction..httpTransaction\"\u003e\u003c/a\u003e\n\n### initHTTPTransaction~httpTransaction(req, res) ⇒ \u003ccode\u003eArray\u003c/code\u003e\nCreate a new HTTP transaction\n\n**Kind**: inner method of [\u003ccode\u003einitHTTPTransaction\u003c/code\u003e](#initHTTPTransaction)  \n**Returns**: \u003ccode\u003eArray\u003c/code\u003e - The normalized request and the HTTP\ntransaction created in an array.  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| req | \u003ccode\u003eHTTPRequest\u003c/code\u003e | A raw NodeJS HTTP incoming message |\n| res | \u003ccode\u003eHTTPResponse\u003c/code\u003e | A raw NodeJS HTTP response |\n\n\u003ca name=\"flattenSwagger\"\u003e\u003c/a\u003e\n\n## flattenSwagger(API) ⇒ \u003ccode\u003eObject\u003c/code\u003e\nFlatten the inputed Swagger file\n object\n\n**Kind**: global function  \n**Returns**: \u003ccode\u003eObject\u003c/code\u003e - The flattened Swagger definition  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| API | \u003ccode\u003eObject\u003c/code\u003e | An Object containing a parser Swagger JSON |\n\n\u003ca name=\"getSwaggerOperations\"\u003e\u003c/a\u003e\n\n## getSwaggerOperations(API) ⇒ \u003ccode\u003eArray\u003c/code\u003e\nReturn a Swagger operation in a more\n convenient way to iterate onto its\n operations\n\n**Kind**: global function  \n**Returns**: \u003ccode\u003eArray\u003c/code\u003e - An array of all the Swagger operations  \n\n| Param | Type | Description |\n| --- | --- | --- |\n| API | \u003ccode\u003eObject\u003c/code\u003e | The flattened Swagger defition |\n\n**Example**  \n```js\ngetSwaggerOperations(API)\n.map((operation) =\u003e {\n   const { path, method, operationId, parameters } = operation;\n\n  // Do something with that operation\n});\n```\n\n# Authors\n- [Nicolas Froidure](http://insertafter.com/en/index.html)\n\n# License\n[MIT](https://github.com/nfroidure/swagger-http-router/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnfroidure%2Fswagger-http-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnfroidure%2Fswagger-http-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnfroidure%2Fswagger-http-router/lists"}