|
1 | | -// api.js file contains this code |
2 | | -// note: npm install axios |
3 | | -const axios = require('axios') |
4 | | - |
5 | | -const baseUrl = "http://127.0.0.1:5000/"; |
6 | | -const todos = baseUrl + "todos"; |
| 1 | +import axios from "axios"; |
| 2 | +//helper function for api endpoint contains GET,POST,PUT, DELETE |
7 | 3 |
|
| 4 | +const apiBase = "localhost:9010"; |
8 | 5 | export default {
|
9 | | - todosFetch() { |
| 6 | + endpoint(url) { |
| 7 | + url = apiBase + url; //concat base url and url with base api endpoint |
10 | 8 | return {
|
11 | | - getOne: ({ id }) => fetch(todos+`/${id}`), //id_url |
12 | | - getAll: () => fetch(todos),//url |
13 | | - create: (data) => fetch(todos, data), //url, data |
14 | | - update: ({ id }, data) => fetch(todos + `/${id}`, data), //url, data |
15 | | - delete: ({ id }) => fetch(todos + `${id}`) |
| 9 | + getOne: (id) => axios.get(url+`/${id}`), //id_url |
| 10 | + getAll: () => axios.get(url), |
| 11 | + create: (data) => axios.post(url, data), //url, data |
| 12 | + update: (id, data) => axios.put(url + `/${id}`, data), //url, data |
| 13 | + delete: (id) => axios.delete(url + `/${id}`), |
16 | 14 | }
|
17 | | - |
18 | 15 | },
|
19 | | - todosAxios() { |
20 | | - return { |
21 | | - getOne: ({id}) => axios.get(todos + `/${id}`), //id_url |
22 | | - getAll: () => axios.get(todos), |
23 | | - create: (data) => axios.post(todos, data), //url, data |
24 | | - update: ({ id }, data) => axios.put(todos + `/${id}`, data), //url, data |
25 | | - delete: ({ id }) => axios.delete(todos + `/${id}`)//id_url |
26 | | - } |
27 | | - } |
28 | 16 | }
|
29 | | -//[Example Component](react-js/ApiIntegration.js) |
30 | | -// componentDidMount(){ |
31 | | -// api.todosFetch().getAll() |
32 | | -// .then(response => response.json() //fetch method need to convert json |
33 | | -// .then(data => this.setState({ todos: data }))); //define converted data state variable |
34 | | -//} |
35 | | -// componentDidMount(){ |
36 | | - // api.todosAxios().getAll() |
37 | | - // .then(response => this.setState({ axiosTodos: response.data })) |
38 | | -//} |
| 17 | + |
0 commit comments