From 67103be07ec5ce4a6109d407784a9ff1da752627 Mon Sep 17 00:00:00 2001 From: janhommes Date: Tue, 7 Sep 2021 13:44:11 +0200 Subject: [PATCH] added correct support for formdata --- package.json | 2 +- src/OHandler.ts | 4 ++-- src/o.spec.ts | 27 ++++++++++++++++++++++++--- src/o.ts | 9 +++------ 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index f38adaf..380f3b1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "o.js", "description": "o.js is a isomorphic Odata Javascript library to simplify the request of data. The main goal is to build a standalone, lightweight and easy to understand Odata lib.", - "version": "1.3.1", + "version": "1.4.0-rc0", "main": "dist/cjs/o.js", "browser": "dist/umd/o.js", "module": "dist/es2015/o.js", diff --git a/src/OHandler.ts b/src/OHandler.ts index bc5b9e1..1c8cb46 100644 --- a/src/OHandler.ts +++ b/src/OHandler.ts @@ -3,7 +3,7 @@ import { OdataConfig } from "./OdataConfig"; import { OdataQuery } from "./OdataQuery"; import { ORequest } from "./ORequest"; -type BodyType = Blob | BufferSource | FormData | URLSearchParams | string | object; +type BodyType = Blob | BufferSource | FormData | URLSearchParams | string | object | Object; export class OHandler { private requests: ORequest[] = []; @@ -232,7 +232,7 @@ export class OHandler { } private getBody(body: BodyType): any { - if (typeof body === "object") { + if (body instanceof Object) { return JSON.stringify(body); } return body; diff --git a/src/o.spec.ts b/src/o.spec.ts index 1101f8c..571b6f9 100644 --- a/src/o.spec.ts +++ b/src/o.spec.ts @@ -350,9 +350,12 @@ describe("Create, Update and Delete request", () => { let oHandler; beforeAll(async () => { - oHandler = o('https://services.odata.org/V4/TripPinServiceRW/(S(ojstest))/', { - headers: { "If-match": "*", "Content-Type": "application/json" }, - }); + oHandler = o( + "https://services.odata.org/V4/TripPinServiceRW/(S(ojstest))/", + { + headers: { "If-match": "*", "Content-Type": "application/json" }, + } + ); }); test("POST a person and request it afterwards", async () => { @@ -429,6 +432,24 @@ describe("Create, Update and Delete request", () => { expect(response[1].status).toBe(204); expect(response[2].FirstName).toBe(data.LastName); }); + + test("POST a person with FormData", async () => { + // given + const resource = "People"; + const data = new FormData(); + data.append("FirstName", "Bar"); + + + // when + try { + const response = await oHandler + .post(resource, data) + .query(); + } catch (ex) { + // expect: FormData is not supported, so this error code is correct + expect(ex.status).toBe(415); + } + }); }); describe("Batching", () => { diff --git a/src/o.ts b/src/o.ts index e767c69..1513694 100644 --- a/src/o.ts +++ b/src/o.ts @@ -21,10 +21,7 @@ import { OHandler } from "./OHandler"; * @param rootUrl The url to query * @param config The odata and fetch configuration. */ -export function o( - rootUrl: string | URL, - config: Partial = {} -) { +export function o(rootUrl: string | URL, config: Partial = {}) { // polyfill fetch if we have no fetch const env = typeof window !== "undefined" ? window : global; if ( @@ -42,7 +39,7 @@ export function o( !config.disablePolyfill && typeof window === "undefined" ) { - require("cross-fetch/polyfill"); + module[`require`]("cross-fetch/polyfill"); } if ( @@ -60,7 +57,7 @@ export function o( !config.disablePolyfill && typeof window === "undefined" ) { - require("universal-url").shim(); + module[`require`]("universal-url").shim(); } // set the default configuration values