Skip to content

Commit

Permalink
feat(mock): use devServer to mock apis
Browse files Browse the repository at this point in the history
  • Loading branch information
lbwa committed Mar 25, 2020
1 parent fccb155 commit c5cf854
Show file tree
Hide file tree
Showing 27 changed files with 72 additions and 7,370 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VUE_APP_REQUEST_BASE_URL=http://localhost:3000
VUE_APP_REQUEST_BASE_URL=/
2 changes: 1 addition & 1 deletion .env.development
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VUE_APP_REQUEST_BASE_URL=http://localhost:3000
VUE_APP_REQUEST_BASE_URL=/
55 changes: 55 additions & 0 deletions mock.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
function createRandomString() {
return Math.random()
.toString(16)
.slice(2)
}

function Response(code, data) {
this.statusCode = code
this.data = data
}

const apis = {
'GET /user/abilities': function(req, res) {
res.json(
new Response(
200,
[
'npm.org.read',
'npm.org.write',
'npm.package.read',
'npm.package.write',
'github.repo.read',
'github.repo.write',
'github.action.read',
'github.action.write'
].map(name => ({
name,
uid: createRandomString(),
createAt: Date.now()
}))
)
)
},
'POST /user/profile': function(req, res) {
res.json(
new Response(200, {
token: createRandomString()
})
)
}
}

module.exports = function mocker(app) {
const methodsReg = /^(?:GET|POST|DELETE|PUT|PATCH|OPTION)$/i
const routeReg = /^\/.+/
Object.keys(apis).forEach(api => {
const [method, route] = api.split(' ')

if (methodsReg.test(method) && routeReg.test(route)) {
app[method.toLowerCase()](route, apis[api])
} else {
console.error('[MOCK]: Unexpected method or route')
}
})
}
24 changes: 0 additions & 24 deletions server/.eslintrc.js

This file was deleted.

34 changes: 0 additions & 34 deletions server/.gitignore

This file was deleted.

73 changes: 0 additions & 73 deletions server/README.md

This file was deleted.

4 changes: 0 additions & 4 deletions server/nest-cli.json

This file was deleted.

73 changes: 0 additions & 73 deletions server/package.json

This file was deleted.

11 changes: 0 additions & 11 deletions server/src/app.module.ts

This file was deleted.

22 changes: 0 additions & 22 deletions server/src/app/app.controller.spec.ts

This file was deleted.

12 changes: 0 additions & 12 deletions server/src/app/app.controller.ts

This file was deleted.

8 changes: 0 additions & 8 deletions server/src/app/app.service.ts

This file was deleted.

11 changes: 0 additions & 11 deletions server/src/main.ts

This file was deleted.

7 changes: 0 additions & 7 deletions server/src/response.interceptor.spec.ts

This file was deleted.

25 changes: 0 additions & 25 deletions server/src/response.interceptor.ts

This file was deleted.

Loading

0 comments on commit c5cf854

Please sign in to comment.