Skip to content

Commit

Permalink
Update dev dependecies
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 14, 2023
1 parent 780e032 commit 356d61c
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 50 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Expand Up @@ -12,7 +12,6 @@ jobs:
node-version:
- 18
- 16
- 14
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
10 changes: 5 additions & 5 deletions package.json
Expand Up @@ -56,23 +56,23 @@
"@types/busboy": "^0.3.1",
"@types/express": "^4.17.14",
"@types/node": "^18.11.7",
"@types/node-fetch": "^2.6.2",
"@types/node-fetch": "^2.6.4",
"abort-controller": "^3.0.0",
"ava": "^4.3.3",
"ava": "^5.3.0",
"body-parser": "^1.20.1",
"busboy": "^0.3.1",
"del-cli": "^4.0.1",
"delay": "^5.0.0",
"expect-type": "^0.14.2",
"expect-type": "^0.16.0",
"express": "^4.18.2",
"form-data": "^4.0.0",
"node-fetch": "^2.6.1",
"pify": "^6.1.0",
"playwright-chromium": "^1.27.1",
"raw-body": "^2.5.1",
"ts-node": "^10.8.1",
"typescript": "^4.8.4",
"xo": "^0.50.0"
"typescript": "^5.1.3",
"xo": "^0.54.2"
},
"sideEffects": false,
"xo": {
Expand Down
14 changes: 6 additions & 8 deletions source/core/Ky.ts
Expand Up @@ -2,12 +2,12 @@ import {HTTPError} from '../errors/HTTPError.js';
import {TimeoutError} from '../errors/TimeoutError.js';
import type {Hooks} from '../types/hooks.js';
import type {Input, InternalOptions, NormalizedOptions, Options, SearchParamsInit} from '../types/options.js';
import {ResponsePromise} from '../types/ResponsePromise.js';
import {type ResponsePromise} from '../types/ResponsePromise.js';
import {deepMerge, mergeHeaders} from '../utils/merge.js';
import {normalizeRequestMethod, normalizeRetryOptions} from '../utils/normalize.js';
import timeout, {TimeoutOptions} from '../utils/timeout.js';
import timeout, {type TimeoutOptions} from '../utils/timeout.js';
import delay from '../utils/delay.js';
import {ObjectEntries} from '../utils/types.js';
import {type ObjectEntries} from '../utils/types.js';
import {
maxSafeTimeout,
responseTypes,
Expand All @@ -19,12 +19,11 @@ import {
} from './constants.js';

export class Ky {
// eslint-disable-next-line @typescript-eslint/promise-function-async
static create(input: Input, options: Options): ResponsePromise {
const ky = new Ky(input, options);

const fn = async (): Promise<Response> => {
if (ky._options.timeout > maxSafeTimeout) {
if (typeof ky._options.timeout === 'number' && ky._options.timeout > maxSafeTimeout) {
throw new RangeError(`The \`timeout\` option cannot be greater than ${maxSafeTimeout}`);
}

Expand Down Expand Up @@ -137,7 +136,7 @@ export class Ky {
prefixUrl: String(options.prefixUrl || ''),
retry: normalizeRetryOptions(options.retry),
throwHttpErrors: options.throwHttpErrors !== false,
timeout: typeof options.timeout === 'undefined' ? 10_000 : options.timeout,
timeout: options.timeout ?? 10_000,
fetch: options.fetch ?? globalThis.fetch.bind(globalThis),
};

Expand Down Expand Up @@ -223,7 +222,7 @@ export class Ky {
after *= 1000;
}

if (typeof this._options.retry.maxRetryAfter !== 'undefined' && after > this._options.retry.maxRetryAfter) {
if (this._options.retry.maxRetryAfter !== undefined && after > this._options.retry.maxRetryAfter) {
return 0;
}

Expand Down Expand Up @@ -253,7 +252,6 @@ export class Ky {
protected async _retry<T extends (...args: any) => Promise<any>>(fn: T): Promise<ReturnType<T> | void> {
try {
return await fn();
// eslint-disable-next-line @typescript-eslint/no-implicit-any-catch
} catch (error) {
const ms = Math.min(this._calculateRetryDelay(error), maxSafeTimeout);
if (ms !== 0 && this._retryCount > 0) {
Expand Down
2 changes: 1 addition & 1 deletion source/core/constants.ts
@@ -1,5 +1,5 @@
import type {Expect, Equal} from '@type-challenges/utils';
import {HttpMethod} from '../types/options.js';
import {type HttpMethod} from '../types/options.js';

export const supportsRequestStreams = (() => {
let duplexAccessed = false;
Expand Down
2 changes: 1 addition & 1 deletion source/index.ts
Expand Up @@ -5,7 +5,7 @@ import {requestMethods, stop} from './core/constants.js';
import type {KyInstance} from './types/ky.js';
import type {Input, Options} from './types/options.js';
import {validateAndMerge} from './utils/merge.js';
import {Mutable} from './utils/types.js';
import {type Mutable} from './utils/types.js';

const createInstance = (defaults?: Partial<Options>): KyInstance => {
// eslint-disable-next-line @typescript-eslint/promise-function-async
Expand Down
6 changes: 3 additions & 3 deletions source/types/ResponsePromise.ts
@@ -1,9 +1,9 @@
/**
Returns a `Response` object with `Body` methods added for convenience. So you can, for example, call `ky.get(input).json()` directly without having to await the `Response` first. When called like that, an appropriate `Accept` header will be set depending on the body method used. Unlike the `Body` methods of `window.Fetch`; these will throw an `HTTPError` if the response status is not in the range of `200...299`. Also, `.json()` will return an empty string if body is empty or the response status is `204` instead of throwing a parse error due to an empty body.
*/
import {KyResponse} from './response.js';
import {type KyResponse} from './response.js';

export interface ResponsePromise extends Promise<KyResponse> {
export type ResponsePromise = {
arrayBuffer: () => Promise<ArrayBuffer>;

blob: () => Promise<Blob>;
Expand Down Expand Up @@ -36,4 +36,4 @@ export interface ResponsePromise extends Promise<KyResponse> {
json: <T = unknown>() => Promise<T>;

text: () => Promise<string>;
}
} & Promise<KyResponse>;
8 changes: 4 additions & 4 deletions source/types/hooks.ts
@@ -1,5 +1,5 @@
import {stop} from '../core/constants.js';
import {HTTPError} from '../index.js';
import {type stop} from '../core/constants.js';
import {type HTTPError} from '../index.js';
import type {NormalizedOptions} from './options.js';

export type BeforeRequestHook = (
Expand All @@ -23,7 +23,7 @@ export type AfterResponseHook = (

export type BeforeErrorHook = (error: HTTPError) => HTTPError | Promise<HTTPError>;

export interface Hooks {
export type Hooks = {
/**
This hook enables you to modify the request right before it is sent. Ky will make no further changes to the request after this. The hook function receives normalized input and options as arguments. You could, forf example, modiy `options.headers` here.
Expand Down Expand Up @@ -126,4 +126,4 @@ export interface Hooks {
```
*/
beforeError?: BeforeErrorHook[];
}
};
6 changes: 3 additions & 3 deletions source/types/ky.ts
@@ -1,8 +1,8 @@
import {stop} from '../core/constants.js';
import {type stop} from '../core/constants.js';
import type {Input, Options} from './options.js';
import type {ResponsePromise} from './ResponsePromise.js';

export interface KyInstance {
export type KyInstance = {
/**
Fetch the given `url`.
Expand Down Expand Up @@ -117,4 +117,4 @@ export interface KyInstance {
```
*/
readonly stop: typeof stop;
}
};
12 changes: 6 additions & 6 deletions source/types/options.ts
Expand Up @@ -12,22 +12,22 @@ export type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete';

export type Input = string | URL | Request;

export interface DownloadProgress {
export type DownloadProgress = {
percent: number;
transferredBytes: number;

/**
Note: If it's not possible to retrieve the body size, it will be `0`.
*/
totalBytes: number;
}
};

export type KyHeadersInit = HeadersInit | Record<string, string | undefined>;

/**
Options are the same as `window.fetch`, with some exceptions.
*/
export interface Options extends Omit<RequestInit, 'headers'> {
export type Options = {
/**
HTTP method used to make the request.
Expand Down Expand Up @@ -221,7 +221,7 @@ export interface Options extends Omit<RequestInit, 'headers'> {
```
*/
fetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
}
} & Omit<RequestInit, 'headers'>;

export type InternalOptions = Required<
Omit<Options, 'hooks' | 'retry'>,
Expand All @@ -236,7 +236,7 @@ Omit<Options, 'hooks' | 'retry'>,
/**
Normalized options passed to the `fetch` call and the `beforeRequest` hooks.
*/
export interface NormalizedOptions extends RequestInit {
export type NormalizedOptions = {
// Extended from `RequestInit`, but ensured to be set (not optional).
method: RequestInit['method'];
credentials: RequestInit['credentials'];
Expand All @@ -245,6 +245,6 @@ export interface NormalizedOptions extends RequestInit {
retry: RetryOptions;
prefixUrl: string;
onDownloadProgress: Options['onDownloadProgress'];
}
} & RequestInit;

export type {RetryOptions} from './retry.js';
4 changes: 2 additions & 2 deletions source/types/response.ts
@@ -1,3 +1,3 @@
export interface KyResponse extends Response {
export type KyResponse = {
json: <T = unknown>() => Promise<T>;
}
} & Response;
4 changes: 2 additions & 2 deletions source/types/retry.ts
@@ -1,4 +1,4 @@
export interface RetryOptions {
export type RetryOptions = {
/**
The number of times to retry failed requests.
Expand Down Expand Up @@ -49,4 +49,4 @@ export interface RetryOptions {
@default Infinity
*/
backoffLimit?: number;
}
};
6 changes: 3 additions & 3 deletions source/utils/delay.ts
@@ -1,11 +1,11 @@
// https://github.com/sindresorhus/delay/tree/ab98ae8dfcb38e1593286c94d934e70d14a4e111

import {composeAbortError} from '../errors/DOMException.js';
import {InternalOptions} from '../types/options.js';
import {type InternalOptions} from '../types/options.js';

export interface DelayOptions {
export type DelayOptions = {
signal?: InternalOptions['signal'];
}
};

export default async function delay(
ms: number,
Expand Down
2 changes: 1 addition & 1 deletion source/utils/merge.ts
Expand Up @@ -3,7 +3,7 @@ import {isObject} from './is.js';

export const validateAndMerge = (...sources: Array<Partial<Options> | undefined>): Partial<Options> => {
for (const source of sources) {
if ((!isObject(source) || Array.isArray(source)) && typeof source !== 'undefined') {
if ((!isObject(source) || Array.isArray(source)) && source !== undefined) {
throw new TypeError('The `options` argument must be an object');
}
}
Expand Down
9 changes: 5 additions & 4 deletions test/browser.ts
@@ -1,13 +1,14 @@
import test, {ExecutionContext} from 'ava';
import test, {type ExecutionContext} from 'ava';
import busboy from 'busboy';
import express from 'express';
import {Page} from 'playwright-chromium';
import ky from '../source/index.js';
import {createHttpTestServer, ExtendedHttpTestServer, HttpServerOptions} from './helpers/create-http-test-server.js';
import {type Page} from 'playwright-chromium';
import type ky from '../source/index.js';
import {createHttpTestServer, type ExtendedHttpTestServer, type HttpServerOptions} from './helpers/create-http-test-server.js';
import {parseRawBody} from './helpers/parse-body.js';
import {withPage} from './helpers/with-page.js';

declare global {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Window {
ky: typeof ky;
}
Expand Down
6 changes: 3 additions & 3 deletions test/helpers/create-http-test-server.ts
@@ -1,5 +1,5 @@
import http from 'node:http';
import net from 'node:net';
import type net from 'node:net';
import express from 'express';
import pify from 'pify';
import bodyParser from 'body-parser';
Expand All @@ -8,13 +8,13 @@ export type HttpServerOptions = {
bodyParser?: express.NextFunction | false;
};

export interface ExtendedHttpTestServer extends express.Express {
export type ExtendedHttpTestServer = {
http: http.Server;
url: string;
port: number;
hostname: string;
close: () => Promise<any>;
}
} & express.Express;

export const createHttpTestServer = async (options: HttpServerOptions = {}): Promise<ExtendedHttpTestServer> => {
const server = express() as ExtendedHttpTestServer;
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/with-page.ts
@@ -1,6 +1,6 @@
import process from 'node:process';
import type {ExecutionContext, Implementation} from 'ava';
import {chromium, Page} from 'playwright-chromium';
import {chromium, type Page} from 'playwright-chromium';

type Run = (t: ExecutionContext, page: Page) => Promise<void>;

Expand Down
2 changes: 1 addition & 1 deletion test/hooks.ts
@@ -1,7 +1,7 @@
import test from 'ava';
import delay from 'delay';
import ky, {HTTPError} from '../source/index.js';
import {Options} from '../source/types/options.js';
import {type Options} from '../source/types/options.js';
import {createHttpTestServer} from './helpers/create-http-test-server.js';

test('hooks can be async', async t => {
Expand Down
2 changes: 1 addition & 1 deletion test/http-error.ts
@@ -1,6 +1,6 @@
import test from 'ava';
import {HTTPError} from '../source/index.js';
import {Mutable} from '../source/utils/types.js';
import {type Mutable} from '../source/utils/types.js';

function createFakeResponse({status, statusText}: {status?: number; statusText?: string}): Response {
// Start with a realistic fetch Response.
Expand Down

0 comments on commit 356d61c

Please sign in to comment.