diff --git a/packages/@uppy/companion-client/src/RequestClient.js b/packages/@uppy/companion-client/src/RequestClient.js index 25a14cd93e..22cd88e17e 100644 --- a/packages/@uppy/companion-client/src/RequestClient.js +++ b/packages/@uppy/companion-client/src/RequestClient.js @@ -150,11 +150,12 @@ export default class RequestClient { })) } - async #request ({ path, method = 'GET', data, skipPostResponse }) { + async #request ({ path, method = 'GET', data, skipPostResponse, signal }) { try { const headers = await this.preflightAndHeaders(path) const response = await fetchWithNetworkError(this.#getUrl(path), { method, + signal, headers, credentials: this.opts.companionCookiesRule || 'same-origin', body: data ? JSON.stringify(data) : null, @@ -167,9 +168,24 @@ export default class RequestClient { } } - async get (path, skipPostResponse) { return this.#request({ path, skipPostResponse }) } + async get (path, options = undefined) { + // TODO: remove boolean support for options that was added for backward compatibility. + // eslint-disable-next-line no-param-reassign + if (typeof options === 'boolean') options = { skipPostResponse: options } + return this.#request({ ...options, path }) + } - async post (path, data, skipPostResponse) { return this.#request({ path, method: 'POST', data, skipPostResponse }) } + async post (path, data, options = undefined) { + // TODO: remove boolean support for options that was added for backward compatibility. + // eslint-disable-next-line no-param-reassign + if (typeof options === 'boolean') options = { skipPostResponse: options } + return this.#request({ ...options, path, method: 'POST', data }) + } - async delete (path, data, skipPostResponse) { return this.#request({ path, method: 'DELETE', data, skipPostResponse }) } + async delete (path, data = undefined, options) { + // TODO: remove boolean support for options that was added for backward compatibility. + // eslint-disable-next-line no-param-reassign + if (typeof options === 'boolean') options = { skipPostResponse: options } + return this.#request({ ...options, path, method: 'DELETE', data }) + } } diff --git a/packages/@uppy/companion-client/types/index.d.ts b/packages/@uppy/companion-client/types/index.d.ts index 4bd886e9f6..8cea2392d7 100644 --- a/packages/@uppy/companion-client/types/index.d.ts +++ b/packages/@uppy/companion-client/types/index.d.ts @@ -10,20 +10,40 @@ export interface TokenStorage { removeItem: (key: string) => Promise } +type CompanionHeaders = Record + export interface RequestClientOptions { companionUrl: string - companionHeaders?: Record + companionHeaders?: CompanionHeaders companionCookiesRule?: RequestCredentials } +type RequestOptions = { + skipPostResponse?: boolean, + signal?: AbortSignal, +} + export class RequestClient { constructor (uppy: Uppy, opts: RequestClientOptions) - get (path: string): Promise + readonly hostname: string + + setCompanionHeaders(headers: CompanionHeaders): void + + get (path: string, options?: RequestOptions): Promise + + /** @deprecated use option bag instead */ + get (path: string, skipPostResponse: boolean): Promise + + post (path: string, data: Record, options?: RequestOptions): Promise + + /** @deprecated use option bag instead */ + post (path: string, data: Record, skipPostResponse: boolean): Promise - post (path: string, data: Record): Promise + delete (path: string, data?: Record, options?: RequestOptions): Promise - delete (path: string, data: Record): Promise + /** @deprecated use option bag instead */ + delete (path: string, data: Record, skipPostResponse: boolean): Promise } /**