Skip to content

Commit

Permalink
Merge pull request #128 from janhommes/bug/#127-form-data
Browse files Browse the repository at this point in the history
added correct support for formdata
  • Loading branch information
janhommes authored Mar 29, 2022
2 parents 1449db5 + 13f0e8e commit 450e51c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
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 450e51c

Please sign in to comment.