Skip to content

Commit

Permalink
v2.0.0 (#34)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JB1905 authored Sep 28, 2019
1 parent 7388cf5 commit 9156a35
Show file tree
Hide file tree
Showing 9 changed files with 519 additions and 330 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# react-wordpress-hooks Changelog

## 2.0.0 beta 3 / 2.0.0 (2019-09-28)
#### Bug Fix
- fixed problem with incorrect values for `post`, `update` and `delete` methods

## 2.0.0 beta 1 (2019-09-08)
#### New Feature
- hooks for Blocks: `useBlocks`, `useCreateBlock`, `useRetrieveBlock`, `useUpdateBlock`, `useDeleteBlock`
Expand Down
68 changes: 48 additions & 20 deletions lib/react-wordpress-hooks.js

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

2 changes: 1 addition & 1 deletion lib/react-wordpress-hooks.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/react-wordpress-hooks.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion lib/utils/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export declare const serialize: (object: any) => string;
export declare const serialize: (options: any) => string;
export declare const optionsToBody: (options: object) => URLSearchParams;
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-wordpress-hooks",
"version": "2.0.0-beta.2",
"version": "2.0.0",
"description": "Set of hooks for WordPress REST API",
"author": "Jakub Biesiada",
"license": "MIT",
Expand Down Expand Up @@ -35,24 +35,24 @@
},
"homepage": "https://github.com/JB1905/react-wordpress-hooks#readme",
"devDependencies": {
"@types/react": "^16.9.2",
"@typescript-eslint/parser": "^2.2.0",
"@types/react": "^16.9.3",
"@typescript-eslint/parser": "^2.3.1",
"docz": "^1.3.2",
"docz-theme-default": "^1.2.0",
"eslint": "^6.4.0",
"eslint-config-prettier": "^6.3.0",
"eslint-plugin-prettier": "^3.1.0",
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-react": "^7.14.3",
"eslint-plugin-react-hooks": "^2.0.1",
"eslint-plugin-react-hooks": "^2.1.0",
"gh-pages": "^2.1.1",
"husky": "^3.0.5",
"lint-staged": "^9.2.5",
"husky": "^3.0.7",
"lint-staged": "^9.4.0",
"prettier": "^1.18.2",
"react": "^16.9.0",
"ts-loader": "^6.1.0",
"ts-loader": "^6.1.2",
"typescript": "^3.6.3",
"webpack": "^4.40.2",
"webpack-cli": "^3.3.8"
"webpack": "^4.41.0",
"webpack-cli": "^3.3.9"
},
"peerDependencies": {
"react": ">=16.8.0"
Expand Down
51 changes: 36 additions & 15 deletions src/hooks/useApiRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useState, useEffect, useContext } from 'react';

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

import { serialize } from '../utils';
import { serialize, optionsToBody } from '../utils';

export const useApiRequest = ({
id,
Expand All @@ -28,24 +28,45 @@ export const useApiRequest = ({

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

if (requsetMethod === 'delete') {
query.push(`/${id}`);
}
const settings = {
method: requsetMethod,
headers
};

switch (requsetMethod) {
case 'get': {
if (typeof options === 'number') {
query.push(`/${options}`);
} else if (Array.isArray(options)) {
query.push(`?include=${options.join(',')}`);
} else {
query.push(serialize(options as object));
}

break;
}

case 'post':
case 'update': {
Object.assign(settings, {
body: optionsToBody(options as object)
});

if (requsetMethod === 'get') {
if (typeof options === 'number') {
query.push(`/${options}`);
} else if (Array.isArray(options)) {
query.push(`?include=${options.join(',')}`);
} else {
query.push(serialize(options));
break;
}

case 'delete': {
query.push(`/${id}`);

Object.assign(settings, {
body: optionsToBody(options as object)
});

break;
}
}

const res = await fetch(query.join(''), {
method: requsetMethod,
headers
});
const res = await fetch(query.join(''), settings);

const response = await res.json();

Expand Down
24 changes: 20 additions & 4 deletions src/utils/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
export const serialize = (object: any): string => {
export const serialize = (options: any): string => {
const string: string[] = [];

for (let key in object) {
if (object.hasOwnProperty(key)) {
for (let key in options) {
if (options.hasOwnProperty(key)) {
string.push(
`${encodeURIComponent(key)}=${encodeURIComponent(object[key])}`
`${encodeURIComponent(key)}=${encodeURIComponent(options[key])}`
);
}
}

return string.length > 0 ? '?' + string.join('&') : '';
};

export const optionsToBody = (options: object) => {
if (!options) {
return options;
}

const params = Object.entries(options);

const urlencoded = new URLSearchParams();

for (let [key, value] of params) {
urlencoded.append(key, value);
}

return urlencoded;
};
Loading

0 comments on commit 9156a35

Please sign in to comment.