Skip to content

Commit

Permalink
[Fixes #29] Reworking formData dependency import. (#30)
Browse files Browse the repository at this point in the history
* Reworking formData dependency import.

* Fixing CI issues.

* Disabling rule for a line instead of the whole project.
  • Loading branch information
Kacper Kula authored Dec 3, 2020
1 parent cc34619 commit a3eb770
Show file tree
Hide file tree
Showing 5 changed files with 1,112 additions and 717 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onfido/api",
"version": "1.5.2",
"version": "1.5.3",
"description": "Node.js library for the Onfido API",
"keywords": [
"onfido",
Expand Down Expand Up @@ -37,23 +37,23 @@
},
"globals": {
"ts-jest": {
"tsConfig": "test/tsconfig.json"
"tsconfig": "test/tsconfig.json"
}
}
},
"devDependencies": {
"@rollup/plugin-json": "^4.1.0",
"@types/jest": "^25.1.4",
"jest": "^25.2.4",
"@types/jest": "^26.0.15",
"jest": "^26.6.3",
"nock": "^11.7.0",
"prettier": "^1.18.2",
"rollup": "^1.24.0",
"rollup-plugin-typescript2": "^0.25.3",
"ts-jest": "^25.3.0",
"ts-jest": "^26.4.4",
"tslint": "^5.20.0",
"tslint-config-prettier": "^1.18.0",
"tslint-plugin-prettier": "^2.0.1",
"typescript": "^3.6.4"
"typescript": "^4.0.5"
},
"dependencies": {
"axios": "^0.19.0",
Expand Down
8 changes: 6 additions & 2 deletions src/formatting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import FormData from "form-data";
import { Readable } from "stream";
import { IFormData } from "./types/formData";

// Using require because "form-data" exports this object as a default export which breaks integration when esModuleInterop: false
// tslint:disable-next-line: no-var-requires
const FormData = require("form-data");

export type SimpleObject = { [key: string]: unknown };

Expand Down Expand Up @@ -41,7 +45,7 @@ export const convertObjectToCamelCase = (
responseBody: SimpleObject
): SimpleObject => deepMapObjectKeys(responseBody, camelCase);

export const toFormData = (object: SimpleObject): FormData => {
export const toFormData = (object: SimpleObject): IFormData => {
return Object.entries(object).reduce((formData, [key, value]) => {
if (value instanceof Object && "contents" in value) {
const { contents, ...options } = value as ContentsAndOptions;
Expand Down
33 changes: 33 additions & 0 deletions src/types/formData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copied over from form-data.
import * as http from "http";
import * as stream from "stream";

export interface IFormData extends stream.Readable {
append(key: string, value: any, options?: IAppendOptions | string): void;
getHeaders(userHeaders?: IHeaders): IHeaders;
submit(
params: string | ISubmitOptions,
callback?: (error: Error | null, response: http.IncomingMessage) => void
): http.ClientRequest;
getBuffer(): Buffer;
getBoundary(): string;
getLength(callback: (err: Error | null, length: number) => void): void;
getLengthSync(): number;
hasKnownLength(): boolean;
}

interface IHeaders {
[key: string]: any;
}

interface IAppendOptions {
header?: string | IHeaders;
knownLength?: number;
filename?: string;
filepath?: string;
contentType?: string;
}

interface ISubmitOptions extends http.RequestOptions {
protocol?: "https:" | "http:";
}
7 changes: 7 additions & 0 deletions test/formatting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ describe("toFormData", () => {
it("omits undefined and null values", () => {
expect(() => toFormData({ a: null, b: undefined })).not.toThrow();
});

it("should return proper FormData object", () => {
const formData = toFormData({ a: "A", b: "B" });
expect(formData.getBoundary()).toContain("----");
expect(formData.getLengthSync()).toEqual(258);
expect(formData.hasKnownLength()).toBeTruthy();
});
});
Loading

0 comments on commit a3eb770

Please sign in to comment.