UNPKG

3.72 kBPlain TextView Raw
1/**
2 * -------------------------------------------------------------------------------------------
3 * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
4 * See License in the project root for license information.
5 * -------------------------------------------------------------------------------------------
6 */
7
8/**
9 * @module BatchResponseContent
10 */
11
12/**
13 * @interface
14 * Signature represents key value pair object
15 */
16interface KeyValuePairObject {
17 [key: string]: any;
18}
19
20/**
21 * @interface
22 * Signature representing Batch response body
23 * @property {KeyValuePairObject[]} responses - An array of key value pair representing response object for every request
24 * @property {string} [@odata.nextLink] - The nextLink value to get next set of responses in case of asynchronous batch requests
25 */
26export interface BatchResponseBody {
27 responses: KeyValuePairObject[];
28 "@odata.nextLink"?: string;
29}
30
31/**
32 * @class
33 * Class that handles BatchResponseContent
34 */
35export class BatchResponseContent {
36 /**
37 * To hold the responses
38 */
39 private responses: Map<string, Response>;
40
41 /**
42 * Holds the next link url
43 */
44 private nextLink: string;
45
46 /**
47 * @public
48 * @constructor
49 * Creates the BatchResponseContent instance
50 * @param {BatchResponseBody} response - The response body returned for batch request from server
51 * @returns An instance of a BatchResponseContent
52 */
53 public constructor(response: BatchResponseBody) {
54 this.responses = new Map();
55 this.update(response);
56 }
57
58 /**
59 * @private
60 * Creates native Response object from the json representation of it.
61 * @param {KeyValuePairObject} responseJSON - The response json value
62 * @returns The Response Object instance
63 */
64 private createResponseObject(responseJSON: KeyValuePairObject): Response {
65 const body = responseJSON.body;
66 const options: KeyValuePairObject = {};
67 options.status = responseJSON.status;
68 if (responseJSON.statusText !== undefined) {
69 options.statusText = responseJSON.statusText;
70 }
71 options.headers = responseJSON.headers;
72 if (options.headers !== undefined && options.headers["Content-Type"] !== undefined) {
73 if (options.headers["Content-Type"].split(";")[0] === "application/json") {
74 const bodyString = JSON.stringify(body);
75 return new Response(bodyString, options);
76 }
77 }
78 return new Response(body, options);
79 }
80
81 /**
82 * @public
83 * Updates the Batch response content instance with given responses.
84 * @param {BatchResponseBody} response - The response json representing batch response message
85 * @returns Nothing
86 */
87 public update(response: BatchResponseBody): void {
88 this.nextLink = response["@odata.nextLink"];
89 const responses = response.responses;
90 for (let i = 0, l = responses.length; i < l; i++) {
91 this.responses.set(responses[i].id, this.createResponseObject(responses[i]));
92 }
93 }
94
95 /**
96 * @public
97 * To get the response of a request for a given request id
98 * @param {string} requestId - The request id value
99 * @returns The Response object instance for the particular request
100 */
101 public getResponseById(requestId: string): Response {
102 return this.responses.get(requestId);
103 }
104
105 /**
106 * @public
107 * To get all the responses of the batch request
108 * @returns The Map of id and Response objects
109 */
110 public getResponses(): Map<string, Response> {
111 return this.responses;
112 }
113
114 /**
115 * @public
116 * To get the iterator for the responses
117 * @returns The Iterable generator for the response objects
118 */
119 public *getResponsesIterator(): IterableIterator<[string, Response]> {
120 const iterator = this.responses.entries();
121 let cur = iterator.next();
122 while (!cur.done) {
123 yield cur.value;
124 cur = iterator.next();
125 }
126 }
127}