From 385ebb650813bf18bbb301d18eae7dd07dd8cd83 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 25 Sep 2024 17:34:24 +0200 Subject: [PATCH] Fix module and exports --- .../src/middlewares/fetch-all-middleware.js | 4 +- packages/api-fetch/src/middlewares/http-v1.js | 4 +- .../api-fetch/src/middlewares/media-upload.js | 4 +- .../src/middlewares/namespace-endpoint.js | 4 +- packages/api-fetch/src/middlewares/nonce.js | 4 +- .../api-fetch/src/middlewares/preloading.js | 4 +- .../api-fetch/src/middlewares/root-url.js | 6 +- .../api-fetch/src/middlewares/singleton.js | 4 +- .../src/middlewares/theme-preview.js | 57 +++++++++++-------- .../api-fetch/src/middlewares/user-locale.js | 4 +- packages/api-fetch/src/module/index.ts | 20 ++++--- 11 files changed, 56 insertions(+), 59 deletions(-) diff --git a/packages/api-fetch/src/middlewares/fetch-all-middleware.js b/packages/api-fetch/src/middlewares/fetch-all-middleware.js index 30834353fde81..21ca2ef4fef90 100644 --- a/packages/api-fetch/src/middlewares/fetch-all-middleware.js +++ b/packages/api-fetch/src/middlewares/fetch-all-middleware.js @@ -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 ); @@ -124,5 +124,3 @@ const fetchAllMiddleware = async ( options, next ) => { } return mergedResults; }; - -export default fetchAllMiddleware; diff --git a/packages/api-fetch/src/middlewares/http-v1.js b/packages/api-fetch/src/middlewares/http-v1.js index 953fc26ff70c8..78daf62282ab7 100644 --- a/packages/api-fetch/src/middlewares/http-v1.js +++ b/packages/api-fetch/src/middlewares/http-v1.js @@ -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 = { @@ -39,5 +39,3 @@ const httpV1Middleware = ( options, next ) => { return next( options ); }; - -export default httpV1Middleware; diff --git a/packages/api-fetch/src/middlewares/media-upload.js b/packages/api-fetch/src/middlewares/media-upload.js index ddd0be4e4ab43..b4cf8457b002d 100644 --- a/packages/api-fetch/src/middlewares/media-upload.js +++ b/packages/api-fetch/src/middlewares/media-upload.js @@ -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 ); } @@ -95,5 +95,3 @@ const mediaUploadMiddleware = ( options, next ) => { parseResponseAndNormalizeError( response, options.parse ) ); }; - -export default mediaUploadMiddleware; diff --git a/packages/api-fetch/src/middlewares/namespace-endpoint.js b/packages/api-fetch/src/middlewares/namespace-endpoint.js index bfb0e824187f3..5ffacabaae250 100644 --- a/packages/api-fetch/src/middlewares/namespace-endpoint.js +++ b/packages/api-fetch/src/middlewares/namespace-endpoint.js @@ -1,7 +1,7 @@ /** * @type {import('../types').APIFetchMiddleware} */ -const namespaceAndEndpointMiddleware = ( options, next ) => { +export const namespaceAndEndpointMiddleware = ( options, next ) => { let path = options.path; let namespaceTrimmed, endpointTrimmed; @@ -26,5 +26,3 @@ const namespaceAndEndpointMiddleware = ( options, next ) => { path, } ); }; - -export default namespaceAndEndpointMiddleware; diff --git a/packages/api-fetch/src/middlewares/nonce.js b/packages/api-fetch/src/middlewares/nonce.js index 60914823661ed..21e432bb4aa9a 100644 --- a/packages/api-fetch/src/middlewares/nonce.js +++ b/packages/api-fetch/src/middlewares/nonce.js @@ -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 }} */ @@ -33,5 +33,3 @@ function createNonceMiddleware( nonce ) { return middleware; } - -export default createNonceMiddleware; diff --git a/packages/api-fetch/src/middlewares/preloading.js b/packages/api-fetch/src/middlewares/preloading.js index ac293738513bd..7b07671c580f8 100644 --- a/packages/api-fetch/src/middlewares/preloading.js +++ b/packages/api-fetch/src/middlewares/preloading.js @@ -7,7 +7,7 @@ import { addQueryArgs, getQueryArgs, normalizePath } from '@wordpress/url'; * @param {Record} 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 ), @@ -78,5 +78,3 @@ function prepareResponse( responseData, parse ) { } ) ); } - -export default createPreloadingMiddleware; diff --git a/packages/api-fetch/src/middlewares/root-url.js b/packages/api-fetch/src/middlewares/root-url.js index cde855f3f9504..58c17ace39ecb 100644 --- a/packages/api-fetch/src/middlewares/root-url.js +++ b/packages/api-fetch/src/middlewares/root-url.js @@ -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; @@ -40,5 +40,3 @@ const createRootURLMiddleware = ( rootURL ) => ( options, next ) => { } ); } ); }; - -export default createRootURLMiddleware; diff --git a/packages/api-fetch/src/middlewares/singleton.js b/packages/api-fetch/src/middlewares/singleton.js index 48fc4ff23b6cd..e38fb71a1fd7d 100644 --- a/packages/api-fetch/src/middlewares/singleton.js +++ b/packages/api-fetch/src/middlewares/singleton.js @@ -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'; @@ -11,7 +11,7 @@ import { userLocaleMiddleware } from './user-locale'; */ export const middlewares = [ userLocaleMiddleware, - namespaceEndpointMiddleware, + namespaceAndEndpointMiddleware, httpV1Middleware, fetchAllMiddleware, ]; diff --git a/packages/api-fetch/src/middlewares/theme-preview.js b/packages/api-fetch/src/middlewares/theme-preview.js index 56098c88cb351..4cc6a1977cd45 100644 --- a/packages/api-fetch/src/middlewares/theme-preview.js +++ b/packages/api-fetch/src/middlewares/theme-preview.js @@ -13,30 +13,41 @@ import { addQueryArgs, getQueryArg, removeQueryArgs } from '@wordpress/url'; * @param {Record} 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 ); + }; diff --git a/packages/api-fetch/src/middlewares/user-locale.js b/packages/api-fetch/src/middlewares/user-locale.js index 18057cb41fa06..fb7644f82ff13 100644 --- a/packages/api-fetch/src/middlewares/user-locale.js +++ b/packages/api-fetch/src/middlewares/user-locale.js @@ -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' ) @@ -23,5 +23,3 @@ const userLocaleMiddleware = ( options, next ) => { return next( options ); }; - -export default userLocaleMiddleware; diff --git a/packages/api-fetch/src/module/index.ts b/packages/api-fetch/src/module/index.ts index 7e71670c006e8..9a8f495a02af0 100644 --- a/packages/api-fetch/src/module/index.ts +++ b/packages/api-fetch/src/module/index.ts @@ -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( @@ -38,7 +41,6 @@ if ( typeof document !== 'undefined' ) { } if ( config.themePreviewPath ) { registerMiddleware( - // @ts-expect-error This is wrong, done for testing. createThemePreviewMiddleware( config.themePreviewPath ) ); }