Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed The Need For .js Extension For Imports #641

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,17 @@
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/naming-convention": "off",
"n/no-unsupported-features/node-builtins": "off"
"n/no-unsupported-features/node-builtins": "off",
"import/extensions": [
"error",
"ignorePackages",
{
"js": "never",
"jsx": "never",
"ts": "never",
"tsx": "never"
}
]
}
},
"ava": {
Expand Down
22 changes: 11 additions & 11 deletions source/core/Ky.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {HTTPError} from '../errors/HTTPError.js';
import {TimeoutError} from '../errors/TimeoutError.js';
import {HTTPError} from '../errors/HTTPError';
import {TimeoutError} from '../errors/TimeoutError';
import type {
Input,
InternalOptions,
NormalizedOptions,
Options,
SearchParamsInit,
} from '../types/options.js';
import {type ResponsePromise} from '../types/ResponsePromise.js';
import {mergeHeaders, mergeHooks} from '../utils/merge.js';
import {normalizeRequestMethod, normalizeRetryOptions} from '../utils/normalize.js';
import timeout, {type TimeoutOptions} from '../utils/timeout.js';
import delay from '../utils/delay.js';
import {type ObjectEntries} from '../utils/types.js';
import {findUnknownOptions} from '../utils/options.js';
} from '../types/options';
import {type ResponsePromise} from '../types/ResponsePromise';
import {mergeHeaders, mergeHooks} from '../utils/merge';
import {normalizeRequestMethod, normalizeRetryOptions} from '../utils/normalize';
import timeout, {type TimeoutOptions} from '../utils/timeout';
import delay from '../utils/delay';
import {type ObjectEntries} from '../utils/types';
import {findUnknownOptions} from '../utils/options';
import {
maxSafeTimeout,
responseTypes,
Expand All @@ -22,7 +22,7 @@
supportsFormData,
supportsResponseStreams,
supportsRequestStreams,
} from './constants.js';
} from './constants';

export class Ky {
static create(input: Input, options: Options): ResponsePromise {
Expand Down Expand Up @@ -60,7 +60,7 @@
error = await hook(error);
}

throw error;

Check failure on line 63 in source/core/Ky.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected an error object to be thrown.
}

// If `onDownloadProgress` is passed, it uses the stream API internally
Expand Down Expand Up @@ -137,7 +137,7 @@
options.hooks,
),
method: normalizeRequestMethod(options.method ?? (this._input as Request).method),
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing

Check failure on line 140 in source/core/Ky.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

'@typescript-eslint/prefer-nullish-coalescing' rule is disabled but never reported.
prefixUrl: String(options.prefixUrl || ''),
retry: normalizeRetryOptions(options.retry),
throwHttpErrors: options.throwHttpErrors !== false,
Expand All @@ -155,10 +155,10 @@
}

if (!this._options.prefixUrl.endsWith('/')) {
this._options.prefixUrl += '/';

Check failure on line 158 in source/core/Ky.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Invalid operand for a '+' operation. Operands must each be a number or string, allowing a string + any of: `boolean`, `null`, `RegExp`, `undefined`. Got `any`.
}

this._input = this._options.prefixUrl + this._input;

Check failure on line 161 in source/core/Ky.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Invalid operand for a '+' operation. Operands must each be a number or string, allowing a string + any of: `boolean`, `null`, `RegExp`, `undefined`. Got `any`.
}

if (supportsAbortController) {
Expand Down Expand Up @@ -189,7 +189,7 @@
? this._options.searchParams.replace(/^\?/, '')
: new URLSearchParams(this._options.searchParams as unknown as SearchParamsInit).toString();
// eslint-disable-next-line unicorn/prevent-abbreviations
const searchParams = '?' + textSearchParams;

Check failure on line 192 in source/core/Ky.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Invalid operand for a '+' operation. Operands must each be a number or string, allowing a string + any of: `boolean`, `null`, `RegExp`, `undefined`. Got `any`.
const url = this.request.url.replace(/(?:\?.*?)?(?=#|$)/, searchParams);

// To provide correct form boundary, Content-Type header should be deleted each time when new Request instantiated from another one
Expand Down
4 changes: 2 additions & 2 deletions source/core/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Expect, Equal} from '@type-challenges/utils';
import {type HttpMethod, type KyOptionsRegistry} from '../types/options.js';
import {type RequestInitRegistry} from '../types/request.js';
import {type HttpMethod, type KyOptionsRegistry} from '../types/options';
import {type RequestInitRegistry} from '../types/request';

export const supportsRequestStreams = (() => {
let duplexAccessed = false;
Expand Down
6 changes: 3 additions & 3 deletions source/errors/HTTPError.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {NormalizedOptions} from '../types/options.js';
import type {KyRequest} from '../types/request.js';
import type {KyResponse} from '../types/response.js';
import type {NormalizedOptions} from '../types/options';
import type {KyRequest} from '../types/request';
import type {KyResponse} from '../types/response';

export class HTTPError<T = unknown> extends Error {
public response: KyResponse<T>;
Expand Down
2 changes: 1 addition & 1 deletion source/errors/TimeoutError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {KyRequest} from '../types/request.js';
import type {KyRequest} from '../types/request';

export class TimeoutError extends Error {
public request: KyRequest;
Expand Down
28 changes: 14 additions & 14 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*! MIT License © Sindre Sorhus */

import {Ky} from './core/Ky.js';
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 {type Mutable} from './utils/types.js';
import {Ky} from './core/Ky';
import {requestMethods, stop} from './core/constants';
import type {KyInstance} from './types/ky';
import type {Input, Options} from './types/options';
import {validateAndMerge} from './utils/merge';
import {type Mutable} from './utils/types';

const createInstance = (defaults?: Partial<Options>): KyInstance => {
// eslint-disable-next-line @typescript-eslint/promise-function-async

Check failure on line 11 in source/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

'@typescript-eslint/promise-function-async' rule is disabled but never reported.
const ky: Partial<Mutable<KyInstance>> = (input: Input, options?: Options) => Ky.create(input, validateAndMerge(defaults, options));

for (const method of requestMethods) {
// eslint-disable-next-line @typescript-eslint/promise-function-async

Check failure on line 15 in source/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

'@typescript-eslint/promise-function-async' rule is disabled but never reported.
ky[method] = (input: Input, options?: Options) => Ky.create(input, validateAndMerge(defaults, options, {method}));
}

Expand All @@ -34,7 +34,7 @@

export default ky;

export type {KyInstance} from './types/ky.js';
export type {KyInstance} from './types/ky';

export type {
Input,
Expand All @@ -43,7 +43,7 @@
RetryOptions,
SearchParamsOption,
DownloadProgress,
} from './types/options.js';
} from './types/options';

export type {
Hooks,
Expand All @@ -52,10 +52,10 @@
BeforeRetryState,
BeforeErrorHook,
AfterResponseHook,
} from './types/hooks.js';
} from './types/hooks';

export type {ResponsePromise} from './types/ResponsePromise.js';
export type {KyRequest} from './types/request.js';
export type {KyResponse} from './types/response.js';
export {HTTPError} from './errors/HTTPError.js';
export {TimeoutError} from './errors/TimeoutError.js';
export type {ResponsePromise} from './types/ResponsePromise';
export type {KyRequest} from './types/request';
export type {KyResponse} from './types/response';
export {HTTPError} from './errors/HTTPError';
export {TimeoutError} from './errors/TimeoutError';
2 changes: 1 addition & 1 deletion source/types/ResponsePromise.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
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 {type KyResponse} from './response.js';
import {type KyResponse} from './response';

export type ResponsePromise<T = unknown> = {
arrayBuffer: () => Promise<ArrayBuffer>;
Expand Down
6 changes: 3 additions & 3 deletions source/types/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {type stop} from '../core/constants.js';
import type {KyRequest, KyResponse, HTTPError} from '../index.js';
import type {NormalizedOptions} from './options.js';
import {type stop} from '../core/constants';
import type {KyRequest, KyResponse, HTTPError} from '../index';
import type {NormalizedOptions} from './options';

export type BeforeRequestHook = (
request: KyRequest,
Expand Down
6 changes: 3 additions & 3 deletions source/types/ky.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {type stop} from '../core/constants.js';
import type {Input, Options} from './options.js';
import type {ResponsePromise} from './ResponsePromise.js';
import {type stop} from '../core/constants';
import type {Input, Options} from './options';
import type {ResponsePromise} from './ResponsePromise';

export type KyInstance = {
/**
Expand Down
8 changes: 4 additions & 4 deletions source/types/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {LiteralUnion, Required} from './common.js';
import type {Hooks} from './hooks.js';
import type {RetryOptions} from './retry.js';
import type {LiteralUnion, Required} from './common';
import type {Hooks} from './hooks';
import type {RetryOptions} from './retry';

// eslint-disable-next-line unicorn/prevent-abbreviations
export type SearchParamsInit = string | string[][] | Record<string, string> | URLSearchParams | undefined;
Expand Down Expand Up @@ -289,4 +289,4 @@ export interface NormalizedOptions extends RequestInit { // eslint-disable-line
onDownloadProgress: Options['onDownloadProgress'];
}

export type {RetryOptions} from './retry.js';
export type {RetryOptions} from './retry';
2 changes: 1 addition & 1 deletion source/utils/delay.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// https://github.com/sindresorhus/delay/tree/ab98ae8dfcb38e1593286c94d934e70d14a4e111

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

export type DelayOptions = {
signal?: InternalOptions['signal'];
Expand Down
6 changes: 3 additions & 3 deletions source/utils/merge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {KyHeadersInit, Options} from '../types/options.js';
import type {Hooks} from '../types/hooks.js';
import {isObject} from './is.js';
import type {KyHeadersInit, Options} from '../types/options';
import type {Hooks} from '../types/hooks';
import {isObject} from './is';

export const validateAndMerge = (...sources: Array<Partial<Options> | undefined>): Partial<Options> => {
for (const source of sources) {
Expand Down
6 changes: 3 additions & 3 deletions source/utils/normalize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {requestMethods} from '../core/constants.js';
import type {HttpMethod} from '../types/options.js';
import type {RetryOptions} from '../types/retry.js';
import {requestMethods} from '../core/constants';
import type {HttpMethod} from '../types/options';
import type {RetryOptions} from '../types/retry';

export const normalizeRequestMethod = (input: string): string =>
requestMethods.includes(input as HttpMethod) ? input.toUpperCase() : input;
Expand Down
2 changes: 1 addition & 1 deletion source/utils/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {kyOptionKeys, requestOptionsRegistry} from '../core/constants.js';
import {kyOptionKeys, requestOptionsRegistry} from '../core/constants';

export const findUnknownOptions = (
request: Request,
Expand Down
2 changes: 1 addition & 1 deletion source/utils/timeout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TimeoutError} from '../errors/TimeoutError.js';
import {TimeoutError} from '../errors/TimeoutError';

export type TimeoutOptions = {
timeout: number;
Expand All @@ -18,7 +18,7 @@
abortController.abort();
}

reject(new TimeoutError(request));

Check failure on line 21 in source/utils/timeout.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected the Promise rejection reason to be an Error.
}, options.timeout);

void options
Expand Down
12 changes: 6 additions & 6 deletions test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import busboy from 'busboy';
import express from 'express';
import {chromium, webkit, type Page} from 'playwright';
import type ky from '../source/index.js'; // eslint-disable-line import/no-duplicates
import type {DownloadProgress} from '../source/index.js'; // eslint-disable-line import/no-duplicates
import {createHttpTestServer, type ExtendedHttpTestServer, type HttpServerOptions} from './helpers/create-http-test-server.js';
import {parseRawBody} from './helpers/parse-body.js';
import {browserTest, defaultBrowsersTest} from './helpers/with-page.js';
import type ky from '../source/index'; // eslint-disable-line import/no-duplicates
import type {DownloadProgress} from '../source/index'; // eslint-disable-line import/no-duplicates
import {createHttpTestServer, type ExtendedHttpTestServer, type HttpServerOptions} from './helpers/create-http-test-server';
import {parseRawBody} from './helpers/parse-body';
import {browserTest, defaultBrowsersTest} from './helpers/with-page';

declare global {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
Expand All @@ -29,7 +29,7 @@
const KY_SCRIPT = {
type: 'module',
content: `
import ky from '/distribution/index.js';
import ky from '/distribution/index';
globalThis.ky = ky;
`,
};
Expand Down Expand Up @@ -332,7 +332,7 @@
const contentType = request.headers['content-type'];
const boundary = contentType!.split('boundary=')[1];

t.truthy(requestBody.includes(boundary!));

Check failure on line 335 in test/browser.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

This assertion is unnecessary since the receiver accepts the original type of the expression.
t.regex(requestBody, /bubblegum pie/);
t.deepEqual(request.query, {foo: '1'});
response.end();
Expand Down
2 changes: 1 addition & 1 deletion test/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import ky from '../source/index.js';
import ky from '../source/index';

const fixture = 'https://example.com/unicorn';

Expand Down
4 changes: 2 additions & 2 deletions test/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import type {IncomingHttpHeaders} from 'node:http';
import test from 'ava';
import type {RequestHandler} from 'express';
import ky from '../source/index.js';
import {createHttpTestServer} from './helpers/create-http-test-server.js';
import ky from '../source/index';
import {createHttpTestServer} from './helpers/create-http-test-server';

const timeout = 60_000;

Expand Down Expand Up @@ -285,7 +285,7 @@
.json<IncomingHttpHeaders>();

t.is(headers['user-agent'], 'undefined');
t.is(headers['unicorn'], 'unicorn');

Check failure on line 288 in test/headers.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

["unicorn"] is better written in dot notation.
});

test('non-existent headers set to undefined are omitted', async t => {
Expand Down
6 changes: 3 additions & 3 deletions test/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './create-http-test-server.js';
export * from './parse-body.js';
export * from './with-page.js';
export * from './create-http-test-server';
export * from './parse-body';
export * from './with-page';
6 changes: 3 additions & 3 deletions test/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import test from 'ava';
import delay from 'delay';
import ky, {HTTPError} from '../source/index.js';
import {type Options} from '../source/types/options.js';
import {createHttpTestServer} from './helpers/create-http-test-server.js';
import ky, {HTTPError} from '../source/index';
import {type Options} from '../source/types/options';
import {createHttpTestServer} from './helpers/create-http-test-server';

test('hooks can be async', async t => {
const server = await createHttpTestServer();
Expand Down
4 changes: 2 additions & 2 deletions test/http-error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava';
import {expectTypeOf} from 'expect-type';
import {HTTPError} from '../source/index.js';
import {type Mutable} from '../source/utils/types.js';
import {HTTPError} from '../source/index';
import {type Mutable} from '../source/utils/types';

function createFakeResponse({status, statusText}: {status?: number; statusText?: string}): Response {
// Start with a realistic fetch Response.
Expand Down
6 changes: 3 additions & 3 deletions test/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {Buffer} from 'node:buffer';
import test from 'ava';
import delay from 'delay';
import {expectTypeOf} from 'expect-type';
import ky, {TimeoutError} from '../source/index.js';
import {createHttpTestServer} from './helpers/create-http-test-server.js';
import {parseRawBody} from './helpers/parse-body.js';
import ky, {TimeoutError} from '../source/index';
import {createHttpTestServer} from './helpers/create-http-test-server';
import {parseRawBody} from './helpers/parse-body';

const fixture = 'fixture';

Expand Down
4 changes: 2 additions & 2 deletions test/methods.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import ky from '../source/index.js';
import {createHttpTestServer} from './helpers/create-http-test-server.js';
import ky from '../source/index';
import {createHttpTestServer} from './helpers/create-http-test-server';

test('common method is normalized', async t => {
const server = await createHttpTestServer();
Expand Down
4 changes: 2 additions & 2 deletions test/prefix-url.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import ky from '../source/index.js';
import {createHttpTestServer} from './helpers/create-http-test-server.js';
import ky from '../source/index';
import {createHttpTestServer} from './helpers/create-http-test-server';

test('prefixUrl option', async t => {
const server = await createHttpTestServer();
Expand Down
6 changes: 3 additions & 3 deletions test/retry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava';
import ky from '../source/index.js';
import {createHttpTestServer} from './helpers/create-http-test-server.js';
import {withPerformance} from './helpers/with-performance.js';
import ky from '../source/index';
import {createHttpTestServer} from './helpers/create-http-test-server';
import {withPerformance} from './helpers/with-performance';

const fixture = 'fixture';
const defaultRetryCount = 2;
Expand Down
8 changes: 4 additions & 4 deletions tsconfig.dist.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"sourceMap": true,
"inlineSources": true,
"rootDir": "./source",
"outDir": "./distribution"
"outDir": "./distribution",
"module": "ESNext",
"moduleResolution": "Bundler"
},
"include": [
"source"
]
"include": ["source"]
}