Skip to content

Commit

Permalink
feat(AjaxResponse): add stricter type (AjaxResponseType) (#6279)
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverJAsh committed Apr 29, 2021
1 parent f43c728 commit 839e192
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions api_guard/dist/types/ajax/index.d.ts
Expand Up @@ -56,13 +56,13 @@ export declare class AjaxResponse<T> {
readonly responseType: XMLHttpRequestResponseType;
readonly status: number;
readonly total: number;
readonly type: string;
readonly type: AjaxResponseType;
readonly xhr: XMLHttpRequest;
constructor(
originalEvent: ProgressEvent,
xhr: XMLHttpRequest,
request: AjaxRequest,
type?: string);
type?: AjaxResponseType);
}

export interface AjaxTimeoutError extends AjaxError {
Expand Down
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, 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 839e192

Please sign in to comment.