This repository was archived by the owner on Jan 20, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
175 lines (161 loc) · 5.28 KB
/
Copy pathindex.js
File metadata and controls
175 lines (161 loc) · 5.28 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict';
/*
* HELPFUL COMMENTS ALERT
*
* This file defines Grove routes nested under `/api`.
*
* You should make this the primary place to configure, install, and
* remove middle-tier endpoints for your Grove project.
*
* We have commented this file to make your job easier.
*/
// See the Express.js documentation to understand the Router.
var router = require('express').Router();
// Grove provides default route implementations. They are configurable.
// We import the routeFactory here and will use it below.
// TODO: create and link to documentation on grove-default-routes.
const routeFactory = require('../../grove-default-routes');
// The authProvider is injected into each route.
// This is so that you can provide a different authProvider if desired.
const authProvider = require('../../grove-node-server-utils/auth-helper');
// mapping URIs to ids for CRUD in other routes
// even possibly going so far as to return IDs instead of URIs
// /dogs/1587 instead of dogs?uri=/somethingUgly
// It is helpful to share this logic between search and CRUD routes,
// particularly those referencing the same type.
const idConverter = {
toId: function(uri) {
return encodeURIComponent(uri);
},
toUri: function(id) {
return decodeURIComponent(id);
}
};
// Helper for sending error responses
const four0four = require('../../grove-node-server-utils/404')();
// Ping route
router.use(
'/ping',
routeFactory.defaultPingRoute({
authProvider: authProvider,
authed: false
})
);
// A minimal authRoute.
router.use(
'/auth',
routeFactory.defaultAuthRoute({
authProvider: authProvider,
neverCache: true
})
);
// Provide routes for pseudo-type 'all'
const types = ['all'];
types.forEach(type => {
router.use(
'/search/' + type + '/',
routeFactory.defaultExtensionRoute({
authProvider: authProvider,
authed: true, // default: true
neverCache: true, // default: true
actions: {
config: {
uri: '/v1/config/query/' + type,
GET: function(body, params, req) {
return {
method: req.method,
body: '',
params: {
format: 'json'
}
};
}
}
}
})
);
// Search route
router.use(
'/search/' + type,
routeFactory.defaultSearchRoute({
authProvider: authProvider,
namedOptions: type, // default: 'all'
// options: {
// // Add JSON search options here.
//
// // default: none
//
// // WARNING: This will override the saved search options referenced by 'namedOptions' above.
//
// // See https://docs.marklogic.com/guide/search-dev/appendixa for
// // details on query options
//
// // Example for making result labels using name property of person sample-data
// 'extract-document-data': {
// 'extract-path': ['/name']
// }
// },
idConverter: idConverter, // default: encodeURIComponent(result.uri)
makeLabel: result => {
// default: none
return (
result.extracted &&
result.extracted.content[0] &&
result.extracted.content[0].name
);
}
// transform: 'extractedToJson'
})
);
// CRUD route
router.use(
'/crud/' + type,
routeFactory.defaultCrudRoute({
authProvider: authProvider,
authed: true, // default: true
neverCache: true, // default: true
directory: '/' + type + '/', // default: '/'
//extension: 'json', // (default)
contentType: '*/*', // default: application/json
//temporalCollection: 'uni-temporal', // default: none
collections: ['data', 'type/' + type], // default: none
idConverter: idConverter, // default: encode/decodeUriComponent
views: {
// By default, a single `metadata` view is configured.
// This metadata view returns metadata including contentType,
// fileName, format, and size of the entity. It is particularly
// useful for binaries.
//
// There is also an implicit `_default` view, which is invoked
// when the crud endpoint is called with only an id (not followed by
// a view name). You can change the behavior of this implicit view
// by configuring a view named `_default`.
//
// The client can access different views when performing a READ
// by adding a view name after the id, as in:
// '/api/crud/all/someID/indent'
//
// Supported view configuration options include `transform`,
// `category`, `format` (which defaults to 'json'),
// `contentType` (the mimetype that will be sent to the browser,
// which defaults to the top-level contentType for the route),
// `call` (a function to override the middle-tiers READ logic,
// which receives req, res, config, id, and the viewName as arguments)
'to-json': {
transform: 'to-json'
},
indent: {
transform: 'indent'
}
}
})
);
});
/*
* Your additional routes here. Or modify the above. Or delete them. :-)
*/
// For requests not matching any of the above, return a 404.
router.use('/*', function(req, res) {
four0four.apiNotFound(req, res, req.url);
});
module.exports = router;