1/* 2 * Copyright (c) 2020 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/** 17 * @syscap SystemCapability.MiscServices.Upload 18 */ 19export interface UploadResponse { 20 /** 21 * Status code returned by the server. 22 * @syscap SystemCapability.MiscServices.Upload 23 * @since 3 24 */ 25 code: number; 26 27 /** 28 * Content returned by the server. 29 * The value type is determined by the returned content. 30 * @syscap SystemCapability.MiscServices.Upload 31 * @since 3 32 */ 33 data: string; 34 35 /** 36 * Headers returned by the server. 37 * @syscap SystemCapability.MiscServices.Upload 38 * @since 3 39 */ 40 headers: Object; 41} 42 43/** 44 * @syscap SystemCapability.MiscServices.Download 45 */ 46export interface DownloadResponse { 47 /** 48 * Download token, which is used to obtain the download status. 49 * @syscap SystemCapability.MiscServices.Download 50 * @since 3 51 */ 52 token: string; 53} 54 55/** 56 * @syscap SystemCapability.MiscServices.Download 57 */ 58export interface OnDownloadCompleteResponse { 59 /** 60 * URI of the download file. 61 * @syscap SystemCapability.MiscServices.Download 62 * @since 3 63 */ 64 uri: string; 65} 66 67/** 68 * @syscap SystemCapability.MiscServices.Upload 69 */ 70export interface RequestFile { 71 /** 72 * File name in the header when multipart is used. 73 * @syscap SystemCapability.MiscServices.Upload 74 * @since 3 75 */ 76 filename?: string; 77 78 /** 79 * Name of a form item when multipart is used. The default value is file. 80 * @syscap SystemCapability.MiscServices.Upload 81 * @since 3 82 */ 83 name?: string; 84 85 /** 86 * Local storage directory of a file. 87 * @syscap SystemCapability.MiscServices.Upload 88 * @since 3 89 */ 90 uri: string; 91 92 /** 93 * Type of the file content. 94 * By default, the type is obtained based on the suffix of the file name or URI. 95 * @syscap SystemCapability.MiscServices.Upload 96 * @since 3 97 */ 98 type?: string; 99} 100 101/** 102 * @syscap SystemCapability.MiscServices.Upload 103 */ 104export interface RequestData { 105 /** 106 * Name of the form element. 107 * @syscap SystemCapability.MiscServices.Upload 108 * @since 3 109 */ 110 name: string; 111 112 /** 113 * Value of the form element. 114 * @syscap SystemCapability.MiscServices.Upload 115 * @since 3 116 */ 117 value: string; 118} 119 120/** 121 * @syscap SystemCapability.MiscServices.Upload 122 */ 123export interface UploadRequestOptions { 124 /** 125 * Resource URL. 126 * @syscap SystemCapability.MiscServices.Upload 127 * @since 3 128 */ 129 url: string; 130 131 /** 132 * Form data in the request body. 133 * @syscap SystemCapability.MiscServices.Upload 134 * @since 3 135 */ 136 data?: Array<RequestData>; 137 138 /** 139 * List of files to upload, which is submitted through multipart/form-data. 140 * @syscap SystemCapability.MiscServices.Upload 141 * @since 3 142 */ 143 files: Array<RequestFile>; 144 145 /** 146 * Request header. 147 * @syscap SystemCapability.MiscServices.Upload 148 * @since 3 149 */ 150 header?: Object; 151 152 /** 153 * Request methods available: POST and PUT. The default value is POST. 154 * @syscap SystemCapability.MiscServices.Upload 155 * @since 3 156 */ 157 method?: string; 158 159 /** 160 * Called when the files are uploaded successfully. 161 * @syscap SystemCapability.MiscServices.Upload 162 * @since 3 163 */ 164 success?: (data: UploadResponse) => void; 165 166 /** 167 * Called when uploading fails. 168 * @syscap SystemCapability.MiscServices.Upload 169 * @since 3 170 */ 171 fail?: (data: any, code: number) => void; 172 173 /** 174 * Called when the execution is completed. 175 * @syscap SystemCapability.MiscServices.Upload 176 * @since 3 177 */ 178 complete?: () => void; 179} 180 181/** 182 * @syscap SystemCapability.MiscServices.Download 183 */ 184export interface DownloadRequestOptions { 185 /** 186 * Resource URL. 187 * @syscap SystemCapability.MiscServices.Download 188 * @since 3 189 */ 190 url: string; 191 192 /** 193 * Name of the file to downloaded. 194 * The value is obtained from the current request or resource URL by default. 195 * @syscap SystemCapability.MiscServices.Download 196 * @since 3 197 */ 198 filename?: string; 199 200 /** 201 * Request header. 202 * @syscap SystemCapability.MiscServices.Download 203 * @since 3 204 */ 205 header?: string; 206 207 /** 208 * Download description. 209 * The default value is the file name. 210 * @syscap SystemCapability.MiscServices.Download 211 * @since 3 212 */ 213 description?: string; 214 215 /** 216 * Called when the files are successfully downloaded. 217 * @syscap SystemCapability.MiscServices.Download 218 * @since 3 219 */ 220 success?: (data: DownloadResponse) => void; 221 222 /** 223 * Called when downloading fails. 224 * @syscap SystemCapability.MiscServices.Download 225 * @since 3 226 */ 227 fail?: (data: any, code: number) => void; 228 229 /** 230 * Called when the execution is completed. 231 * @syscap SystemCapability.MiscServices.Download 232 * @since 3 233 */ 234 complete?: () => void; 235} 236 237/** 238 * @syscap SystemCapability.MiscServices.Download 239 */ 240export interface OnDownloadCompleteOptions { 241 /** 242 * Token of the result returned by the download function. 243 * @syscap SystemCapability.MiscServices.Download 244 * @since 3 245 */ 246 token: string; 247 248 /** 249 * Called when the downloads are successfully obtained 250 * @syscap SystemCapability.MiscServices.Download 251 * @since 3 252 */ 253 success?: (data: OnDownloadCompleteResponse) => void; 254 255 /** 256 * Called when the downloads fail to be obtained. 257 * @syscap SystemCapability.MiscServices.Download 258 * @since 3 259 */ 260 fail?: (data: any, code: number) => void; 261 262 /** 263 * Called when the execution is completed. 264 * @syscap SystemCapability.MiscServices.Download 265 * @since 3 266 */ 267 complete?: () => void; 268} 269 270export default class Request { 271 /** 272 * Upload files. 273 * @param options Options. 274 * @syscap SystemCapability.MiscServices.Upload 275 */ 276 static upload(options: UploadRequestOptions): void; 277 278 /** 279 * This API is used to download files. 280 * @param options Options. 281 * @syscap SystemCapability.MiscServices.Download 282 */ 283 static download(options: DownloadRequestOptions): void; 284 285 /** 286 * Listens to download task status. 287 * @param options Options. 288 * @syscap SystemCapability.MiscServices.Download 289 */ 290 static onDownloadComplete(options: OnDownloadCompleteOptions): void; 291}