Skip to content

Commit

Permalink
[optimize] Form Data builder supports Array fields
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery committed Sep 5, 2023
1 parent d3c9400 commit b0563cc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koajax",
"version": "0.9.2",
"version": "0.9.3",
"license": "LGPL-3.0",
"author": "[email protected]",
"description": "HTTP Client based on Koa-like middlewares",
Expand Down Expand Up @@ -36,7 +36,7 @@
"devDependencies": {
"@parcel/packager-ts": "~2.9.3",
"@parcel/transformer-typescript-types": "~2.9.3",
"@types/core-js": "^2.5.5",
"@types/core-js": "^2.5.6",
"@types/jest": "^29.5.4",
"@types/jsdom": "^21.1.2",
"@types/node": "^18.17.14",
Expand All @@ -51,7 +51,7 @@
"parcel": "~2.9.3",
"prettier": "^3.0.3",
"ts-jest": "^29.1.1",
"typedoc": "^0.25.0",
"typedoc": "^0.25.1",
"typedoc-plugin-mdn-links": "^3.1.0",
"typescript": "~5.2.2"
},
Expand Down
22 changes: 11 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions source/utility.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Observable } from 'iterable-observer';
import { isTypedArray, stringifyDOM, formToJSON } from 'web-utility';
import { likeArray, isTypedArray, stringifyDOM, formToJSON } from 'web-utility';

export async function parseDocument(text: string, contentType = '') {
const [type] = contentType?.split(';') || [];
Expand All @@ -13,8 +13,13 @@ export async function parseDocument(text: string, contentType = '') {
export function makeFormData(data: Record<string, any>) {
const formData = new FormData();

for (const [key, value] of Object.entries(data))
formData.append(key, value);
for (const [key, value] of Object.entries(data)) {
const list = (
typeof value !== 'string' && likeArray(value) ? value : [value]
) as ArrayLike<string | Blob>;

Array.from(list, item => formData.append(key, item));
}

return formData;
}
Expand Down
25 changes: 24 additions & 1 deletion test/utility.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'core-js/es/object/from-entries';
import 'core-js/es/string/match-all';
import { parseHeaders, serializeNode } from '../source';

import { makeFormData, parseHeaders, serializeNode } from '../source';

describe('HTTP utility', () => {
describe('Parse Headers', () => {
Expand Down Expand Up @@ -30,6 +31,28 @@ describe('HTTP utility', () => {
});
});

describe('Form Data', () => {
it('should make a Form Data instance from a Plain Object', () => {
const formData = makeFormData({
a: 1,
b: [2, 3],
c: new Blob()
});
const entries = [...formData.entries()];

expect(entries.filter(([key]) => key === 'a')).toEqual([
['a', '1']
]);
expect(entries.filter(([key]) => key === 'b')).toEqual([
['b', '2'],
['b', '3']
]);
expect(entries.find(([key]) => key === 'c')?.[1]).toBeInstanceOf(
Blob
);
});
});

describe('Serialize DOM nodes', () => {
it('should serialize Input fields to String', () => {
document.body.innerHTML = `
Expand Down

0 comments on commit b0563cc

Please sign in to comment.