@@ -3,43 +3,40 @@ import { plainToClass } from 'class-transformer';
3
3
import { ClientContext , ClientMetadata } from 'src/common/models' ;
4
4
import { sessionFromRequestFactory } from 'src/common/decorators' ;
5
5
import { Validator } from 'class-validator' ;
6
- import { API_PARAM_DATABASE_ID } from 'src/common/constants' ;
6
+ import { API_HEADER_DATABASE_INDEX , API_PARAM_DATABASE_ID } from 'src/common/constants' ;
7
+ import { ApiHeader , ApiParam } from '@nestjs/swagger' ;
7
8
8
9
const validator = new Validator ( ) ;
9
10
10
11
export interface IClientMetadataParamOptions {
11
12
databaseIdParam ?: string ,
12
13
uniqueIdParam ?: string ,
13
14
context ?: ClientContext ,
15
+ ignoreDbIndex ?: boolean ,
14
16
}
15
17
16
18
export const clientMetadataParamFactory = (
17
19
options : IClientMetadataParamOptions ,
18
20
ctx : ExecutionContext ,
19
21
) : ClientMetadata => {
20
- const opts : IClientMetadataParamOptions = {
21
- context : ClientContext . Common ,
22
- databaseIdParam : API_PARAM_DATABASE_ID ,
23
- ...options ,
24
- } ;
25
-
26
22
const req = ctx . switchToHttp ( ) . getRequest ( ) ;
27
23
28
24
let databaseId ;
29
- if ( opts ?. databaseIdParam ) {
30
- databaseId = req . params ?. [ opts . databaseIdParam ] ;
25
+ if ( options ?. databaseIdParam ) {
26
+ databaseId = req . params ?. [ options . databaseIdParam ] ;
31
27
}
32
28
33
29
let uniqueId ;
34
- if ( opts ?. uniqueIdParam ) {
35
- uniqueId = req . params ?. [ opts . uniqueIdParam ] ;
30
+ if ( options ?. uniqueIdParam ) {
31
+ uniqueId = req . params ?. [ options . uniqueIdParam ] ;
36
32
}
37
33
38
34
const clientMetadata = plainToClass ( ClientMetadata , {
39
35
session : sessionFromRequestFactory ( undefined , ctx ) ,
40
36
databaseId,
41
37
uniqueId,
42
- context : opts ?. context || ClientContext . Common ,
38
+ context : options ?. context || ClientContext . Common ,
39
+ db : options ?. ignoreDbIndex ? undefined : req ?. headers ?. [ API_HEADER_DATABASE_INDEX ] ,
43
40
} ) ;
44
41
45
42
const errors = validator . validateSync ( clientMetadata , {
@@ -53,4 +50,35 @@ export const clientMetadataParamFactory = (
53
50
return clientMetadata ;
54
51
} ;
55
52
56
- export const ClientMetadataParam = createParamDecorator ( clientMetadataParamFactory ) ;
53
+ export const ClientMetadataParam = (
54
+ options ?: IClientMetadataParamOptions ,
55
+ ) => {
56
+ const opts : IClientMetadataParamOptions = {
57
+ context : ClientContext . Common ,
58
+ databaseIdParam : API_PARAM_DATABASE_ID ,
59
+ ignoreDbIndex : false ,
60
+ ...options ,
61
+ } ;
62
+
63
+ return createParamDecorator ( clientMetadataParamFactory , [
64
+ ( target : any , key : string ) => {
65
+ // Here it is. Use the `@ApiQuery` decorator purely as a function to define the meta only once here.
66
+ ApiParam ( {
67
+ name : opts . databaseIdParam ,
68
+ schema : { type : 'string' } ,
69
+ required : true ,
70
+ } ) ( target , key , Object . getOwnPropertyDescriptor ( target , key ) ) ;
71
+ if ( ! opts . ignoreDbIndex ) {
72
+ ApiHeader ( {
73
+ name : API_HEADER_DATABASE_INDEX ,
74
+ schema : {
75
+ default : undefined ,
76
+ type : 'number' ,
77
+ minimum : 0 ,
78
+ } ,
79
+ required : false ,
80
+ } ) ( target , key , Object . getOwnPropertyDescriptor ( target , key ) ) ;
81
+ }
82
+ } ,
83
+ ] ) ( opts ) ;
84
+ } ;
0 commit comments