| 1234567891011121314151617181920212223242526272829303132333435363738 |
- import {Plugin} from '@nuxt/types'
- import Hydra from '../../services/hydra'
- declare module '@nuxt/types' {
- interface Context {
- $rest: { [key: string]: any }
- }
- }
- const restPlugin: Plugin = (ctx) => {
- const getCollection = async (uri: string) => {
- const responseQuery = await ctx.$http.get(`${uri}`);
- return await queries(responseQuery);
- }
- const getItem = async (uri: string, id: number) => {
- const query = await ctx.$http.get(`${uri}/${id}`);
- return await queries(query);
- }
- const queries = async (responseQuery: any) => {
- try {
- let response = await responseQuery.json();
- return Hydra.parse(response);
- } catch (err) {
- console.log(err)
- }
- }
- ctx.$rest = {
- 'getCollection': getCollection,
- 'getItem': getItem
- }
- }
- export default restPlugin
|