Last active
June 6, 2020 08:13
-
-
Save thaycacac/9df12c6d9e40bb3d50c751abe77e849f to your computer and use it in GitHub Desktop.
[VueJS] API Pattern
This file contains hidden or 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 PostsList from './PostsList' | |
import RepositoryFactory from '../repositories/RepositoryFactory' | |
const PostRepository = RepositoryFactory.get('posts') | |
export default { | |
methods: { | |
async fetch() { | |
const { data } = await PostsRepository.get() | |
} | |
} | |
} |
This file contains hidden or 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 Repository from './Repository' | |
const resource = '/posts' | |
export default { | |
get() { | |
return Repository.get(`${resource}`) | |
} | |
getPost(postId) { | |
return Repository.get(`${resource}/${postId}`) | |
} | |
createPost(payload) { | |
return Repository.post(`${resource}`, payload) | |
} | |
updatePost(payload) { | |
return Repository.put(`${resource}`, payload) | |
} | |
deletePost(payload) { | |
return Repository.delete(`${resource}/${postId}`) | |
} | |
} |
This file contains hidden or 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' | |
const baseDomain = 'https://example.com' | |
const baseUrl = `${baseDomain}/api/v1` | |
export default axios.create({ | |
baseUrl, | |
header: { "Authorization": "Bearer yourToken" } | |
}) |
This file contains hidden or 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 PostsRepository from './postsRepository' | |
const repositories = { | |
posts: PostsRepository | |
} | |
export const RepositoryFactory = { | |
get: name => repositories[name] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment