From ca97ae89b7fc227100584f2b58adad31f55b11b9 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Fiorello Jr" Date: Sun, 7 Jun 2020 14:17:44 -0400 Subject: [PATCH 1/2] add configuration useRelativeURLs --- README.md | 1 + src/OdataConfig.ts | 4 ++++ src/__snapshots__/o.spec.ts.snap | 1 + src/o.ts | 3 ++- 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ad5833b..b80c5be 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,7 @@ Basic configuration is based on [RequestInit](https://developer.mozilla.org/en-U "Content-Type": "multipart/mixed", }), useChangset: false, + useRelativeURLs: false, }, credentials: "omit", fragment: "value", diff --git a/src/OdataConfig.ts b/src/OdataConfig.ts index 2246759..a85696e 100644 --- a/src/OdataConfig.ts +++ b/src/OdataConfig.ts @@ -6,6 +6,10 @@ export interface OdataBatchConfig { boundaryPrefix?: string; useChangset: boolean; changsetBoundaryPrefix?: string; + /** + * When truthy, relative URL's will be used in batch elements + */ + useRelativeURLs: boolean; } export type OdataConfig = RequestInit & { diff --git a/src/__snapshots__/o.spec.ts.snap b/src/__snapshots__/o.spec.ts.snap index 3ece6be..17f94d4 100644 --- a/src/__snapshots__/o.spec.ts.snap +++ b/src/__snapshots__/o.spec.ts.snap @@ -12,6 +12,7 @@ Object { }, }, "useChangset": false, + "useRelativeURLs": false, }, "credentials": "omit", "fragment": "value", diff --git a/src/o.ts b/src/o.ts index 0040049..bd9d0fa 100644 --- a/src/o.ts +++ b/src/o.ts @@ -40,7 +40,8 @@ export function o(rootUrl: string | URL, config: OdataConfig | any = {}) { headers: new Headers({ "Content-Type": "multipart/mixed" }), - useChangset: false + useChangset: false, + useRelativeURLs: false }, credentials: "omit", fragment: "value", From 58f13c389d2862a2e7f7acbaf0d7ba6c36003d4f Mon Sep 17 00:00:00 2001 From: "Nicholas J. Fiorello Jr" Date: Sun, 7 Jun 2020 15:05:33 -0400 Subject: [PATCH 2/2] add getRequestURL --- src/OBatch.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/OBatch.ts b/src/OBatch.ts index 205ccf6..cdda0e8 100644 --- a/src/OBatch.ts +++ b/src/OBatch.ts @@ -41,7 +41,7 @@ export class OBatch { "Content-Transfer-Encoding: binary", `Content-ID: ${contentId}`, "", - `${req.config.method} ${req.url.href} HTTP/1.1`, + `${req.config.method} ${this.getRequestURL(req)} HTTP/1.1`, `${this.getHeaders(req)}`, `${this.getBody(req)}` ].join(CRLF); @@ -172,4 +172,13 @@ export class OBatch { } return mapped.join(CRLF); } + + private getRequestURL(req: ORequest): string { + let href = req.url.href; + if (this.batchConfig.batch.useRelativeURLs) { + // Strip away matching root from request. + href = href.replace(this.batchConfig.rootUrl.href, ''); + } + return href; + } }