Skip to content

Commit

Permalink
fix: allow setting retryDelay as a request option
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Apr 17, 2024
1 parent a6cfbed commit 08e5f24
Show file tree
Hide file tree
Showing 27 changed files with 38 additions and 34 deletions.
1 change: 0 additions & 1 deletion src/middleware.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export * from './middleware/promise'
export * from './middleware/proxy'
export * from './middleware/retry/browser-retry'
export * from './middleware/urlEncoded'
export type * from './types'

import {agent} from './middleware/agent/browser-agent'
import {buildKeepAlive} from './middleware/keepAlive'
Expand Down
1 change: 0 additions & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export * from './middleware/promise'
export * from './middleware/proxy'
export * from './middleware/retry/node-retry'
export * from './middleware/urlEncoded'
export type * from './types'

import {agent} from './middleware/agent/node-agent'
import {buildKeepAlive} from './middleware/keepAlive'
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Middleware} from '../types'
import type {Middleware} from 'get-it'

const leadingSlash = /^\//
const trailingSlash = /\/$/
Expand Down
3 changes: 1 addition & 2 deletions src/middleware/debug.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import debugIt from 'debug'

import type {Middleware} from '../types'
import type {Middleware} from 'get-it'

const SENSITIVE_HEADERS = ['cookie', 'authorization']

Expand Down
2 changes: 1 addition & 1 deletion src/middleware/defaultOptionsProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {MiddlewareHooks, RequestOptions} from '../types'
import type {MiddlewareHooks, RequestOptions} from 'get-it'

const isReactNative = typeof navigator === 'undefined' ? false : navigator.product === 'ReactNative'

Expand Down
2 changes: 1 addition & 1 deletion src/middleware/defaultOptionsValidator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {MiddlewareHooks} from '../types'
import type {MiddlewareHooks} from 'get-it'

const validUrl = /^https?:\/\//i

Expand Down
2 changes: 1 addition & 1 deletion src/middleware/headers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Middleware} from '../types'
import type {Middleware} from 'get-it'

/** @public */
export function headers(_headers: any, opts: any = {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/httpErrors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Middleware} from '../types'
import type {Middleware} from 'get-it'

class HttpError extends Error {
response: any
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/injectResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Middleware, MiddlewareHooks, MiddlewareResponse} from '../types'
import type {Middleware, MiddlewareHooks, MiddlewareResponse} from 'get-it'

/** @public */
export function injectResponse(
Expand Down
3 changes: 2 additions & 1 deletion src/middleware/jsonRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {Middleware} from '../types'
import type {Middleware} from 'get-it'

import {isBuffer} from '../util/isBuffer'
import {isPlainObject} from '../util/isPlainObject'

Expand Down
2 changes: 1 addition & 1 deletion src/middleware/jsonResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Middleware} from '../types'
import type {Middleware} from 'get-it'

/** @public */
export function jsonResponse(opts?: any) {
Expand Down
3 changes: 2 additions & 1 deletion src/middleware/mtls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {Middleware} from '../types'
import type {Middleware} from 'get-it'

import {isBrowserOptions} from '../util/isBrowserOptions'

/** @public */
Expand Down
3 changes: 2 additions & 1 deletion src/middleware/observable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {Middleware} from '../types'
import type {Middleware} from 'get-it'

import global from '../util/global'

/** @public */
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/progress/browser-progress.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Middleware} from '../../types'
import type {Middleware} from 'get-it'

/** @public */
export function progress() {
Expand Down
3 changes: 1 addition & 2 deletions src/middleware/progress/node-progress.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {Middleware} from 'get-it'
import progressStream from 'progress-stream'

import type {Middleware} from '../../types'

function normalizer(stage: 'download' | 'upload') {
return (prog: any) => ({
stage,
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/promise.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Middleware} from '../types'
import type {Middleware} from 'get-it'

/** @public */
export const promise = (
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Middleware} from '../types'
import type {Middleware} from 'get-it'

/** @public */
export function proxy(_proxy: any) {
Expand Down
3 changes: 2 additions & 1 deletion src/middleware/retry/browser-retry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {RetryOptions} from '../../types'
import type {RetryOptions} from 'get-it'

import defaultShouldRetry from '../../util/browser-shouldRetry'
import sharedRetry from './shared-retry'

Expand Down
3 changes: 2 additions & 1 deletion src/middleware/retry/node-retry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {RetryOptions} from '../../types'
import type {RetryOptions} from 'get-it'

import defaultShouldRetry from '../../util/node-shouldRetry'
import sharedRetry from './shared-retry'

Expand Down
5 changes: 3 additions & 2 deletions src/middleware/retry/shared-retry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Middleware, RetryOptions} from '../../types'
import type {Middleware, RetryOptions} from 'get-it'

const isStream = (stream: any) =>
stream !== null && typeof stream === 'object' && typeof stream.pipe === 'function'
Expand All @@ -13,6 +13,7 @@ export default (opts: RetryOptions) => {
onError: (err, context) => {
const options = context.options
const max = options.maxRetries || maxRetries
const delay = options.retryDelay || retryDelay
const shouldRetry = options.shouldRetry || allowRetry
const attemptNumber = options.attemptNumber || 0

Expand All @@ -32,7 +33,7 @@ export default (opts: RetryOptions) => {
})

// Wait a given amount of time before doing the request again
setTimeout(() => context.channels.request.publish(newContext), retryDelay(attemptNumber))
setTimeout(() => context.channels.request.publish(newContext), delay(attemptNumber))

// Signal that we've handled the error and that it should not propagate further
return null
Expand Down
3 changes: 2 additions & 1 deletion src/middleware/urlEncoded.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {Middleware} from '../types'
import type {Middleware} from 'get-it'

import {isBuffer} from '../util/isBuffer'
import {isPlainObject} from '../util/isPlainObject'

Expand Down
2 changes: 1 addition & 1 deletion src/request/browser-request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {HttpRequest, MiddlewareResponse, RequestAdapter, RequestOptions} from 'get-it'
import parseHeaders from 'parse-headers'

import type {HttpRequest, MiddlewareResponse, RequestAdapter, RequestOptions} from '../types'
import {FetchXhr} from './browser/fetchXhr'

/**
Expand Down
12 changes: 6 additions & 6 deletions src/request/node-request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import decompressResponse from 'decompress-response'
import follow, {type FollowResponse, type RedirectableRequest} from 'follow-redirects'
import type {
FinalizeNodeOptionsPayload,
HttpRequest,
MiddlewareResponse,
RequestAdapter,
} from 'get-it'
import http from 'http'
import https from 'https'
import toStream from 'into-stream'
Expand All @@ -8,12 +14,6 @@ import progressStream from 'progress-stream'
import qs from 'querystring'
import url from 'url'

import type {
FinalizeNodeOptionsPayload,
HttpRequest,
MiddlewareResponse,
RequestAdapter,
} from '../types'
import {lowerCaseHeaders} from '../util/lowerCaseHeaders'
import {getProxyOptions, rewriteUriForProxy} from './node/proxy'
import {concat} from './node/simpleConcat'
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface RequestOptions {
headers?: any
maxRedirects?: number
maxRetries?: number
retryDelay?: (attemptNumber: number) => number
method?: string
proxy?: any
query?: any
Expand Down
2 changes: 1 addition & 1 deletion src/util/isBrowserOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {RequestOptions} from '../types'
import type {RequestOptions} from 'get-it'

export function isBrowserOptions(options: unknown): options is RequestOptions {
return typeof options === 'object' && options !== null && !('protocol' in options)
Expand Down
2 changes: 1 addition & 1 deletion src/util/middlewareReducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {ApplyMiddleware, MiddlewareReducer} from '../types'
import type {ApplyMiddleware, MiddlewareReducer} from 'get-it'

export const middlewareReducer = (middleware: MiddlewareReducer) =>
function applyMiddleware(hook, defaultValue, ...args) {
Expand Down
2 changes: 1 addition & 1 deletion src/util/pubsub.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code borrowed from https://github.com/bjoerge/nano-pubsub

import type {PubSub, Subscriber} from '../types'
import type {PubSub, Subscriber} from 'get-it'

export function createPubSub<Message = void>(): PubSub<Message> {
const subscribers: {[id: string]: Subscriber<Message>} = Object.create(null)
Expand Down

0 comments on commit 08e5f24

Please sign in to comment.