Skip to content

Commit dc558b4

Browse files
committed
fix: arguments call of controllers tree functions in client side
1 parent e6e948b commit dc558b4

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

lib/plugins/api.template.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ function generateAPI(controllerMapping) {
2929
Object.keys(controllerMapping).forEach(function (key) {
3030
const context = controllerMapping[key];
3131
if (context && context.path && context.verb) {
32-
api[key] = function (params, values) {
32+
api[key] = function ({params, ...values}) {
3333
return ApiHandler(
3434
injectParamsIntoPath(
3535
context.path[context.path.length - 1] === '/' ?
3636
context.path.slice(0, context.path.length - 1) :
3737
context.path,
3838
params
3939
),
40-
context.verb, values || {},
40+
context.verb,
41+
values || {},
4142
<%= options.apiConfig %>
4243
);
4344
}

tests/api.flow.test.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,14 @@ test.before(globalBeforeAll({
77
}));
88
test.after(globalAfterAll());
99

10-
test('Test api prefix (GET /api/v2)', async (t) => {
11-
try {
12-
await api.get('/users/categories');
13-
} catch (err) {
14-
t.is(err.response.status, 404);
15-
}
16-
});
17-
1810
test('Test first level api (GET /api/v2/users)', async (t) => {
1911
const {data} = await api.get('/users');
2012
t.true(data.ok);
2113
});
2214

23-
test('Test second level api (GET /api/v2/users/categories) - should give 404', async (t) => {
15+
test('Test second level api (GET /api/v2/foo) - should give 404', async (t) => {
2416
try {
25-
await api.get('/users/categories');
17+
await api.get('/foo');
2618
} catch (err) {
2719
t.is(err.response.status, 404);
2820
}
@@ -60,11 +52,13 @@ test('Test hybrid api data flow client side', async (t) => {
6052
const clickEvent = new window.Event('click');
6153
const path = window.document.querySelector('.index span.path');
6254
const okay = window.document.querySelector('.index span.okay');
55+
const idParam = window.document.querySelector('.index span.id-param');
6356
const changePath = window.document.querySelector('.index .change-path');
6457

6558
changePath.dispatchEvent(clickEvent);
6659
await new Promise(resolve => setTimeout(resolve, 1000)); // wait for API request
6760

68-
t.is(path.textContent, '/api/v2/users');
61+
t.is(path.textContent, '/api/v2/users/1');
6962
t.is(okay.textContent, "It's okay!");
63+
t.is(idParam.textContent, '1');
7064
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = TestController;

tests/fixtures/api/users/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ class UserController {
1313
}
1414
}
1515

16+
async getAction({params, query}) {
17+
return {
18+
ok: true,
19+
path: this.request.originalUrl,
20+
params,
21+
query,
22+
}
23+
}
24+
1625
createAction({params, body}) {
1726
return {
1827
ok: true,
@@ -47,6 +56,10 @@ UserController.ROUTES = {
4756
path: '/',
4857
verb: 'GET'
4958
},
59+
getAction: {
60+
path: '/:id',
61+
verb: 'GET'
62+
},
5063
createAction: {
5164
path: '/',
5265
verb: 'POST'

tests/fixtures/pages/index.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<template>
22
<div class="index">
33
<span class="path">{{ data.path }}</span>
4+
<span class="id-param">{{ data.params && data.params.id }}</span>
45
<span class="okay" v-if="data.ok">It's okay!</span>
56
<span class="okay" v-else>It's not okay...</span>
67

@@ -14,7 +15,7 @@
1415
asyncData: async ({app}) => ({data: await app.$api.users.categories.types.allAction()}),
1516
methods: {
1617
async handleClick() {
17-
this.data = await this.$api.users.allAction();
18+
this.data = await this.$api.users.getAction({params: {id: 1}});
1819
}
1920
}
2021
}

0 commit comments

Comments
 (0)