Skip to content

Commit

Permalink
Merge pull request #106 from melgish/bugfix/batch-line-breaks
Browse files Browse the repository at this point in the history
Use CRLF instead of LF in batch body
  • Loading branch information
janhommes authored Jun 4, 2020
2 parents cba5d7d + 3e190c8 commit c1bde9f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ Basic configuration is based on [RequestInit](https://developer.mozilla.org/en-U
```
{
batch: {
boundaryPrefix: "batch_",
changsetBoundaryPrefix: "changset_",
endpoint: "$batch",
headers: new Headers({
"Content-Type": "multipart/mixed",
}),
useChangset: false,
},
boundaryPrefix: "batch_",
credentials: "omit",
fragment: "value",
headers: new Headers({
Expand Down
49 changes: 25 additions & 24 deletions src/OBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { OdataConfig } from "./OdataConfig";
import { OdataQuery } from "./OdataQuery";
import { ORequest } from "./ORequest";

const CRLF = "\r\n";

export class OBatch {
private batchBody: string;
// "" here prevents 'undefined' at start of body under some conditions.
private batchBody = "";
private batchUid;
private batchConfig: OdataConfig;

Expand Down Expand Up @@ -32,20 +35,19 @@ export class OBatch {
let contentId = 0;
this.batchBody += resources.map((req) => {
contentId++;
return `
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: ${contentId}
${req.config.method} ${req.url.href} HTTP/1.1
${this.getHeaders(req)}
${this.getBody(req)}`;
}).join(`
--${this.batchUid}`);

this.batchBody += `
--${this.batchUid}--
`;
return [
"",
"Content-Type: application/http",
"Content-Transfer-Encoding: binary",
`Content-ID: ${contentId}`,
"",
`${req.config.method} ${req.url.href} HTTP/1.1`,
`${this.getHeaders(req)}`,
`${this.getBody(req)}`
].join(CRLF);
}).join(`${CRLF}--${this.batchUid}`);

this.batchBody += `${CRLF}--${this.batchUid}--${CRLF}`;
}

public async fetch(url: URL) {
Expand Down Expand Up @@ -105,10 +107,12 @@ ${this.getBody(req)}`;
const changeRes = this.getChangeResources(resources);

if (this.changeset) {
this.batchBody += `
Content-Type: multipart/mixed; boundary=${this.batchUid}
--${this.batchUid}`;
this.batchBody += [
"",
`Content-Type: multipart/mixed; boundary=${this.batchUid}`,
"",
`--${this.batchUid}`
].join(CRLF);
} else if (changeRes.length > 0) {
this.batchBody = `--${this.batchUid}`;
this.batchBody += new OBatch(
Expand All @@ -134,10 +138,7 @@ Content-Type: multipart/mixed; boundary=${this.batchUid}

private getBody(req: ORequest) {
if (req.config.body) {
return `
${req.config.body}
`;
return `${CRLF}${req.config.body}${CRLF}${CRLF}`;
}
return "";
}
Expand All @@ -159,6 +160,6 @@ Content-Type: multipart/mixed; boundary=${this.batchUid}
private getHeaders(req: ORequest) {
return Object.keys(req.config.headers)
.map((name) => `${name}:${req.config.headers[name]}`)
.join("\n");
.join(CRLF);
}
}
2 changes: 1 addition & 1 deletion src/__snapshots__/o.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
exports[`initialize a new oHandler config should be default if not given 1`] = `
Object {
"batch": Object {
"boundaryPrefix": "batch_",
"changsetBoundaryPrefix": "changset_",
"endpoint": "$batch",
"headers": Headers {
Expand All @@ -12,7 +13,6 @@ Object {
},
"useChangset": false,
},
"boundaryPrefix": "batch_",
"credentials": "omit",
"fragment": "value",
"headers": Headers {
Expand Down
2 changes: 1 addition & 1 deletion src/o.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ export function o(rootUrl: string | URL, config: OdataConfig | any = {}) {
// set the default configuration values
const defaultConfigValues = {
batch: {
boundaryPrefix: "batch_",
changsetBoundaryPrefix: "changset_",
endpoint: "$batch",
headers: new Headers({
"Content-Type": "multipart/mixed"
}),
useChangset: false
},
boundaryPrefix: "batch_",
credentials: "omit",
fragment: "value",
headers: new Headers({
Expand Down

0 comments on commit c1bde9f

Please sign in to comment.