Skip to content

Commit

Permalink
@uppy/companion-client: update types (#4927)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 authored Feb 20, 2024
1 parent eebc9a0 commit 448ccc8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/@uppy/companion-client/src/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export default class Provider<
return `${this.hostname}/${this.id}/get/${id}`
}

protected async request<ResBody extends Record<string, unknown>>(
protected async request<ResBody>(
...args: Parameters<RequestClient<M, B>['request']>
): Promise<ResBody> {
await this.#refreshingTokenPromise
Expand Down
43 changes: 21 additions & 22 deletions packages/@uppy/companion-client/src/RequestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ export type Opts = {
companionKeysParams?: Record<string, string>
}

export type RequestOptions =
export type RequestOptions = {
method?: string
data?: Record<string, unknown>
skipPostResponse?: boolean
signal?: AbortSignal
qs?: Record<string, string>
}
type _RequestOptions =
| boolean // TODO: remove this on the next major
| {
method?: string
data?: Record<string, unknown>
skipPostResponse?: boolean
signal?: AbortSignal
qs?: Record<string, string>
}
| RequestOptions

// Remove the trailing slash so we can always safely append /xyz.
function stripSlash(url: string) {
Expand Down Expand Up @@ -64,9 +65,7 @@ class HttpError extends Error {
}
}

async function handleJSONResponse<ResJson extends Record<string, unknown>>(
res: Response,
): Promise<ResJson> {
async function handleJSONResponse<ResJson>(res: Response): Promise<ResJson> {
if (res.status === authErrorStatusCode) {
throw new AuthError()
}
Expand Down Expand Up @@ -163,7 +162,7 @@ export default class RequestClient<M extends Meta, B extends Body> {
return `${this.hostname}/${url}`
}

protected async request<ResBody extends Record<string, unknown>>({
protected async request<ResBody>({
path,
method = 'GET',
data,
Expand Down Expand Up @@ -203,32 +202,32 @@ export default class RequestClient<M extends Meta, B extends Body> {
}
}

async get<PostBody extends Record<string, unknown>>(
async get<PostBody>(
path: string,
options?: RequestOptions,
options?: _RequestOptions,
): Promise<PostBody> {
// 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<PostBody extends Record<string, unknown>>(
async post<PostBody>(
path: string,
data: Record<string, unknown>,
options?: RequestOptions,
options?: _RequestOptions,
): Promise<PostBody> {
// 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<PostBody>({ ...options, path, method: 'POST', data })
}

async delete(
async delete<T>(
path: string,
data: Record<string, unknown>,
options?: RequestOptions,
): Promise<unknown> {
data?: Record<string, unknown>,
options?: _RequestOptions,
): Promise<T> {
// 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 }
Expand Down Expand Up @@ -350,7 +349,7 @@ export default class RequestClient<M extends Meta, B extends Body> {
throw new Error('Cannot connect to an undefined URL')
}

const res = await this.post(
const res = await this.post<{ token: string }>(
file.remote.url,
{
...file.remote.body,
Expand All @@ -359,7 +358,7 @@ export default class RequestClient<M extends Meta, B extends Body> {
{ signal },
)

return res.token as string
return res.token
}

/**
Expand Down

0 comments on commit 448ccc8

Please sign in to comment.