Skip to content

Commit 9156a35

Browse files
authored
v2.0.0 (#34)
* Add missing endpoints & update docs * Update docs * Update useBlocks.tsx * Update docs * Update types * Update useBlockRenderer.tsx * v2.0.0 alpha 1 * Update * Update devDependencies * v2.0.0 beta 2 * Update devDependencies * Fix post, update & delete methods incorrect options error * v2.0.0 beta 3 * v2.0.0
1 parent 7388cf5 commit 9156a35

File tree

9 files changed

+519
-330
lines changed

9 files changed

+519
-330
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# react-wordpress-hooks Changelog
22

3+
## 2.0.0 beta 3 / 2.0.0 (2019-09-28)
4+
#### Bug Fix
5+
- fixed problem with incorrect values for `post`, `update` and `delete` methods
6+
37
## 2.0.0 beta 1 (2019-09-08)
48
#### New Feature
59
- hooks for Blocks: `useBlocks`, `useCreateBlock`, `useRetrieveBlock`, `useUpdateBlock`, `useDeleteBlock`

lib/react-wordpress-hooks.js

Lines changed: 48 additions & 20 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/react-wordpress-hooks.min.js

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export declare const serialize: (object: any) => string;
1+
export declare const serialize: (options: any) => string;
2+
export declare const optionsToBody: (options: object) => URLSearchParams;

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-wordpress-hooks",
3-
"version": "2.0.0-beta.2",
3+
"version": "2.0.0",
44
"description": "Set of hooks for WordPress REST API",
55
"author": "Jakub Biesiada",
66
"license": "MIT",
@@ -35,24 +35,24 @@
3535
},
3636
"homepage": "https://github.com/JB1905/react-wordpress-hooks#readme",
3737
"devDependencies": {
38-
"@types/react": "^16.9.2",
39-
"@typescript-eslint/parser": "^2.2.0",
38+
"@types/react": "^16.9.3",
39+
"@typescript-eslint/parser": "^2.3.1",
4040
"docz": "^1.3.2",
4141
"docz-theme-default": "^1.2.0",
4242
"eslint": "^6.4.0",
4343
"eslint-config-prettier": "^6.3.0",
44-
"eslint-plugin-prettier": "^3.1.0",
44+
"eslint-plugin-prettier": "^3.1.1",
4545
"eslint-plugin-react": "^7.14.3",
46-
"eslint-plugin-react-hooks": "^2.0.1",
46+
"eslint-plugin-react-hooks": "^2.1.0",
4747
"gh-pages": "^2.1.1",
48-
"husky": "^3.0.5",
49-
"lint-staged": "^9.2.5",
48+
"husky": "^3.0.7",
49+
"lint-staged": "^9.4.0",
5050
"prettier": "^1.18.2",
5151
"react": "^16.9.0",
52-
"ts-loader": "^6.1.0",
52+
"ts-loader": "^6.1.2",
5353
"typescript": "^3.6.3",
54-
"webpack": "^4.40.2",
55-
"webpack-cli": "^3.3.8"
54+
"webpack": "^4.41.0",
55+
"webpack-cli": "^3.3.9"
5656
},
5757
"peerDependencies": {
5858
"react": ">=16.8.0"

src/hooks/useApiRequest.tsx

Lines changed: 36 additions & 15 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 } from '../utils';
5+
import { serialize, optionsToBody } from '../utils';
66

77
export const useApiRequest = ({
88
id,
@@ -28,24 +28,45 @@ export const useApiRequest = ({
2828

2929
const query = [`${url}/wp-json/wp/v2/${endpoint}`];
3030

31-
if (requsetMethod === 'delete') {
32-
query.push(`/${id}`);
33-
}
31+
const settings = {
32+
method: requsetMethod,
33+
headers
34+
};
35+
36+
switch (requsetMethod) {
37+
case 'get': {
38+
if (typeof options === 'number') {
39+
query.push(`/${options}`);
40+
} else if (Array.isArray(options)) {
41+
query.push(`?include=${options.join(',')}`);
42+
} else {
43+
query.push(serialize(options as object));
44+
}
45+
46+
break;
47+
}
48+
49+
case 'post':
50+
case 'update': {
51+
Object.assign(settings, {
52+
body: optionsToBody(options as object)
53+
});
3454

35-
if (requsetMethod === 'get') {
36-
if (typeof options === 'number') {
37-
query.push(`/${options}`);
38-
} else if (Array.isArray(options)) {
39-
query.push(`?include=${options.join(',')}`);
40-
} else {
41-
query.push(serialize(options));
55+
break;
56+
}
57+
58+
case 'delete': {
59+
query.push(`/${id}`);
60+
61+
Object.assign(settings, {
62+
body: optionsToBody(options as object)
63+
});
64+
65+
break;
4266
}
4367
}
4468

45-
const res = await fetch(query.join(''), {
46-
method: requsetMethod,
47-
headers
48-
});
69+
const res = await fetch(query.join(''), settings);
4970

5071
const response = await res.json();
5172

src/utils/index.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
1-
export const serialize = (object: any): string => {
1+
export const serialize = (options: any): string => {
22
const string: string[] = [];
33

4-
for (let key in object) {
5-
if (object.hasOwnProperty(key)) {
4+
for (let key in options) {
5+
if (options.hasOwnProperty(key)) {
66
string.push(
7-
`${encodeURIComponent(key)}=${encodeURIComponent(object[key])}`
7+
`${encodeURIComponent(key)}=${encodeURIComponent(options[key])}`
88
);
99
}
1010
}
1111

1212
return string.length > 0 ? '?' + string.join('&') : '';
1313
};
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+
};

0 commit comments

Comments
 (0)