1
1
import "reflect-metadata" ;
2
- import axios , { AxiosInstance } from "axios" ;
2
+
3
3
import {
4
- ppMetadataKey ,
4
+ GetSimplePathParams ,
5
5
ParamDecorator ,
6
6
ParsePathParamDecorator ,
7
- GetSimplePathParams ,
7
+ ppMetadataKey ,
8
8
} from "./pathparams" ;
9
9
10
- export function Metadata ( anns : string ) {
11
- const annsArr = anns . split ( ", " ) ;
12
- return Reflect . metadata ( annsArr [ 0 ] , annsArr [ 1 ] ) ;
10
+ interface propInfo {
11
+ key : string | symbol ;
12
+ type : any ;
13
+ elemType : any ;
14
+ elemDepth : number ;
15
+ }
16
+
17
+ function isSpeakeasyBase ( type : any ) : boolean {
18
+ return type && Object . getPrototypeOf ( type ) ?. name == "SpeakeasyBase" ;
19
+ }
20
+
21
+ function handleArray ( value : any , elemType : any , elemDepth : number ) : any {
22
+ if ( ! Array . isArray ( value ) ) {
23
+ return value ;
24
+ }
25
+
26
+ if ( elemDepth == 1 ) {
27
+ return value . map ( ( v : any ) => new elemType ( v ) ) ;
28
+ } else {
29
+ return value . map ( ( v : any ) => {
30
+ if ( Array . isArray ( v ) ) {
31
+ return handleArray ( v , elemType , elemDepth - 1 ) ;
32
+ } else if ( typeof v == "object" ) {
33
+ return handleObject ( v , elemType , elemDepth - 1 ) ;
34
+ } else {
35
+ return v ;
36
+ }
37
+ } ) ;
38
+ }
39
+ }
40
+
41
+ function handleObject ( value : any , elemType : any , elemDepth : number ) : any {
42
+ if ( typeof value != "object" ) {
43
+ return value ;
44
+ }
45
+
46
+ if ( elemDepth == 1 ) {
47
+ return Object . keys ( value ) . reduce ( ( acc : any , key : string ) => {
48
+ acc [ key ] = new elemType ( value [ key ] ) ;
49
+ return acc ;
50
+ } , { } ) ;
51
+ } else {
52
+ return Object . keys ( value ) . reduce ( ( acc : any , key : string ) => {
53
+ const v = value [ key ] ;
54
+ if ( Array . isArray ( v ) ) {
55
+ acc [ key ] = handleArray ( v , elemType , elemDepth - 1 ) ;
56
+ } else if ( typeof v == "object" ) {
57
+ acc [ key ] = handleObject ( v , elemType , elemDepth - 1 ) ;
58
+ } else {
59
+ acc [ key ] = v ;
60
+ }
61
+ return acc ;
62
+ } , { } ) ;
63
+ }
64
+ }
65
+
66
+ export class SpeakeasyBase {
67
+ constructor ( payload ?: Record < string | symbol , unknown > ) {
68
+ const props : propInfo [ ] = ( this as any ) [ "__props__" ] ;
69
+ if ( props ) {
70
+ for ( const prop of props ) {
71
+ if ( payload && payload . hasOwnProperty ( prop . key ) ) {
72
+ const value = payload [ prop . key ] ;
73
+ if ( isSpeakeasyBase ( prop . type ) ) {
74
+ ( this as any ) [ prop . key ] = new prop . type ( value ) ;
75
+ } else if (
76
+ prop . type . name == "Array" &&
77
+ isSpeakeasyBase ( prop . elemType )
78
+ ) {
79
+ ( this as any ) [ prop . key ] = handleArray (
80
+ value ,
81
+ prop . elemType ,
82
+ prop . elemDepth
83
+ ) ;
84
+ } else if (
85
+ prop . type . name == "Object" &&
86
+ isSpeakeasyBase ( prop . elemType )
87
+ ) {
88
+ ( this as any ) [ prop . key ] = handleObject (
89
+ value ,
90
+ prop . elemType ,
91
+ prop . elemDepth
92
+ ) ;
93
+ } else {
94
+ ( this as any ) [ prop . key ] = value ;
95
+ }
96
+ }
97
+ }
98
+ }
99
+ }
100
+ }
101
+
102
+ export function Metadata <
103
+ T extends SpeakeasyBase = Record < string | symbol , unknown >
104
+ > ( params ?: {
105
+ data ?: string ;
106
+ elemType ?: { new ( ) : T } ;
107
+ elemDepth ?: number ;
108
+ } ) : PropertyDecorator {
109
+ return ( target , propertyKey ) => {
110
+ if ( params ?. data ) {
111
+ const annsArr = params . data . split ( ", " ) ;
112
+
113
+ for ( let i = 0 ; i < annsArr . length ; i += 2 ) {
114
+ Reflect . defineMetadata ( annsArr [ i ] , annsArr [ i + 1 ] , target , propertyKey ) ;
115
+ }
116
+ }
117
+
118
+ let props : propInfo [ ] ;
119
+ if ( target . hasOwnProperty ( "__props__" ) ) {
120
+ props = ( target as any ) [ "__props__" ] ;
121
+ } else {
122
+ props = ( target as any ) [ "__props__" ] = [ ] ;
123
+ }
124
+
125
+ const prop = {
126
+ key : propertyKey ,
127
+ type : Reflect . getMetadata ( "design:type" , target , propertyKey ) ,
128
+ } as propInfo ;
129
+
130
+ if ( params ?. elemType ) {
131
+ prop . elemType = params . elemType ;
132
+ prop . elemDepth = params . elemDepth || 1 ;
133
+ }
134
+
135
+ props . push ( prop ) ;
136
+ } ;
13
137
}
14
138
15
139
export function ReplaceParameters (
@@ -29,7 +153,7 @@ export function GenerateURL(
29
153
path : string ,
30
154
pathParams : any
31
155
) : string {
32
- let url : string = serverURL . replace ( / \/ $ / , "" ) + path ;
156
+ const url : string = serverURL . replace ( / \/ $ / , "" ) + path ;
33
157
const parsedParameters : Map < string , string > = new Map < string , string > ( ) ;
34
158
const fieldNames : string [ ] = Object . getOwnPropertyNames ( pathParams ) ;
35
159
fieldNames . forEach ( ( fname ) => {
@@ -41,12 +165,13 @@ export function GenerateURL(
41
165
case "simple" :
42
166
const simpleParams : Map < string , string > = GetSimplePathParams (
43
167
ppDecorator . ParamName ,
44
- pathParams [ fname ]
168
+ pathParams [ fname ] ,
169
+ ppDecorator . Explode
45
170
) ;
46
171
simpleParams . forEach ( ( value , key ) => {
47
172
parsedParameters . set ( key , value ) ;
48
173
} ) ;
49
174
}
50
175
} ) ;
51
176
return ReplaceParameters ( url , parsedParameters ) ;
52
- }
177
+ }
0 commit comments