Skip to content

Commit

Permalink
api-fetch: Add types to media-upload middleware (#29901)
Browse files Browse the repository at this point in the history
* api-fetch: Add types to media-upload middleware

* Remove runtime changes and denormalize return type

* Add descriptions to functions missing them
  • Loading branch information
sarayourfriend authored Mar 22, 2021
1 parent 63947e0 commit 2bae770
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
15 changes: 8 additions & 7 deletions packages/api-fetch/src/middlewares/media-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@ import {
/**
* Middleware handling media upload failures and retries.
*
* @param {Object} options Fetch options.
* @param {Function} next [description]
*
* @return {*} The evaluated result of the remaining middleware chain.
* @type {import('../types').ApiFetchMiddleware}
*/
function mediaUploadMiddleware( options, next ) {
const mediaUploadMiddleware = ( options, next ) => {
const isMediaUploadRequest =
( options.path && options.path.indexOf( '/wp/v2/media' ) !== -1 ) ||
( options.url && options.url.indexOf( '/wp/v2/media' ) !== -1 );

if ( ! isMediaUploadRequest ) {
return next( options, next );
return next( options );
}
let retries = 0;
const maxRetries = 5;

/**
* @param {string} attachmentId
* @return {Promise<any>} Processed post response.
*/
const postProcess = ( attachmentId ) => {
retries++;
return next( {
Expand Down Expand Up @@ -78,6 +79,6 @@ function mediaUploadMiddleware( options, next ) {
.then( ( response ) =>
parseResponseAndNormalizeError( response, options.parse )
);
}
};

export default mediaUploadMiddleware;
18 changes: 16 additions & 2 deletions packages/api-fetch/src/utils/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { __ } from '@wordpress/i18n';
* @param {Response} response
* @param {boolean} shouldParseResponse
*
* @return {Promise} Parsed response
* @return {Promise<any> | null | Response} Parsed response.
*/
const parseResponse = ( response, shouldParseResponse = true ) => {
if ( shouldParseResponse ) {
Expand All @@ -23,6 +23,13 @@ const parseResponse = ( response, shouldParseResponse = true ) => {
return response;
};

/**
* Calls the `json` function on the Response, throwing an error if the response
* doesn't have a json function or if parsing the json itself fails.
*
* @param {Response} response
* @return {Promise<any>} Parsed response.
*/
const parseJsonAndNormalizeError = ( response ) => {
const invalidJsonError = {
code: 'invalid_json',
Expand All @@ -44,7 +51,7 @@ const parseJsonAndNormalizeError = ( response ) => {
* @param {Response} response
* @param {boolean} shouldParseResponse
*
* @return {Promise} Parsed response.
* @return {Promise<any>} Parsed response.
*/
export const parseResponseAndNormalizeError = (
response,
Expand All @@ -55,6 +62,13 @@ export const parseResponseAndNormalizeError = (
).catch( ( res ) => parseAndThrowError( res, shouldParseResponse ) );
};

/**
* Parses a response, throwing an error if parsing the response fails.
*
* @param {Response} response
* @param {boolean} shouldParseResponse
* @return {Promise<any>} Parsed response.
*/
export function parseAndThrowError( response, shouldParseResponse = true ) {
if ( ! shouldParseResponse ) {
throw response;
Expand Down
2 changes: 2 additions & 0 deletions packages/api-fetch/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
{ "path": "../url" }
],
"include": [
"src/middlewares/media-upload.js",
"src/middlewares/namespace-endpoint.js",
"src/middlewares/nonce.js",
"src/middlewares/preloading.js",
"src/middlewares/root-url.js",
"src/middlewares/user-locale.js",
"src/utils/**/*",
"src/types.ts"
]
}

0 comments on commit 2bae770

Please sign in to comment.