Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api-fetch: Add types to http-v1 middleware #30150

Merged
merged 2 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
3 changes: 2 additions & 1 deletion 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
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
headers?: Record< string, string >;
path?: string;
url?: string;
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