Skip to content

Commit

Permalink
Merge pull request #43 from moon0326/update/fix-error-with-fetch-over…
Browse files Browse the repository at this point in the history
…ride

Fix error with fetch override
  • Loading branch information
moon0326 authored Sep 20, 2024
2 parents 6161078 + 7243f97 commit 2d4984b
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions resources/scripts/wp-openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@ import "@stoplight/elements/styles.min.css";

const elementsAppContainer = document.getElementById("elements-app");


const { fetch: originalFetch } = window;

window.fetch = async (...args) => {
let [resource, config] = args;
window.fetch = (resource, config) => {
if ( ! config ) {
config = {};
}

if ( ! config.headers ) {
config.headers = new Headers();
}

if (
resource.indexOf("wp-json") !== false &&
config?.headers &&
config.headers["X-WP-Nonce"] === undefined
) {
config.headers["X-WP-Nonce"] = wpOpenApi.nonce;
config.headers["X-WP-Nonce"] = window.wpOpenApi.nonce;
}

const response = await originalFetch(resource, config);

// response interceptor here
return response;
return originalFetch( resource, config );
};

const elements = (
Expand Down

0 comments on commit 2d4984b

Please sign in to comment.