项目作者: davidnussio

项目描述 :
Moleculer swagger service
高级语言: JavaScript
项目地址: git://github.com/davidnussio/moleculer-swagger.git
创建时间: 2020-12-10T21:51:16Z
项目社区:https://github.com/davidnussio/moleculer-swagger

开源协议:MIT License

下载


DEPRECATED

use moleculer-auto-openapi


Build Status
Coverage Status
Code Climate maintainability
David
Known Vulnerabilities

moleculer-swagger NPM version

Moleculer Service Mixin that convert internals moleculer structure to Swagger OpenAPI

More about openapi specification

Getting started

Create moleculer services

  1. const { ServiceBroker } = require("moleculer");
  2. const APIGateway = require("moleculer-web");
  3. const Swagger = require("moleculer-swagger");
  4. broker.createService({
  5. name: "swagger",
  6. mixins: [Swagger()],
  7. });
  8. broker.createService({
  9. name: "api",
  10. mixins: [APIGateway],
  11. settings: {
  12. routes: [
  13. {
  14. path: "/swagger.json",
  15. aliases: {
  16. "GET /": "swagger.openapi",
  17. },
  18. },
  19. {
  20. path: "/",
  21. autoAliases: true,
  22. },
  23. ],
  24. assets: {
  25. folder: "example/public", // Where create swagger.html page
  26. },
  27. },
  28. });
  29. broker.createService({
  30. name: "service-1",
  31. actions: {
  32. list: {
  33. rest: "GET /",
  34. params: {
  35. filter: "string",
  36. counter: "number|min:0|max:100",
  37. superCounter: { type: "number", min: 100, max: 1000 },
  38. },
  39. handler(ctx) {
  40. return { name: "sevice-1.list", ...ctx.params };
  41. },
  42. },
  43. superApi: {
  44. description: `Lorem ipsum dolor sit amet, [consectetur adipiscing elit](https://www.google.com), sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`,
  45. tags: ["service-1", "super-api"],
  46. rest: "POST /:id/super/:mode/api",
  47. params: {
  48. id: "number",
  49. mode: "string",
  50. filter: { type: "array", items: "string" },
  51. },
  52. handler(ctx) {
  53. return { name: "sevice-1.list", ...ctx.params };
  54. },
  55. },
  56. },
  57. });
  58. broker.start();

Swagger UI

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Swagger UI</title>
  5. <!-- needed for adaptive design -->
  6. <meta charset="utf-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1" />
  8. <link
  9. href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700"
  10. rel="stylesheet"
  11. />
  12. <link
  13. rel="stylesheet"
  14. type="text/css"
  15. href="https://unpkg.com/swagger-ui-dist@3/swagger-ui.css"
  16. />
  17. <!--
  18. script doesn't change outer page styles
  19. -->
  20. <style>
  21. body {
  22. margin: 0;
  23. padding: 0;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="swagger-ui"></div>
  29. <script
  30. src="https://unpkg.com/swagger-ui-dist@3/swagger-ui-standalone-preset.js"
  31. charset="UTF-8"
  32. ></script>
  33. <script
  34. src="https://unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"
  35. charset="UTF-8"
  36. ></script>
  37. <script type="text/javascript">
  38. window.onload = function () {
  39. const ui = SwaggerUIBundle({
  40. url: "/swagger.json",
  41. dom_id: "#swagger-ui",
  42. deepLinking: true,
  43. presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset],
  44. plugins: [SwaggerUIBundle.plugins.DownloadUrl],
  45. layout: "StandaloneLayout",
  46. });
  47. window.ui = ui;
  48. };
  49. </script>
  50. </body>
  51. </html>
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>ReDoc</title>
  5. <!-- needed for adaptive design -->
  6. <meta charset="utf-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1" />
  8. <link
  9. href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700"
  10. rel="stylesheet"
  11. />
  12. <!--
  13. ReDoc doesn't change outer page styles
  14. -->
  15. <style>
  16. body {
  17. margin: 0;
  18. padding: 0;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <redoc spec-url="/swagger.json"></redoc>
  24. <script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"></script>
  25. </body>
  26. </html>

Swagger settings

These settings would be updated to match openapi 3.0 specification

  1. Swagger({
  2. openapiSchema: {
  3. openapi: "3.0.3",
  4. info: {
  5. title: "The title of the API.",
  6. description:
  7. "A short description of the API. CommonMark syntax MAY be used for rich text representation.",
  8. termsOfService: "http://swagger.io/terms/",
  9. contact: {
  10. email: "apiteam@swagger.io",
  11. },
  12. license: {
  13. name: "Apache 2.0",
  14. url: "http://www.apache.org/licenses/LICENSE-2.0.html",
  15. },
  16. version: "3.0.3",
  17. },
  18. components: {
  19. responses: {
  20. Success: {
  21. description: "Successful operation",
  22. content: {
  23. "application/json": { schema: { type: "string" } },
  24. "text/html": { schema: { type: "string" } },
  25. },
  26. },
  27. Unauthorized: {
  28. description: "Unauthorized",
  29. },
  30. NotFound: {
  31. description: "The specified resource was not found",
  32. },
  33. ServiceUnaviable: {
  34. description: "Service Unaviable",
  35. },
  36. },
  37. },
  38. },
  39. defaults: {
  40. responses: {
  41. 200: {
  42. $ref: "#/components/responses/Success",
  43. },
  44. 401: {
  45. $ref: "#/components/responses/Unauthorized",
  46. },
  47. 404: {
  48. $ref: "#/components/responses/NotFound",
  49. },
  50. 503: { $ref: "#/components/responses/ServiceUnaviable" },
  51. },
  52. },
  53. });