-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
326 lines (317 loc) · 8.06 KB
/
util.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
const { toJSON } = require('./vtl');
const log = require('logdown')('appsync-emulator:util');
class Unauthorized extends Error {}
class TemplateSentError extends Error {
constructor(gqlMessage, errorType, data, errorInfo) {
super(gqlMessage);
Object.assign(this, { gqlMessage, errorType, data, errorInfo });
}
}
class ValidateError extends Error {
constructor(gqlMessage, type, data) {
super(gqlMessage);
Object.assign(this, { gqlMessage, type, data });
}
}
const create = (errors = [], now = new Date()) => ({
quiet: () => '',
qr: () => '',
escapeJavaScript(value) {
return require('js-string-escape')(value);
},
urlEncode(value) {
return encodeURI(value);
},
urlDecode(value) {
return decodeURI(value);
},
base64Encode(value) {
// eslint-disable-next-line
return new Buffer(value).toString('base64');
},
base64Decode(value) {
// eslint-disable-next-line
return new Buffer(value, 'base64').toString('ascii');
},
parseJson(value) {
return JSON.parse(value);
},
toJson(value) {
return JSON.stringify(value);
},
autoId() {
return require('uuid/v4')();
},
unauthorized() {
const err = new Unauthorized('Unauthorized');
errors.push(err);
throw err;
},
error(message, type = null, data = null, errorInfo = null) {
const err = new TemplateSentError(message, type, data, errorInfo);
errors.push(err);
throw err;
},
appendError(message, type = null, data = null, errorInfo = null) {
errors.push(new TemplateSentError(message, type, data, errorInfo));
return '';
},
getErrors() {
return errors;
},
validate(allGood, message, type, data) {
if (allGood) return '';
throw new ValidateError(message, type, data);
},
isNull(value) {
return value === null;
},
isNullOrEmpty(value) {
return !!value;
},
isNullOrBlank(value) {
return !!value;
},
defaultIfNull(value, defaultValue = '') {
if (value !== null && value !== undefined) return value;
return defaultValue;
},
defaultIfNullOrEmpty(value, defaultValue) {
if (value) return value;
return defaultValue;
},
defaultIfNullOrBlank(value, defaultValue) {
if (value) return value;
return defaultValue;
},
isString(value) {
return typeof value === 'string';
},
isNumber(value) {
return typeof value === 'number';
},
isBoolean(value) {
return typeof value === 'boolean';
},
isList(value) {
return Array.isArray(value);
},
isMap(value) {
if (value instanceof Map) return value;
return value != null && typeof value === 'object';
},
typeOf(value) {
if (value === null) return 'Null';
if (this.isList(value)) return 'List';
if (this.isMap(value)) return 'Map';
switch (typeof value) {
case 'number':
return 'Number';
case 'string':
return 'String';
case 'boolean':
return 'Boolean';
default:
return 'Object';
}
},
matches(pattern, value) {
return new RegExp(pattern).test(value);
},
time: {
nowISO8601() {
return now.toISOString();
},
nowEpochSeconds() {
return parseInt(now.valueOf() / 1000, 10);
},
nowEpochMilliSeconds() {
return now.valueOf();
},
nowFormatted(format, timezone = null) {
if (timezone) throw new Error('no support for setting timezone!');
return require('dateformat')(now, format);
},
parseFormattedToEpochMilliSeconds() {
throw new Error('not implemented');
},
parseISO8601ToEpochMilliSeconds() {
throw new Error('not implemented');
},
epochMilliSecondsToSeconds() {
throw new Error('not implemented');
},
epochMilliSecondsToISO8601() {
throw new Error('not implemented');
},
epochMilliSecondsToFormatted() {
throw new Error('not implemented');
},
},
list: {
copyAndRetainAll(list, intersect) {
return list.filter(value => intersect.indexOf(value) !== -1);
},
copyAndRemoveAll(list, toRemove) {
return list.filter(value => toRemove.indexOf(value) === -1);
},
},
map: {
copyAndRetainAllKeys(map, keys = []) {
return Object.entries(map).reduce((sum, [key, value]) => {
if (keys.indexOf(key) === -1) return sum;
return {
...sum,
[key]: value,
};
}, {});
},
copyAndRemoveAllKeys(map, keys = []) {
const result = { ...map };
for (const key of keys) {
delete result[key];
}
return result;
},
},
dynamodb: {
toDynamoDB(value) {
const {
DynamoDB: { Converter },
} = require('aws-sdk');
return Converter.input(toJSON(value));
},
$toSet(values, fn = value => value) {
const DynamoDBSet = require('aws-sdk/lib/dynamodb/set');
return this.toDynamoDB(
new DynamoDBSet([].concat(values).map(value => fn(value))),
);
},
toDynamoDBJson(value) {
return JSON.stringify(this.toDynamoDB(value));
},
toString(value) {
return this.toDynamoDB(String(value));
},
toStringJson(value) {
return this.toDynamoDBJson(value);
},
toStringSet(value) {
return this.$toSet(value, String);
},
toStringSetJson(value) {
return JSON.stringify(this.toStringSet(value));
},
toNumber(value) {
return this.toDynamoDB(Number(value));
},
toNumberJson(value) {
return JSON.stringify(this.toNumber(value));
},
toNumberSet(value) {
return this.$toSet(value, Number);
},
toNumberSetJson(value) {
return JSON.stringify(this.toNumberSet(value));
},
toBinary(value) {
return { B: toJSON(value) };
},
toBinaryJson(value) {
// this is probably wrong.
return JSON.stringify(this.toBinary(value));
},
toBinarySet(value) {
return { BS: [].concat(value) };
},
toBinarySetJson(value) {
return JSON.stringify(this.toBinarySet(value));
},
toBoolean(value) {
return { BOOL: value };
},
toBooleanJson(value) {
return JSON.stringify(this.toBoolean(value));
},
toNull() {
return { NULL: null };
},
toNullJson() {
return JSON.stringify(this.toNull());
},
toList(value) {
return this.toDynamoDB(value);
},
toListJson(value) {
return JSON.stringify(this.toList(value));
},
toMap(value) {
// this should probably do some kind of conversion.
return this.toDynamoDB(toJSON(value));
},
toMapJson(value) {
return JSON.stringify(this.toMap(value));
},
toMapValues(values) {
return Object.entries(toJSON(values)).reduce(
(sum, [key, value]) => ({
...sum,
[key]: this.toDynamoDB(value),
}),
{},
);
},
toMapValuesJson(values) {
return JSON.stringify(this.toMapValues(values));
},
toS3ObjectJson() {
throw new Error('not implemented');
},
toS3Object() {
throw new Error('not implemented');
},
fromS3ObjectJson() {
throw new Error('not implemented');
},
},
});
const getAppSyncConfig = cfConfig => {
const { custom: { appSync: appSyncConfig } = {} } = cfConfig;
if (!Array.isArray(appSyncConfig)) {
return appSyncConfig;
}
if (appSyncConfig.length > 1) {
const apiName = appSyncConfig[0].name || 'api';
log.warn(
`Multiple API's are not supported, using first instance: ${apiName}`,
);
}
return appSyncConfig[0];
};
const flatten = arr =>
arr.reduce((acc, curr) => {
if (Array.isArray(curr)) {
return [...acc, ...flatten(curr)];
}
return [...acc, curr];
}, []);
const flatteningMappingTemplatesAndDataSources = appSync => {
// eslint-disable-next-line
appSync.mappingTemplates =
typeof appSync.mappingTemplates !== 'undefined'
? flatten(appSync.mappingTemplates)
: [];
// eslint-disable-next-line
appSync.dataSources =
typeof appSync.dataSources !== 'undefined'
? flatten(appSync.dataSources)
: [];
};
module.exports = {
create,
TemplateSentError,
Unauthorized,
ValidateError,
getAppSyncConfig,
flatteningMappingTemplatesAndDataSources,
};