-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29092ae
commit 8ec46b8
Showing
8 changed files
with
155 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# @wordpress/blob | ||
|
||
Wrapper around `jQuery.ajax` to call WordPress REST APIs. | ||
|
||
## Installation | ||
|
||
Install the module | ||
|
||
```bash | ||
npm install @wordpress/api-request --save | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
import apiRequest from '@wordpress/api-request'; | ||
|
||
apiRequest( { path: '/wp/v2/posts' } ).then( posts => { | ||
console.log( posts ); | ||
} ); | ||
``` | ||
|
||
<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "@wordpress/api-request", | ||
"version": "1.0.0", | ||
"description": "Utility to call WordPress REST APIs", | ||
"author": "WordPress", | ||
"license": "GPL-2.0-or-later", | ||
"keywords": [ | ||
"wordpress", | ||
"rest-api", | ||
"fetch" | ||
], | ||
"homepage": "https://github.com/WordPress/gutenberg/tree/master/packages/api-request/README.md", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/WordPress/gutenberg.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/WordPress/gutenberg/issues" | ||
}, | ||
"main": "build/index.js", | ||
"module": "build-module/index.js", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"jquery": "^3.3.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import jQuery from 'jquery'; | ||
|
||
const wpApiSettings = window.wpApiSettings; | ||
|
||
function apiRequest( options ) { | ||
options = apiRequest.buildAjaxOptions( options ); | ||
return apiRequest.transport( options ); | ||
} | ||
|
||
apiRequest.buildAjaxOptions = function( options ) { | ||
let url = options.url; | ||
let path = options.path; | ||
let namespaceTrimmed, endpointTrimmed, apiRoot; | ||
let headers, addNonceHeader, headerName; | ||
|
||
if ( | ||
typeof options.namespace === 'string' && | ||
typeof options.endpoint === 'string' | ||
) { | ||
namespaceTrimmed = options.namespace.replace( /^\/|\/$/g, '' ); | ||
endpointTrimmed = options.endpoint.replace( /^\//, '' ); | ||
if ( endpointTrimmed ) { | ||
path = namespaceTrimmed + '/' + endpointTrimmed; | ||
} else { | ||
path = namespaceTrimmed; | ||
} | ||
} | ||
if ( typeof path === 'string' ) { | ||
apiRoot = wpApiSettings.root; | ||
path = path.replace( /^\//, '' ); | ||
|
||
// API root may already include query parameter prefix if site is | ||
// configured to use plain permalinks. | ||
if ( 'string' === typeof apiRoot && -1 !== apiRoot.indexOf( '?' ) ) { | ||
path = path.replace( '?', '&' ); | ||
} | ||
|
||
url = apiRoot + path; | ||
} | ||
|
||
// If ?_wpnonce=... is present, no need to add a nonce header. | ||
addNonceHeader = ! ( options.data && options.data._wpnonce ); | ||
|
||
headers = options.headers || {}; | ||
|
||
// If an 'X-WP-Nonce' header (or any case-insensitive variation | ||
// thereof) was specified, no need to add a nonce header. | ||
if ( addNonceHeader ) { | ||
for ( headerName in headers ) { | ||
if ( headers.hasOwnProperty( headerName ) ) { | ||
if ( headerName.toLowerCase() === 'x-wp-nonce' ) { | ||
addNonceHeader = false; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if ( addNonceHeader ) { | ||
// Do not mutate the original headers object, if any. | ||
headers = jQuery.extend( { | ||
'X-WP-Nonce': wpApiSettings.nonce, | ||
}, headers ); | ||
} | ||
|
||
// Do not mutate the original options object. | ||
options = jQuery.extend( {}, options, { | ||
headers: headers, | ||
url: url, | ||
} ); | ||
|
||
delete options.path; | ||
delete options.namespace; | ||
delete options.endpoint; | ||
|
||
return options; | ||
}; | ||
|
||
apiRequest.transport = jQuery.ajax; | ||
|
||
export default apiRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters