Skip to content

Commit

Permalink
refactor(ajax): Use simple Observable
Browse files Browse the repository at this point in the history
- Refactors `ajax` to use a simple `Observable` implementation.
- Moves code to a new ajax-specific folder.
- Adds a lot more documentation to classes and support classes.
- Differentiates between the configuration passed to `ajax` (`AjaxConfig`) and the request values used to make the HTTP request (`AjaxRequest`), as the latter has more required values.
- Ensures that no configuration values are mutated while making the request.
- Ensures there is at least one valid test for `ajax.patch`. The old one was sketchy.
- Adds better comments throughout the code.
- Adds better typing to `ajax` functions.

BREAKING CHANGE: For TypeScript users, `AjaxRequest` is no longer the type that should be explicitly used to create an `ajax`. It is now `AjaxConfig`, although the two types are compatible, only `AjaxConfig` has `progressSubscriber` and `createXHR`.
  • Loading branch information
benlesh committed Sep 3, 2020
1 parent 8d63155 commit 17b9add
Show file tree
Hide file tree
Showing 9 changed files with 716 additions and 521 deletions.
50 changes: 31 additions & 19 deletions api_guard/dist/types/ajax/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
export declare const ajax: AjaxCreationMethod;

export interface AjaxError extends Error {
request: AjaxRequest;
response: any;
responseType: XMLHttpRequestResponseType;
status: number;
xhr: XMLHttpRequest;
}

export declare const AjaxError: AjaxErrorCtor;

export interface AjaxRequest {
export interface AjaxConfig {
async?: boolean;
body?: any;
createXHR?: () => XMLHttpRequest;
crossDomain?: boolean;
hasContent?: boolean;
headers?: object;
headers?: Readonly<Record<string, any>>;
method?: string;
password?: string;
progressSubscriber?: PartialObserver<ProgressEvent>;
responseType?: string;
responseType?: XMLHttpRequestResponseType;
timeout?: number;
url?: string;
url: string;
user?: string;
withCredentials?: boolean;
}

export declare class AjaxResponse {
originalEvent: Event;
export interface AjaxError extends Error {
request: AjaxRequest;
response: any;
responseText: string;
responseType: string;
responseType: XMLHttpRequestResponseType;
status: number;
xhr: XMLHttpRequest;
}

export declare const AjaxError: AjaxErrorCtor;

export interface AjaxRequest {
async: boolean;
body?: any;
crossDomain: boolean;
headers: Readonly<Record<string, any>>;
method: string;
password?: string;
responseType: XMLHttpRequestResponseType;
timeout: number;
url: string;
user?: string;
withCredentials: boolean;
}

export declare class AjaxResponse<T> {
readonly originalEvent: Event;
readonly request: AjaxRequest;
readonly response: T;
readonly responseType: XMLHttpRequestResponseType;
readonly status: number;
readonly xhr: XMLHttpRequest;
constructor(originalEvent: Event, xhr: XMLHttpRequest, request: AjaxRequest);
}

Expand Down
Loading

0 comments on commit 17b9add

Please sign in to comment.