Skip to content

Commit

Permalink
added correct support for formdata
Browse files Browse the repository at this point in the history
  • Loading branch information
janhommes committed Sep 7, 2021
1 parent ef5d27e commit 67103be
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/OHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 24 additions & 3 deletions src/o.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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", () => {
Expand Down
9 changes: 3 additions & 6 deletions src/o.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<OdataConfig> = {}
) {
export function o(rootUrl: string | URL, config: Partial<OdataConfig> = {}) {
// polyfill fetch if we have no fetch
const env = typeof window !== "undefined" ? window : global;
if (
Expand All @@ -42,7 +39,7 @@ export function o(
!config.disablePolyfill &&
typeof window === "undefined"
) {
require("cross-fetch/polyfill");
module[`require`]("cross-fetch/polyfill");
}

if (
Expand All @@ -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
Expand Down

0 comments on commit 67103be

Please sign in to comment.