moahmedwafy commited on
Commit
73c4c20
·
1 Parent(s): 7a8d84c

feat: swagger query

Browse files
src/lib/decorators/swagger-query.decorator.ts ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { swaggerRegistry } from "@lib/swagger/swagger";
2
+ import { getCallingFileName } from "@lib/utils/calling-file.helper";
3
+
4
+ type QueryType = "string" | "number" | "boolean";
5
+
6
+ export const SwaggerQuery = (
7
+ querySchema: Record<
8
+ string,
9
+ | {
10
+ type: QueryType | (string | number | boolean)[];
11
+ required?: boolean;
12
+ }
13
+ | QueryType
14
+ | (string | number | boolean)[]
15
+ >
16
+ ) => {
17
+ return (target: any, propertyKey: string) => {
18
+ const queryParams = Object.entries(querySchema).map(([key, type]) => {
19
+ let schema;
20
+
21
+ if (Array.isArray(type)) {
22
+ schema = {
23
+ type: "string",
24
+ enum: type,
25
+ };
26
+ } else if (typeof type === "string") {
27
+ schema = {
28
+ type,
29
+ };
30
+ } else {
31
+ schema = Array.isArray(type["type"])
32
+ ? {
33
+ type: "string",
34
+ enum: type["type"],
35
+ }
36
+ : {
37
+ type: type.type,
38
+ };
39
+ }
40
+
41
+ return {
42
+ name: key,
43
+ in: "query",
44
+ required: type["required"] || false,
45
+ schema,
46
+ };
47
+ });
48
+
49
+ target.constructor["targetName"] =
50
+ target.constructor.name + getCallingFileName();
51
+ swaggerRegistry.updateRoute(target.constructor["targetName"], {
52
+ propertyKey,
53
+ queryParams,
54
+ });
55
+ };
56
+ };
src/lib/swagger/swagger.ts CHANGED
@@ -13,7 +13,7 @@ class SwaggerRegistry {
13
  method?: "get" | "post" | "put" | "patch" | "delete";
14
  request?: any;
15
  response?: any;
16
- query?: any;
17
  description?: string;
18
  summary?: string;
19
  tags?: string[];
@@ -81,7 +81,7 @@ class SwaggerRegistry {
81
  method?: "get" | "post" | "put" | "patch" | "delete";
82
  request?: any;
83
  response?: any;
84
- query?: any;
85
  description?: string;
86
  summary?: string;
87
  tags?: string[];
@@ -137,8 +137,8 @@ class SwaggerRegistry {
137
  tags: [...(controllerData.tags || []), ...(route.tags || [])],
138
  summary: route.summary,
139
  description: route.description,
140
- parameters:
141
- (params &&
142
  params.map((param) => {
143
  return {
144
  name: param.replace(":", ""),
@@ -149,7 +149,9 @@ class SwaggerRegistry {
149
  },
150
  };
151
  })) ||
152
- [],
 
 
153
  responses: {
154
  200: {
155
  description: "Success",
 
13
  method?: "get" | "post" | "put" | "patch" | "delete";
14
  request?: any;
15
  response?: any;
16
+ queryParams?: any;
17
  description?: string;
18
  summary?: string;
19
  tags?: string[];
 
81
  method?: "get" | "post" | "put" | "patch" | "delete";
82
  request?: any;
83
  response?: any;
84
+ queryParams?: any;
85
  description?: string;
86
  summary?: string;
87
  tags?: string[];
 
137
  tags: [...(controllerData.tags || []), ...(route.tags || [])],
138
  summary: route.summary,
139
  description: route.description,
140
+ parameters: [
141
+ ...((params &&
142
  params.map((param) => {
143
  return {
144
  name: param.replace(":", ""),
 
149
  },
150
  };
151
  })) ||
152
+ []),
153
+ ...(route.queryParams || []),
154
+ ],
155
  responses: {
156
  200: {
157
  description: "Success",
src/modules/console/modules/admins/controllers/admins.controller.ts CHANGED
@@ -26,6 +26,7 @@ import { SwaggerResponse } from "@lib/decorators/swagger-response.decorator";
26
  import { SwaggerRequest } from "@lib/decorators/swagger-request.decorator";
27
  import { SwaggerSummary } from "@lib/decorators/swagger-summary.decorator";
28
  import { SwaggerDescription } from "@lib/decorators/swagger-description.decorator";
 
29
 
30
  @Controller("/console/admins")
31
  @ControllerMiddleware(AdminGuardMiddleware({ roles: [Role.SUPER_ADMIN] }))
@@ -57,6 +58,15 @@ export class AdminsController extends BaseController {
57
  @SwaggerResponse([AdminSerialization])
58
  @SwaggerSummary("List all admins")
59
  @SwaggerDescription("List all admins")
 
 
 
 
 
 
 
 
 
60
  list = async (
61
  req: Request,
62
  res: Response<IJSONSuccessResponse<AdminSerialization[]>>
 
26
  import { SwaggerRequest } from "@lib/decorators/swagger-request.decorator";
27
  import { SwaggerSummary } from "@lib/decorators/swagger-summary.decorator";
28
  import { SwaggerDescription } from "@lib/decorators/swagger-description.decorator";
29
+ import { SwaggerQuery } from "@lib/decorators/swagger-query.decorator";
30
 
31
  @Controller("/console/admins")
32
  @ControllerMiddleware(AdminGuardMiddleware({ roles: [Role.SUPER_ADMIN] }))
 
58
  @SwaggerResponse([AdminSerialization])
59
  @SwaggerSummary("List all admins")
60
  @SwaggerDescription("List all admins")
61
+ @SwaggerQuery({
62
+ exampleRequiredQuery: {
63
+ type: "string",
64
+ required: true,
65
+ },
66
+ exampleOptionQuery: ["option 1", "option 2", 3],
67
+ exampleSimpleQuery: "number",
68
+ exampleSimpleQuery2: "boolean",
69
+ })
70
  list = async (
71
  req: Request,
72
  res: Response<IJSONSuccessResponse<AdminSerialization[]>>