Skip to content

Commit

Permalink
including init in graph batch responses
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-rodgers committed Jan 23, 2024
1 parent 4e5eb01 commit ad7ec13
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/graph/batching.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isUrlAbsolute, hOP, TimelinePipe, getGUID, CopyFrom, objectDefinedNotNull, isFunc, combine } from "@pnp/core";
import { isUrlAbsolute, hOP, TimelinePipe, getGUID, CopyFrom, objectDefinedNotNull, isFunc, combine, jsS } from "@pnp/core";
import { parseBinderWithErrorCheck, Queryable, body, InjectHeaders } from "@pnp/queryable";
import { IGraphQueryable, _GraphQueryable } from "./graphqueryable.js";
import { graphPost } from "./operations.js";
Expand Down Expand Up @@ -48,9 +48,7 @@ interface IGraphBatchResponseFragment {
statusText?: string;
method: string;
url: string;
headers?: string[][] | {
[key: string]: string;
};
headers?: [string, string][] | Record<string, string>;
body?: any;
}

Expand Down Expand Up @@ -389,32 +387,39 @@ function parseResponse(graphResponse: IGraphBatchResponse): ParsedGraphResponse
// the array of requests and make it easier to map them by index
const responseId = parseInt(response.id, 10) - 1;
const contentType = response.headers["Content-Type"];
const { status, statusText } = response;
const { status, statusText, headers, body } = response;
const init = { status, statusText, headers };


// this is to handle special cases before we pass to the default parsing logic
if (status === 204) {

// this handles cases where the response body is empty and has a 204 response status (No Content)
parsedResponses[responseId] = new Response(null, { status, statusText });
parsedResponses[responseId] = new Response(null, init);

} else if (status === 302) {

// this is the case where (probably) a file download was included in the batch and the service has returned a 302 redirect to that file
// the url should be in the response's location header, so we transform the response to a 200 with the location in the body as 302 will be an
// error in the default parser used on the individual request

init.status = 200;
// eslint-disable-next-line @typescript-eslint/dot-notation
parsedResponses[responseId] = new Response(JSON.stringify({ location: response.headers["Location"] || "" }), { status: 200, statusText });
parsedResponses[responseId] = new Response(jsS({ location: headers["Location"] || "" }), init);

} else if (status === 200 && /^image[\\|/]/i.test(contentType)) {

// this handles the case where image content is returned as base 64 data in the batch body, such as /me/photos/$value (https://github.com/pnp/pnpjs/issues/2825)

const encoder = new TextEncoder();
parsedResponses[responseId] = new Response(encoder.encode(response.body), { status, statusText });
parsedResponses[responseId] = new Response(encoder.encode(body), init);

} else {

parsedResponses[responseId] = new Response(JSON.stringify(response.body), <any>response);
// this is the default case where we have a json body which we remake into a string for the downstream parser to parse again
// a bit circular, but this provides consistent behavior for downstream parsers

parsedResponses[responseId] = new Response(jsS(body), init);
}
}

Expand Down

0 comments on commit ad7ec13

Please sign in to comment.