Skip to content

Commit

Permalink
Reuse DownloadProgress to Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jadedevin13 committed Sep 7, 2024
1 parent 7ba8edc commit e612cc4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
8 changes: 4 additions & 4 deletions source/core/Ky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class Ky {
throw new Error('Streams are not supported in your environment. `ReadableStream` is missing.');
}

return ky._stream(response.clone(), ky._options.onDownloadProgress);
return ky._streamResponse(response.clone(), ky._options.onDownloadProgress);
}

return response;
Expand Down Expand Up @@ -216,7 +216,7 @@ export class Ky {
this.request
= new globalThis.Request(this._input, {
...this._options,
body: this._wrapBodyWithUploadProgress(
body: this._streamRequest(
originalBody, totalBytes, this._options.onUploadProgress),
});
}
Expand Down Expand Up @@ -328,7 +328,7 @@ export class Ky {
}

/* istanbul ignore next */
protected _stream(response: Response, onDownloadProgress: Options['onDownloadProgress']) {
protected _streamResponse(response: Response, onDownloadProgress: Options['onDownloadProgress']) {
const totalBytes = Number(response.headers.get('content-length')) || 0;
let transferredBytes = 0;

Expand Down Expand Up @@ -430,7 +430,7 @@ export class Ky {
return 0; // Default case, unable to determine size
}

protected _wrapBodyWithUploadProgress(
protected _streamRequest(
body: BodyInit,
totalBytes: number,
onUploadProgress: (progress: {percent: number; transferredBytes: number; totalBytes: number}) => void,
Expand Down
2 changes: 1 addition & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type {
NormalizedOptions,
RetryOptions,
SearchParamsOption,
DownloadProgress,
Progress,
} from './types/options.js';

export type {
Expand Down
16 changes: 3 additions & 13 deletions source/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,7 @@ export type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete';

export type Input = string | URL | Request;

export type UploadProgress = {
percent: number;
transferredBytes: number;

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

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

Expand Down Expand Up @@ -196,7 +186,7 @@ export type KyOptions = {
});
```
*/
onDownloadProgress?: (progress: DownloadProgress, chunk: Uint8Array) => void;
onDownloadProgress?: (progress: Progress, chunk: Uint8Array) => void;

/**
Upload progress event handler.
Expand All @@ -215,7 +205,7 @@ export type KyOptions = {
});
```
*/
onUploadProgress?: (progress: UploadProgress) => void;
onUploadProgress?: (progress: Progress) => void;

/**
User-defined `fetch` function.
Expand Down

0 comments on commit e612cc4

Please sign in to comment.