rest.ts 829 B

1234567891011121314151617181920212223242526272829
  1. import {$hydraParser} from '../../services/utils/hydraParser'
  2. import {NuxtHTTPInstance} from "@nuxt/http";
  3. export class Rest{
  4. private $http: NuxtHTTPInstance
  5. constructor($http: NuxtHTTPInstance) {
  6. this.$http = $http
  7. }
  8. public getCollection = async (uri: string) => {
  9. const responseQuery = await this.$http.get(`${uri}`);
  10. return await this.queries(responseQuery);
  11. }
  12. public getItem = async (uri: string, key: number|string) => {
  13. const query = await this.$http.get(`${uri}/${key}`);
  14. return await this.queries(query);
  15. }
  16. private queries = async (responseQuery: any) => {
  17. try {
  18. let response = await responseQuery.json();
  19. return $hydraParser.parse(response);
  20. } catch (err) {
  21. console.log(err)
  22. }
  23. }
  24. }
  25. export const $rest = ($http: NuxtHTTPInstance) => new Rest($http)