Skip to content

Commit e3b0810

Browse files
amirai21asafgardin
authored andcommitted
feat: convert upload to non async
1 parent c88bf02 commit e3b0810

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/APIClient.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { AI21EnvConfig } from './EnvConfig';
1515
import { createFetchInstance, createFilesHandlerInstance } from './runtime';
1616
import { Fetch } from 'fetch';
1717
import { BaseFilesHandler } from 'files/BaseFilesHandler';
18+
import { FormDataRequest } from 'types/API';
1819

1920
const validatePositiveInteger = (name: string, n: unknown): number => {
2021
if (typeof n !== 'number' || !Number.isInteger(n)) {
@@ -79,27 +80,27 @@ export abstract class APIClient {
7980
return this.prepareAndExecuteRequest('delete', path, opts);
8081
}
8182

82-
async upload<Req, Rsp>(path: string, file: FilePathOrFileObject, opts?: RequestOptions<Req>): Promise<Rsp> {
83-
const formDataRequest = await this.filesHandler.prepareFormDataRequest(file);
84-
85-
if (opts?.body) {
86-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
87-
appendBodyToFormData(formDataRequest.formData, opts.body as Record<string, any>);
88-
}
89-
90-
const headers = {
91-
...opts?.headers,
92-
...formDataRequest.headers,
93-
};
94-
95-
const options: FinalRequestOptions = {
96-
method: 'post',
97-
path: path,
98-
body: formDataRequest.formData,
99-
headers,
100-
};
101-
102-
return this.performRequest(options).then((response) => this.fetch.handleResponse<Rsp>(response) as Rsp);
83+
upload<Req, Rsp>(path: string, file: FilePathOrFileObject, opts?: RequestOptions<Req>): Promise<Rsp> {
84+
return this.filesHandler.prepareFormDataRequest(file).then((formDataRequest: FormDataRequest) => {
85+
if (opts?.body) {
86+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
87+
appendBodyToFormData(formDataRequest.formData, opts.body as Record<string, any>);
88+
}
89+
90+
const headers = {
91+
...opts?.headers,
92+
...formDataRequest.headers,
93+
};
94+
95+
const options: FinalRequestOptions = {
96+
method: 'post',
97+
path: path,
98+
body: formDataRequest.formData,
99+
headers,
100+
};
101+
102+
return this.performRequest(options).then((response) => this.fetch.handleResponse<Rsp>(response) as Rsp);
103+
});
103104
}
104105

105106
protected getUserAgent(): string {

0 commit comments

Comments
 (0)