From 6618a262537258b59083da0458216c7ff4d978b8 Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Tue, 23 Mar 2021 09:16:32 -0700 Subject: [PATCH 1/2] api-fetch: Add types to http-v1 middleware --- packages/api-fetch/src/middlewares/http-v1.js | 11 ++++------- packages/api-fetch/src/types.ts | 3 ++- packages/api-fetch/tsconfig.json | 1 + 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/api-fetch/src/middlewares/http-v1.js b/packages/api-fetch/src/middlewares/http-v1.js index cce48b5ed79422..8f8ced80c34152 100644 --- a/packages/api-fetch/src/middlewares/http-v1.js +++ b/packages/api-fetch/src/middlewares/http-v1.js @@ -1,7 +1,7 @@ /** * Set of HTTP methods which are eligible to be overridden. * - * @type {Set} + * @type {Set} */ const OVERRIDE_METHODS = new Set( [ 'PATCH', 'PUT', 'DELETE' ] ); @@ -21,12 +21,9 @@ const DEFAULT_METHOD = 'GET'; * API Fetch middleware which overrides the request method for HTTP v1 * compatibility leveraging the REST API X-HTTP-Method-Override header. * - * @param {Object} options Fetch options. - * @param {Function} next [description] - * - * @return {*} The evaluated result of the remaining middleware chain. + * @type {import('../types').ApiFetchMiddleware} */ -function httpV1Middleware( options, next ) { +const httpV1Middleware = ( options, next ) => { const { method = DEFAULT_METHOD } = options; if ( OVERRIDE_METHODS.has( method.toUpperCase() ) ) { options = { @@ -41,6 +38,6 @@ function httpV1Middleware( options, next ) { } return next( options ); -} +}; export default httpV1Middleware; diff --git a/packages/api-fetch/src/types.ts b/packages/api-fetch/src/types.ts index 05e6bb461134ab..51aa08ba7440d8 100644 --- a/packages/api-fetch/src/types.ts +++ b/packages/api-fetch/src/types.ts @@ -1,4 +1,5 @@ -export interface ApiFetchRequestProps { +export interface ApiFetchRequestProps extends RequestInit { + // Override headers, we only accept it as an object due to the `nonce` middleware headers?: Record< string, string >; path?: string; url?: string; diff --git a/packages/api-fetch/tsconfig.json b/packages/api-fetch/tsconfig.json index 6cbac8bf71f9c7..3ad48264a46d28 100644 --- a/packages/api-fetch/tsconfig.json +++ b/packages/api-fetch/tsconfig.json @@ -8,6 +8,7 @@ { "path": "../url" } ], "include": [ + "src/middlewares/http-v1.js", "src/middlewares/media-upload.js", "src/middlewares/namespace-endpoint.js", "src/middlewares/nonce.js", From 469968627f0912b2960739e1fc3fe6686f8ea83c Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Tue, 23 Mar 2021 12:15:01 -0700 Subject: [PATCH 2/2] Remove duplicate api fetch props --- packages/api-fetch/src/types.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/api-fetch/src/types.ts b/packages/api-fetch/src/types.ts index 51aa08ba7440d8..93c466e8cd916b 100644 --- a/packages/api-fetch/src/types.ts +++ b/packages/api-fetch/src/types.ts @@ -10,7 +10,6 @@ export interface ApiFetchRequestProps extends RequestInit { data?: any; namespace?: string; endpoint?: string; - method?: string; } export type ApiFetchMiddleware = (