From 2acf29a73ecba3e329593a2c89868222e96598f0 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Fiorello Jr" Date: Wed, 3 Jun 2020 08:02:28 -0400 Subject: [PATCH 1/2] move boundaryPrefix to config.batch --- README.md | 2 +- src/__snapshots__/o.spec.ts.snap | 2 +- src/o.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c922ef6..ef88876 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ Basic configuration is based on [RequestInit](https://developer.mozilla.org/en-U ``` { batch: { + boundaryPrefix: "batch_", changsetBoundaryPrefix: "changset_", endpoint: "$batch", headers: new Headers({ @@ -121,7 +122,6 @@ Basic configuration is based on [RequestInit](https://developer.mozilla.org/en-U }), useChangset: false, }, - boundaryPrefix: "batch_", credentials: "omit", fragment: "value", headers: new Headers({ diff --git a/src/__snapshots__/o.spec.ts.snap b/src/__snapshots__/o.spec.ts.snap index 3320010..3ece6be 100644 --- a/src/__snapshots__/o.spec.ts.snap +++ b/src/__snapshots__/o.spec.ts.snap @@ -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 { @@ -12,7 +13,6 @@ Object { }, "useChangset": false, }, - "boundaryPrefix": "batch_", "credentials": "omit", "fragment": "value", "headers": Headers { diff --git a/src/o.ts b/src/o.ts index 7446b42..0040049 100644 --- a/src/o.ts +++ b/src/o.ts @@ -34,6 +34,7 @@ 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({ @@ -41,7 +42,6 @@ export function o(rootUrl: string | URL, config: OdataConfig | any = {}) { }), useChangset: false }, - boundaryPrefix: "batch_", credentials: "omit", fragment: "value", headers: new Headers({ From 3e190c8a3e173a1b198596bd6699bd943a93b2e6 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Fiorello Jr" Date: Wed, 3 Jun 2020 08:16:53 -0400 Subject: [PATCH 2/2] Change line breaks to CRLF --- src/OBatch.ts | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/OBatch.ts b/src/OBatch.ts index 2d85fd2..b39f4fe 100644 --- a/src/OBatch.ts +++ b/src/OBatch.ts @@ -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; @@ -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) { @@ -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( @@ -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 ""; } @@ -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); } }