https://github.com/AlexKorole/testExp...
1) npm install swagger-ui-express
2) tsconfig.json -- "resolveJsonModule": true
3) src -- swagger -- openapi.json
4) index.ts
import swaggerUi from 'swagger-ui-express';
import * as swaggerDocument from '../src/swagger/openapi.json';
...
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
5) change port to 3000
.env-- PORT=3000
6) npm install cors
7) npm install @types/cors
8) index.ts --
import cors from 'cors';
...
app.use(cors() as RequestHandler);
9) Swagger Editor
openapi
openapi: 3.0.1
info:
title: test API
version: v1
servers:
url: 'http://localhost:3000'
tags:
name: API functions
description: 'стрелка вправо'-
API functions of our application
paths:
/users/{id}:
get:
summary: returns simple answer from get
tags:
API functions
parameters:
name: id
in: path
required: true
description: simple parameter
schema:
type : string
example: '1'
description: parameter id just for test
responses:
'200': #status code
description: OK
content:
document:
schema:
type: string
example: some text
post:
summary: returns simple answer from post
tags:
API functions
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Info'
example:
country: Russia
city: Spb
parameters:
name: id
in: path
required: true
description: simple parameter
schema:
type : string
example: '1'
description: parameter id just for test
responses:
'204': #status code
description: OK
components:
schemas:
Info:
type: object
properties:
country:
type: string
city:
type: string