{"id":316660,"date":"2021-01-20T15:00:57","date_gmt":"2021-01-20T15:00:57","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=316660"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=316660","title":{"rendered":"\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0430\u043c\u043e\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e\u0441\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043d\u0430 Node.JS"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0423\u0441\u043b\u043e\u0432\u0438\u044f:<\/p>\n<ul>\n<li>\n<p>\u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0447\u0435\u0440\u0435\u0437 Joi<\/p>\n<\/li>\n<li>\n<p>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Typescript<\/p>\n<\/li>\n<li>\n<p>Express \u0441\u0435\u0440\u0432\u0435\u0440<\/p>\n<\/li>\n<li>\n<p>SWAGGER \u043d\u0430 \/api-docs<\/p>\n<\/li>\n<\/ul>\n<p>\u0417\u0430\u0434\u0430\u0447\u0430: <strong>DRY<\/strong><\/p>\n<h2>\u0420\u0435\u0448\u0435\u043d\u0438\u0435:<\/h2>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u0447\u0442\u043e \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e: \u0441\u0445\u0435\u043c\u0430 Joi, Swagger \u0438\u043b\u0438 TypeScript \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441. \u042d\u043c\u043f\u0438\u0440\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043f\u0443\u0442\u0451\u043c \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e \u0447\u0442\u043e \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c Joi.<\/p>\n<h2>1. \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0432\u0441\u0435\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439 \u043d\u0430 Express<\/h2>\n<pre><code>npm install --save swagger-ui-express<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 app.ts (index.ts):<\/p>\n<pre><code>import swaggerUI = require('swagger-ui-express') import swDocument from '.\/openapi' ... app.use('\/api-docs',swaggerUI.serve,swaggerUI.setup(swDocument)) <\/code><\/pre>\n<h2>2. \u0421\u043e\u0437\u0434\u0430\u0442\u044c .\/openapi.ts<\/h2>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0441\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0435. \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0435\u0433\u043e (\u043a\u0430\u043a \u0438 \u0432\u0441\u0435 \u0441\u0445\u0435\u043c\u044b, \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u043d\u043d\u044b\u0435 \u043d\u0438\u0436\u0435) \u043c\u043e\u0436\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e SWAGGER-\u0443\u0442\u0438\u043b\u0438\u0442\u044b. \u0412\u0430\u0436\u043d\u043e \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b openapi v3.0.0<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e:<\/p>\n<pre><code>import {swLoginRoute} from '.\/routes\/login'  const swagger = {   openapi: '3.0.0',   info: {     title: 'Express API for Dangle',     version: '1.0.0',     description: 'The REST API for Dangle Panel service'   },   servers: [     {       url: 'http:\/\/localhost:3001',       description: 'Development server'     }   ],   paths: {     ...swLoginRoute   }, }  export default swagger<\/code><\/pre>\n<p>\u041f\u0443\u0442\u0438 \u0437\u0430\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0438\u0437 \u0440\u043e\u0443\u0442\u0435\u0440\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u043a\u043b\u0443\u0434\u044b.<\/p>\n<h2>3. \u041d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u0440\u043e\u0443\u0442\u0435\u0440\u0430<\/h2>\n<p>\u0412 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u043e\u0443\u0442\u0435\u0440\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c openapi-\u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 <strong>.\/routes\/login\/index.ts<\/strong>:<\/p>\n<pre><code>import {swGetUserInfo} from '.\/get-user-info' import {swUpdateInfo} from '.\/update-info' export const swLoginRoute = {   \"\/login\": {     \"get\": {       ...swGetUserInfo     },     \"patch\": {       ...swUpdateInfo     }   } }<\/code><\/pre>\n<p>\u0412\u044b\u0448\u0435 \u043e\u043f\u0438\u0441\u0430\u043d \u043f\u0443\u0442\u044c \/login, \u043f\u043e\u0434\u0434\u0435\u0436\u0438\u0432\u0430\u044e\u0449\u0438\u0439 \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430: get \u0438 patch. \u0421\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0431\u0435\u0440\u0443\u0442\u0441\u044f \u0438\u043d\u043b\u0443\u0434\u0430\u043c\u0438 \u0438\u0437 \u0444\u0430\u0439\u043b\u043e\u0432 get-user-into.ts \u0438 update-info.ts. \u042d\u0442\u0438 \u0436\u0435 \u0444\u0430\u0439\u043b\u044b \u0443 \u043c\u0435\u043d\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u0441\u0430\u043c\u0438 \u0440\u043e\u0443\u0442\u044b.<\/p>\n<h2>4. \u041d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u0440\u043e\u0443\u0442\u0430 \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445<\/h2>\n<p>\u0421\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0440\u043e\u0443\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 Joi-\u0441\u0445\u0435\u043c\u044b.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0438\u043d\u043a\u043b\u0443\u0434 \u0431\u0443\u0434\u0443\u0449\u0435\u0439 \u0441\u0445\u0435\u043c\u044b \u0432 \u043d\u0430\u0448\u0435\u043c \u0440\u043e\u0443\u0442\u0435.<\/p>\n<p><em>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435: \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043d\u0435 \u0432\u0430\u0436\u043d\u043e \u043a\u0430\u043a \u0432\u044b \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0435 \u0432\u0430\u0448\u0438 \u0444\u0430\u0439\u043b\u044b, \u0435\u0441\u043b\u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442\u0435 \u0438\u043d\u043a\u043b\u0443\u0434\u044b.<\/em><\/p>\n<p>\u0421\u0442\u0440\u043e\u043a\u0438 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 update-info.ts, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d \u043c\u043e\u0439 \u0440\u043e\u0443\u0442 (\u043a\u043e\u0434 \u043a\u043e\u0434 \u0435\u0433\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u043d\u0430\u043c \u043d\u0435 \u0432\u0430\u0436\u0435\u043d):<\/p>\n<pre><code>import schema, {joiSchema} from '.\/update-info.spec\/schema'  export const swUpdateInfo = {   \"summary\": \"update the user info\",   \"tags\": [     \"login\"   ],   \"parameters\": [     {       \"name\": \"key\",       \"in\": \"header\",       \"schema\": {         \"type\": \"string\"       },       \"required\": true     }   ],   \"requestBody\": {     \"content\": {       \"application\/json\": {         \"schema\": {           ...schema         }       }     }   },   \"responses\": {     \"200\": {       \"description\": \"Done\"     },     \"default\": {       \"description\": \"Error message\"     }   } } \/\/ ...\u0434\u0430\u043b\u0435\u0435 \u0438\u0434\u0451\u0442 \u043a\u043e\u0434 \u0440\u043e\u0443\u0442\u0430 <\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 JSON-\u043e\u0431\u044a\u0435\u043a\u0442 \u043c\u043e\u0436\u043d\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0442\u044c \u0442\u043e\u0439 \u0436\u0435 Swagger-\u0443\u0442\u0438\u043b\u0438\u0442\u043e\u0439, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043c\u0443\u0447\u0430\u0442\u044c \u0441\u0435\u0431\u044f. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443:<\/p>\n<pre><code>\"schema\": {   ...schema } <\/code><\/pre>\n<p>\u042d\u0442\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0448\u0435\u0439 \u0441\u0445\u0435\u043c\u044b.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c Joi-\u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0432 \u0440\u043e\u0443\u0442\u0435:<\/p>\n<pre><code>await joiSchema.validateAsync(req.body)<\/code><\/pre>\n<h2>4. \u041f\u0438\u0448\u0435\u043c Joi-\u0441\u0445\u0435\u043c\u0443<\/h2>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 Joi:<\/p>\n<pre><code>npm install --save joi joi-to-swagger<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430:<\/p>\n<pre><code>const joi = require('joi') const j2s = require('joi-to-swagger')  \/\/ Joi export const joiSchema = joi.object().keys({   mode:    joi.string().required(),   email:   joi.string().email() }) \/\/ end of Joi  const schema = j2s(joiSchema).swagger export default schema <\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442 \u044d\u043a\u0441\u043f\u043e\u0440\u0442 Joi-\u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u0435\u0433\u043e swagger-\u0441\u0445\u0435\u043c\u044b.<\/p>\n<p><em>\u0427\u0442\u043e\u0436, \u043d\u0430 \u0434\u0430\u043d\u043d\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0441\u0430\u043c\u043e\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439\u0441\u044f SWAGGER-\u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445. \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e TypeScript-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432<\/em><\/p>\n<h2>5. \u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 TypeScript<\/h2>\n<pre><code>npm install --save-dev gulp @babel\/register @babel\/plugin-proposal-class-properties @babel\/preset-env @babel\/preset-typescript<\/code><\/pre>\n<p>\u0417\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 \u0441\u0435\u0431\u044f \u0432\u043e\u0437\u044c\u043c\u0451\u0442 Gulp. \u042d\u0442\u043e \u0441\u0430\u043c\u0430\u044f \u0447\u0443\u0432\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u043f\u043e\u0434 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430. \u0412\u043e\u0442 \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 gulpfile.ts \u0443 \u043c\u0435\u043d\u044f:<\/p>\n<pre><code>const gulp = require('gulp') const through = require('through2') import { compile } from 'json-schema-to-typescript' const fs = require('fs')  const endName = \"schema.ts\" const routes = `.\/routes\/**\/*.spec\/*${endName}`  function path(str: string) : string {    let base = str    if(base.lastIndexOf(endName) != -1)      base = base.substring(0, base.lastIndexOf(endName))    return base }  gulp.task('schema', () =&gt; {   return gulp.src(routes)     .pipe(through.obj((chunk, enc, cb) =&gt; {       const filename = chunk.path       import(filename).then(schema =&gt; { \/\/ dynamic import         console.log('Converting', filename)         compile(schema.default, `IDTO`)           .then(ts =&gt; {             \/\/console.log(path(filename).concat('interface.ts'), ts)             fs.writeFileSync(path(filename).concat('interface.ts'), ts)           })         })       cb(null, chunk)     })) })   \/\/ watch service const { watch, series } = require('gulp') exports.default = function() {   watch(routes, series('schema')) }<\/code><\/pre>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u043e\u0431\u0445\u043e\u0434\u0438\u0442 \u0432\u0441\u0435 \u043f\u043e\u0434\u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0438 \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c *<em>.spec <\/em>\u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430 \u0441 \u0440\u043e\u0443\u0442\u0435\u0440\u0430. \u0422\u0430\u043c \u043e\u043d \u0438\u0449\u0435\u0442 \u0444\u0430\u0439\u043b\u044b \u0441 \u0438\u043c\u0435\u043d\u0430\u043c\u0438 <em>*<\/em>schema.ts \u0438 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0440\u044f\u0434\u043e\u043c \u0444\u0430\u0439\u043b\u044b c \u0438\u043c\u0435\u043d\u0430\u043c\u0438 *interface.ts<\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0420\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u044d\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0438 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 JSON-\u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0441 openAPI-\u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439 \u043f\u0443\u0433\u0430\u044e\u0442, \u043d\u043e \u043d\u0430\u0434\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u043e\u043d\u0438 \u043d\u0435 \u043f\u0438\u0448\u0443\u0442\u0441\u044f \u0432\u0440\u0443\u0447\u043d\u0443\u044e, \u0430 \u0433\u0435\u0440\u0435\u043d\u044f\u0442\u0441\u044f <a href=\"https:\/\/swagger.io\/tools\/swagger-editor\/\" rel=\"noopener noreferrer nofollow\">SWAGGER-\u0443\u0442\u0438\u043b\u0438\u0442\u043e\u0439.<\/a><\/p>\n<p>\u0418\u0437-\u0437\u0430 \u043d\u0435\u043e\u043f\u044b\u0442\u043d\u043e\u0441\u0442\u0438 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u0430\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043d\u044f\u0442\u044c \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0432\u0440\u0435\u043c\u044f, \u043d\u043e \u044d\u0442\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0438 \u0441\u043e\u0442\u0435\u043d \u0447\u0430\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0431\u044b\u0442\u044c \u043f\u043e\u0442\u0440\u0430\u0447\u0435\u043d\u044b \u043d\u0430 \u0440\u0443\u0442\u0438\u043d\u0443!<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/538260\/\"> https:\/\/habr.com\/ru\/post\/538260\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0423\u0441\u043b\u043e\u0432\u0438\u044f:<\/p>\n<ul>\n<li>\n<p>\u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0447\u0435\u0440\u0435\u0437 Joi<\/p>\n<\/li>\n<li>\n<p>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Typescript<\/p>\n<\/li>\n<li>\n<p>Express \u0441\u0435\u0440\u0432\u0435\u0440<\/p>\n<\/li>\n<li>\n<p>SWAGGER \u043d\u0430 \/api-docs<\/p>\n<\/li>\n<\/ul>\n<p>\u0417\u0430\u0434\u0430\u0447\u0430: <strong>DRY<\/strong><\/p>\n<h2>\u0420\u0435\u0448\u0435\u043d\u0438\u0435:<\/h2>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u0447\u0442\u043e \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e: \u0441\u0445\u0435\u043c\u0430 Joi, Swagger \u0438\u043b\u0438 TypeScript \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441. \u042d\u043c\u043f\u0438\u0440\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043f\u0443\u0442\u0451\u043c \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e \u0447\u0442\u043e \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c Joi.<\/p>\n<h2>1. \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0432\u0441\u0435\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439 \u043d\u0430 Express<\/h2>\n<pre><code>npm install --save swagger-ui-express<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 app.ts (index.ts):<\/p>\n<pre><code>import swaggerUI = require('swagger-ui-express') import swDocument from '.\/openapi' ... app.use('\/api-docs',swaggerUI.serve,swaggerUI.setup(swDocument)) <\/code><\/pre>\n<h2>2. \u0421\u043e\u0437\u0434\u0430\u0442\u044c .\/openapi.ts<\/h2>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0441\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0435. \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0435\u0433\u043e (\u043a\u0430\u043a \u0438 \u0432\u0441\u0435 \u0441\u0445\u0435\u043c\u044b, \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u043d\u043d\u044b\u0435 \u043d\u0438\u0436\u0435) \u043c\u043e\u0436\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e SWAGGER-\u0443\u0442\u0438\u043b\u0438\u0442\u044b. \u0412\u0430\u0436\u043d\u043e \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b openapi v3.0.0<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e:<\/p>\n<pre><code>import {swLoginRoute} from '.\/routes\/login'  const swagger = {   openapi: '3.0.0',   info: {     title: 'Express API for Dangle',     version: '1.0.0',     description: 'The REST API for Dangle Panel service'   },   servers: [     {       url: 'http:\/\/localhost:3001',       description: 'Development server'     }   ],   paths: {     ...swLoginRoute   }, }  export default swagger<\/code><\/pre>\n<p>\u041f\u0443\u0442\u0438 \u0437\u0430\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0438\u0437 \u0440\u043e\u0443\u0442\u0435\u0440\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u043a\u043b\u0443\u0434\u044b.<\/p>\n<h2>3. \u041d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u0440\u043e\u0443\u0442\u0435\u0440\u0430<\/h2>\n<p>\u0412 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u043e\u0443\u0442\u0435\u0440\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c openapi-\u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 <strong>.\/routes\/login\/index.ts<\/strong>:<\/p>\n<pre><code>import {swGetUserInfo} from '.\/get-user-info' import {swUpdateInfo} from '.\/update-info' export const swLoginRoute = {   \"\/login\": {     \"get\": {       ...swGetUserInfo     },     \"patch\": {       ...swUpdateInfo     }   } }<\/code><\/pre>\n<p>\u0412\u044b\u0448\u0435 \u043e\u043f\u0438\u0441\u0430\u043d \u043f\u0443\u0442\u044c \/login, \u043f\u043e\u0434\u0434\u0435\u0436\u0438\u0432\u0430\u044e\u0449\u0438\u0439 \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430: get \u0438 patch. \u0421\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0431\u0435\u0440\u0443\u0442\u0441\u044f \u0438\u043d\u043b\u0443\u0434\u0430\u043c\u0438 \u0438\u0437 \u0444\u0430\u0439\u043b\u043e\u0432 get-user-into.ts \u0438 update-info.ts. \u042d\u0442\u0438 \u0436\u0435 \u0444\u0430\u0439\u043b\u044b \u0443 \u043c\u0435\u043d\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u0441\u0430\u043c\u0438 \u0440\u043e\u0443\u0442\u044b.<\/p>\n<h2>4. \u041d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u0440\u043e\u0443\u0442\u0430 \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445<\/h2>\n<p>\u0421\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0440\u043e\u0443\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 Joi-\u0441\u0445\u0435\u043c\u044b.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0438\u043d\u043a\u043b\u0443\u0434 \u0431\u0443\u0434\u0443\u0449\u0435\u0439 \u0441\u0445\u0435\u043c\u044b \u0432 \u043d\u0430\u0448\u0435\u043c \u0440\u043e\u0443\u0442\u0435.<\/p>\n<p><em>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435: \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043d\u0435 \u0432\u0430\u0436\u043d\u043e \u043a\u0430\u043a \u0432\u044b \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0435 \u0432\u0430\u0448\u0438 \u0444\u0430\u0439\u043b\u044b, \u0435\u0441\u043b\u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442\u0435 \u0438\u043d\u043a\u043b\u0443\u0434\u044b.<\/em><\/p>\n<p>\u0421\u0442\u0440\u043e\u043a\u0438 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 update-info.ts, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d \u043c\u043e\u0439 \u0440\u043e\u0443\u0442 (\u043a\u043e\u0434 \u043a\u043e\u0434 \u0435\u0433\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u043d\u0430\u043c \u043d\u0435 \u0432\u0430\u0436\u0435\u043d):<\/p>\n<pre><code>import schema, {joiSchema} from '.\/update-info.spec\/schema'  export const swUpdateInfo = {   \"summary\": \"update the user info\",   \"tags\": [     \"login\"   ],   \"parameters\": [     {       \"name\": \"key\",       \"in\": \"header\",       \"schema\": {         \"type\": \"string\"       },       \"required\": true     }   ],   \"requestBody\": {     \"content\": {       \"application\/json\": {         \"schema\": {           ...schema         }       }     }   },   \"responses\": {     \"200\": {       \"description\": \"Done\"     },     \"default\": {       \"description\": \"Error message\"     }   } } \/\/ ...\u0434\u0430\u043b\u0435\u0435 \u0438\u0434\u0451\u0442 \u043a\u043e\u0434 \u0440\u043e\u0443\u0442\u0430 <\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 JSON-\u043e\u0431\u044a\u0435\u043a\u0442 \u043c\u043e\u0436\u043d\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0442\u044c \u0442\u043e\u0439 \u0436\u0435 Swagger-\u0443\u0442\u0438\u043b\u0438\u0442\u043e\u0439, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043c\u0443\u0447\u0430\u0442\u044c \u0441\u0435\u0431\u044f. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443:<\/p>\n<pre><code>\"schema\": {   ...schema } <\/code><\/pre>\n<p>\u042d\u0442\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0448\u0435\u0439 \u0441\u0445\u0435\u043c\u044b.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c Joi-\u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0432 \u0440\u043e\u0443\u0442\u0435:<\/p>\n<pre><code>await joiSchema.validateAsync(req.body)<\/code><\/pre>\n<h2>4. \u041f\u0438\u0448\u0435\u043c Joi-\u0441\u0445\u0435\u043c\u0443<\/h2>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 Joi:<\/p>\n<pre><code>npm install --save joi joi-to-swagger<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430:<\/p>\n<pre><code>const joi = require('joi') const j2s = require('joi-to-swagger')  \/\/ Joi export const joiSchema = joi.object().keys({   mode:    joi.string().required(),   email:   joi.string().email() }) \/\/ end of Joi  const schema = j2s(joiSchema).swagger export default schema <\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442 \u044d\u043a\u0441\u043f\u043e\u0440\u0442 Joi-\u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u0435\u0433\u043e swagger-\u0441\u0445\u0435\u043c\u044b.<\/p>\n<p><em>\u0427\u0442\u043e\u0436, \u043d\u0430 \u0434\u0430\u043d\u043d\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0441\u0430\u043c\u043e\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439\u0441\u044f SWAGGER-\u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445. \u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e TypeScript-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432<\/em><\/p>\n<h2>5. \u0413\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u0432 TypeScript<\/h2>\n<pre><code>npm install --save-dev gulp @babel\/register @babel\/plugin-proposal-class-properties @babel\/preset-env @babel\/preset-typescript<\/code><\/pre>\n<p>\u0417\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 \u0441\u0435\u0431\u044f \u0432\u043e\u0437\u044c\u043c\u0451\u0442 Gulp. \u042d\u0442\u043e \u0441\u0430\u043c\u0430\u044f \u0447\u0443\u0432\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u043f\u043e\u0434 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430. \u0412\u043e\u0442 \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 gulpfile.ts \u0443 \u043c\u0435\u043d\u044f:<\/p>\n<pre><code>const gulp = require('gulp') const through = require('through2') import { compile } from 'json-schema-to-typescript' const fs = require('fs')  const endName = \"schema.ts\" const routes = `.\/routes\/**\/*.spec\/*${endName}`  function path(str: string) : string {    let base = str    if(base.lastIndexOf(endName) != -1)      base = base.substring(0, base.lastIndexOf(endName))    return base }  gulp.task('schema', () =&gt; {   return gulp.src(routes)     .pipe(through.obj((chunk, enc, cb) =&gt; {       const filename = chunk.path       import(filename).then(schema =&gt; { \/\/ dynamic import         console.log('Converting', filename)         compile(schema.default, `IDTO`)           .then(ts =&gt; {             \/\/console.log(path(filename).concat('interface.ts'), ts)             fs.writeFileSync(path(filename).concat('interface.ts'), ts)           })         })       cb(null, chunk)     })) })   \/\/ watch service const { watch, series } = require('gulp') exports.default = function() {   watch(routes, series('schema')) }<\/code><\/pre>\n<p>\u0421\u043a\u0440\u0438\u043f\u0442 \u043e\u0431\u0445\u043e\u0434\u0438\u0442 \u0432\u0441\u0435 \u043f\u043e\u0434\u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0438 \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c *<em>.spec <\/em>\u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430 \u0441 \u0440\u043e\u0443\u0442\u0435\u0440\u0430. \u0422\u0430\u043c \u043e\u043d \u0438\u0449\u0435\u0442 \u0444\u0430\u0439\u043b\u044b \u0441 \u0438\u043c\u0435\u043d\u0430\u043c\u0438 <em>*<\/em>schema.ts \u0438 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0440\u044f\u0434\u043e\u043c \u0444\u0430\u0439\u043b\u044b c \u0438\u043c\u0435\u043d\u0430\u043c\u0438 *interface.ts<\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0420\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u044d\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0438 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 JSON-\u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0441 openAPI-\u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439 \u043f\u0443\u0433\u0430\u044e\u0442, \u043d\u043e \u043d\u0430\u0434\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u043e\u043d\u0438 \u043d\u0435 \u043f\u0438\u0448\u0443\u0442\u0441\u044f \u0432\u0440\u0443\u0447\u043d\u0443\u044e, \u0430 \u0433\u0435\u0440\u0435\u043d\u044f\u0442\u0441\u044f <a href=\"https:\/\/swagger.io\/tools\/swagger-editor\/\" rel=\"noopener noreferrer nofollow\">SWAGGER-\u0443\u0442\u0438\u043b\u0438\u0442\u043e\u0439.<\/a><\/p>\n<p>\u0418\u0437-\u0437\u0430 \u043d\u0435\u043e\u043f\u044b\u0442\u043d\u043e\u0441\u0442\u0438 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u0430\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043d\u044f\u0442\u044c \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0432\u0440\u0435\u043c\u044f, \u043d\u043e \u044d\u0442\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0438 \u0441\u043e\u0442\u0435\u043d \u0447\u0430\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0431\u044b\u0442\u044c \u043f\u043e\u0442\u0440\u0430\u0447\u0435\u043d\u044b \u043d\u0430 \u0440\u0443\u0442\u0438\u043d\u0443!<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/538260\/\"> https:\/\/habr.com\/ru\/post\/538260\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-316660","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/316660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=316660"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/316660\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=316660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=316660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=316660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}