/
deserializationPolicy.ts
311 lines (282 loc) · 10.8 KB
/
deserializationPolicy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { HttpOperationResponse } from "../httpOperationResponse";
import { OperationResponse } from "../operationResponse";
import { OperationSpec, isStreamOperation } from "../operationSpec";
import { RestError } from "../restError";
import { MapperType } from "../serializer";
import { parseXML } from "../util/xml";
import { WebResourceLike } from "../webResource";
import {
BaseRequestPolicy,
RequestPolicy,
RequestPolicyFactory,
RequestPolicyOptions
} from "./requestPolicy";
/**
* Options to configure API response deserialization.
*/
export interface DeserializationOptions {
/**
* Configures the expected content types for the deserialization of
* JSON and XML response bodies.
*/
expectedContentTypes: DeserializationContentTypes;
}
/**
* The content-types that will indicate that an operation response should be deserialized in a
* particular way.
*/
export interface DeserializationContentTypes {
/**
* The content-types that indicate that an operation response should be deserialized as JSON.
* Defaults to [ "application/json", "text/json" ].
*/
json?: string[];
/**
* The content-types that indicate that an operation response should be deserialized as XML.
* Defaults to [ "application/xml", "application/atom+xml" ].
*/
xml?: string[];
}
/**
* Create a new serialization RequestPolicyCreator that will serialized HTTP request bodies as they
* pass through the HTTP pipeline.
*/
export function deserializationPolicy(
deserializationContentTypes?: DeserializationContentTypes
): RequestPolicyFactory {
return {
create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
return new DeserializationPolicy(nextPolicy, deserializationContentTypes, options);
}
};
}
export const defaultJsonContentTypes = ["application/json", "text/json"];
export const defaultXmlContentTypes = ["application/xml", "application/atom+xml"];
export const DefaultDeserializationOptions: DeserializationOptions = {
expectedContentTypes: {
json: defaultJsonContentTypes,
xml: defaultXmlContentTypes
}
};
/**
* A RequestPolicy that will deserialize HTTP response bodies and headers as they pass through the
* HTTP pipeline.
*/
export class DeserializationPolicy extends BaseRequestPolicy {
public readonly jsonContentTypes: string[];
public readonly xmlContentTypes: string[];
constructor(
nextPolicy: RequestPolicy,
deserializationContentTypes: DeserializationContentTypes | undefined,
options: RequestPolicyOptions
) {
super(nextPolicy, options);
this.jsonContentTypes =
(deserializationContentTypes && deserializationContentTypes.json) || defaultJsonContentTypes;
this.xmlContentTypes =
(deserializationContentTypes && deserializationContentTypes.xml) || defaultXmlContentTypes;
}
public async sendRequest(request: WebResourceLike): Promise<HttpOperationResponse> {
return this._nextPolicy
.sendRequest(request)
.then((response: HttpOperationResponse) =>
deserializeResponseBody(this.jsonContentTypes, this.xmlContentTypes, response)
);
}
}
function getOperationResponse(
parsedResponse: HttpOperationResponse
): undefined | OperationResponse {
let result: OperationResponse | undefined;
const request: WebResourceLike = parsedResponse.request;
const operationSpec: OperationSpec | undefined = request.operationSpec;
if (operationSpec) {
const operationResponseGetter:
| undefined
| ((
operationSpec: OperationSpec,
response: HttpOperationResponse
) => undefined | OperationResponse) = request.operationResponseGetter;
if (!operationResponseGetter) {
result = operationSpec.responses[parsedResponse.status];
} else {
result = operationResponseGetter(operationSpec, parsedResponse);
}
}
return result;
}
function shouldDeserializeResponse(parsedResponse: HttpOperationResponse): boolean {
const shouldDeserialize: undefined | boolean | ((response: HttpOperationResponse) => boolean) =
parsedResponse.request.shouldDeserialize;
let result: boolean;
if (shouldDeserialize === undefined) {
result = true;
} else if (typeof shouldDeserialize === "boolean") {
result = shouldDeserialize;
} else {
result = shouldDeserialize(parsedResponse);
}
return result;
}
export function deserializeResponseBody(
jsonContentTypes: string[],
xmlContentTypes: string[],
response: HttpOperationResponse
): Promise<HttpOperationResponse> {
return parse(jsonContentTypes, xmlContentTypes, response).then((parsedResponse) => {
if (!shouldDeserializeResponse(parsedResponse)) {
return parsedResponse;
}
const operationSpec = parsedResponse.request.operationSpec;
if (!operationSpec || !operationSpec.responses) {
return parsedResponse;
}
const responseSpec = getOperationResponse(parsedResponse);
const expectedStatusCodes = Object.keys(operationSpec.responses);
const hasNoExpectedStatusCodes =
expectedStatusCodes.length === 0 ||
(expectedStatusCodes.length === 1 && expectedStatusCodes[0] === "default");
const isExpectedStatusCode: boolean = hasNoExpectedStatusCodes
? 200 <= parsedResponse.status && parsedResponse.status < 300
: !!responseSpec;
// There is no operation response spec for current status code.
// So, treat it as an error case and use the default response spec to deserialize the response.
if (!isExpectedStatusCode) {
const defaultResponseSpec = operationSpec.responses.default;
if (!defaultResponseSpec) {
return parsedResponse;
}
const defaultBodyMapper = defaultResponseSpec.bodyMapper;
const defaultHeadersMapper = defaultResponseSpec.headersMapper;
const initialErrorMessage = isStreamOperation(operationSpec)
? `Unexpected status code: ${parsedResponse.status}`
: (parsedResponse.bodyAsText as string);
const error = new RestError(
initialErrorMessage,
undefined,
parsedResponse.status,
parsedResponse.request,
parsedResponse
);
try {
// If error response has a body, try to extract error code & message from it
// Then try to deserialize it using default body mapper
if (parsedResponse.parsedBody) {
const parsedBody = parsedResponse.parsedBody;
const internalError: any = parsedBody.error || parsedBody;
error.code = internalError.code;
if (internalError.message) {
error.message = internalError.message;
}
if (defaultBodyMapper) {
let valueToDeserialize: any = parsedBody;
if (operationSpec.isXML && defaultBodyMapper.type.name === MapperType.Sequence) {
valueToDeserialize =
typeof parsedBody === "object" ? parsedBody[defaultBodyMapper.xmlElementName!] : [];
}
error.response!.parsedBody = operationSpec.serializer.deserialize(
defaultBodyMapper,
valueToDeserialize,
"error.response.parsedBody"
);
}
}
// If error response has headers, try to deserialize it using default header mapper
if (parsedResponse.headers && defaultHeadersMapper) {
error.response!.parsedHeaders = operationSpec.serializer.deserialize(
defaultHeadersMapper,
parsedResponse.headers.rawHeaders(),
"operationRes.parsedHeaders"
);
}
} catch (defaultError) {
error.message = `Error "${defaultError.message}" occurred in deserializing the responseBody - "${parsedResponse.bodyAsText}" for the default response.`;
}
throw error;
}
// An operation response spec does exist for current status code, so
// use it to deserialize the response.
if (responseSpec) {
if (responseSpec.bodyMapper) {
let valueToDeserialize: any = parsedResponse.parsedBody;
if (operationSpec.isXML && responseSpec.bodyMapper.type.name === MapperType.Sequence) {
valueToDeserialize =
typeof valueToDeserialize === "object"
? valueToDeserialize[responseSpec.bodyMapper.xmlElementName!]
: [];
}
try {
parsedResponse.parsedBody = operationSpec.serializer.deserialize(
responseSpec.bodyMapper,
valueToDeserialize,
"operationRes.parsedBody"
);
} catch (error) {
const restError = new RestError(
`Error ${error} occurred in deserializing the responseBody - ${parsedResponse.bodyAsText}`,
undefined,
parsedResponse.status,
parsedResponse.request,
parsedResponse
);
throw restError;
}
} else if (operationSpec.httpMethod === "HEAD") {
// head methods never have a body, but we return a boolean to indicate presence/absence of the resource
parsedResponse.parsedBody = response.status >= 200 && response.status < 300;
}
if (responseSpec.headersMapper) {
parsedResponse.parsedHeaders = operationSpec.serializer.deserialize(
responseSpec.headersMapper,
parsedResponse.headers.rawHeaders(),
"operationRes.parsedHeaders"
);
}
}
return parsedResponse;
});
}
function parse(
jsonContentTypes: string[],
xmlContentTypes: string[],
operationResponse: HttpOperationResponse
): Promise<HttpOperationResponse> {
const errorHandler = (err: Error & { code: string }): Promise<never> => {
const msg = `Error "${err}" occurred while parsing the response body - ${operationResponse.bodyAsText}.`;
const errCode = err.code || RestError.PARSE_ERROR;
const e = new RestError(
msg,
errCode,
operationResponse.status,
operationResponse.request,
operationResponse
);
return Promise.reject(e);
};
if (!operationResponse.request.streamResponseBody && operationResponse.bodyAsText) {
const text = operationResponse.bodyAsText;
const contentType: string = operationResponse.headers.get("Content-Type") || "";
const contentComponents: string[] = !contentType
? []
: contentType.split(";").map((component) => component.toLowerCase());
if (
contentComponents.length === 0 ||
contentComponents.some((component) => jsonContentTypes.indexOf(component) !== -1)
) {
return new Promise<HttpOperationResponse>((resolve) => {
operationResponse.parsedBody = JSON.parse(text);
resolve(operationResponse);
}).catch(errorHandler);
} else if (contentComponents.some((component) => xmlContentTypes.indexOf(component) !== -1)) {
return parseXML(text)
.then((body) => {
operationResponse.parsedBody = body;
return operationResponse;
})
.catch(errorHandler);
}
}
return Promise.resolve(operationResponse);
}