Created
August 18, 2019 18:10
-
-
Save NovoManu/618952fc8d300c8a199b14d26e1b97f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { BaseApi } from '@/api/baseApi' | |
import { FetchApi } from '@/api/fetchApi' | |
import { AxiosApi } from '@/api/axiosApi' | |
import { IApi } from '@/types' | |
export class Api extends BaseApi implements IApi { | |
private provider: any = new AxiosApi() | |
async fetch(url: string): Promise<any> { | |
return await this.provider.fetch(url) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
import { BaseApi } from '@/api/baseApi' | |
import { IApi } from '@/types' | |
export class AxiosApi extends BaseApi implements IApi { | |
constructor() { | |
super() | |
} | |
async fetch(url: string): Promise<any> { | |
const { data } = await axios.get(`${this.baseUrl}${url}`) | |
return data | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { IApi } from '@/types' | |
export class BaseApi implements IApi { | |
protected baseUrl: string = 'https://jsonplaceholder.typicode.com/' | |
async fetch(url: string): Promise<any> {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { BaseApi } from '@/api/baseApi' | |
import { IApi } from '@/types' | |
export class FetchApi extends BaseApi implements IApi { | |
constructor() { | |
super() | |
} | |
async fetch(url: string): Promise<any> { | |
const response = await fetch(`${this.baseUrl}${url}`) | |
return await response.json() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<Header listName="My new todo list" /> | |
<main> | |
<TodoList> | |
<!--<TodoCard--> | |
<!--v-for="{ id, title, completed } in todos"--> | |
<!--:key="id"--> | |
<!--:title="title"--> | |
<!--:completed="completed"--> | |
<!--/>--> | |
<TodoRow | |
v-for="{ id, title, completed } in todos" | |
:key="id" | |
:id="id" | |
:title="title" | |
:completed="completed" | |
/> | |
</TodoList> | |
</main> | |
</div> | |
</template> | |
<script lang="ts"> | |
import { Component, Vue } from 'vue-property-decorator' | |
import { ITodo, IApi } from '@/types' | |
import { Api } from '@/api/api' | |
import Header from '@/components/Header.vue' | |
import TodoList from '@/components/TodoList.vue' | |
import TodoCard from '@/components/TodoCard.vue' | |
import TodoRow from '@/components/TodoRow.vue' | |
@Component({ | |
components: { Header, TodoList, TodoCard, TodoRow } | |
}) | |
export default class Home extends Vue implements IApi { | |
todos: ITodo[] = [] | |
async mounted() { | |
this.todos = await this.fetch() | |
} | |
async fetch(): Promise<ITodo[]> { | |
const api = new Api() | |
return await api.fetch('todos') | |
} | |
} | |
</script> | |
<style lang="scss"> | |
.container { | |
padding: 1.5rem; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment