Skip to content

Commit

Permalink
Fix module and exports
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Sep 25, 2024
1 parent 8a5824f commit 385ebb6
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 59 deletions.
4 changes: 1 addition & 3 deletions packages/api-fetch/src/middlewares/fetch-all-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const requestContainsUnboundedQuery = ( options ) => {
*
* @type {import('../types').APIFetchMiddleware}
*/
const fetchAllMiddleware = async ( options, next ) => {
export const fetchAllMiddleware = async ( options, next ) => {
if ( options.parse === false ) {
// If a consumer has opted out of parsing, do not apply middleware.
return next( options );
Expand Down Expand Up @@ -124,5 +124,3 @@ const fetchAllMiddleware = async ( options, next ) => {
}
return mergedResults;
};

export default fetchAllMiddleware;
4 changes: 1 addition & 3 deletions packages/api-fetch/src/middlewares/http-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const DEFAULT_METHOD = 'GET';
*
* @type {import('../types').APIFetchMiddleware}
*/
const httpV1Middleware = ( options, next ) => {
export const httpV1Middleware = ( options, next ) => {
const { method = DEFAULT_METHOD } = options;
if ( OVERRIDE_METHODS.has( method.toUpperCase() ) ) {
options = {
Expand All @@ -39,5 +39,3 @@ const httpV1Middleware = ( options, next ) => {

return next( options );
};

export default httpV1Middleware;
4 changes: 1 addition & 3 deletions packages/api-fetch/src/middlewares/media-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function isMediaUploadRequest( options ) {
*
* @type {import('../types').APIFetchMiddleware}
*/
const mediaUploadMiddleware = ( options, next ) => {
export const mediaUploadMiddleware = ( options, next ) => {
if ( ! isMediaUploadRequest( options ) ) {
return next( options );
}
Expand Down Expand Up @@ -95,5 +95,3 @@ const mediaUploadMiddleware = ( options, next ) => {
parseResponseAndNormalizeError( response, options.parse )
);
};

export default mediaUploadMiddleware;
4 changes: 1 addition & 3 deletions packages/api-fetch/src/middlewares/namespace-endpoint.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @type {import('../types').APIFetchMiddleware}
*/
const namespaceAndEndpointMiddleware = ( options, next ) => {
export const namespaceAndEndpointMiddleware = ( options, next ) => {
let path = options.path;
let namespaceTrimmed, endpointTrimmed;

Expand All @@ -26,5 +26,3 @@ const namespaceAndEndpointMiddleware = ( options, next ) => {
path,
} );
};

export default namespaceAndEndpointMiddleware;
4 changes: 1 addition & 3 deletions packages/api-fetch/src/middlewares/nonce.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param {string} nonce
* @return {import('../types').APIFetchMiddleware & { nonce: string }} A middleware to enhance a request with a nonce.
*/
function createNonceMiddleware( nonce ) {
export function createNonceMiddleware( nonce ) {
/**
* @type {import('../types').APIFetchMiddleware & { nonce: string }}
*/
Expand Down Expand Up @@ -33,5 +33,3 @@ function createNonceMiddleware( nonce ) {

return middleware;
}

export default createNonceMiddleware;
4 changes: 1 addition & 3 deletions packages/api-fetch/src/middlewares/preloading.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { addQueryArgs, getQueryArgs, normalizePath } from '@wordpress/url';
* @param {Record<string, any>} preloadedData
* @return {import('../types').APIFetchMiddleware} Preloading middleware.
*/
function createPreloadingMiddleware( preloadedData ) {
export function createPreloadingMiddleware( preloadedData ) {
const cache = Object.fromEntries(
Object.entries( preloadedData ).map( ( [ path, data ] ) => [
normalizePath( path ),
Expand Down Expand Up @@ -78,5 +78,3 @@ function prepareResponse( responseData, parse ) {
} )
);
}

export default createPreloadingMiddleware;
6 changes: 2 additions & 4 deletions packages/api-fetch/src/middlewares/root-url.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Internal dependencies
*/
import namespaceAndEndpointMiddleware from './namespace-endpoint';
import { namespaceAndEndpointMiddleware } from './namespace-endpoint';

/**
* @param {string} rootURL
* @return {import('../types').APIFetchMiddleware} Root URL middleware.
*/
const createRootURLMiddleware = ( rootURL ) => ( options, next ) => {
export const createRootURLMiddleware = ( rootURL ) => ( options, next ) => {
return namespaceAndEndpointMiddleware( options, ( optionsWithPath ) => {
let url = optionsWithPath.url;
let path = optionsWithPath.path;
Expand Down Expand Up @@ -40,5 +40,3 @@ const createRootURLMiddleware = ( rootURL ) => ( options, next ) => {
} );
} );
};

export default createRootURLMiddleware;
4 changes: 2 additions & 2 deletions packages/api-fetch/src/middlewares/singleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import { fetchAllMiddleware } from './fetch-all-middleware';
import { namespaceEndpointMiddleware } from './namespace-endpoint';
import { namespaceAndEndpointMiddleware } from './namespace-endpoint';
import { httpV1Middleware } from './http-v1';
import { userLocaleMiddleware } from './user-locale';

Expand All @@ -11,7 +11,7 @@ import { userLocaleMiddleware } from './user-locale';
*/
export const middlewares = [
userLocaleMiddleware,
namespaceEndpointMiddleware,
namespaceAndEndpointMiddleware,
httpV1Middleware,
fetchAllMiddleware,
];
Expand Down
57 changes: 34 additions & 23 deletions packages/api-fetch/src/middlewares/theme-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,41 @@ import { addQueryArgs, getQueryArg, removeQueryArgs } from '@wordpress/url';
* @param {Record<string, any>} themePath
* @return {import('../types').APIFetchMiddleware} Preloading middleware.
*/
const createThemePreviewMiddleware = ( themePath ) => ( options, next ) => {
if ( typeof options.url === 'string' ) {
const wpThemePreview = getQueryArg( options.url, 'wp_theme_preview' );
if ( wpThemePreview === undefined ) {
options.url = addQueryArgs( options.url, {
wp_theme_preview: themePath,
} );
} else if ( wpThemePreview === '' ) {
options.url = removeQueryArgs( options.url, 'wp_theme_preview' );
export const createThemePreviewMiddleware =
( themePath ) => ( options, next ) => {
if ( typeof options.url === 'string' ) {
const wpThemePreview = getQueryArg(
options.url,
'wp_theme_preview'
);
if ( wpThemePreview === undefined ) {
options.url = addQueryArgs( options.url, {
wp_theme_preview: themePath,
} );
} else if ( wpThemePreview === '' ) {
options.url = removeQueryArgs(
options.url,
'wp_theme_preview'
);
}
}
}

if ( typeof options.path === 'string' ) {
const wpThemePreview = getQueryArg( options.path, 'wp_theme_preview' );
if ( wpThemePreview === undefined ) {
options.path = addQueryArgs( options.path, {
wp_theme_preview: themePath,
} );
} else if ( wpThemePreview === '' ) {
options.path = removeQueryArgs( options.path, 'wp_theme_preview' );
if ( typeof options.path === 'string' ) {
const wpThemePreview = getQueryArg(
options.path,
'wp_theme_preview'
);
if ( wpThemePreview === undefined ) {
options.path = addQueryArgs( options.path, {
wp_theme_preview: themePath,
} );
} else if ( wpThemePreview === '' ) {
options.path = removeQueryArgs(
options.path,
'wp_theme_preview'
);
}
}
}

return next( options );
};

export default createThemePreviewMiddleware;
return next( options );
};
4 changes: 1 addition & 3 deletions packages/api-fetch/src/middlewares/user-locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { addQueryArgs, hasQueryArg } from '@wordpress/url';
/**
* @type {import('../types').APIFetchMiddleware}
*/
const userLocaleMiddleware = ( options, next ) => {
export const userLocaleMiddleware = ( options, next ) => {
if (
typeof options.url === 'string' &&
! hasQueryArg( options.url, '_locale' )
Expand All @@ -23,5 +23,3 @@ const userLocaleMiddleware = ( options, next ) => {

return next( options );
};

export default userLocaleMiddleware;
20 changes: 11 additions & 9 deletions packages/api-fetch/src/module/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable no-console */

/**
* Internal dependencies
*/
import { createNonceMiddleware } from './middlewares/nonce';
import { createRootURLMiddleware } from './middlewares/root-url';
import { createPreloadingMiddleware } from './middlewares/preloading';
import { fetchAllMiddleware } from './middlewares/fetch-all-middleware';
import { mediaUploadMiddleware } from './middlewares/media-upload';
import { createThemePreviewMiddleware } from './middlewares/theme-preview';
import { apiFetch, setFetchHandler } from './core';
import { registerMiddleware } from './middlewares/singleton';
import { createNonceMiddleware } from '../middlewares/nonce';
import { createRootURLMiddleware } from '../middlewares/root-url';
import { createPreloadingMiddleware } from '../middlewares/preloading';
import { fetchAllMiddleware } from '../middlewares/fetch-all-middleware';
import { mediaUploadMiddleware } from '../middlewares/media-upload';
import { createThemePreviewMiddleware } from '../middlewares/theme-preview';
import { apiFetch, setFetchHandler } from '../core';
import { registerMiddleware } from '../middlewares/singleton';

if ( typeof document !== 'undefined' ) {
const el = document.getElementById(
Expand Down Expand Up @@ -38,7 +41,6 @@ if ( typeof document !== 'undefined' ) {
}
if ( config.themePreviewPath ) {
registerMiddleware(
// @ts-expect-error This is wrong, done for testing.
createThemePreviewMiddleware( config.themePreviewPath )
);
}
Expand Down

0 comments on commit 385ebb6

Please sign in to comment.