http.ts 2.6 KB

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