Fix some TypeScript TODOs (#355)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Luka Jeran · committed by GitHub · parent 757cd0a · Browse files · 4 files·+30 added−29 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+30 added−29 removed
- tests:+2 added−1 removed
- other:+28 added−28 removed
Provenance
- Commit
dfba243- Tree
5888281- Author
Luka Jeran <lukaw3d@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
757cd0a
4 files changed+30 added−29 removed
modified:source/core/Ky.ts+6 added-6 removed
144 unmodified lines145 return response;146 };147 148 const isRetriableMethod = ky._options.retry.methods.includes(ky.request.method.toLowerCase());149- const result = isRetriableMethod ? ky._retry(fn) : fn();149+ const result = (isRetriableMethod ? ky._retry(fn) : fn()) as ResponsePromise;150 151- for (const [type, mimeType] of Object.entries(responseTypes)) {152- // @ts-expect-error not sure how to properly type this!151+ for (const entry of Object.entries(responseTypes)) {152+ const [type, mimeType] = entry as [keyof typeof responseTypes, string];153+154 result[type] = async () => {155 // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing156 ky.request.headers.set('accept', ky.request.headers.get('accept') || mimeType);157 157- const response = ((await result) as Response).clone();158+ const response = (await result).clone();159 160 if (type === 'json') {161 if (response.status === 204) {162 return '';3 unmodified lines166 return options.parseJson(await response.text());167 }168 }169 169- // @ts-expect-error not sure how to properly type this!170 return response[type]();171 };172 }173 174- return result as ResponsePromise;174+ return result;175 }176 177 protected _calculateRetryDelay(error: Error) {178 this._retryCount++;136 unmodified linesmodified:source/types/ky.ts+18 added-18 removed
2 unmodified lines34567891011121314151617181920212223242526272887 unmodified lines1161171181191021031041051061071081091101111121131141151161171181191202 unmodified linesimport type {ResponsePromise} from './response.js';
export interface ky { /** Fetch the given `url`.
@param url - `Request` object, `URL` object, or URL string. @returns A promise with `Body` method added.
@example ``` import ky from 'ky';
const json = await ky('https://example.com', {json: {foo: true}}).json();
console.log(json); //=> `{data: '🦄'}` ``` */ (url: Input, options?: Options): ResponsePromise;
/** Fetch the given `url` using the option `{method: 'get'}`.
@param url - `Request` object, `URL` object, or URL string. @returns A promise with `Body` methods added.87 unmodified lines const text = await ky('https://example.com', options).text(); ``` */ readonly stop: typeof stop;
/** Fetch the given `url`.
@param url - `Request` object, `URL` object, or URL string. @returns A promise with `Body` method added.
@example ``` import ky from 'ky';
const json = await ky('https://example.com', {json: {foo: true}}).json();
console.log(json); //=> `{data: '🦄'}` ``` */ (url: Input, options?: Options): ResponsePromise;}modified:source/index.ts+4 added-4 removed
6 unmodified lines7891011111213141416151617181920212223232425262720 unmodified lines6 unmodified linesimport {validateAndMerge} from './utils/merge.js';
const createInstance = (defaults?: Partial<Options>): KyInterface => { // eslint-disable-next-line @typescript-eslint/promise-function-async const ky = (input: Input, options?: Options) => Ky.create(input, validateAndMerge(defaults, options)); const ky = ((input: Input, options?: Options) => Ky.create(input, validateAndMerge(defaults, options))) as KyInterface;
for (const method of requestMethods) { // @ts-expect-error not sure how to properly type this! // eslint-disable-next-line @typescript-eslint/promise-function-async ky[method] = (input: Input, options: Options) => Ky.create(input, validateAndMerge(defaults, options, {method})); ky[method] = (input: Input, options?: Options) => Ky.create(input, validateAndMerge(defaults, options, {method})); }
ky.create = (newDefaults?: Partial<Options>) => createInstance(validateAndMerge(newDefaults)); ky.extend = (newDefaults?: Partial<Options>) => createInstance(validateAndMerge(defaults, newDefaults)); // @ts-expect-error Readonly property ky.stop = stop;
return ky as KyInterface; return ky;};
const ky = createInstance();
20 unmodified linesmodified:test/hooks.ts+2 added-1 removed
268 unmodified lines269 .post(server.url, {270 json,271 hooks: {272 afterResponse: [273- // @ts-expect-error273 async (request, options, response) => {274 if (response.status === 403) {275 // Retry request with valid token276 return ky(request, {4 unmodified lines281 token: 'valid:token'282 }283 });284 }285+286+ return undefined;287 }288 ]289 }290 })315 unmodified lines