├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 muhgumus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Wordpress REST API (v2.0) 2 | ========= 3 | 4 | Wordpress REST API (v2.0) component for JS (React, React native or pureJS) 5 | 6 | ## Installation 7 | 8 | `npm install react-wp-api` 9 | 10 | 11 | ## Usage 12 | 13 | import _WP from 'react-wp-api'; 14 | var WP = new _WP('http://muhammedgumus.com') 15 | 16 | 17 | Categories 18 | 19 | WP.Categories().then((results) => { 20 | console.log(results) 21 | this.setState({ categories: results }); 22 | }) 23 | 24 | -- or -- 25 | 26 | WP.Categories({id:2, search:'Tek'}).then((results) => { 27 | console.log(results) 28 | this.setState({ categories: results }); 29 | }) 30 | 31 | 32 | Posts With Featured Images 33 | 34 | WP.Posts().then((results) => { 35 | console.log(results) 36 | this.setState({ posts: results }); 37 | 38 | results.forEach((item) => { 39 | WP.Media({ id: item.featured_media }).then((result) => { 40 | let medias = this.state.medias; 41 | medias['' + result.id] = { 42 | thumbnail: result.media_details.sizes.thumbnail.source_url, 43 | fullImage: result.media_details.sizes.full.source_url 44 | } 45 | this.setState({ medias }); 46 | }) 47 | }, this); 48 | 49 | }) 50 | 51 | -- or -- 52 | 53 | WP.Posts({id:2, category:4, search:'Ani'}).then((results) => { 54 | console.log(results) 55 | this.setState({ posts: results }); 56 | }) 57 | 58 | 59 | Pages 60 | 61 | WP.Pages().then((results) => { 62 | console.log(results) 63 | this.setState({ pages: results }); 64 | }) 65 | 66 | -- or -- 67 | 68 | WP.Pages({id:2, search:'Tek'}).then((results) => { 69 | console.log(results) 70 | this.setState({ pages: results }); 71 | }) 72 | 73 | Media 74 | 75 | WP.Media().then((results) => { 76 | console.log(results) 77 | this.setState({ pages: results }); 78 | }) 79 | 80 | -- or -- 81 | 82 | WP.Media({id:2, search:'Tek'}).then((results) => { 83 | console.log(results) 84 | this.setState({ pages: results }); 85 | }) 86 | 87 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var webUrl = ""; 4 | class WP { 5 | constructor(url) { 6 | webUrl = url 7 | } 8 | 9 | /** {id:2, search:'Ani'} */ 10 | Categories(data) { 11 | let url = webUrl + "/wp-json/wp/v2/categories"; 12 | if (data) { 13 | if (data.search != null && data.search != '') { 14 | url = webUrl + "/wp-json/wp/v2/categories?search=" + data.search; 15 | } 16 | 17 | if (data.id != null && data.id != '') { 18 | url = webUrl + "/wp-json/wp/v2/categories/" + data.id; 19 | } 20 | } 21 | return fetch(url, 22 | { 23 | method: "GET", 24 | headers: { 25 | 'Cache-Control': 'no-cache' 26 | } 27 | }) 28 | .then((response) => { 29 | return response.json() 30 | } 31 | ) 32 | .catch((ex) => { 33 | throw ex; 34 | }); 35 | } 36 | 37 | /** {id:2, category:4, search:'Ani'} */ 38 | Posts(data) { 39 | let url = webUrl + "/wp-json/wp/v2/posts"; 40 | if (data) { 41 | if (data.search != null && data.search != '') { 42 | url = webUrl + "/wp-json/wp/v2/posts?search=" + data.search; 43 | } 44 | 45 | if (data.category != null && data.category != '') { 46 | url = webUrl + "/wp-json/wp/v2/posts?categories=" + data.category; 47 | 48 | if (data.search != null && data.search != '') 49 | url = url + "&search=" + data.search; 50 | } 51 | 52 | if (data.id != null && data.id != '') { 53 | url = webUrl + "/wp-json/wp/v2/posts/" + data.id; 54 | } 55 | } 56 | 57 | 58 | return fetch(url, 59 | { 60 | method: "GET", 61 | headers: { 62 | 'Cache-Control': 'no-cache' 63 | } 64 | }) 65 | .then((response) => { 66 | return response.json() 67 | } 68 | ) 69 | .catch((ex) => { 70 | throw ex; 71 | }); 72 | } 73 | 74 | /** {id:2, search:'Ani'} */ 75 | Pages(data) { 76 | let url = webUrl + "/wp-json/wp/v2/pages"; 77 | if (data) { 78 | if (data.search != null && data.search != '') { 79 | url = webUrl + "/wp-json/wp/v2/pages?search=" + data.search; 80 | } 81 | 82 | if (data.id != null && data.id != '') { 83 | url = webUrl + "/wp-json/wp/v2/pages/" + data.id; 84 | } 85 | } 86 | return fetch(url, 87 | { 88 | method: "GET", 89 | headers: { 90 | 'Cache-Control': 'no-cache' 91 | } 92 | }) 93 | .then((response) => { 94 | return response.json() 95 | } 96 | ) 97 | .catch((ex) => { 98 | throw ex; 99 | }); 100 | } 101 | 102 | Media(data) { 103 | let url = webUrl + "/wp-json/wp/v2/media"; 104 | if (data) { 105 | if (data.search != null && data.search != '') { 106 | url = webUrl + "/wp-json/wp/v2/media?search=" + data.search; 107 | } 108 | 109 | if (data.id != null && data.id != '') { 110 | url = webUrl + "/wp-json/wp/v2/media/" + data.id; 111 | } 112 | } 113 | return fetch(url, 114 | { 115 | method: "GET", 116 | headers: { 117 | 'Cache-Control': 'no-cache' 118 | } 119 | }) 120 | .then((response) => { 121 | return response.json() 122 | } 123 | ) 124 | .catch((ex) => { 125 | throw ex; 126 | }); 127 | } 128 | 129 | Comments(data) { 130 | let url = webUrl + "/wp-json/wp/v2/comments?a=a"; 131 | if (data) { 132 | if (data.search != null && data.search != '') { 133 | url = url + "&search=" + data.search; 134 | } 135 | 136 | if (data.post != null && data.post != '') { 137 | url = url + "&post=" + data.post; 138 | } 139 | 140 | if (data.id != null && data.id != '') { 141 | url = webUrl + "/wp-json/wp/v2/comment/" + data.id; 142 | } 143 | } 144 | return fetch(url, 145 | { 146 | method: "GET", 147 | headers: { 148 | 'Cache-Control': 'no-cache' 149 | } 150 | }) 151 | .then((response) => { 152 | return response.json() 153 | } 154 | ) 155 | .catch((ex) => { 156 | throw ex; 157 | }); 158 | } 159 | 160 | } 161 | 162 | export default WP; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-wp-api", 3 | "version": "0.1.0", 4 | "description": "Wordpress Rest Api for React, React native , pure JS", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/muhgumus/react-wp-api.git" 12 | }, 13 | "keywords": [ 14 | "Wordpress", 15 | "Rest", 16 | "api", 17 | "react", 18 | "react", 19 | "native", 20 | "wp", 21 | "wp-api" 22 | ], 23 | "author": "Muhammed GÜMÜŞ ", 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/muhgumus/react-wp-api/issues" 27 | }, 28 | "homepage": "https://github.com/muhgumus/react-wp-api#readme" 29 | } 30 | --------------------------------------------------------------------------------