Skip to content

Commit e704f2b

Browse files
dmsnellockham
authored andcommitted
Data Layer: Add helper to GET data from generic URL (#28867)
See #28304 In this patch we're adding a small helper to be used to get raw information from a given URL though a `GET` request. The way we'd use this is inside of the `waitForData()` function. This is meant to obscure the global http-data ID so that the user doesn't need to worry about it. ```js waitForData( { planets: requestFromUrl( 'https://swapi.co/api/planets/' ), } ).then( ( { planets } ) => { console.log( planets.data ); } ); ```
1 parent 2c91f81 commit e704f2b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

client/state/data-getters/index.js

+18
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ import {
2323
import { convertToSnakeCase } from 'state/data-layer/utils';
2424
import { dummyTaxRate } from 'lib/tax'; // #tax-on-checkout-placeholder
2525

26+
/**
27+
* Fetches content from a URL with a GET request
28+
*
29+
* @example
30+
* waitForData( {
31+
* planets: requestFromUrl( 'https://swapi.co/api/planets/' ),
32+
* } ).then( ( { planets } ) => {
33+
* console.log( planets.data );
34+
* } );
35+
*
36+
* @param {string} url location from which to GET data
37+
* @return {object} HTTP data wrapped value
38+
*/
39+
export const requestFromUrl = url =>
40+
requestHttpData( `get-at-url-${ url }`, rawHttp( { method: 'GET', url } ), {
41+
fromApi: () => data => [ `get-at-url-${ url }`, data ],
42+
} );
43+
2644
export const requestActivityActionTypeCounts = (
2745
siteId,
2846
filter,

0 commit comments

Comments
 (0)