1
1
import "reflect-metadata" ;
2
2
3
3
import { getSimplePathParams , ppMetadataKey } from "./pathparams" ;
4
+
4
5
import { plainToInstance } from "class-transformer" ;
5
6
6
- interface propInfo {
7
+ export interface PropInfo {
7
8
key : string | symbol ;
8
9
type : any ;
9
10
elemType : any ;
@@ -61,7 +62,7 @@ function handleObject(value: any, elemType: any, elemDepth: number): any {
61
62
62
63
export class SpeakeasyBase {
63
64
constructor ( payload ?: Record < string | symbol , unknown > ) {
64
- const props : propInfo [ ] = ( this as any ) [ "__props__" ] ;
65
+ const props : PropInfo [ ] = ( this as any ) [ "__props__" ] ;
65
66
if ( props ) {
66
67
for ( const prop of props ) {
67
68
if ( payload && payload . hasOwnProperty ( prop . key ) ) {
@@ -132,7 +133,7 @@ export function SpeakeasyMetadata<
132
133
}
133
134
}
134
135
135
- let props : propInfo [ ] ;
136
+ let props : PropInfo [ ] ;
136
137
if ( target . hasOwnProperty ( "__props__" ) ) {
137
138
props = ( target as any ) [ "__props__" ] ;
138
139
} else {
@@ -142,7 +143,7 @@ export function SpeakeasyMetadata<
142
143
const prop = {
143
144
key : propertyKey ,
144
145
type : Reflect . getMetadata ( "design:type" , target , propertyKey ) ,
145
- } as propInfo ;
146
+ } as PropInfo ;
146
147
147
148
if ( params ?. elemType ) {
148
149
prop . elemType = params . elemType ;
@@ -168,11 +169,16 @@ export function templateUrl(
168
169
export function generateURL (
169
170
serverURL : string ,
170
171
path : string ,
171
- pathParams : any
172
+ pathParams : any ,
173
+ globals ?: any
172
174
) : string {
173
175
const url : string = serverURL . replace ( / \/ $ / , "" ) + path ;
174
176
const parsedParameters : Record < string , string > = { } ;
175
- const fieldNames : string [ ] = Object . getOwnPropertyNames ( pathParams ) ;
177
+
178
+ const fieldNames : string [ ] =
179
+ "__props__" in pathParams
180
+ ? pathParams [ "__props__" ] . map ( ( prop : any ) => prop . key )
181
+ : Object . getOwnPropertyNames ( pathParams ) ;
176
182
fieldNames . forEach ( ( fname ) => {
177
183
const ppAnn : string = Reflect . getMetadata ( ppMetadataKey , pathParams , fname ) ;
178
184
if ( ppAnn == null ) return ;
@@ -183,11 +189,15 @@ export function generateURL(
183
189
false
184
190
) ;
185
191
if ( ppDecorator == null ) return ;
192
+
193
+ let value = pathParams [ fname ] ;
194
+ value = populateFromGlobals ( value , fname , "pathParam" , globals ) ;
195
+
186
196
switch ( ppDecorator . Style ) {
187
197
case "simple" :
188
198
const simpleParams : Map < string , string > = getSimplePathParams (
189
199
ppDecorator . ParamName ,
190
- pathParams [ fname ] ,
200
+ value ,
191
201
ppDecorator . Explode ,
192
202
ppDecorator . DateTimeFormat
193
203
) ;
@@ -306,29 +316,37 @@ export function encodeAndConvertPrimitiveVal(
306
316
export function deserializeJSONResponse < T > (
307
317
value : T ,
308
318
klass ?: any ,
309
- elemDepth : number = 0 ) : any {
310
-
319
+ elemDepth : number = 0
320
+ ) : any {
311
321
if ( value !== Object ( value ) ) {
312
322
return value ;
313
323
}
314
324
315
325
if ( elemDepth === 0 && klass != null ) {
316
- return plainToInstance ( klass , value , { excludeExtraneousValues : true } ) as typeof klass ;
326
+ return plainToInstance ( klass , value , {
327
+ excludeExtraneousValues : true ,
328
+ } ) as typeof klass ;
317
329
}
318
330
319
331
if ( Array . isArray ( value ) ) {
320
- return value . map ( ( v ) => deserializeJSONResponse ( v , klass , elemDepth - 1 ) ) ;
332
+ return value . map ( ( v ) => deserializeJSONResponse ( v , klass , elemDepth - 1 ) ) ;
321
333
}
322
334
323
- if ( typeof value === ' object' && value != null ) {
335
+ if ( typeof value === " object" && value != null ) {
324
336
let copiedRecord : Record < string , any > = { } ;
325
337
for ( const key in value ) {
326
- copiedRecord [ key ] = deserializeJSONResponse ( value [ key ] , klass , elemDepth - 1 ) ;
338
+ copiedRecord [ key ] = deserializeJSONResponse (
339
+ value [ key ] ,
340
+ klass ,
341
+ elemDepth - 1
342
+ ) ;
327
343
}
328
344
return copiedRecord ;
329
345
}
330
346
331
- return plainToInstance ( klass , value , { excludeExtraneousValues : true } ) as typeof klass ;
347
+ return plainToInstance ( klass , value , {
348
+ excludeExtraneousValues : true ,
349
+ } ) as typeof klass ;
332
350
}
333
351
334
352
export function getResFieldDepth ( res : any ) : number {
@@ -353,3 +371,21 @@ export function getResFieldDepth(res: any): number {
353
371
354
372
return resFieldDepth ;
355
373
}
374
+
375
+ export function populateFromGlobals (
376
+ value : any ,
377
+ fieldName : string ,
378
+ paramType : string ,
379
+ globals : any
380
+ ) : any {
381
+ if ( globals && value === undefined ) {
382
+ if ( "parameters" in globals && paramType in globals . parameters ) {
383
+ let globalValue = globals . parameters [ paramType ] [ fieldName ] ;
384
+ if ( globalValue !== undefined ) {
385
+ value = globalValue ;
386
+ }
387
+ }
388
+ }
389
+
390
+ return value ;
391
+ }
0 commit comments