apiRequestService.ts 874 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * A basic api request service
  3. *
  4. * It will send basic http requests and returns raw results
  5. */
  6. import { $Fetch, FetchOptions } from "ohmyfetch";
  7. class ApiRequestService {
  8. private readonly fetch: $Fetch;
  9. public constructor(fetch: $Fetch) {
  10. this.fetch = fetch;
  11. }
  12. /**
  13. * Send a GET request
  14. *
  15. * @param url
  16. * @param query
  17. */
  18. public async get<T>(
  19. url: string,
  20. query: AssociativeArray | null = null
  21. ): Promise<T> {
  22. return await this.request<T>("GET", url, null, query);
  23. }
  24. protected async request<T>(
  25. method: string,
  26. url: string,
  27. body: string | null = null,
  28. query: AssociativeArray | null = null
  29. ): Promise<T> {
  30. const config: FetchOptions = { method };
  31. if (query) {
  32. config.query = query;
  33. }
  34. // @ts-ignore
  35. return this.fetch<T>(url, config);
  36. }
  37. }
  38. export default ApiRequestService;