Skip to content

Commit 2cc0f21

Browse files
committed
Clean up utils
1 parent 9156a35 commit 2cc0f21

File tree

9 files changed

+89
-52
lines changed

9 files changed

+89
-52
lines changed

lib/react-wordpress-hooks.js

Lines changed: 50 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/react-wordpress-hooks.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/utils/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export declare const serialize: (options: any) => string;
2-
export declare const optionsToBody: (options: object) => URLSearchParams;
1+
export { serializeOptions } from './serializeOptions';
2+
export { passToBody } from './passToBody';

lib/utils/passToBody.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare const passToBody: (options: object) => URLSearchParams;

lib/utils/serializeOptions.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare const serializeOptions: (options: any) => string;

src/hooks/useApiRequest.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useState, useEffect, useContext } from 'react';
22

33
import { WPContext } from '../context';
44

5-
import { serialize, optionsToBody } from '../utils';
5+
import { serializeOptions, passToBody } from '../utils';
66

77
export const useApiRequest = ({
88
id,
@@ -40,7 +40,7 @@ export const useApiRequest = ({
4040
} else if (Array.isArray(options)) {
4141
query.push(`?include=${options.join(',')}`);
4242
} else {
43-
query.push(serialize(options as object));
43+
query.push(serializeOptions(options as object));
4444
}
4545

4646
break;
@@ -49,7 +49,7 @@ export const useApiRequest = ({
4949
case 'post':
5050
case 'update': {
5151
Object.assign(settings, {
52-
body: optionsToBody(options as object)
52+
body: passToBody(options as object)
5353
});
5454

5555
break;
@@ -59,7 +59,7 @@ export const useApiRequest = ({
5959
query.push(`/${id}`);
6060

6161
Object.assign(settings, {
62-
body: optionsToBody(options as object)
62+
body: passToBody(options as object)
6363
});
6464

6565
break;

src/utils/index.tsx

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,2 @@
1-
export const serialize = (options: any): string => {
2-
const string: string[] = [];
3-
4-
for (let key in options) {
5-
if (options.hasOwnProperty(key)) {
6-
string.push(
7-
`${encodeURIComponent(key)}=${encodeURIComponent(options[key])}`
8-
);
9-
}
10-
}
11-
12-
return string.length > 0 ? '?' + string.join('&') : '';
13-
};
14-
15-
export const optionsToBody = (options: object) => {
16-
if (!options) {
17-
return options;
18-
}
19-
20-
const params = Object.entries(options);
21-
22-
const urlencoded = new URLSearchParams();
23-
24-
for (let [key, value] of params) {
25-
urlencoded.append(key, value);
26-
}
27-
28-
return urlencoded;
29-
};
1+
export { serializeOptions } from './serializeOptions';
2+
export { passToBody } from './passToBody';

src/utils/passToBody.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const passToBody = (options: object) => {
2+
if (!options) {
3+
return options;
4+
}
5+
6+
const params = Object.entries(options);
7+
8+
const urlencoded = new URLSearchParams();
9+
10+
for (let [key, value] of params) {
11+
urlencoded.append(key, value);
12+
}
13+
14+
return urlencoded;
15+
};

src/utils/serializeOptions.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const serializeOptions = (options: any): string => {
2+
const string: string[] = [];
3+
4+
for (let key in options) {
5+
if (options.hasOwnProperty(key)) {
6+
string.push(
7+
`${encodeURIComponent(key)}=${encodeURIComponent(options[key])}`
8+
);
9+
}
10+
}
11+
12+
return string.length > 0 ? '?' + string.join('&') : '';
13+
};

0 commit comments

Comments
 (0)