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: update types #4927

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading