mirror of
https://github.com/transloadit/uppy.git
synced 2026-07-25 11:14:05 +00:00
@uppy/companion-client: add support for AbortSignal (#4201)
This commit is contained in:
parent
875bc0bbea
commit
0b0289fc89
2 changed files with 44 additions and 8 deletions
|
|
@ -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 })
|
||||
}
|
||||
}
|
||||
|
|
|
|||
28
packages/@uppy/companion-client/types/index.d.ts
vendored
28
packages/@uppy/companion-client/types/index.d.ts
vendored
|
|
@ -10,20 +10,40 @@ export interface TokenStorage {
|
|||
removeItem: (key: string) => Promise<void>
|
||||
}
|
||||
|
||||
type CompanionHeaders = Record<string, string>
|
||||
|
||||
export interface RequestClientOptions {
|
||||
companionUrl: string
|
||||
companionHeaders?: Record<string, unknown>
|
||||
companionHeaders?: CompanionHeaders
|
||||
companionCookiesRule?: RequestCredentials
|
||||
}
|
||||
|
||||
type RequestOptions = {
|
||||
skipPostResponse?: boolean,
|
||||
signal?: AbortSignal,
|
||||
}
|
||||
|
||||
export class RequestClient {
|
||||
constructor (uppy: Uppy, opts: RequestClientOptions)
|
||||
|
||||
get (path: string): Promise<any>
|
||||
readonly hostname: string
|
||||
|
||||
post (path: string, data: Record<string, unknown>): Promise<any>
|
||||
setCompanionHeaders(headers: CompanionHeaders): void
|
||||
|
||||
delete (path: string, data: Record<string, unknown>): Promise<any>
|
||||
get<T = unknown> (path: string, options?: RequestOptions): Promise<T>
|
||||
|
||||
/** @deprecated use option bag instead */
|
||||
get<T = unknown> (path: string, skipPostResponse: boolean): Promise<T>
|
||||
|
||||
post<T = unknown> (path: string, data: Record<string, unknown>, options?: RequestOptions): Promise<T>
|
||||
|
||||
/** @deprecated use option bag instead */
|
||||
post<T = unknown> (path: string, data: Record<string, unknown>, skipPostResponse: boolean): Promise<T>
|
||||
|
||||
delete<T = unknown> (path: string, data?: Record<string, unknown>, options?: RequestOptions): Promise<T>
|
||||
|
||||
/** @deprecated use option bag instead */
|
||||
delete<T = unknown> (path: string, data: Record<string, unknown>, skipPostResponse: boolean): Promise<T>
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue