From 588f45331a4e98872e74d628a04deeeb7bd601e0 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sat, 16 May 2020 18:32:09 +0800 Subject: [PATCH] Meta tweaks --- index.d.ts | 40 ++++++++++++++++++++-------------------- index.test-d.ts | 12 +++++++----- license | 2 +- package.json | 6 +++--- readme.md | 2 +- test/main.js | 16 ++++++++-------- 6 files changed, 40 insertions(+), 38 deletions(-) diff --git a/index.d.ts b/index.d.ts index a21b236..65a3f14 100644 --- a/index.d.ts +++ b/index.d.ts @@ -14,11 +14,11 @@ export type BeforeRequestHook = ( ) => Request | Response | void | Promise; export type BeforeRetryHook = (options: { - request: Request, - response: Response, - options: NormalizedOptions, - error: Error, - retryCount: number, + request: Request; + response: Response; + options: NormalizedOptions; + error: Error; + retryCount: number; }) => void | Promise; export type AfterResponseHook = ( @@ -329,11 +329,11 @@ export interface NormalizedOptions extends RequestInit { 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 the response status is `204` instead of throwing a parse error due to an empty body. */ export interface ResponsePromise extends Promise { - arrayBuffer(): Promise; + arrayBuffer: () => Promise; - blob(): Promise; + blob: () => Promise; - formData(): Promise; + formData: () => Promise; // TODO: Use `json(): Promise;` when it's fixed in TS. // See https://github.com/microsoft/TypeScript/issues/15300 and https://github.com/sindresorhus/ky/pull/80 @@ -358,9 +358,9 @@ export interface ResponsePromise extends Promise { const result = await ky(…).json(); ``` */ - json(): Promise; + json: () => Promise; - text(): Promise; + text: () => Promise; } /** @@ -405,7 +405,7 @@ declare const ky: { @param url - `Request` object, `URL` object, or URL string. @returns A promise with `Body` methods added. */ - get(url: Input, options?: Options): ResponsePromise; + get: (url: Input, options?: Options) => ResponsePromise; /** Fetch the given `url` using the option `{method: 'post'}`. @@ -413,7 +413,7 @@ declare const ky: { @param url - `Request` object, `URL` object, or URL string. @returns A promise with `Body` methods added. */ - post(url: Input, options?: Options): ResponsePromise; + post: (url: Input, options?: Options) => ResponsePromise; /** Fetch the given `url` using the option `{method: 'put'}`. @@ -421,7 +421,7 @@ declare const ky: { @param url - `Request` object, `URL` object, or URL string. @returns A promise with `Body` methods added. */ - put(url: Input, options?: Options): ResponsePromise; + put: (url: Input, options?: Options) => ResponsePromise; /** Fetch the given `url` using the option `{method: 'delete'}`. @@ -429,7 +429,7 @@ declare const ky: { @param url - `Request` object, `URL` object, or URL string. @returns A promise with `Body` methods added. */ - delete(url: Input, options?: Options): ResponsePromise; + delete: (url: Input, options?: Options) => ResponsePromise; /** Fetch the given `url` using the option `{method: 'patch'}`. @@ -437,7 +437,7 @@ declare const ky: { @param url - `Request` object, `URL` object, or URL string. @returns A promise with `Body` methods added. */ - patch(url: Input, options?: Options): ResponsePromise; + patch: (url: Input, options?: Options) => ResponsePromise; /** Fetch the given `url` using the option `{method: 'head'}`. @@ -445,14 +445,14 @@ declare const ky: { @param url - `Request` object, `URL` object, or URL string. @returns A promise with `Body` methods added. */ - head(url: Input, options?: Options): ResponsePromise; + head: (url: Input, options?: Options) => ResponsePromise; /** Create a new Ky instance with complete new defaults. @returns A new Ky instance. */ - create(defaultOptions: Options): typeof ky; + create: (defaultOptions: Options) => typeof ky; /** Create a new Ky instance with some defaults overridden with your own. @@ -461,7 +461,7 @@ declare const ky: { @returns A new Ky instance. */ - extend(defaultOptions: Options): typeof ky; + extend: (defaultOptions: Options) => typeof ky; /** A `Symbol` that can be returned by a `beforeRetry` hook to stop the retry. @@ -493,8 +493,8 @@ declare const ky: { }; declare namespace ky { - export type TimeoutError = InstanceType; - export type HTTPError = InstanceType; + export type TimeoutError = InstanceType; + export type HTTPError = InstanceType; } export default ky; diff --git a/index.test-d.ts b/index.test-d.ts index bafd490..b1671ab 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -12,7 +12,7 @@ const requestMethods = [ 'put', 'delete', 'patch', - 'head', + 'head' ] as const; // Test Ky HTTP methods @@ -80,8 +80,8 @@ ky(new Request(url)); const input: Input = new URL('https://sindresorhus'); const options: Options = { method: 'get', - timeout: 5000, -} + timeout: 5000 +}; ky(input, options); // Extending Ky @@ -89,11 +89,13 @@ interface CustomOptions extends Options { foo?: boolean; } async function customKy(input: Input, options?: CustomOptions) { - if (options && options.foo) { + if (options?.foo) { options.json = {foo: options.foo}; } + return ky(input, options); } + customKy(input, options); // `searchParams` option @@ -151,6 +153,6 @@ try { } else if (error instanceof ky.TimeoutError) { expectType(error); } else { - throw error; + throw error; } } diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/package.json b/package.json index ee36cab..53be314 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, "engines": { "node": ">=10" @@ -58,8 +58,8 @@ "form-data": "^3.0.0", "node-fetch": "^2.5.0", "nyc": "^15.0.0", - "puppeteer": "^2.1.0", - "rollup": "^1.31.0", + "puppeteer": "^3.0.4", + "rollup": "^2.10.2", "tsd": "^0.11.0", "xo": "^0.25.3" }, diff --git a/readme.md b/readme.md index d69313f..58b58e3 100644 --- a/readme.md +++ b/readme.md @@ -127,7 +127,7 @@ Type: `object` ##### method Type: `string`\ -Default: `get` +Default: `'get'` HTTP method used to make the request. diff --git a/test/main.js b/test/main.js index db794d6..5f98b05 100644 --- a/test/main.js +++ b/test/main.js @@ -333,19 +333,19 @@ test('searchParams option', async t => { response.end(request.url.slice(1)); }); - const arrayParams = [['cats', 'meow'], ['dogs', true], ['opossums', false]]; - const objectParams = { + const arrayParameters = [['cats', 'meow'], ['dogs', true], ['opossums', false]]; + const objectParameters = { cats: 'meow', dogs: true, opossums: false }; - const searchParams = new URLSearchParams(arrayParams); - const stringParams = '?cats=meow&dogs=true&opossums=false'; + const searchParameters = new URLSearchParams(arrayParameters); + const stringParameters = '?cats=meow&dogs=true&opossums=false'; - t.is(await ky(server.url, {searchParams: arrayParams}).text(), stringParams); - t.is(await ky(server.url, {searchParams: objectParams}).text(), stringParams); - t.is(await ky(server.url, {searchParams}).text(), stringParams); - t.is(await ky(server.url, {searchParams: stringParams}).text(), stringParams); + t.is(await ky(server.url, {searchParams: arrayParameters}).text(), stringParameters); + t.is(await ky(server.url, {searchParams: objectParameters}).text(), stringParameters); + t.is(await ky(server.url, {searchParams: searchParameters}).text(), stringParameters); + t.is(await ky(server.url, {searchParams: stringParameters}).text(), stringParameters); await server.close(); });