Skip to content

Commit 5dfb705

Browse files
Merge branch 'feature/simplify-types' into main
2 parents e597b38 + e1ce213 commit 5dfb705

File tree

4 files changed

+50
-70
lines changed

4 files changed

+50
-70
lines changed

src/lib/create-form-data.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { CreateFormDataConfig, FormValue } from '../types/create-form-data';
1+
import { CreateFormDataConfig } from '../types/create-form-data';
2+
import { PrimitiveTypes } from '../types/primitive-types';
23

34
import { getKeyString } from './utils/get-key-string';
45

@@ -8,8 +9,8 @@ import { getKeyString } from './utils/get-key-string';
89
* @param config - configuration object
910
* @param formData - form data instance
1011
*/
11-
const fillFormData = (
12-
value: FormValue | Record<string, FormValue | Array<FormValue>>,
12+
const fillFormData = <T extends object>(
13+
value: T | T[] | PrimitiveTypes,
1314
config: CreateFormDataConfig,
1415
formData: FormData
1516
): void => {
@@ -42,49 +43,53 @@ const fillFormData = (
4243
});
4344
}
4445
} else if (typeof value === 'object') {
45-
if (value instanceof File || value instanceof Blob) {
46+
if (value instanceof File) {
47+
formData.append(
48+
keyPrefix,
49+
value,
50+
value.name
51+
);
52+
}
53+
else if(value instanceof Blob){
4654
formData.append(
4755
keyPrefix,
4856
value,
49-
'name' in value ? value.name : undefined
5057
);
5158
} else {
5259
Object.keys(value).forEach((key) => {
5360
const keyString = getKeyString(keyPrefix, key, index);
54-
fillFormData(value[key], { ...config, keyPrefix: keyString }, formData);
61+
fillFormData(value[key as keyof object], { ...config, keyPrefix: keyString }, formData);
5562
});
5663
}
5764
} else {
58-
const simpleValue = value.toString();
59-
if (simpleValue === '' && !allowEmptyValues) {
65+
const primitiveValue = value.toString();
66+
if (primitiveValue === '' && !allowEmptyValues) {
6067
return;
6168
}
62-
formData.append(keyPrefix, simpleValue);
69+
formData.append(keyPrefix, primitiveValue);
6370
}
6471
};
6572

73+
const defaultConfig = {
74+
keyPrefix: '',
75+
index: null,
76+
booleanMapper: (val: boolean) => (val ? '1' : '0'),
77+
allowNullableValues: false,
78+
allowEmptyValues: false,
79+
}
6680
/**
6781
* fill form data recursive function
6882
* @param value - form value
6983
* @param options - configuration object
7084
* @param existingFormData - optional existing form data instance
7185
*/
72-
export const createFormData = (
73-
value: Record<string, FormValue> | FormData,
86+
export const createFormData = <T extends object>(
87+
value: T | FormData,
7488
options?: Partial<CreateFormDataConfig>,
7589
existingFormData?: FormData
7690
): FormData => {
7791
// create config from default and argument options
78-
const config = Object.assign(
79-
{
80-
keyPrefix: '',
81-
index: null,
82-
booleanMapper: (val: boolean) => (val ? '1' : '0'),
83-
allowNullableValues: false,
84-
allowEmptyValues: false,
85-
},
86-
options || {}
87-
);
92+
const config = Object.assign({...defaultConfig},options || {});
8893

8994
// return form data if value instanceof FormData
9095
if (value instanceof FormData) {
@@ -97,6 +102,6 @@ export const createFormData = (
97102
}
98103
// fill form data by form value and return
99104
const formData = existingFormData || new FormData();
100-
fillFormData(value, config, formData);
105+
fillFormData({...value}, config, formData);
101106
return formData;
102107
};

src/lib/create-url-params.ts

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,64 @@
11
import {
2-
ArrayRecordType,
3-
CreateURLParamsConfig,
4-
RecordValue,
2+
CreateURLParamsConfig
53
} from '../types/create-url-params';
64

75
import { getKeyString } from './utils/get-key-string';
86

9-
export const createURLParams = (
10-
value: Readonly<Record<string, RecordValue>> | ArrayRecordType,
7+
const defaultConfig: CreateURLParamsConfig = {
8+
keyPrefix: '',
9+
toString: false,
10+
index: null,
11+
booleanMapper: (val: boolean) => (val ? '1' : '0')
12+
};
13+
14+
export const createURLParams = <T extends object>(
15+
value: T | T[] | null | undefined,
1116
config?: Partial<CreateURLParamsConfig>
12-
): Record<string, RecordValue> => {
17+
): object => {
1318
const { keyPrefix, toString, index, booleanMapper }: CreateURLParamsConfig =
14-
Object.assign(
15-
{
16-
keyPrefix: '',
17-
toString: false,
18-
index: null,
19-
booleanMapper: (val: boolean) => (val ? '1' : '0'),
20-
},
21-
config || {}
22-
);
19+
Object.assign({ ...defaultConfig }, config || {});
2320

2421
if (value === undefined || value === null) {
2522
return {};
2623
}
2724

2825
if (Array.isArray(value)) {
29-
return value.reduce((acc: Record<string, RecordValue>, curr, i) => {
26+
return value.reduce((acc: object, curr, i) => {
3027
if (typeof curr === 'object') {
3128
return {
3229
...acc,
3330
...createURLParams(curr, {
3431
...config,
3532
keyPrefix,
36-
index: i,
37-
}),
33+
index: i
34+
})
3835
};
3936
}
40-
if (!keyPrefix) {
37+
if (!keyPrefix || curr === null || curr === undefined) {
4138
return acc;
4239
}
4340
const value =
4441
typeof curr === 'boolean'
4542
? booleanMapper(curr)
4643
: toString
47-
? String(curr)
48-
: curr;
44+
? String(curr)
45+
: curr;
4946
return { ...acc, [`${keyPrefix}[${i}]`]: value };
5047
}, {});
5148
}
5249

5350
return Object.keys(value).reduce(
54-
(acc: Record<string, RecordValue>, key: string) => {
51+
(acc: object, key: string) => {
5552
const keyString = getKeyString(keyPrefix, key, index);
56-
const item = value[key];
53+
const item = value[key as keyof object];
5754

5855
if (Array.isArray(item)) {
5956
return {
6057
...acc,
6158
...createURLParams(item, {
6259
...config,
63-
keyPrefix: keyString,
64-
}),
60+
keyPrefix: keyString
61+
})
6562
};
6663
}
6764

@@ -72,7 +69,7 @@ export const createURLParams = (
7269
if (item && typeof item === 'object') {
7370
return {
7471
...acc,
75-
...createURLParams(item, { ...config, keyPrefix: keyString }),
72+
...createURLParams(item, { ...config, keyPrefix: keyString })
7673
};
7774
}
7875

src/types/create-form-data.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
import { PrimitiveTypes } from './primitive-types';
2-
3-
type Value = PrimitiveTypes | Date | File | Blob;
4-
5-
type ArrayFormValue = Array<Record<string, FormValue> | FormValue>;
6-
7-
export type FormValue =
8-
| ArrayFormValue
9-
| Record<string, ArrayFormValue | Value>
10-
| Value;
11-
121
export interface CreateFormDataConfig {
132
keyPrefix: string;
143
index: number | null;

src/types/create-url-params.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
import { PrimitiveTypes } from './primitive-types';
2-
3-
export type ArrayRecordType = Array<
4-
Record<string, RecordValue> | string | number | boolean
5-
>;
6-
7-
export type RecordValue =
8-
| PrimitiveTypes
9-
| Record<string, PrimitiveTypes>
10-
| ArrayRecordType;
11-
121
export interface CreateURLParamsConfig {
132
keyPrefix: string;
143
toString: boolean;

0 commit comments

Comments
 (0)