Spaces:
Running
Running
Commit
·
d86b75d
1
Parent(s):
7c6b60f
feat: get call file name
Browse files- src/common/serializers/user-registered-workout.serialization.ts +2 -1
- src/lib/decorators/prefix.decorator.ts +5 -2
- src/lib/decorators/swagger-request.decorator.ts +3 -1
- src/lib/decorators/swagger-response-property.decorator.ts +5 -2
- src/lib/decorators/swagger-response.decorator.ts +4 -2
- src/lib/decorators/swagger-routes.decorator.ts +3 -1
- src/lib/decorators/swagger-tags.decorator.ts +3 -1
- src/lib/utils/calling-file.helper.ts +12 -0
- test.ts +5 -0
src/common/serializers/user-registered-workout.serialization.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import { Expose, Transform } from "class-transformer";
|
2 |
import { serialize } from "@helpers/serialize";
|
3 |
import { SwaggerResponseProperty } from "@lib/decorators/swagger-response-property.decorator";
|
|
|
4 |
|
5 |
class Days {
|
6 |
@Expose()
|
@@ -16,7 +17,7 @@ class Days {
|
|
16 |
day_type: string;
|
17 |
|
18 |
@Expose({ name: "exercises" })
|
19 |
-
@SwaggerResponseProperty({ type:
|
20 |
exercises: any;
|
21 |
|
22 |
@Expose()
|
|
|
1 |
import { Expose, Transform } from "class-transformer";
|
2 |
import { serialize } from "@helpers/serialize";
|
3 |
import { SwaggerResponseProperty } from "@lib/decorators/swagger-response-property.decorator";
|
4 |
+
import { ExerciseSerialization } from "./exercise.serialization";
|
5 |
|
6 |
class Days {
|
7 |
@Expose()
|
|
|
17 |
day_type: string;
|
18 |
|
19 |
@Expose({ name: "exercises" })
|
20 |
+
@SwaggerResponseProperty({ type: [ExerciseSerialization] })
|
21 |
exercises: any;
|
22 |
|
23 |
@Expose()
|
src/lib/decorators/prefix.decorator.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import { swaggerRegistry } from "@lib/swagger/swagger";
|
2 |
import { BaseController } from "../controllers/controller.base";
|
|
|
3 |
|
4 |
export const Controller = (prefix: string) => {
|
5 |
return (target: typeof BaseController) => {
|
@@ -10,11 +11,13 @@ export const Controller = (prefix: string) => {
|
|
10 |
return instance;
|
11 |
};
|
12 |
newConstructor.prototype = originalConstructor.prototype;
|
|
|
|
|
13 |
swaggerRegistry.setControllerPrefix(
|
14 |
-
target.prototype.constructor
|
15 |
prefix
|
16 |
);
|
17 |
-
swaggerRegistry.setControllerTags(target.prototype.constructor
|
18 |
prefix
|
19 |
.split("/")
|
20 |
.slice(1)
|
|
|
1 |
import { swaggerRegistry } from "@lib/swagger/swagger";
|
2 |
import { BaseController } from "../controllers/controller.base";
|
3 |
+
import { getCallingFileName } from "@lib/utils/calling-file.helper";
|
4 |
|
5 |
export const Controller = (prefix: string) => {
|
6 |
return (target: typeof BaseController) => {
|
|
|
11 |
return instance;
|
12 |
};
|
13 |
newConstructor.prototype = originalConstructor.prototype;
|
14 |
+
|
15 |
+
target.prototype.constructor['targetName'] = target.prototype.constructor.name + getCallingFileName();
|
16 |
swaggerRegistry.setControllerPrefix(
|
17 |
+
target.prototype.constructor['targetName'],
|
18 |
prefix
|
19 |
);
|
20 |
+
swaggerRegistry.setControllerTags(target.prototype.constructor['targetName'], [
|
21 |
prefix
|
22 |
.split("/")
|
23 |
.slice(1)
|
src/lib/decorators/swagger-request.decorator.ts
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import { swaggerRegistry } from "@lib/swagger/swagger";
|
|
|
2 |
|
3 |
export const SwaggerRequest = (joiSchema: any) => {
|
4 |
return (target: any, propertyKey: string) => {
|
@@ -14,7 +15,8 @@ export const SwaggerRequest = (joiSchema: any) => {
|
|
14 |
};
|
15 |
});
|
16 |
|
17 |
-
|
|
|
18 |
propertyKey,
|
19 |
request: schema,
|
20 |
});
|
|
|
1 |
import { swaggerRegistry } from "@lib/swagger/swagger";
|
2 |
+
import { getCallingFileName } from "@lib/utils/calling-file.helper";
|
3 |
|
4 |
export const SwaggerRequest = (joiSchema: any) => {
|
5 |
return (target: any, propertyKey: string) => {
|
|
|
15 |
};
|
16 |
});
|
17 |
|
18 |
+
target.constructor['targetName'] = target.constructor.name + getCallingFileName();
|
19 |
+
swaggerRegistry.updateRoute(target.constructor['targetName'], {
|
20 |
propertyKey,
|
21 |
request: schema,
|
22 |
});
|
src/lib/decorators/swagger-response-property.decorator.ts
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import { swaggerRegistry } from "@lib/swagger/swagger";
|
|
|
2 |
|
3 |
export const SwaggerResponseProperty = (
|
4 |
props?:
|
@@ -10,8 +11,9 @@ export const SwaggerResponseProperty = (
|
|
10 |
) => {
|
11 |
if (typeof props === "string") {
|
12 |
return (target: any, propertyKey: string) => {
|
|
|
13 |
swaggerRegistry.updateSchemaProperty({
|
14 |
-
schema: target.constructor
|
15 |
property: propertyKey,
|
16 |
type: props,
|
17 |
});
|
@@ -19,8 +21,9 @@ export const SwaggerResponseProperty = (
|
|
19 |
}
|
20 |
|
21 |
return (target: any, propertyKey: string) => {
|
|
|
22 |
swaggerRegistry.updateSchemaProperty({
|
23 |
-
schema: target.constructor
|
24 |
property: propertyKey,
|
25 |
newName: props?.name,
|
26 |
type: props?.type,
|
|
|
1 |
import { swaggerRegistry } from "@lib/swagger/swagger";
|
2 |
+
import { getCallingFileName } from "@lib/utils/calling-file.helper";
|
3 |
|
4 |
export const SwaggerResponseProperty = (
|
5 |
props?:
|
|
|
11 |
) => {
|
12 |
if (typeof props === "string") {
|
13 |
return (target: any, propertyKey: string) => {
|
14 |
+
target.constructor['targetName'] = target.constructor.name + getCallingFileName();
|
15 |
swaggerRegistry.updateSchemaProperty({
|
16 |
+
schema: target.constructor['targetName'],
|
17 |
property: propertyKey,
|
18 |
type: props,
|
19 |
});
|
|
|
21 |
}
|
22 |
|
23 |
return (target: any, propertyKey: string) => {
|
24 |
+
target.constructor['targetName'] = target.constructor.name + getCallingFileName();
|
25 |
swaggerRegistry.updateSchemaProperty({
|
26 |
+
schema: target.constructor['targetName'],
|
27 |
property: propertyKey,
|
28 |
newName: props?.name,
|
29 |
type: props?.type,
|
src/lib/decorators/swagger-response.decorator.ts
CHANGED
@@ -1,10 +1,11 @@
|
|
1 |
import { swaggerRegistry } from "@lib/swagger/swagger";
|
|
|
2 |
import { instanceToPlain } from "class-transformer";
|
3 |
|
4 |
const responseToSwaggerSchema = (response: any) => {
|
5 |
const isClass = typeof response === "function";
|
6 |
const responseName =
|
7 |
-
(isClass && response.prototype.constructor
|
8 |
const responseData = swaggerRegistry.schemasRegistry.get(responseName);
|
9 |
|
10 |
// turn class to swagger schema
|
@@ -120,7 +121,8 @@ export const SwaggerResponse = (responseClass: any) => {
|
|
120 |
};
|
121 |
}
|
122 |
|
123 |
-
|
|
|
124 |
propertyKey,
|
125 |
response: standardResponseSchema,
|
126 |
});
|
|
|
1 |
import { swaggerRegistry } from "@lib/swagger/swagger";
|
2 |
+
import { getCallingFileName } from "@lib/utils/calling-file.helper";
|
3 |
import { instanceToPlain } from "class-transformer";
|
4 |
|
5 |
const responseToSwaggerSchema = (response: any) => {
|
6 |
const isClass = typeof response === "function";
|
7 |
const responseName =
|
8 |
+
(isClass && response.prototype.constructor['targetName']) || undefined;
|
9 |
const responseData = swaggerRegistry.schemasRegistry.get(responseName);
|
10 |
|
11 |
// turn class to swagger schema
|
|
|
121 |
};
|
122 |
}
|
123 |
|
124 |
+
target.constructor['targetName'] = target.constructor.name + getCallingFileName();
|
125 |
+
swaggerRegistry.updateRoute(target.constructor['targetName'], {
|
126 |
propertyKey,
|
127 |
response: standardResponseSchema,
|
128 |
});
|
src/lib/decorators/swagger-routes.decorator.ts
CHANGED
@@ -1,11 +1,13 @@
|
|
1 |
import { swaggerRegistry } from "@lib/swagger/swagger";
|
|
|
2 |
|
3 |
export const SwaggerRoute = (
|
4 |
path: string = "",
|
5 |
method: "get" | "post" | "put" | "patch" | "delete"
|
6 |
) => {
|
7 |
return (target: any, propertyKey: string) => {
|
8 |
-
|
|
|
9 |
propertyKey,
|
10 |
path,
|
11 |
method,
|
|
|
1 |
import { swaggerRegistry } from "@lib/swagger/swagger";
|
2 |
+
import { getCallingFileName } from "@lib/utils/calling-file.helper";
|
3 |
|
4 |
export const SwaggerRoute = (
|
5 |
path: string = "",
|
6 |
method: "get" | "post" | "put" | "patch" | "delete"
|
7 |
) => {
|
8 |
return (target: any, propertyKey: string) => {
|
9 |
+
target.constructor['targetName'] = target.constructor.name + getCallingFileName()
|
10 |
+
swaggerRegistry.updateRoute(target.constructor.name + getCallingFileName(), {
|
11 |
propertyKey,
|
12 |
path,
|
13 |
method,
|
src/lib/decorators/swagger-tags.decorator.ts
CHANGED
@@ -1,9 +1,11 @@
|
|
1 |
import { swaggerRegistry } from "@lib/swagger/swagger";
|
|
|
2 |
|
3 |
export const SwaggerTags = (...tags: string[]) => {
|
4 |
if (!tags.length) return;
|
5 |
return (target: any) => {
|
6 |
-
target = target.prototype.constructor.name;
|
|
|
7 |
swaggerRegistry.setControllerTags(target, tags);
|
8 |
};
|
9 |
};
|
|
|
1 |
import { swaggerRegistry } from "@lib/swagger/swagger";
|
2 |
+
import { getCallingFileName } from "@lib/utils/calling-file.helper";
|
3 |
|
4 |
export const SwaggerTags = (...tags: string[]) => {
|
5 |
if (!tags.length) return;
|
6 |
return (target: any) => {
|
7 |
+
target.constructor['targetName'] = target.prototype.constructor.name = getCallingFileName();
|
8 |
+
target = target.prototype.constructor.name + getCallingFileName();
|
9 |
swaggerRegistry.setControllerTags(target, tags);
|
10 |
};
|
11 |
};
|
src/lib/utils/calling-file.helper.ts
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export function getCallingFileName(): string {
|
2 |
+
try {
|
3 |
+
const error = new Error();
|
4 |
+
const callerFile = error.stack?.split('\n')[3].trim().replace(/^at /, '');
|
5 |
+
const insidePerantheses = callerFile?.match(/\(([^)]+)\)/)?.[1];
|
6 |
+
const getOnlyfilePaths = insidePerantheses?.split(':')[0]
|
7 |
+
return getOnlyfilePaths;
|
8 |
+
} catch (error) {
|
9 |
+
console.error(error)
|
10 |
+
return '';
|
11 |
+
}
|
12 |
+
}
|
test.ts
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const error = new Error();
|
2 |
+
const callerFile = error.stack?.split('\n')[3].trim().replace(/^at /, '');
|
3 |
+
const insidePerantheses = callerFile?.match(/\(([^)]+)\)/)?.[1];
|
4 |
+
const getOnlyfilePaths = insidePerantheses?.split(':')[0]
|
5 |
+
console.log(getOnlyfilePaths);
|