-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (51 loc) · 1.64 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
import grpc from '@grpc/grpc-js'
import Ajv from 'ajv'
import uuidPlugin from '@socketkit/ajv-uuid'
const ajv = new Ajv({ removeAdditional: true })
uuidPlugin(ajv)
export default ajv
/**
* @function addSchemas
* @description Adds JSON schemas to Mali instance
* @param {import('mali')} app Mali Instance
* @param {object} schemas Schema function declerations
* @param {object} options Ajv Mali Options
* @param {import('@grpc/grpc-js/build/src/constants').Status} options.errorCode gRPC error code
* @returns {Function} callback function
*/
export function addSchemas(
app,
schemas = {},
options = { errorCode: grpc.status.FAILED_PRECONDITION, ajv: null },
) {
const compiled = new Map()
for (const [service_name, functions] of Object.entries(schemas)) {
if (!compiled.has(service_name)) {
compiled.set(service_name, new Map())
}
for (const [function_name, function_self] of Object.entries(functions)) {
compiled.get(service_name).set(function_name, (options.ajv ?? ajv).compile(function_self))
}
}
return function (context, next) {
const service_name = context.service.toLowerCase()
const function_name = context.name
const service = compiled.get(service_name)
// Service does not exist
if (!service) {
return next()
}
const schema = service.get(function_name)
// Service schema does not include the given function
if (!schema) {
return next()
}
if (!schema(context.request.req)) {
const error = new Error((options.ajv ?? ajv).errorsText(schema.errors))
// @ts-ignore
error.code = options.errorCode
throw error
}
return next()
}
}