소스 검색

@uppy/companion-client: add support for `AbortSignal` (#4201)

Antoine du Hamel 2 년 전
부모
커밋
0b0289fc89
2개의 변경된 파일44개의 추가작업 그리고 8개의 파일을 삭제
  1. 20 4
      packages/@uppy/companion-client/src/RequestClient.js
  2. 24 4
      packages/@uppy/companion-client/types/index.d.ts

+ 20 - 4
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 })
+  }
 }

+ 24 - 4
packages/@uppy/companion-client/types/index.d.ts

@@ -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
+
+  setCompanionHeaders(headers: CompanionHeaders): void
+
+  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>
 
-  post (path: string, data: Record<string, unknown>): Promise<any>
+  delete<T = unknown> (path: string, data?: Record<string, unknown>, options?: RequestOptions): Promise<T>
 
-  delete (path: string, data: Record<string, unknown>): Promise<any>
+  /** @deprecated use option bag instead */
+  delete<T = unknown> (path: string, data: Record<string, unknown>, skipPostResponse: boolean): Promise<T>
 }
 
 /**