-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigation.js
74 lines (65 loc) · 1.65 KB
/
navigation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
const { NavigationError } = require('../utils/NavigationError');
/**
* navigation.js controller
*
* @description: A set of functions called "actions" of the `navigation` plugin.
*/
const parseParams = (params) =>
Object.keys(params).reduce((prev, curr) => {
const value = params[curr];
const parsedValue = isNaN(Number(value)) ? value : parseInt(value, 10);
return {
...prev,
[curr]: parsedValue,
};
}, {});
const errorHandler = (ctx) => (error) => {
if (error instanceof NavigationError) {
return ctx.badRequest(error.message, error.additionalInfo);
}
throw error;
};
module.exports = {
getService() {
return strapi.plugins.navigation.services.navigation;
},
/**
* Default action.
*
* @return {Object}
*/
async config() {
return await this.getService().config();
},
async get() {
return await this.getService().get();
},
async getById(ctx) {
const { params } = ctx;
const { id } = parseParams(params);
return await this.getService().getById(id);
},
async render(ctx) {
const { params, query = {} } = ctx;
const { type, menu: menuOnly } = query;
const { idOrSlug } = parseParams(params);
return await this.getService().render(
idOrSlug,
type,
menuOnly,
);
},
post(ctx) {
const { auditLog } = ctx;
const { body = {} } = ctx.request;
return this.getService().post(body, auditLog);
},
put(ctx) {
const { params, auditLog } = ctx;
const { id } = parseParams(params);
const { body = {} } = ctx.request;
return this.getService().put(id, body, auditLog)
.catch(errorHandler(ctx));
},
};