From b3d386380e2c972c24183fd9f45926fa2ed1bc6a Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Tue, 25 Sep 2018 19:16:27 -0700 Subject: [PATCH] Gutenberg: example api middleware for oembeds --- .../gutenberg/editor/api-middleware/index.js | 34 +++++++++++++++++++ .../gutenberg/editor/api-middleware/utils.js | 4 +++ 2 files changed, 38 insertions(+) diff --git a/client/gutenberg/editor/api-middleware/index.js b/client/gutenberg/editor/api-middleware/index.js index ba47d39e5c4b7..3638a4fd43740 100644 --- a/client/gutenberg/editor/api-middleware/index.js +++ b/client/gutenberg/editor/api-middleware/index.js @@ -4,6 +4,8 @@ * External dependencies */ import wpcomProxyRequest from 'wpcom-proxy-request'; +import url from 'url'; +import { stringify } from 'qs'; /** * Internal dependencies @@ -34,6 +36,38 @@ export const pathRewriteMiddleware = ( options, next, siteSlug ) => { return next( { ...options, path: wpcomPath } ); }; +export const oembedMiddleware = ( options, next, siteSlug ) => { + // Updates https://public-api.wordpress.com/wp/v2/oembed/1.0/proxy?url= to + // https://public-api.wordpress.com/rest/v1.1/sites//embeds/render?embed_url=&force=wpcom + // This intentionally breaks the middleware chain if we match an embed. + if ( /oembed\/1.0\/proxy/.test( options.url ) ) { + const urlObject = url.parse( options.url, { parseQueryString: true } ); + // Make authenticated calls using the WordPress.com REST Proxy + // bypassing the apiFetch call that uses window.fetch. + // This intentionally breaks the middleware chain. + return new Promise( ( resolve, reject ) => { + const query = { + force: 'wpcom', + embed_url: urlObject.query.url, + }; + wpcomProxyRequest( + { + path: `/sites/${ siteSlug }/embeds/render?${ stringify( query ) }`, + apiVersion: '1.1', + }, + ( error, bodyOrData ) => { + if ( error ) { + return reject( error ); + } + + return resolve( bodyOrData ); + } + ); + } ); + } + return next( options ); +}; + export const wpcomProxyMiddleware = parameters => { // Make authenticated calls using the WordPress.com REST Proxy // bypassing the apiFetch call that uses window.fetch. diff --git a/client/gutenberg/editor/api-middleware/utils.js b/client/gutenberg/editor/api-middleware/utils.js index 7ea9adc85b7c4..e815c4c8fe1c3 100644 --- a/client/gutenberg/editor/api-middleware/utils.js +++ b/client/gutenberg/editor/api-middleware/utils.js @@ -14,6 +14,7 @@ import { pathRewriteMiddleware, urlRewriteMiddleware, wpcomProxyMiddleware, + oembedMiddleware, } from './index'; export class WithAPIMiddleware extends Component { @@ -44,6 +45,9 @@ export class WithAPIMiddleware extends Component { // This call intentionally breaks the middleware chain. apiFetch.use( options => wpcomProxyMiddleware( options ) ); + // This call may break the middleware chain if we match an oembed + apiFetch.use( ( options, next ) => oembedMiddleware( options, next, siteSlug ) ); + apiFetch.use( ( options, next ) => debugMiddleware( options, next ) ); apiFetch.use( ( options, next ) => urlRewriteMiddleware( options, next, siteSlug ) );