Skip to content

Instantly share code, notes, and snippets.

@RuNpiXelruN
Last active March 18, 2019 00:41
Show Gist options
  • Save RuNpiXelruN/b4fc7d09a6a14722b2b3b2b2bb920299 to your computer and use it in GitHub Desktop.
Save RuNpiXelruN/b4fc7d09a6a14722b2b3b2b2bb920299 to your computer and use it in GitHub Desktop.
//plugins/api.js
import apiLayer from '~/api'
import delayResponse from './axios-delay'
export default (ctx, inject) => {
// inject the api in the context (ctx.app.$api)
// ..and can accessed in the Vue instances and store by (this.$api)
const errorCheck = status => {
return status < 200 && status >= 300
}
ctx.$axios['isErrorStatus'] = errorCheck
const apiLayerWithAxios = apiLayer(ctx.$axios, delayResponse)
inject('api', apiLayerWithAxios)
}
// api/index.js
import contactEndpoints from './contact'
import applicationEndpoints from './application'
import partnersEndpoints from './partners'
import addressEndpoints from './address'
import employmentEndpoints from './employment'
import liabilitiesEndpoint from './liabilities'
import kleberEndpoints from './kleber'
import quoteEndpoints from './quote'
import incomeEndpoints from './income'
import expenseEndpoints from './expense'
export default (axios, delayResponse) => ({
...contactEndpoints(axios),
...applicationEndpoints(axios),
...partnersEndpoints(axios),
...addressEndpoints(axios),
...employmentEndpoints(axios),
...quoteEndpoints(axios, delayResponse),
...incomeEndpoints(axios),
...kleberEndpoints,
...liabilitiesEndpoint(axios),
...expenseEndpoints(axios)
})
// api/quote.js
export default (axios, delayResponse) => ({
async getQuote (contactId, applicationId) {
let apiError
let response
response = await delayResponse(2000, axios.get(`/contact/${contactId}/application/${applicationId}/quote`))
if (axios.isErrorStatus(response.status)) {
apiError = {
name: 'getQuote error',
message: `Error fetching a Quote with:` +
`\ncontactId of: ${contactId}` +
`\napplicationId of: ${applicationId}`,
status: response.status
}
return apiError
}
return response.data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment