Browse Source

Add request class to define services

pull/341/head
Hironsan 5 years ago
parent
commit
5114c42fab
4 changed files with 109 additions and 0 deletions
  1. 1
      frontend/package.json
  2. 40
      frontend/services/request.js
  3. 56
      frontend/test/request.spec.js
  4. 12
      frontend/yarn.lock

1
frontend/package.json

@ -26,6 +26,7 @@
"@nuxtjs/eslint-config": "^0.0.1",
"@nuxtjs/eslint-module": "^0.0.1",
"@vue/test-utils": "^1.0.0-beta.27",
"axios-mock-adapter": "^1.17.0",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.1.0",

40
frontend/services/request.js

@ -0,0 +1,40 @@
import axios from 'axios'
export default class Request {
constructor(baseURL) {
this.instance = axios.create({
baseURL
})
}
request(method, url, data = {}, config = {}) {
return this.instance({
method,
url,
data,
...config
})
.then(response => response.data)
.catch(error => error)
}
get(url, config = {}) {
return this.request('GET', url, config)
}
post(url, data, config = {}) {
return this.request('POST', url, data, config)
}
put(url, data, config = {}) {
return this.request('PUT', url, data, config)
}
patch(url, data, config = {}) {
return this.request('PATCH', url, data, config)
}
delete(url, config = {}) {
return this.request('DELETE', url, {}, config)
}
}

56
frontend/test/request.spec.js

@ -0,0 +1,56 @@
import MockAdapter from 'axios-mock-adapter'
import Request from '@/services/request.js'
describe('Request', () => {
const r = new Request('')
const mockAxios = new MockAdapter(r.instance)
test('can get resources', async () => {
const data = [
{
id: 1,
title: 'title',
body: 'body'
}
]
mockAxios.onGet('/posts').reply(200, data)
const response = await r.get('/posts')
expect(response).toEqual(data)
})
test('can create a resource', async () => {
const data = {
title: 'foo',
body: 'bar'
}
mockAxios.onPost('/posts').reply(201, data)
const response = await r.post('/posts', data)
expect(response.title).toEqual(data.title)
})
test('can update a resource', async () => {
const data = {
id: 1,
title: 'foo',
body: 'bar'
}
mockAxios.onPut('/posts/1').reply(204, data)
const response = await r.put('/posts/1', data)
expect(response.title).toEqual(data.title)
})
test('can partially update a resource', async () => {
const data = {
title: 'foo'
}
mockAxios.onPatch('/posts/1').reply(200, data)
const response = await r.patch('/posts/1', data)
expect(response.title).toEqual(data.title)
})
test('can delete a resource', async () => {
mockAxios.onDelete('/posts/1').reply(204, {})
const response = await r.delete('/posts/1')
expect(response).toEqual({})
})
})

12
frontend/yarn.lock

@ -1851,6 +1851,13 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f"
integrity sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==
axios-mock-adapter@^1.17.0:
version "1.17.0"
resolved "https://registry.yarnpkg.com/axios-mock-adapter/-/axios-mock-adapter-1.17.0.tgz#0dbee43c606d4aaba5a43d88d96d6661a7cc3c04"
integrity sha512-q3efmwJUOO4g+wsLNSk9Ps1UlJoF3fQ3FSEe4uEEhkRtu7SoiAVPj8R3Hc/WP55MBTVFzaDP9QkdJhdVhP8A1Q==
dependencies:
deep-equal "^1.0.1"
axios-retry@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/axios-retry/-/axios-retry-3.1.2.tgz#4f4dcbefb0b434e22b72bd5e28a027d77b8a3458"
@ -3242,6 +3249,11 @@ decode-uri-component@^0.2.0:
resolved "https://registry.yarnpkg.com/decode-uri-component/-/decode-uri-component-0.2.0.tgz#eb3913333458775cb84cd1a1fae062106bb87545"
integrity sha1-6zkTMzRYd1y4TNGh+uBiEGu4dUU=
deep-equal@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/deep-equal/-/deep-equal-1.0.1.tgz#f5d260292b660e084eff4cdbc9f08ad3247448b5"
integrity sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=
deep-extend@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.6.0.tgz#c4fa7c95404a17a9c3e8ca7e1537312b736330ac"

Loading…
Cancel
Save