http.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { promisified } from './tauri'
  2. export enum ResponseType {
  3. JSON = 1,
  4. Text = 2,
  5. Binary = 3
  6. }
  7. export enum BodyType {
  8. Form = 1,
  9. File = 2,
  10. Auto = 3
  11. }
  12. export type Body = object | string | BinaryType
  13. export type HttpVerb =
  14. | 'GET'
  15. | 'POST'
  16. | 'PUT'
  17. | 'DELETE'
  18. | 'PATCH'
  19. | 'HEAD'
  20. | 'OPTIONS'
  21. | 'CONNECT'
  22. | 'TRACE'
  23. export interface HttpOptions {
  24. method: HttpVerb
  25. url: string
  26. headers?: Record<string, any>
  27. params?: Record<string, any>
  28. body?: Body
  29. followRedirects: boolean
  30. maxRedirections: boolean
  31. connectTimeout: number
  32. readTimeout: number
  33. timeout: number
  34. allowCompression: boolean
  35. responseType?: ResponseType
  36. bodyType: BodyType
  37. }
  38. export type PartialOptions = Omit<HttpOptions, 'method' | 'url'>
  39. /**
  40. * makes a HTTP request
  41. *
  42. * @param options request options
  43. *
  44. * @return promise resolving to the response
  45. */
  46. async function request<T>(options: HttpOptions): Promise<T> {
  47. return await promisified({
  48. cmd: 'httpRequest',
  49. options: options
  50. })
  51. }
  52. /**
  53. * makes a GET request
  54. *
  55. * @param url request URL
  56. * @param options request options
  57. *
  58. * @return promise resolving to the response
  59. */
  60. async function get<T>(url: string, options: PartialOptions): Promise<T> {
  61. return await request({
  62. method: 'GET',
  63. url,
  64. ...options
  65. })
  66. }
  67. /**
  68. * makes a POST request
  69. *
  70. * @param url request URL
  71. * @param body request body
  72. * @param options request options
  73. *
  74. * @return promise resolving to the response
  75. */
  76. async function post<T>(
  77. url: string,
  78. body: Body,
  79. options: PartialOptions
  80. ): Promise<T> {
  81. return await request({
  82. method: 'POST',
  83. url,
  84. body,
  85. ...options
  86. })
  87. }
  88. /**
  89. * makes a PUT request
  90. *
  91. * @param url request URL
  92. * @param body request body
  93. * @param options request options
  94. *
  95. * @return promise resolving to the response
  96. */
  97. async function put<T>(
  98. url: string,
  99. body: Body,
  100. options: PartialOptions
  101. ): Promise<T> {
  102. return await request({
  103. method: 'PUT',
  104. url,
  105. body,
  106. ...options
  107. })
  108. }
  109. /**
  110. * makes a PATCH request
  111. *
  112. * @param url request URL
  113. * @param options request options
  114. *
  115. * @return promise resolving to the response
  116. */
  117. async function patch<T>(url: string, options: PartialOptions): Promise<T> {
  118. return await request({
  119. method: 'PATCH',
  120. url,
  121. ...options
  122. })
  123. }
  124. /**
  125. * makes a DELETE request
  126. *
  127. * @param url request URL
  128. * @param options request options
  129. *
  130. * @return promise resolving to the response
  131. */
  132. async function deleteRequest<T>(
  133. url: string,
  134. options: PartialOptions
  135. ): Promise<T> {
  136. return await request({
  137. method: 'DELETE',
  138. url,
  139. ...options
  140. })
  141. }
  142. export default {
  143. request,
  144. get,
  145. post,
  146. put,
  147. patch,
  148. delete: deleteRequest,
  149. ResponseType,
  150. BodyType
  151. }