ohMyFetchConnector.ts 1021 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import {AssociativeArray, Connector, HTTP_METHOD} from "../data";
  2. import {$Fetch} from "nitropack";
  3. import {FetchOptions} from "ohmyfetch";
  4. /**
  5. * Connector for the ohmyfetch library
  6. *
  7. * @see https://github.com/unjs/ohmyfetch
  8. */
  9. class OhMyFetchConnector implements Connector {
  10. private readonly fetch: $Fetch
  11. public constructor(
  12. fetcher: $Fetch,
  13. ) {
  14. this.fetch = fetcher
  15. }
  16. /**
  17. * Send an HTTP request
  18. *
  19. * @param method
  20. * @param url
  21. * @param body
  22. * @param params
  23. * @param query
  24. */
  25. request(
  26. method: HTTP_METHOD,
  27. url: string,
  28. body: string | null = null,
  29. params: AssociativeArray | null = null,
  30. query: AssociativeArray | null = null
  31. ) {
  32. const config: FetchOptions = { body }
  33. if (params) {
  34. config.params = params
  35. }
  36. if (query) {
  37. config.query = query
  38. }
  39. return this.fetch(url, config)
  40. }
  41. }
  42. export default OhMyFetchConnector