Skip to content

Commit

Permalink
api-fetch: Add types to http-v1 middleware (#30150)
Browse files Browse the repository at this point in the history
* api-fetch: Add types to http-v1 middleware

* Remove duplicate api fetch props
  • Loading branch information
sarayourfriend authored Mar 24, 2021
1 parent 66e2b35 commit 499d0a2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
11 changes: 4 additions & 7 deletions packages/api-fetch/src/middlewares/http-v1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Set of HTTP methods which are eligible to be overridden.
*
* @type {Set}
* @type {Set<string>}
*/
const OVERRIDE_METHODS = new Set( [ 'PATCH', 'PUT', 'DELETE' ] );

Expand All @@ -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 = {
Expand All @@ -41,6 +38,6 @@ function httpV1Middleware( options, next ) {
}

return next( options );
}
};

export default httpV1Middleware;
4 changes: 2 additions & 2 deletions packages/api-fetch/src/types.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,7 +10,6 @@ export interface ApiFetchRequestProps {
data?: any;
namespace?: string;
endpoint?: string;
method?: string;
}

export type ApiFetchMiddleware = (
Expand Down
1 change: 1 addition & 0 deletions packages/api-fetch/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 499d0a2

Please sign in to comment.