Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const applyAPIMiddleware = getSiteSlug => {
// First middleware in, last out.
// This call intentionally breaks the middleware chain.
apiFetch.use( wpcomProxyMiddleware );
apiFetch.use( debugMiddleware );
apiFetch.use( wpcomPathMappingMiddleware( getSiteSlug ) );
//depends on wpcomPathMappingMiddleware
apiFetch.use( apiFetch.fetchAllMiddleware );
apiFetch.use( apiFetch.createRootURLMiddleware( 'https://public-api.wordpress.com/' ) );
};
async function foo() {
apiFetch({ path: '/wp/v2/posts' }).then(posts =>
posts.map(({ date, title }) => `Post "${title.rendered}" at ${date}`)
);
const response = await apiFetch({ parse: false });
if (response.ok) {
console.log(await response.json());
}
}
const x: Middleware = async (undefined, next) => {
const x = await next({});
return next({});
};
apiFetch.use(apiFetch.fetchAllMiddleware);
apiFetch.use(apiFetch.createRootURLMiddleware('https://foo.bar/wp-json'));
apiFetch.setFetchHandler(options => {
const { url, path, data, method } = options;
return fetch(url || path || '', {
method,
body: JSON.stringify(data),
});
});