Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Abort all requests #111

Open
wants to merge 1 commit into
base: multiple-clients
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/lib/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,40 @@ class Client implements IClient {
JSON.stringify(data)
}

getSignal (options?: any): AbortSignal {
return options && options.signal;
}

get (url: string, data: any, options?: any): Promise<any> {
return fetch(`${this.host}/api/v1/${encodeURI(url)}?${this.getParams(data)}`, {
method: 'GET',
headers: this.getHeaders(options)
headers: this.getHeaders(options),
signal: this.getSignal(options)
}).then(this.handle)
}
post (url: string, data: any, options?: any): Promise<any> {
return fetch(`${this.host}/api/v1/${encodeURI(url)}`, {
method: 'POST',
body: this.getBody(data),
headers: this.getHeaders(options)
headers: this.getHeaders(options),
signal: this.getSignal(options)
}).then(this.handle)
}
put (url: string, data: any, options?: any): Promise<any> {
return fetch(`${this.host}/api/v1/${encodeURI(url)}`, {
method: 'PUT',
body: this.getBody(data),
headers: this.getHeaders(options)
headers: this.getHeaders(options),
signal: this.getSignal(options)
}).then(this.handle)
}

delete (url: string, data?: any, options?: any): Promise<any> {
return fetch(`${this.host}/api/v1/${encodeURI(url)}`, {
method: 'DELETE',
body: this.getBody(data),
headers: this.getHeaders(options)
headers: this.getHeaders(options),
signal: this.getSignal(options)
}).then(this.handle)
}
private async handle (r: any) {
Expand Down Expand Up @@ -174,11 +182,13 @@ export default class Api extends EventEmitter {
authToken: string,
result: ILoginResultAPI
} | null = null
controller: AbortController

constructor ({ client, host, logger = Logger }: any) {
super()
this.client = client || new Client({ host } as any)
this.logger = Logger
this.controller = new AbortController();
}

get username () {
Expand Down Expand Up @@ -210,6 +220,10 @@ export default class Api extends EventEmitter {
if (auth && !this.loggedIn()) {
throw new Error('')
}

const { signal } = this.controller;
options = { ...options, signal };

let result
switch (method) {
case 'GET': result = await this.client.get(endpoint, data, options); break
Expand Down Expand Up @@ -240,6 +254,8 @@ export default class Api extends EventEmitter {
/** Do a DELETE request to an API endpoint. */
del: IAPIRequest = (endpoint, data, auth, ignore, options = {}) => this.request('DELETE', endpoint, data, auth, ignore, options)

abort = (): void => this.controller.abort()

/** Check result data for success, allowing override to ignore some errors */
success (result: any, ignore?: RegExp) {
return (
Expand Down