Skip to content

Commit

Permalink
api-fetch: Align exported type names with DefinitelyTyped types (#30570)
Browse files Browse the repository at this point in the history
* api-fetch: Align exported type names with DefinitelyTyped types

* Update changelog
  • Loading branch information
sarayourfriend authored Apr 7, 2021
1 parent 9c09a52 commit f84d26e
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 22 deletions.
4 changes: 4 additions & 0 deletions packages/api-fetch/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fixes

- Align exported type names with the DefinitelyTyped type names and actually export those types.

## 3.23.0 (2021-04-06)

### New Feature
Expand Down
12 changes: 6 additions & 6 deletions packages/api-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ const DEFAULT_OPTIONS = {
credentials: 'include',
};

/** @typedef {import('./types').ApiFetchMiddleware} ApiFetchMiddleware */
/** @typedef {import('./types').ApiFetchRequestProps} ApiFetchRequestProps */
/** @typedef {import('./types').APIFetchMiddleware} APIFetchMiddleware */
/** @typedef {import('./types').APIFetchOptions} APIFetchOptions */

/**
* @type {import('./types').ApiFetchMiddleware[]}
* @type {import('./types').APIFetchMiddleware[]}
*/
const middlewares = [
userLocaleMiddleware,
Expand All @@ -59,7 +59,7 @@ const middlewares = [
/**
* Register a middleware
*
* @param {import('./types').ApiFetchMiddleware} middleware
* @param {import('./types').APIFetchMiddleware} middleware
*/
function registerMiddleware( middleware ) {
middlewares.unshift( middleware );
Expand All @@ -80,7 +80,7 @@ const checkStatus = ( response ) => {
throw response;
};

/** @typedef {(options: import('./types').ApiFetchRequestProps) => Promise<any>} FetchHandler*/
/** @typedef {(options: import('./types').APIFetchOptions) => Promise<any>} FetchHandler*/

/**
* @type {FetchHandler}
Expand Down Expand Up @@ -149,7 +149,7 @@ function setFetchHandler( newFetchHandler ) {

/**
* @template T
* @param {import('./types').ApiFetchRequestProps} options
* @param {import('./types').APIFetchOptions} options
* @return {Promise<T>} A promise representing the request processed via the registered middlewares.
*/
function apiFetch( options ) {
Expand Down
8 changes: 4 additions & 4 deletions packages/api-fetch/src/middlewares/fetch-all-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import apiFetch from '..';
/**
* Apply query arguments to both URL and Path, whichever is present.
*
* @param {import('../types').ApiFetchRequestProps} props
* @param {import('../types').APIFetchOptions} props
* @param {Record<string, string | number>} queryArgs
* @return {import('../types').ApiFetchRequestProps} The request with the modified query args
* @return {import('../types').APIFetchOptions} The request with the modified query args
*/
const modifyQuery = ( { path, url, ...options }, queryArgs ) => ( {
...options,
Expand Down Expand Up @@ -56,7 +56,7 @@ const getNextPageUrl = ( response ) => {
};

/**
* @param {import('../types').ApiFetchRequestProps} options
* @param {import('../types').APIFetchOptions} options
* @return {boolean} True if the request contains an unbounded query.
*/
const requestContainsUnboundedQuery = ( options ) => {
Expand All @@ -72,7 +72,7 @@ const requestContainsUnboundedQuery = ( options ) => {
* collections, apiFetch consumers can pass `per_page=-1`; this middleware will
* then recursively assemble a full response array from all available pages.
*
* @type {import('../types').ApiFetchMiddleware}
* @type {import('../types').APIFetchMiddleware}
*/
const fetchAllMiddleware = async ( options, next ) => {
if ( options.parse === false ) {
Expand Down
2 changes: 1 addition & 1 deletion packages/api-fetch/src/middlewares/http-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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.
*
* @type {import('../types').ApiFetchMiddleware}
* @type {import('../types').APIFetchMiddleware}
*/
const httpV1Middleware = ( options, next ) => {
const { method = DEFAULT_METHOD } = options;
Expand Down
2 changes: 1 addition & 1 deletion packages/api-fetch/src/middlewares/media-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
/**
* Middleware handling media upload failures and retries.
*
* @type {import('../types').ApiFetchMiddleware}
* @type {import('../types').APIFetchMiddleware}
*/
const mediaUploadMiddleware = ( options, next ) => {
const isMediaUploadRequest =
Expand Down
2 changes: 1 addition & 1 deletion packages/api-fetch/src/middlewares/namespace-endpoint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @type {import('../types').ApiFetchMiddleware}
* @type {import('../types').APIFetchMiddleware}
*/
const namespaceAndEndpointMiddleware = ( options, next ) => {
let path = options.path;
Expand Down
4 changes: 2 additions & 2 deletions packages/api-fetch/src/middlewares/nonce.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* @param {string} nonce
* @return {import('../types').ApiFetchMiddleware & { nonce: string }} A middleware to enhance a request with a nonce.
* @return {import('../types').APIFetchMiddleware & { nonce: string }} A middleware to enhance a request with a nonce.
*/
function createNonceMiddleware( nonce ) {
/**
* @type {import('../types').ApiFetchMiddleware & { nonce: string }}
* @type {import('../types').APIFetchMiddleware & { nonce: string }}
*/
const middleware = ( options, next ) => {
const { headers = {} } = options;
Expand Down
2 changes: 1 addition & 1 deletion packages/api-fetch/src/middlewares/preloading.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function getStablePath( path ) {

/**
* @param {Record<string, any>} preloadedData
* @return {import('../types').ApiFetchMiddleware} Preloading middleware.
* @return {import('../types').APIFetchMiddleware} Preloading middleware.
*/
function createPreloadingMiddleware( preloadedData ) {
const cache = Object.keys( preloadedData ).reduce( ( result, path ) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/api-fetch/src/middlewares/root-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import namespaceAndEndpointMiddleware from './namespace-endpoint';

/**
* @param {string} rootURL
* @return {import('../types').ApiFetchMiddleware} Root URL middleware.
* @return {import('../types').APIFetchMiddleware} Root URL middleware.
*/
const createRootURLMiddleware = ( rootURL ) => ( options, next ) => {
return namespaceAndEndpointMiddleware( options, ( optionsWithPath ) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/api-fetch/src/middlewares/user-locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { addQueryArgs, hasQueryArg } from '@wordpress/url';

/**
* @type {import('../types').ApiFetchMiddleware}
* @type {import('../types').APIFetchMiddleware}
*/
const userLocaleMiddleware = ( options, next ) => {
if (
Expand Down
8 changes: 4 additions & 4 deletions packages/api-fetch/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface ApiFetchRequestProps extends RequestInit {
export interface APIFetchOptions extends RequestInit {
// Override headers, we only accept it as an object due to the `nonce` middleware
headers?: Record< string, string >;
path?: string;
Expand All @@ -12,7 +12,7 @@ export interface ApiFetchRequestProps extends RequestInit {
endpoint?: string;
}

export type ApiFetchMiddleware = (
options: ApiFetchRequestProps,
next: ( nextOptions: ApiFetchRequestProps ) => Promise< any >
export type APIFetchMiddleware = (
options: APIFetchOptions,
next: ( nextOptions: APIFetchOptions ) => Promise< any >
) => Promise< any >;

0 comments on commit f84d26e

Please sign in to comment.