Skip to content

Commit

Permalink
Fixes a problem in Promote Post with Jetpack sites using plain permal…
Browse files Browse the repository at this point in the history
…inks (#94728)
  • Loading branch information
sbarbosa authored Sep 24, 2024
1 parent 8dec86d commit 3215f1a
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions client/lib/promote-post/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,59 +278,72 @@ export function getRecordDSPEventHandler( dispatch: Dispatch, dspOriginProps?: D
};
}

type SupportedDSPMethods = 'GET' | 'POST' | 'PUT' | 'DELETE';

export const requestDSP = async < T >(
siteId: number,
apiUri: string,
method = 'GET',
body: Record< string, any > | undefined = undefined
method: SupportedDSPMethods = 'GET',
body: Record< string, unknown > | undefined = undefined
): Promise< T > => {
const URL_BASE = `/sites/${ siteId }/wordads/dsp/api/v1`;
const path = `${ URL_BASE }${ apiUri }`;

let requestBody = body;
let path = `${ URL_BASE }${ apiUri }`;

// Merges the query params into the requestBody, and removes them from the path variable
// Query params in the path causes problems on Jetpack sites using plain permalinks
if ( path.indexOf( '?' ) > 0 ) {
const search = path.substring( path.indexOf( '?' ) );
path = path.substring( 0, path.indexOf( '?' ) );

const queryParams = Object.fromEntries( new URLSearchParams( search ) );
requestBody = {
...requestBody,
...queryParams,
};
}

const params = {
path,
method,
apiNamespace: config.isEnabled( 'is_running_in_jetpack_site' )
? 'jetpack/v4/blaze-app'
: 'wpcom/v2',
body,
};

switch ( method ) {
case 'POST':
return await wpcom.req.post( params );
return await wpcom.req.post( params, requestBody );
case 'PUT':
return await wpcom.req.put( params );
return await wpcom.req.put( params, requestBody );
case 'DELETE':
return await wpcom.req.del( params );
return await wpcom.req.del( params, requestBody );
default:
return await wpcom.req.get( params );
return await wpcom.req.get( params, requestBody );
}
};

const handleDSPError = async < T >(
error: DSPError,
export const requestDSPHandleErrors = async < T >(
siteId: number,
currentURL: string
apiUri: string,
method: SupportedDSPMethods = 'GET',
body: Record< string, unknown > | undefined = undefined
): Promise< T > => {
if ( error.errorCode === DSP_ERROR_NO_LOCAL_USER ) {
const createUserQuery = await requestDSP< NewDSPUserResult >(
siteId,
DSP_URL_CHECK_UPSERT_USER
);
if ( createUserQuery.new_dsp_user ) {
// then we should retry the original query
return await requestDSP< T >( siteId, currentURL );
}
}
throw error;
};

export const requestDSPHandleErrors = async < T >( siteId: number, url: string ): Promise< T > => {
try {
return await requestDSP( siteId, url );
} catch ( e ) {
return await handleDSPError( e as DSPError, siteId, url );
return await requestDSP( siteId, apiUri, method, body );
} catch ( error ) {
if ( ( error as DSPError ).errorCode === DSP_ERROR_NO_LOCAL_USER ) {
const createUserQuery = await requestDSP< NewDSPUserResult >(
siteId,
DSP_URL_CHECK_UPSERT_USER
);
if ( createUserQuery.new_dsp_user ) {
// then we should retry the original query
return await requestDSP( siteId, apiUri, method, body );
}
}
throw error;
}
};

Expand Down

0 comments on commit 3215f1a

Please sign in to comment.