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

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

Merged
merged 4 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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 packages/@uppy/companion-client/src/RequestClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 })
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
}

async post (path, data, skipPostResponse) { return this.#request({ path, method: 'POST', data, skipPostResponse }) }
async post (path, data, options = undefined) {
mifi marked this conversation as resolved.
Show resolved Hide resolved
// 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, options = undefined) {
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
// 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 changes: 24 additions & 4 deletions packages/@uppy/companion-client/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
aduh95 marked this conversation as resolved.
Show resolved Hide resolved

post<T = unknown> (path: string, data: Record<string, unknown>, options?: RequestOptions): Promise<T>
aduh95 marked this conversation as resolved.
Show resolved Hide resolved

/** @deprecated use option bag instead */
post<T = unknown> (path: string, data: Record<string, unknown>, skipPostResponse?: boolean): Promise<T>
aduh95 marked this conversation as resolved.
Show resolved Hide resolved

post (path: string, data: Record<string, unknown>): Promise<any>
delete<T = unknown> (path: string, data: Record<string, unknown>, options?: RequestOptions): Promise<T>
aduh95 marked this conversation as resolved.
Show resolved Hide resolved

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>
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down