mirror of
https://github.com/sindresorhus/ky.git
synced 2022-05-22 02:13:20 +03:00
Meta tweaks
This commit is contained in:
40
index.d.ts
vendored
40
index.d.ts
vendored
@@ -14,11 +14,11 @@ export type BeforeRequestHook = (
|
||||
) => Request | Response | void | Promise<Request | Response | void>;
|
||||
|
||||
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<void>;
|
||||
|
||||
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<Response> {
|
||||
arrayBuffer(): Promise<ArrayBuffer>;
|
||||
arrayBuffer: () => Promise<ArrayBuffer>;
|
||||
|
||||
blob(): Promise<Blob>;
|
||||
blob: () => Promise<Blob>;
|
||||
|
||||
formData(): Promise<FormData>;
|
||||
formData: () => Promise<FormData>;
|
||||
|
||||
// TODO: Use `json<T extends JSONValue>(): Promise<T>;` 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<Response> {
|
||||
const result = await ky(…).json<Result>();
|
||||
```
|
||||
*/
|
||||
json<T>(): Promise<T>;
|
||||
json: <T>() => Promise<T>;
|
||||
|
||||
text(): Promise<string>;
|
||||
text: () => Promise<string>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -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<typeof ky.TimeoutError>;
|
||||
export type HTTPError = InstanceType<typeof ky.HTTPError>;
|
||||
export type TimeoutError = InstanceType<typeof TimeoutError>;
|
||||
export type HTTPError = InstanceType<typeof HTTPError>;
|
||||
}
|
||||
|
||||
export default ky;
|
||||
|
||||
@@ -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<ky.TimeoutError>(error);
|
||||
} else {
|
||||
throw error;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
2
license
2
license
@@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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:
|
||||
|
||||
|
||||
@@ -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"
|
||||
},
|
||||
|
||||
@@ -127,7 +127,7 @@ Type: `object`
|
||||
##### method
|
||||
|
||||
Type: `string`\
|
||||
Default: `get`
|
||||
Default: `'get'`
|
||||
|
||||
HTTP method used to make the request.
|
||||
|
||||
|
||||
16
test/main.js
16
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();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user