http.d.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * @since 1.0.0
  3. */
  4. interface Duration {
  5. secs: number;
  6. nanos: number;
  7. }
  8. /**
  9. * @since 1.0.0
  10. */
  11. interface ClientOptions {
  12. /**
  13. * Defines the maximum number of redirects the client should follow.
  14. * If set to 0, no redirects will be followed.
  15. */
  16. maxRedirections?: number;
  17. connectTimeout?: number | Duration;
  18. }
  19. /**
  20. * @since 1.0.0
  21. */
  22. declare enum ResponseType {
  23. JSON = 1,
  24. Text = 2,
  25. Binary = 3
  26. }
  27. /**
  28. * @since 1.0.0
  29. */
  30. interface FilePart<T> {
  31. file: string | T;
  32. mime?: string;
  33. fileName?: string;
  34. }
  35. type Part = string | Uint8Array | FilePart<Uint8Array>;
  36. type FormInput = Record<string, Part> | FormData;
  37. /**
  38. * The body object to be used on POST and PUT requests.
  39. *
  40. * @since 1.0.0
  41. */
  42. declare class Body {
  43. type: string;
  44. payload: unknown;
  45. /** @ignore */
  46. private constructor();
  47. /**
  48. * Creates a new form data body. The form data is an object where each key is the entry name,
  49. * and the value is either a string or a file object.
  50. *
  51. * By default it sets the `application/x-www-form-urlencoded` Content-Type header,
  52. * but you can set it to `multipart/form-data` if the Cargo feature `http-multipart` is enabled.
  53. *
  54. * Note that a file path must be allowed in the `fs` allowlist scope.
  55. *
  56. * @example
  57. * ```typescript
  58. * import { Body } from "@tauri-apps/api/http"
  59. * const body = Body.form({
  60. * key: 'value',
  61. * image: {
  62. * file: '/path/to/file', // either a path or an array buffer of the file contents
  63. * mime: 'image/jpeg', // optional
  64. * fileName: 'image.jpg' // optional
  65. * }
  66. * });
  67. *
  68. * // alternatively, use a FormData:
  69. * const form = new FormData();
  70. * form.append('key', 'value');
  71. * form.append('image', file, 'image.png');
  72. * const formBody = Body.form(form);
  73. * ```
  74. *
  75. * @param data The body data.
  76. *
  77. * @returns The body object ready to be used on the POST and PUT requests.
  78. */
  79. static form(data: FormInput): Body;
  80. /**
  81. * Creates a new JSON body.
  82. * @example
  83. * ```typescript
  84. * import { Body } from "@tauri-apps/api/http"
  85. * Body.json({
  86. * registered: true,
  87. * name: 'tauri'
  88. * });
  89. * ```
  90. *
  91. * @param data The body JSON object.
  92. *
  93. * @returns The body object ready to be used on the POST and PUT requests.
  94. */
  95. static json(data: Record<any, any>): Body;
  96. /**
  97. * Creates a new UTF-8 string body.
  98. * @example
  99. * ```typescript
  100. * import { Body } from "@tauri-apps/api/http"
  101. * Body.text('The body content as a string');
  102. * ```
  103. *
  104. * @param value The body string.
  105. *
  106. * @returns The body object ready to be used on the POST and PUT requests.
  107. */
  108. static text(value: string): Body;
  109. /**
  110. * Creates a new byte array body.
  111. * @example
  112. * ```typescript
  113. * import { Body } from "@tauri-apps/api/http"
  114. * Body.bytes(new Uint8Array([1, 2, 3]));
  115. * ```
  116. *
  117. * @param bytes The body byte array.
  118. *
  119. * @returns The body object ready to be used on the POST and PUT requests.
  120. */
  121. static bytes(bytes: Iterable<number> | ArrayLike<number> | ArrayBuffer): Body;
  122. }
  123. /** The request HTTP verb. */
  124. type HttpVerb = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'CONNECT' | 'TRACE';
  125. /**
  126. * Options object sent to the backend.
  127. *
  128. * @since 1.0.0
  129. */
  130. interface HttpOptions {
  131. method: HttpVerb;
  132. url: string;
  133. headers?: Record<string, any>;
  134. query?: Record<string, any>;
  135. body?: Body;
  136. timeout?: number | Duration;
  137. responseType?: ResponseType;
  138. }
  139. /** Request options. */
  140. type RequestOptions = Omit<HttpOptions, 'method' | 'url'>;
  141. /** Options for the `fetch` API. */
  142. type FetchOptions = Omit<HttpOptions, 'url'>;
  143. /** @ignore */
  144. interface IResponse<T> {
  145. url: string;
  146. status: number;
  147. headers: Record<string, string>;
  148. rawHeaders: Record<string, string[]>;
  149. data: T;
  150. }
  151. /**
  152. * Response object.
  153. *
  154. * @since 1.0.0
  155. * */
  156. declare class Response<T> {
  157. /** The request URL. */
  158. url: string;
  159. /** The response status code. */
  160. status: number;
  161. /** A boolean indicating whether the response was successful (status in the range 200–299) or not. */
  162. ok: boolean;
  163. /** The response headers. */
  164. headers: Record<string, string>;
  165. /** The response raw headers. */
  166. rawHeaders: Record<string, string[]>;
  167. /** The response data. */
  168. data: T;
  169. /** @ignore */
  170. constructor(response: IResponse<T>);
  171. }
  172. /**
  173. * @since 1.0.0
  174. */
  175. declare class Client {
  176. id: number;
  177. /** @ignore */
  178. constructor(id: number);
  179. /**
  180. * Drops the client instance.
  181. * @example
  182. * ```typescript
  183. * import { getClient } from '@tauri-apps/api/http';
  184. * const client = await getClient();
  185. * await client.drop();
  186. * ```
  187. */
  188. drop(): Promise<void>;
  189. /**
  190. * Makes an HTTP request.
  191. * @example
  192. * ```typescript
  193. * import { getClient } from '@tauri-apps/api/http';
  194. * const client = await getClient();
  195. * const response = await client.request({
  196. * method: 'GET',
  197. * url: 'http://localhost:3003/users',
  198. * });
  199. * ```
  200. */
  201. request<T>(options: HttpOptions): Promise<Response<T>>;
  202. /**
  203. * Makes a GET request.
  204. * @example
  205. * ```typescript
  206. * import { getClient, ResponseType } from '@tauri-apps/api/http';
  207. * const client = await getClient();
  208. * const response = await client.get('http://localhost:3003/users', {
  209. * timeout: 30,
  210. * // the expected response type
  211. * responseType: ResponseType.JSON
  212. * });
  213. * ```
  214. */
  215. get<T>(url: string, options?: RequestOptions): Promise<Response<T>>;
  216. /**
  217. * Makes a POST request.
  218. * @example
  219. * ```typescript
  220. * import { getClient, Body, ResponseType } from '@tauri-apps/api/http';
  221. * const client = await getClient();
  222. * const response = await client.post('http://localhost:3003/users', {
  223. * body: Body.json({
  224. * name: 'tauri',
  225. * password: 'awesome'
  226. * }),
  227. * // in this case the server returns a simple string
  228. * responseType: ResponseType.Text,
  229. * });
  230. * ```
  231. */
  232. post<T>(url: string, body?: Body, options?: RequestOptions): Promise<Response<T>>;
  233. /**
  234. * Makes a PUT request.
  235. * @example
  236. * ```typescript
  237. * import { getClient, Body } from '@tauri-apps/api/http';
  238. * const client = await getClient();
  239. * const response = await client.put('http://localhost:3003/users/1', {
  240. * body: Body.form({
  241. * file: {
  242. * file: '/home/tauri/avatar.png',
  243. * mime: 'image/png',
  244. * fileName: 'avatar.png'
  245. * }
  246. * })
  247. * });
  248. * ```
  249. */
  250. put<T>(url: string, body?: Body, options?: RequestOptions): Promise<Response<T>>;
  251. /**
  252. * Makes a PATCH request.
  253. * @example
  254. * ```typescript
  255. * import { getClient, Body } from '@tauri-apps/api/http';
  256. * const client = await getClient();
  257. * const response = await client.patch('http://localhost:3003/users/1', {
  258. * body: Body.json({ email: 'contact@tauri.app' })
  259. * });
  260. * ```
  261. */
  262. patch<T>(url: string, options?: RequestOptions): Promise<Response<T>>;
  263. /**
  264. * Makes a DELETE request.
  265. * @example
  266. * ```typescript
  267. * import { getClient } from '@tauri-apps/api/http';
  268. * const client = await getClient();
  269. * const response = await client.delete('http://localhost:3003/users/1');
  270. * ```
  271. */
  272. delete<T>(url: string, options?: RequestOptions): Promise<Response<T>>;
  273. }
  274. /**
  275. * Creates a new client using the specified options.
  276. * @example
  277. * ```typescript
  278. * import { getClient } from '@tauri-apps/api/http';
  279. * const client = await getClient();
  280. * ```
  281. *
  282. * @param options Client configuration.
  283. *
  284. * @returns A promise resolving to the client instance.
  285. *
  286. * @since 1.0.0
  287. */
  288. declare function getClient(options?: ClientOptions): Promise<Client>;
  289. /**
  290. * Perform an HTTP request using the default client.
  291. * @example
  292. * ```typescript
  293. * import { fetch } from '@tauri-apps/api/http';
  294. * const response = await fetch('http://localhost:3003/users/2', {
  295. * method: 'GET',
  296. * timeout: 30,
  297. * });
  298. * ```
  299. */
  300. declare function fetch<T>(url: string, options?: FetchOptions): Promise<Response<T>>;
  301. export type { Duration, ClientOptions, Part, FormInput, HttpVerb, HttpOptions, RequestOptions, FetchOptions };
  302. export { getClient, fetch, Body, Client, Response, ResponseType, type FilePart };