UNPKG

@grouparoo/core

Version:
171 lines (170 loc) 6.92 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Swagger = void 0; const actionhero_1 = require("actionhero"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const authenticatedAction_1 = require("../classes/actions/authenticatedAction"); const SWAGGER_VERSION = "2.0"; const API_VERSION = "v1"; const parentPackageJSON = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json")).toString()); const responses = { 200: { description: "successful operation", }, 400: { description: "Invalid input", }, 404: { description: "Not Found", }, 422: { description: "Missing or invalid params", }, 500: { description: "Server error", }, }; class Swagger extends actionhero_1.Action { constructor() { super(...arguments); this.name = "swagger"; this.description = "return API documentation in the OpenAPI specification"; this.outputExample = {}; } getLatestAction(route) { let matchedAction; Object.keys(actionhero_1.api.actions.actions).forEach((actionName) => { Object.keys(actionhero_1.api.actions.actions[actionName]).forEach((version) => { const action = actionhero_1.api.actions.actions[actionName][version]; if (action.name === route.action) { matchedAction = action; } }); }); return matchedAction; } checkSkippedRoutes(route) { if (route.path.match(/\/resque\//)) return false; return true; } buildSwaggerPaths() { const swaggerPaths = {}; const tags = []; for (const [method, routes] of Object.entries(actionhero_1.api.routes.routes)) { routes.map((route) => { const action = this.getLatestAction(route); if (!action) return; const tag = action.name.split(":")[0]; const formattedPath = route.path .replace("/v:apiVersion", "") .replace(/\/:(\w*)/g, "/{$1}"); swaggerPaths[formattedPath] = swaggerPaths[formattedPath] || {}; swaggerPaths[formattedPath][method] = { tags: [tag], summary: action.description, // description: action.description, // this is redundant consumes: ["application/json"], produces: ["application/json"], parameters: Object.keys(action.inputs) .sort() .map((inputName) => { return { in: route.path.includes(`:${inputName}`) ? "path" : "query", name: inputName, type: "string", required: action.inputs[inputName].required || route.path.includes(`:${inputName}`) ? true : false, default: action.inputs[inputName].default !== null && action.inputs[inputName].default !== undefined ? typeof action.inputs[inputName].default === "object" ? JSON.stringify(action.inputs[inputName].default) : typeof action.inputs[inputName].default === "function" ? action.inputs[inputName].default() : `${action.inputs[inputName].default}` : undefined, }; }), responses, security: action instanceof authenticatedAction_1.AuthenticatedAction && action["permission"] ? [ { GrouparooCSRFTokenAndSessionCookie: [] }, { GrouparooAPIKey: [] }, ] : [], }; if (!tags.includes(tag)) { tags.push(tag); } }); } return { swaggerPaths, tags }; } async run() { const { swaggerPaths, tags } = this.buildSwaggerPaths(); return { swagger: SWAGGER_VERSION, info: { description: parentPackageJSON.description, version: parentPackageJSON.version, title: parentPackageJSON.name, license: { name: parentPackageJSON.license }, }, host: process.env.WEB_URL ? process.env.WEB_URL.replace("https://", "").replace("http://", "") : "localhost", basePath: `/api/${API_VERSION}`, // tags: tags.map((tag) => { // return { name: tag, description: `topic: ${tag}` }; // }), schemes: process.env.WEB_URL && process.env.WEB_URL.includes("https://") ? ["https", "http"] : ["http"], paths: swaggerPaths, securityDefinitions: { GrouparooAPIKey: { type: "apiKey", name: "apiKey", in: "query", }, GrouparooCSRFTokenAndSessionCookie: { type: "apiKey", name: "csrfToken", in: "query", }, }, externalDocs: { description: "Learn more about Grouparoo", url: "https://www.grouparoo.com", }, }; } } exports.Swagger = Swagger;