-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
147 lines (125 loc) · 4.56 KB
/
index.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
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
/************************************************
* This code is a function for interact with Indexer.
* Deploy on AWS Lambda (triggered by AWS API Gateway)
************************************************/
exports.handler = async (event, context, callback) => {
// import module for submitting request.
const axios = require('axios');
/************************************************
* Information for interact with Indexer
* You can setup these environment variables below on the AWS Lambda function's configuration.
************************************************/
const env = {
api_host: process.env.OPENSEARCH_API_HOST || '{YOUR_OPENSEARCH_API_HOST}',
username: process.env.OPENSEARCH_USERNAME || '{YOUR_OPENSEARCH_USERNAME}',
password: process.env.OPENSEARCH_PASSWORD || '{YOUR_OPENSEARCH_PASSWORD}',
};
const normalizeObject = object => Object.fromEntries(Object.entries(object).map(([key, value]) => [key, typeof value === 'object' ? normalizeObject(value) : typeof value === 'boolean' ? value : !isNaN(value) ? Number(value) : value]));
// response data variable
let response = null;
const body = (event.body && JSON.parse(event.body)) || event.queryStringParameters;
if (body && body.index) {
const _body = { ...body };
// set table name
const index = _body.index;
delete body.index;
// set method
const method = _body.method; // search, get, update, delete
delete body.method;
// set id
const id = _body.id;
delete body.id;
// initial requester object
const requester = axios.create({ baseURL: env.api_host });
const auth = {
username: env.username,
password: env.password,
};
// initial response object
let res = null;
// initial path parameter
let path = _body.path;
// remove path parameter (if exist) before setup parameters
if (path) {
delete body.path;
}
// initial params parameter
let params = null;
// normalize path parameter
path = path || '';
// setup query string parameters
if (!isNaN(body.height)) {
body.height = Number(body.height);
}
const objectFields = ['aggs', 'query', 'sort'];
objectFields.forEach(objectField => {
if (body[objectField]) {
try {
body[objectField] = body[objectField] && body[objectField].startsWith('[') && body[objectField].endsWith(']') ? JSON.parse(body[objectField]) : normalizeObject(JSON.parse(body[objectField]));
} catch (err) {}
}
});
params = { ...body };
// do action
switch(method) {
case 'search':
if (!path) {
path = `/${index}/_search`;
}
// send request
res = await requester.post(path, body, { auth })
// set response data from error handled by exception
.catch(error => { return { data: { error } }; });
break;
case 'get':
if (!path) {
path = `/${index}/_doc/${id}`;
}
// send request
res = await requester.get(path, { params, auth })
// set response data from error handled by exception
.catch(error => { return { data: { error } }; });
break;
case 'update':
if (!path) {
path = `/${index}/_doc/${id}`;
}
if (body) {
// send request
res = await requester.put(path, path.includes('_update') ? { doc: body } : body, { auth })
// set response data from error handled by exception
.catch(error => { return { data: { error } }; });
if (res && res.data && res.data.error) {
if (path) {
path = path.replace(path.includes('_doc') ? '_doc' : '_update', path.includes('_update') ? '_update' : '_doc');
}
// send request
res = await requester.post(path, path.includes('_update') ? { doc: body } : body, { auth })
// set response data from error handled by exception
.catch(error => { return { data: { error } }; });
}
}
break;
case 'delete':
if (!path) {
path = `/${index}/_doc/${id}`;
}
// send request
res = await requester.delete(path, { params, auth })
// set response data from error handled by exception
.catch(error => { return { data: { error } }; });
break;
default:
break;
}
// set response data
if (res && res.data) {
if (res.data.error) {
delete res.data.error;
}
response = res.data;
}
}
// return response data
return response;
};