Skip to content

Commit f046374

Browse files
Improve types of upload/download functions
1 parent 8d205c0 commit f046374

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

packages/sdk.geometry-api-sdk-v2/src/UtilsApi.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ export class UtilsApi extends BaseAPI {
4242
/**
4343
* Upload the given file to the specified URL.
4444
* @param {string} url The target URL of the upload request.
45-
* @param {string | Record<string, any> | ArrayBuffer | File} data The data that should be uploaded. Warning: Type `File` in Node.js might lead to problems!
45+
* @param {*} data The data that should be uploaded.
4646
* @param {string} contentType Indicate the original media type of the resource.
4747
* @param {string} [filename] The name of the file to be uploaded. When a filename has been specified in the request-upload call, then the same filename has to be specified for the upload as well.
4848
* @param {*} [options] Override http request option.
4949
*/
5050
public upload(
5151
url: string,
52-
data: string | Record<string, any> | ArrayBuffer | File,
52+
data: any,
5353
contentType: string,
5454
filename?: string,
5555
options?: RawAxiosRequestConfig
@@ -71,13 +71,13 @@ export class UtilsApi extends BaseAPI {
7171
/**
7272
* Upload the given asset to the specified ShapeDiver URL.
7373
* @param {string} url The target URL of the upload request.
74-
* @param {string | Record<string, any> | ArrayBuffer | File} data The data that should be uploaded. Warning: Type `File` in Node.js might lead to problems!
74+
* @param {*} data The data that should be uploaded.
7575
* @param {ResAssetUploadHeaders} headers The headers object that was returned from the request-upload call.
7676
* @param {*} [options] Override http request option.
7777
*/
7878
public uploadAsset(
7979
url: string,
80-
data: string | Record<string, any> | ArrayBuffer | File,
80+
data: any,
8181
headers: ResAssetUploadHeaders,
8282
options?: RawAxiosRequestConfig
8383
): AxiosPromise<unknown> {
@@ -98,25 +98,50 @@ export class UtilsApi extends BaseAPI {
9898

9999
/**
100100
* Download from the specified URL.
101+
*
102+
* The response type can be controlled by setting the `responseType` in the `options` object.
101103
* @param {string} url The target URL of the download request.
102104
* @param {*} [options] Override http request option.
103105
*/
104-
public download(url: string, options?: RawAxiosRequestConfig): AxiosPromise<File> {
106+
public download(
107+
url: string,
108+
options: { responseType: 'arraybuffer' | 'blob' } & RawAxiosRequestConfig
109+
): AxiosPromise<File>;
110+
public download(
111+
url: string,
112+
options: { responseType: 'json' } & RawAxiosRequestConfig
113+
): AxiosPromise<Record<string, unknown>>;
114+
public download(
115+
url: string,
116+
options: { responseType: 'text' } & RawAxiosRequestConfig
117+
): AxiosPromise<string>;
118+
public download(url: string, options?: RawAxiosRequestConfig): AxiosPromise<unknown>;
119+
public download(url: string, options?: RawAxiosRequestConfig): AxiosPromise<unknown> {
105120
const request = this.buildRequest('GET', url, undefined, options)();
106121
return request();
107122
}
108123

109124
/**
110125
* Downloads a ShapeDiver export, output, or texture asset from the specified URL. The type of
111126
* the asset is determined by the URL and returned with the promise.
127+
*
128+
* The response type can be controlled by setting the `responseType` in the `options` object.
112129
* @param {string} url The URL of the asset to download.
113130
* @param {*} [options] Override http request option.
114131
* @throws {IllegalArgumentError} in case the URL is not a valid ShapeDiver asset URL.
115132
*/
133+
public downloadAsset(
134+
url: string,
135+
options: { responseType: 'arraybuffer' | 'blob' } & RawAxiosRequestConfig
136+
): [AxiosPromise<File>, 'export' | 'output' | 'texture'];
137+
public downloadAsset(
138+
url: string,
139+
options?: RawAxiosRequestConfig
140+
): [AxiosPromise<unknown>, 'export' | 'output' | 'texture'];
116141
public downloadAsset(
117142
url: string,
118143
options?: RawAxiosRequestConfig
119-
): [AxiosPromise<File>, 'export' | 'output' | 'texture'] {
144+
): [AxiosPromise<unknown>, 'export' | 'output' | 'texture'] {
120145
let type: 'output' | 'export' | 'texture';
121146
this.disableAuthHeaderForShapeDiverUris(url, options);
122147

@@ -130,21 +155,33 @@ export class UtilsApi extends BaseAPI {
130155
);
131156
}
132157

133-
return [this.download(url, options), type];
158+
return [this.download(url, options) as any, type];
134159
}
135160

136161
/**
137162
* Helper function that downloads all ShapeDiver texture URLs directly, and redirects all other
138163
* URLs to the `AssetsApi.downloadImage` endpoint to avoid CORS issues.
164+
*
165+
* The response type can be controlled by setting the `responseType` in the `options` object.
139166
* @param {string} sessionId The session ID.
140167
* @param {string} url The URL of the image to download.
141168
* @param {*} [options] Override http request option.
142169
*/
170+
public downloadImage(
171+
sessionId: string,
172+
url: string,
173+
options: { responseType: 'arraybuffer' | 'blob' } & RawAxiosRequestConfig
174+
): AxiosPromise<File>;
143175
public downloadImage(
144176
sessionId: string,
145177
url: string,
146178
options?: RawAxiosRequestConfig
147-
): AxiosPromise<File> {
179+
): AxiosPromise<unknown>;
180+
public downloadImage(
181+
sessionId: string,
182+
url: string,
183+
options?: RawAxiosRequestConfig
184+
): AxiosPromise<unknown> {
148185
this.disableAuthHeaderForShapeDiverUris(url, options);
149186

150187
if (
@@ -153,7 +190,7 @@ export class UtilsApi extends BaseAPI {
153190
directDownloadUri.test(url)
154191
) {
155192
// Call ShapeDiver texture-asset URLs directly
156-
return this.download(url, options);
193+
return this.download(url, options) as any;
157194
} else {
158195
// All other source URLs are called via the download-image endpoint
159196
return new AssetsApi(this.configuration).downloadImage(sessionId, url, options);

0 commit comments

Comments
 (0)