Skip to content

Commit 40f1f5e

Browse files
committed
apiMock
1 parent fd1aa80 commit 40f1f5e

File tree

8 files changed

+114
-60
lines changed

8 files changed

+114
-60
lines changed

mocks/api/history/GET.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"keyword": "검색기록2",
4+
"date": "12.03"
5+
},
6+
{
7+
"keyword": "검색기록1",
8+
"date": "12.02"
9+
},
10+
{
11+
"keyword": "검색기록0",
12+
"date": "12.01"
13+
}
14+
]

mocks/api/search/GET.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[{
2+
"id": 1,
3+
"name": "[버거] 치즈버거처럼 생긴 새우버거",
4+
"image": "https://images.unsplash.com/photo-1547584370-2cc98b8b8dc8?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60"
5+
}, {
6+
"id": 2,
7+
"name": "[샐러드] 맛있는 색깔의 콥셀러드",
8+
"image": "https://images.unsplash.com/photo-1512621776951-a57141f2eefd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60"
9+
}, {
10+
"id": 3,
11+
"name": "[피자] 썸네일 주소가 잘못된 상품",
12+
"image": "http://foo.bar/image.jpg"
13+
}]

package-lock.json

Lines changed: 43 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"test": "echo \"Error: no test specified\" && exit 1",
88
"build": "webpack",
99
"lint": "eslint",
10-
"start": "webpack-dev-server"
10+
"start": "webpack-dev-server --progress"
1111
},
1212
"keywords": [],
1313
"author": "",
@@ -18,6 +18,7 @@
1818
"@babel/preset-env": "^7.7.4",
1919
"babel-loader": "^8.0.6",
2020
"clean-webpack-plugin": "^3.0.0",
21+
"connect-api-mocker": "^1.7.0",
2122
"css-loader": "^3.2.0",
2223
"eslint": "^6.7.2",
2324
"file-loader": "^5.0.2",
@@ -28,5 +29,8 @@
2829
"webpack": "^4.41.2",
2930
"webpack-cli": "^3.3.10",
3031
"webpack-dev-server": "^3.9.0"
32+
},
33+
"dependencies": {
34+
"axios": "^0.19.0"
3135
}
3236
}

src/models/HistoryModel.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1+
import axios from 'axios'
2+
13
export default {
2-
data: [{
3-
keyword: '검색기록2',
4-
date: '12.03'
5-
},
6-
{
7-
keyword: '검색기록1',
8-
date: '12.02'
9-
},
10-
{
11-
keyword: '검색기록0',
12-
date: '12.01'
13-
},
14-
],
4+
data: [],
5+
6+
async list() {
7+
if (this.data.length) return this.data;
158

16-
list() {
17-
return Promise.resolve(this.data)
9+
const result = await axios.get('/api/history')
10+
this.data = result.data;
11+
return this.data;
1812
},
1913

2014
add(keyword = '') {

src/models/KeywordModel.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
1-
export default {
2-
data: [{
3-
keyword: '이탈리아'
4-
},
5-
{
6-
keyword: '세프의요리'
7-
},
8-
{
9-
keyword: '제철'
10-
},
11-
{
12-
keyword: '홈파티'
13-
}
14-
],
1+
import axios from 'axios'
152

16-
list() {
17-
return new Promise(res => {
18-
setTimeout(() => {
19-
res(this.data)
20-
}, 200)
21-
})
3+
export default {
4+
async list() {
5+
const result = await axios.get('/api/keywords');
6+
return result.data;
227
}
238
}

src/models/SearchModel.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,8 @@
1-
const data = [{
2-
id: 1,
3-
name: '[버거] 치즈버거처럼 생긴 새우버거',
4-
image: 'https://images.unsplash.com/photo-1547584370-2cc98b8b8dc8?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60'
5-
},
6-
{
7-
id: 2,
8-
name: '[샐러드] 맛있는 색깔의 콥셀러드',
9-
image: 'https://images.unsplash.com/photo-1512621776951-a57141f2eefd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60'
10-
},
11-
{
12-
id: 3,
13-
name: '[피자] 썸네일 주소가 잘못된 상품',
14-
image: 'http://foo.bar/image.jpg'
15-
}
16-
]
1+
import axios from 'axios'
172

183
export default {
19-
list() {
20-
return new Promise(res => {
21-
setTimeout(() => {
22-
res(data)
23-
}, 200);
24-
})
4+
async list() {
5+
const result = await axios.get('/api/search');
6+
return result.data;
257
}
268
}

webpack.config.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require('path')
22
const HtmlWebpackPlugin = require('html-webpack-plugin')
33
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
44
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
5+
const apiMocker = require('connect-api-mocker');
56

67
module.exports = {
78
mode: 'development',
@@ -13,7 +14,27 @@ module.exports = {
1314
path: path.resolve('./dist'),
1415
},
1516
devServer: {
17+
contentBase: './dist',
18+
publicPath: '/',
19+
open: true,
20+
before: (app, server, compiler) => {
21+
app.get('/api/keywords', (req, res) => {
22+
res.json([
23+
{ keyword: '이탈리아' },
24+
{ keyword: '세프의요리' },
25+
{ keyword: '제철' },
26+
{ keyword: '홈파티'}
27+
])
28+
})
29+
app.use(apiMocker('/api', 'mocks/api'))
30+
},
31+
host: 'dev.domain.com',
32+
stats: 'errors-only',
33+
overlay: true,
34+
port: 8081,
35+
historyApiFallback: true,
1636
},
37+
watch: false,
1738
module: {
1839
rules: [
1940
{

0 commit comments

Comments
 (0)