Skip to content

Commit

Permalink
feat(AjaxResponse): add stricter type (AjaxResponseType)
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverJAsh committed Apr 28, 2021
1 parent e00b347 commit e3b0d39
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/internal/ajax/AjaxResponse.ts
@@ -1,4 +1,4 @@
import { AjaxRequest } from './types';
import { AjaxRequest, AjaxResponseType } from './types';
import { getXHRResponse } from './getXHRResponse';

/**
Expand Down Expand Up @@ -90,7 +90,7 @@ export class AjaxResponse<T> {
* `download_load` is the type of event when download has finished and the
* response is available.
*/
public readonly type: string = 'download_load'
public readonly type: AjaxResponseType = 'download_load'
) {
const { status, responseType } = xhr;
this.status = status ?? 0;
Expand Down
4 changes: 2 additions & 2 deletions src/internal/ajax/ajax.ts
@@ -1,6 +1,6 @@
import { map } from '../operators/map';
import { Observable } from '../Observable';
import { AjaxConfig, AjaxRequest, AjaxDirection } from './types';
import { AjaxConfig, AjaxRequest, AjaxDirection, AjaxResponseType, ProgressEventType } from './types';
import { AjaxResponse } from './AjaxResponse';
import { AjaxTimeoutError, AjaxError } from './errors';

Expand Down Expand Up @@ -414,7 +414,7 @@ export function fromAjax<T>(config: AjaxConfig): Observable<AjaxResponse<T>> {
* @param event the actual event object.
*/
const createResponse = (direction: AjaxDirection, event: ProgressEvent) =>
new AjaxResponse<T>(event, xhr, _request, `${direction}_${event.type}`);
new AjaxResponse<T>(event, xhr, _request, `${direction}_${event.type as ProgressEventType}` as const);

/**
* Wires up an event handler that emits a Response object to the consumer, used for
Expand Down
4 changes: 4 additions & 0 deletions src/internal/ajax/types.ts
Expand Up @@ -7,6 +7,10 @@ import { PartialObserver } from '../types';
*/
export type AjaxDirection = 'upload' | 'download';

export type ProgressEventType = 'loadstart' | 'progress' | 'load';

export type AjaxResponseType = `${AjaxDirection}_${ProgressEventType}`;

/**
* The object containing values RxJS used to make the HTTP request.
*
Expand Down

0 comments on commit e3b0d39

Please sign in to comment.