diff --git a/src/OnboardingSPA/utils/api/common.js b/src/OnboardingSPA/utils/api/common.js new file mode 100644 index 000000000..94f4754ea --- /dev/null +++ b/src/OnboardingSPA/utils/api/common.js @@ -0,0 +1,5 @@ +import { onboardingRestBase } from '../../../constants'; + +export const onboardingRestURL = (api) => { + return (`${ onboardingRestBase }/${ api }` + ( window.nfdOnboarding?.currentFlow ? `&flow=${window.nfdOnboarding.currentFlow}` : '')); +} diff --git a/src/OnboardingSPA/utils/api/events.js b/src/OnboardingSPA/utils/api/events.js index c1356b10a..b4b40cec2 100644 --- a/src/OnboardingSPA/utils/api/events.js +++ b/src/OnboardingSPA/utils/api/events.js @@ -1,5 +1,6 @@ import apiFetch from '@wordpress/api-fetch'; -import { onboardingRestBase } from '../../../constants'; + +import { onboardingRestURL } from './common'; class Event { constructor( eventSlug, eventData = {} ) { @@ -9,7 +10,7 @@ class Event { send() { apiFetch( { - url: `${ onboardingRestBase }/events`, + url: onboardingRestURL( 'events' ), method: 'POST', data: { slug: this.eventSlug, diff --git a/src/OnboardingSPA/utils/api/flow.js b/src/OnboardingSPA/utils/api/flow.js index 03e41980e..73507e06c 100644 --- a/src/OnboardingSPA/utils/api/flow.js +++ b/src/OnboardingSPA/utils/api/flow.js @@ -1,18 +1,19 @@ import { resolve } from './resolve'; import { onboardingRestBase } from '../../../constants'; +import { onboardingRestURL } from './common'; import apiFetch from '@wordpress/api-fetch'; export async function getFlow() { return await resolve( - apiFetch( { url: `${ onboardingRestBase }/flow` } ).then() + apiFetch( { url: onboardingRestURL( 'flow' ) } ).then() ); } export async function setFlow( data ) { return await resolve( apiFetch( { - url: `${ onboardingRestBase }/flow`, + url: onboardingRestURL( 'flow' ), method: 'POST', data, } ).then() diff --git a/src/OnboardingSPA/utils/api/plugins.js b/src/OnboardingSPA/utils/api/plugins.js index c812bdb46..64d107bc1 100644 --- a/src/OnboardingSPA/utils/api/plugins.js +++ b/src/OnboardingSPA/utils/api/plugins.js @@ -1,10 +1,10 @@ -import { onboardingRestBase } from '../../../constants'; - import apiFetch from '@wordpress/api-fetch'; +import { onboardingRestURL } from './common'; + export const init = () => { apiFetch( { - url: `${ onboardingRestBase }/plugins/initialize&flow=${ window.nfdOnboarding.currentFlow }`, + url: onboardingRestURL( 'plugins/initialize' ), method: 'POST', headers: { 'X-NFD-ONBOARDING': window.nfdOnboarding.pluginInstallHash, diff --git a/src/OnboardingSPA/utils/api/settings.js b/src/OnboardingSPA/utils/api/settings.js index ecf8022d6..9c571f752 100644 --- a/src/OnboardingSPA/utils/api/settings.js +++ b/src/OnboardingSPA/utils/api/settings.js @@ -1,19 +1,19 @@ import apiFetch from '@wordpress/api-fetch'; -import { onboardingRestBase } from '../../../constants'; import { resolve } from './resolve.js'; import { MAX_RETRIES_SETTINGS_INIT } from '../../../constants'; +import { onboardingRestURL } from './common'; export async function getSettings() { return await resolve( - apiFetch( { url: `${ onboardingRestBase }/settings` } ).then() + apiFetch( { url: onboardingRestURL( 'settings' ) } ).then() ); } export async function setSettings( data ) { return await resolve( apiFetch( { - url: `${ onboardingRestBase }/settings`, + url: onboardingRestURL( 'settings' ), method: 'POST', data, } ).then() @@ -25,7 +25,7 @@ export const initialize = ( retries = 0 ) => { return false; } return apiFetch( { - url: `${ onboardingRestBase }/settings/initialize`, + url: onboardingRestURL( 'settings/initialize' ), method: 'POST', } ).catch( () => { retries = retries + 1; diff --git a/src/OnboardingSPA/utils/api/themes.js b/src/OnboardingSPA/utils/api/themes.js index 575af7956..9ea26e9fb 100644 --- a/src/OnboardingSPA/utils/api/themes.js +++ b/src/OnboardingSPA/utils/api/themes.js @@ -1,10 +1,10 @@ -import { onboardingRestBase } from '../../../constants'; - import apiFetch from '@wordpress/api-fetch'; +import { onboardingRestURL } from './common'; + export const init = () => { apiFetch( { - url: `${ onboardingRestBase }/themes/initialize&flow=${ window.nfdOnboarding.currentFlow }`, + url: onboardingRestURL( 'themes/initialize' ), method: 'POST', } ).catch( ( error ) => { console.error( error );