Skip to content

Commit be7fc2a

Browse files
committed
added authorization support in swagger view
1 parent 04da0e0 commit be7fc2a

File tree

10 files changed

+144
-80
lines changed

10 files changed

+144
-80
lines changed

source/api/v1/validation/openapi/responses/AuthResponses.ts renamed to source/api/v1/openapi/responses/AuthResponses.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { AUTH_EXCEPTIONS } from "../../../shared/exceptions/AuthExceptions";
2-
import { USER_EXCEPTIONS } from "../../../shared/exceptions/UserExceptions";
1+
import { AUTH_EXCEPTIONS } from "../../shared/exceptions/AuthExceptions";
2+
import { USER_EXCEPTIONS } from "../../shared/exceptions/UserExceptions";
33

44
export const AUTH_RESPONSES = {
55
Authorize: {

source/api/v1/validation/openapi/responses/NoteResponses.ts renamed to source/api/v1/openapi/responses/NoteResponses.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { excludeProperties } from "typing-assets";
2-
import { NOTE_EXCEPTIONS } from "../../../shared/exceptions/NoteExceptions";
3-
import { USER_EXCEPTIONS } from "../../../shared/exceptions/UserExceptions";
4-
import { BaseNoteSchema } from "../../schemas/base/Note";
5-
import { BaseUserSchema } from "../../schemas/base/User";
2+
import { NOTE_EXCEPTIONS } from "../../shared/exceptions/NoteExceptions";
3+
import { USER_EXCEPTIONS } from "../../shared/exceptions/UserExceptions";
4+
import { BaseNoteSchema } from "../../validation/schemas/base/Note";
5+
import { BaseUserSchema } from "../../validation/schemas/base/User";
66

77
export const NOTE_RESPONSES = {
88
CreateNote: {

source/api/v1/validation/openapi/responses/UserResponses.ts renamed to source/api/v1/openapi/responses/UserResponses.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { excludeProperties } from "typing-assets";
2-
import { BaseUserSchema } from "../../schemas/base/User";
3-
import { USER_EXCEPTIONS } from "../../../shared/exceptions/UserExceptions";
2+
import { BaseUserSchema } from "../../validation/schemas/base/User";
3+
import { USER_EXCEPTIONS } from "../../shared/exceptions/UserExceptions";
44

55
export const USER_RESPONSES = {
66
CreateUser: {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import {FastifyInstance} from "fastify";
2+
const swagger = require("@fastify/swagger");
3+
const swaggerUI = require("@fastify/swagger-ui");
4+
5+
export const initSwaggerViewer = async (
6+
server: FastifyInstance
7+
): Promise<void> => {
8+
await server.register(swagger, {
9+
openapi: {
10+
openapi: "3.0.0",
11+
info: {
12+
title: "NodeNotes (api - v1)",
13+
description:
14+
"NodeNotes backend REST API endpoints specification",
15+
version: "1.0.0"
16+
},
17+
servers: [
18+
{
19+
url: "http://localhost:8000",
20+
description: "Development server"
21+
}
22+
],
23+
tags: [
24+
{
25+
name: "users",
26+
description:
27+
'User related end-points (may require JWT in "Bearer" auth header)'
28+
},
29+
{
30+
name: "auth",
31+
description:
32+
'Authorization end-points (may require JWT in "Bearer" auth header)'
33+
},
34+
{
35+
name: "notes",
36+
description:
37+
'Note related end-points (requires JWT in "Bearer" auth header)'
38+
}
39+
],
40+
components: {
41+
securitySchemes: {
42+
bearerAuth: {
43+
type: 'http',
44+
scheme: 'bearer'
45+
}
46+
}
47+
},
48+
externalDocs: {
49+
url: "https://github.com/LCcodder/NodeNotes",
50+
description: "GitHub repo"
51+
}
52+
}
53+
});
54+
55+
await server.register(swaggerUI, {
56+
routePrefix: "/documentation",
57+
uiConfig: {
58+
deepLinking: false
59+
},
60+
staticCSP: true,
61+
transformStaticCSP: (header: any) => header,
62+
transformSpecification: (
63+
swaggerObject: any,
64+
_request: any,
65+
_reply: any
66+
) => {
67+
return swaggerObject;
68+
},
69+
transformSpecificationClone: true
70+
});
71+
};

source/api/v1/shared/utils/typing/FastifySchemaOverride.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import { FastifySchema as OGFastifySchema} from "fastify";
33
export type FastifySchema = {
44
description?: string
55
tags?: string[]
6+
security?: any[]
67
} & OGFastifySchema
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
1-
import { pickProperties } from "typing-assets";
2-
import { BaseUserSchema } from "./base/User";
3-
import { AUTH_RESPONSES } from "../openapi/responses/AuthResponses";
4-
import { FastifySchema } from "../../shared/utils/typing/FastifySchemaOverride";
1+
import {pickProperties} from "typing-assets";
2+
import {BaseUserSchema} from "./base/User";
3+
import {AUTH_RESPONSES} from "../../openapi/responses/AuthResponses";
4+
import {FastifySchema} from "../../shared/utils/typing/FastifySchemaOverride";
55

66
export const AuthUserSchema: FastifySchema = {
7-
87
body: {
9-
type: 'object',
8+
type: "object",
109
properties: pickProperties(
1110
{...BaseUserSchema.properties},
1211
"email",
1312
"password"
1413
),
15-
required: ['email', 'password'],
14+
required: ["email", "password"]
1615
},
1716

1817
// openapi snippets
19-
description: 'Authorization with email and password which returns token in response',
18+
description:
19+
"Authorization with email and password which returns token in response",
2020
tags: ["auth"],
2121
response: AUTH_RESPONSES.Authorize
22-
}
23-
22+
};
2423

2524
export const ChangePasswordSchema: FastifySchema = {
26-
2725
body: {
28-
type: 'object',
26+
type: "object",
2927
properties: {
3028
oldPassword: BaseUserSchema.properties.password,
3129
newPassword: BaseUserSchema.properties.password
3230
},
33-
required: ['oldPassword', 'newPassword']
31+
required: ["oldPassword", "newPassword"]
3432
},
3533

3634
// openapi snippets
37-
description: 'Password change and reset current token',
35+
description: "Password change and reset current token",
3836
tags: ["auth"],
39-
response: AUTH_RESPONSES.ChangePassword
40-
}
37+
response: AUTH_RESPONSES.ChangePassword,
38+
security: [{
39+
bearerAuth: []
40+
}]
41+
};

source/api/v1/validation/schemas/NoteSchemas.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { excludeProperties, pickProperties } from "typing-assets"
22
import { BaseNoteSchema, OperateNoteSchema } from "./base/Note"
3-
import { NOTE_RESPONSES } from "../openapi/responses/NoteResponses"
3+
import { NOTE_RESPONSES } from "../../openapi/responses/NoteResponses"
44
import { FastifySchema } from "../../shared/utils/typing/FastifySchemaOverride"
55

66
export const CreateNoteSchema: FastifySchema = {
@@ -19,7 +19,10 @@ export const CreateNoteSchema: FastifySchema = {
1919
// openapi snippets
2020
description: "Creates and returns created note (also adds provided collaborators to the note)",
2121
tags: ["notes"],
22-
response: NOTE_RESPONSES.CreateNote
22+
response: NOTE_RESPONSES.CreateNote,
23+
security: [{
24+
bearerAuth: []
25+
}]
2326
}
2427

2528
export const GetNotesSchema: FastifySchema = {
@@ -37,7 +40,10 @@ export const GetNotesSchema: FastifySchema = {
3740
// openapi snippets
3841
description: "Returns notes array by provided params",
3942
tags: ["notes"],
40-
response: NOTE_RESPONSES.GetNotes
43+
response: NOTE_RESPONSES.GetNotes,
44+
security: [{
45+
bearerAuth: []
46+
}]
4147
}
4248

4349

@@ -49,7 +55,10 @@ export const GetNoteSchema: FastifySchema = {
4955
// openapi snippets
5056
description: "Returns note by id",
5157
tags: ["notes"],
52-
response: NOTE_RESPONSES.GetNote
58+
response: NOTE_RESPONSES.GetNote,
59+
security: [{
60+
bearerAuth: []
61+
}]
5362
}
5463

5564
export const DeleteNoteSchema: FastifySchema = {
@@ -59,7 +68,10 @@ export const DeleteNoteSchema: FastifySchema = {
5968
// openapi snippets
6069
description: "Deletes note by id",
6170
tags: ["notes"],
62-
response: NOTE_RESPONSES.DeleteNote
71+
response: NOTE_RESPONSES.DeleteNote,
72+
security: [{
73+
bearerAuth: []
74+
}]
6375
}
6476

6577
export const GetNoteCollaboratorsSchema: FastifySchema = {
@@ -69,7 +81,10 @@ export const GetNoteCollaboratorsSchema: FastifySchema = {
6981
// openapi snippets
7082
description: "Returns note collaborators array",
7183
tags: ["notes"],
72-
response: NOTE_RESPONSES.GetCollaborators
84+
response: NOTE_RESPONSES.GetCollaborators,
85+
security: [{
86+
bearerAuth: []
87+
}]
7388
}
7489

7590
export const UpdateNoteSchema: FastifySchema = {
@@ -84,7 +99,10 @@ export const UpdateNoteSchema: FastifySchema = {
8499
// openapi snippets
85100
description: "Updates note and returns updated value (can also be updated by collaborator)",
86101
tags: ["notes"],
87-
response: NOTE_RESPONSES.UpdateNote
102+
response: NOTE_RESPONSES.UpdateNote,
103+
security: [{
104+
bearerAuth: []
105+
}]
88106
}
89107

90108
export const AddCollaboratorSchema: FastifySchema = {
@@ -105,7 +123,10 @@ export const AddCollaboratorSchema: FastifySchema = {
105123
// openapi snippets
106124
description: "Adds collaborator to the note",
107125
tags: ["notes"],
108-
response: NOTE_RESPONSES.AddCollaborator
126+
response: NOTE_RESPONSES.AddCollaborator,
127+
security: [{
128+
bearerAuth: []
129+
}]
109130
}
110131

111132
export const RemoveCollaboratorSchema: FastifySchema = {
@@ -126,5 +147,8 @@ export const RemoveCollaboratorSchema: FastifySchema = {
126147
// openapi snippets
127148
description: "Removes collaborator from note",
128149
tags: ["notes"],
129-
response: NOTE_RESPONSES.RemoveCollaborator
150+
response: NOTE_RESPONSES.RemoveCollaborator,
151+
security: [{
152+
bearerAuth: []
153+
}]
130154
}

source/api/v1/validation/schemas/UserSchemas.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FastifySchema } from "../../shared/utils/typing/FastifySchemaOverride";
22
import { excludeProperties, pickProperties } from "typing-assets"
33
import { BaseUserSchema } from "./base/User"
4-
import { USER_RESPONSES } from "../openapi/responses/UserResponses"
4+
import { USER_RESPONSES } from "../../openapi/responses/UserResponses";
55

66
export const CreateUserSchema: FastifySchema = {
77
body: {
@@ -34,14 +34,20 @@ export const GetUserSchema: FastifySchema = {
3434
// openapi snippets
3535
description: "Returns user profile by login",
3636
tags: ["users"],
37-
response: USER_RESPONSES.GetUser
37+
response: USER_RESPONSES.GetUser,
38+
security: [{
39+
bearerAuth: []
40+
}]
3841
}
3942

4043
export const GetMyProfileSchema: FastifySchema = {
4144
// openapi snippets
4245
description: "Returns current user profile by provided JWT",
4346
tags: ["users"],
44-
response: USER_RESPONSES.GetUser
47+
response: USER_RESPONSES.GetUser,
48+
security: [{
49+
bearerAuth: []
50+
}]
4551
}
4652

4753
export const UpdateUserSchema: FastifySchema = {
@@ -59,5 +65,8 @@ export const UpdateUserSchema: FastifySchema = {
5965
// openapi snippets
6066
description: "Updates and returns updated user",
6167
tags: ["users"],
62-
response: USER_RESPONSES.UpdateUser
68+
response: USER_RESPONSES.UpdateUser,
69+
security: [{
70+
bearerAuth: []
71+
}]
6372
}

source/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import "reflect-metadata"
1313
import { UserEntity } from './api/v1/database/entities/User'
1414
import { initAndGetDataSource } from './api/v1/database/InitDataSource'
1515
import { NoteEntity } from './api/v1/database/entities/Note'
16-
import { initSwaggerViewer } from './openapi/InitSwagger'
16+
import { initSwaggerViewer } from './api/v1/openapi/swagger/InitSwagger'
1717
import { connectAndGetRedisInstance } from './api/v1/cache/InitRedisInstance'
1818

1919
const main = async () => {

source/openapi/InitSwagger.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)