2
2
* Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
3
3
*/
4
4
5
- import { AxiosInstance } from "axios" ;
6
-
7
5
const securityMetadataKey = "security" ;
8
6
9
- export function createSecurityClient (
10
- client : AxiosInstance ,
7
+ export type SecurityProperties = {
8
+ params : Record < string , string > ,
9
+ headers : Record < string , string > ,
10
+ }
11
+
12
+ export function parseSecurityProperties (
11
13
security : any
12
- ) : AxiosInstance {
13
- return parseSecurityClass ( client , security ) ;
14
+ ) : SecurityProperties {
15
+ return parseSecurityClass ( security ) ;
14
16
}
15
17
16
18
function parseSecurityDecorator ( securityAnn : string ) : SecurityDecorator {
@@ -51,10 +53,13 @@ function parseSecurityDecorator(securityAnn: string): SecurityDecorator {
51
53
}
52
54
53
55
function parseSecurityClass (
54
- client : AxiosInstance ,
55
56
security : any
56
- ) : AxiosInstance {
57
+ ) : SecurityProperties {
57
58
const fieldNames : string [ ] = Object . getOwnPropertyNames ( security ) ;
59
+ const properties : SecurityProperties = {
60
+ params : { } ,
61
+ headers : { } ,
62
+ }
58
63
fieldNames . forEach ( ( fname ) => {
59
64
const securityAnn : string = Reflect . getMetadata (
60
65
securityMetadataKey ,
@@ -69,23 +74,23 @@ function parseSecurityClass(
69
74
const value = security [ fname ] ;
70
75
71
76
if ( securityDecorator . Option ) {
72
- return parseSecurityOption ( client , value ) ;
77
+ return parseSecurityOption ( properties , value ) ;
73
78
} else if ( securityDecorator . Scheme ) {
74
79
if ( securityDecorator . SubType === "basic" && value !== Object ( value ) ) {
75
- return parseSecurityScheme ( client , securityDecorator , security ) ;
80
+ return parseSecurityScheme ( properties , securityDecorator , security ) ;
76
81
} else {
77
- client = parseSecurityScheme ( client , securityDecorator , value ) ;
82
+ return parseSecurityScheme ( properties , securityDecorator , value ) ;
78
83
}
79
84
}
80
85
} ) ;
81
86
82
- return client ;
87
+ return properties ;
83
88
}
84
89
85
90
function parseSecurityOption (
86
- client : AxiosInstance ,
91
+ properties : SecurityProperties ,
87
92
optionType : any
88
- ) : AxiosInstance {
93
+ ) : void {
89
94
const fieldNames : string [ ] = Object . getOwnPropertyNames ( optionType ) ;
90
95
fieldNames . forEach ( ( fname ) => {
91
96
const securityAnn : string = Reflect . getMetadata (
@@ -97,23 +102,21 @@ function parseSecurityOption(
97
102
const securityDecorator : SecurityDecorator =
98
103
parseSecurityDecorator ( securityAnn ) ;
99
104
if ( securityDecorator == null || ! securityDecorator . Scheme ) return ;
100
- return parseSecurityScheme ( client , securityDecorator , optionType [ fname ] ) ;
105
+ return parseSecurityScheme ( properties , securityDecorator , optionType [ fname ] ) ;
101
106
} ) ;
102
-
103
- return client ;
104
107
}
105
108
106
109
function parseSecurityScheme (
107
- client : AxiosInstance ,
110
+ properties : SecurityProperties ,
108
111
schemeDecorator : SecurityDecorator ,
109
112
scheme : any
110
- ) : AxiosInstance {
113
+ ) : void {
111
114
if ( scheme === Object ( scheme ) ) {
112
115
if (
113
116
schemeDecorator . Type === "http" &&
114
117
schemeDecorator . SubType === "basic"
115
118
) {
116
- return parseBasicAuthScheme ( client , scheme ) ;
119
+ return parseBasicAuthScheme ( properties , scheme ) ;
117
120
}
118
121
119
122
const fieldNames : string [ ] = Object . getOwnPropertyNames ( scheme ) ;
@@ -128,64 +131,62 @@ function parseSecurityScheme(
128
131
parseSecurityDecorator ( securityAnn ) ;
129
132
if ( securityDecorator == null || securityDecorator . Name === "" ) return ;
130
133
131
- client = parseSecuritySchemeValue (
132
- client ,
134
+ return parseSecuritySchemeValue (
135
+ properties ,
133
136
schemeDecorator ,
134
137
securityDecorator ,
135
138
scheme [ fname ]
136
139
) ;
137
140
} ) ;
138
141
} else {
139
- client = parseSecuritySchemeValue (
140
- client ,
142
+ return parseSecuritySchemeValue (
143
+ properties ,
141
144
schemeDecorator ,
142
145
schemeDecorator ,
143
146
scheme
144
147
) ;
145
148
}
146
-
147
- return client ;
148
149
}
149
150
150
151
function parseSecuritySchemeValue (
151
- client : AxiosInstance ,
152
+ properties : SecurityProperties ,
152
153
schemeDecorator : SecurityDecorator ,
153
154
securityDecorator : SecurityDecorator ,
154
155
value : any
155
- ) : AxiosInstance {
156
+ ) : void {
156
157
switch ( schemeDecorator . Type ) {
157
158
case "apiKey" :
158
159
switch ( schemeDecorator . SubType ) {
159
160
case "header" :
160
- client . defaults . headers . common [ securityDecorator . Name ] = value ;
161
+ properties . headers [ securityDecorator . Name ] = value ;
161
162
break ;
162
163
case "query" :
163
- client . defaults . params [ securityDecorator . Name ] = value ;
164
+ properties . params [ securityDecorator . Name ] = value ;
164
165
break ;
165
166
case "cookie" : {
166
167
const securityDecoratorName : string = securityDecorator . Name ;
167
168
const val : string = value ;
168
- client . defaults . headers . common [
169
+ properties . headers [
169
170
"Cookie"
170
- ] = `${ securityDecoratorName } =${ val } ` ;
171
+ ] = `${ securityDecoratorName } =${ val } ` ;
171
172
break ;
172
173
}
173
174
default :
174
175
throw new Error ( "not supported" ) ;
175
176
}
176
177
break ;
177
178
case "openIdConnect" :
178
- client . defaults . headers . common [ securityDecorator . Name ] = value ;
179
+ properties . headers [ securityDecorator . Name ] = value ;
179
180
break ;
180
181
case "oauth2" :
181
- client . defaults . headers . common [ securityDecorator . Name ] = value ;
182
+ properties . headers [ securityDecorator . Name ] = value ;
182
183
break ;
183
184
case "http" :
184
185
switch ( schemeDecorator . SubType ) {
185
186
case "basic" :
186
187
break ;
187
188
case "bearer" :
188
- client . defaults . headers . common [ securityDecorator . Name ] = value . toLowerCase ( ) . startsWith ( "bearer " ) ? value : `Bearer ${ value } ` ;
189
+ properties . headers [ securityDecorator . Name ] = value . toLowerCase ( ) . startsWith ( "bearer " ) ? value : `Bearer ${ value } ` ;
189
190
break ;
190
191
default :
191
192
throw new Error ( "not supported" ) ;
@@ -194,14 +195,12 @@ function parseSecuritySchemeValue(
194
195
default :
195
196
throw new Error ( "not supported" ) ;
196
197
}
197
-
198
- return client ;
199
198
}
200
199
201
200
function parseBasicAuthScheme (
202
- client : AxiosInstance ,
201
+ properties : SecurityProperties ,
203
202
scheme : any
204
- ) : AxiosInstance {
203
+ ) : void {
205
204
let username ,
206
205
password = "" ;
207
206
@@ -227,11 +226,7 @@ function parseBasicAuthScheme(
227
226
}
228
227
} ) ;
229
228
230
- client . defaults . headers . common [ "Authorization" ] = `Basic ${ Buffer . from (
231
- `${ username } :${ password } `
232
- ) . toString ( "base64" ) } `;
233
-
234
- return client ;
229
+ properties . headers [ "Authorization" ] = `Basic ${ Buffer . from ( `${ username } :${ password } ` ) . toString ( 'base64' ) } ` ;
235
230
}
236
231
237
232
class SecurityDecorator {
0 commit comments