Skip to content

Commit

Permalink
refactor(http): Extract GotTask type (#25713)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Nov 12, 2023
1 parent deeab52 commit 19e9e8e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/util/http/index.ts
Expand Up @@ -19,6 +19,7 @@ import { Throttle, getThrottle } from './throttle';
import type {
GotJSONOptions,
GotOptions,
GotTask,
HttpOptions,
HttpRequestOptions,
HttpResponse,
Expand All @@ -43,8 +44,6 @@ type JsonArgs<
schema?: Schema;
};

type Task<T> = () => Promise<HttpResponse<T>>;

// Copying will help to avoid circular structure
// and mutation of the cached response.
function copyResponse<T>(
Expand Down Expand Up @@ -207,7 +206,7 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
// istanbul ignore else: no cache tests
if (!resPromise) {
const startTime = Date.now();
const httpTask: Task<T> = () => {
const httpTask: GotTask<T> = () => {
const queueDuration = Date.now() - startTime;
return gotTask(url, options, {
method: options.method,
Expand All @@ -217,12 +216,12 @@ export class Http<Opts extends HttpOptions = HttpOptions> {
};

const throttle = this.getThrottle(url);
const throttledTask: Task<T> = throttle
const throttledTask: GotTask<T> = throttle
? () => throttle.add<HttpResponse<T>>(httpTask)
: httpTask;

const queue = getQueue(url);
const queuedTask: Task<T> = queue
const queuedTask: GotTask<T> = queue
? () => queue.add<HttpResponse<T>>(throttledTask)
: throttledTask;

Expand Down
2 changes: 2 additions & 0 deletions lib/util/http/types.ts
Expand Up @@ -91,3 +91,5 @@ export interface HttpResponse<T = string> {
headers: HttpHeaders;
authorization?: boolean;
}

export type GotTask<T> = () => Promise<HttpResponse<T>>;

0 comments on commit 19e9e8e

Please sign in to comment.