Skip to content

Commit

Permalink
Merge pull request #124 from bprucha/master
Browse files Browse the repository at this point in the history
On batch changeset, include the content ids, status codes, and response bodies in the result
  • Loading branch information
janhommes authored Jun 1, 2021
2 parents 44323ce + 0f9b65f commit 9f70115
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
39 changes: 21 additions & 18 deletions src/OBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,7 @@ export class OBatch {
const data = await res.text();
return this.parseResponse(data, res.headers.get("Content-Type"));
} else {
// check if return is JSON
try {
const error = await res.json();
throw { res, error };
} catch (ex) {
throw res;
}
throw res;
}
}

Expand All @@ -98,18 +92,27 @@ export class OBatch {
dataSegments.shift();
wasWithChangesetresponse = true;
return this.parseResponse(dataSegments.join("\r\n\r\n"), header);
} else if (dataSegments.length === 3) {
// if length >= 3 we have a body, try to parse if JSON and return that!
try {
const parsed = JSON.parse(dataSegments[2]);
const hasFragment = parsed[this.batchConfig.fragment];
return hasFragment || parsed;
} catch (ex) {
return dataSegments[2];
}
} else {
// it seems like we have no body, return the status code
return +dataSegments[1].split(" ")[1];
var contentIdHeader = dataSegments[0].split("\r\n").find(function (x) { return x.startsWith("Content-ID: "); });
if (contentIdHeader) {
try {
var contentId = parseInt(contentIdHeader.substring(12), 10);
} catch (ex) {
}
}
var status = +dataSegments[1].split(" ")[1];
if (dataSegments.length === 3) {
// if length == 3 we have a body, try to parse if JSON and return that!
var body;
try {
const parsed = JSON.parse(dataSegments[2]);
const hasFragment = parsed[this.batchConfig.fragment];
body = hasFragment || parsed;
} catch (ex) {
body = dataSegments[2];
}
}
return { contentId, status, body };
}
});
if (wasWithChangesetresponse) {
Expand Down
16 changes: 8 additions & 8 deletions src/o.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ describe("Batching", () => {
const data = await oHandler.get(resource1).get(resource2).batch();
// expect
expect(data.length).toBe(2);
expect(data[0].length).toBeDefined();
expect(data[0].body.length).toBeDefined();
});

test("Batch multiple GET requests and allow to add a query", async () => {
Expand All @@ -466,7 +466,7 @@ describe("Batching", () => {
.get(resource2)
.batch({ $top: 2 });
// expect
expect(data[0].length).toBe(2);
expect(data[0].body.length).toBe(2);
});

test("Batch multiple GET requests and patch something", async () => {
Expand All @@ -480,8 +480,8 @@ describe("Batching", () => {
.batch();
// expect
expect(data.length).toBe(3);
expect(data[1]).toBe(204);
expect(data[2].Name).toBe("New");
expect(data[1].status).toBe(204);
expect(data[2].body.Name).toBe("New");
});

test("Batch POST and PATCH with useChangeset=true", async () => {
Expand All @@ -502,8 +502,8 @@ describe("Batching", () => {
const data = await request.batch();
// expect
expect(data.length).toBe(2);
expect(data[0].LastName).toBe(resouce1data.LastName);
expect(data[1]).toBe(204);
expect(data[0].body.LastName).toBe(resouce1data.LastName);
expect(data[1].status).toBe(204);
});

// Content ID seems to have a problem in the test implementation (or I don't get the right implementation)
Expand All @@ -525,7 +525,7 @@ describe("Batching", () => {
.batch();
// expect
expect(result.length).toBe(3);
expect(result[1]).toBe(204);
expect(result[2].LastName).toBe("Bar");
expect(result[1].status).toBe(204);
expect(result[2].body.LastName).toBe("Bar");
});
});

0 comments on commit 9f70115

Please sign in to comment.