-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
167 lines (134 loc) · 4.24 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const R = require('ramda')
const _ = require('lodash')
const {
SchemaIsNotDefinedError,
WrongRefTypeError,
WrongFormatterError,
AttributeNotFoundError,
WrongTypeError
} = require('./errors')
const { arrayElementCount } = require('./utils')
class Serializer {
constructor () {
this.schemas = new Map([])
this.stack = []
}
add (name, schema) {
this.schemas.set(name, schema)
this[name] = (data, options) => {
this.stack = []
const serializer = this.get(name)
return serializer.serialize(data, options)
}
}
remove (name) {
return this.schemas.delete(name)
}
checkRecursion (name) {
if (arrayElementCount(this.stack, name) > 1) {
return false
}
return true
}
get (name) {
const schema = this.schemas.get(name)
this.stack.push(name)
if (!this.checkRecursion(name)) {
return {
serialize: false
}
}
if (!schema) {
throw new SchemaIsNotDefinedError(name)
}
const serialize = (data, options = {}) => {
if (!_.isObject(data) && !Array.isArray(data) && !_.isNull(data)) {
throw new WrongTypeError('data', 'object or array', data)
}
const serializeData = (data) => {
if (_.isNull(data)) {
return null
}
if (_.isFunction(data.toJSON)) {
data = data.toJSON()
}
let serialized = {}
let attributes = (Array.isArray(schema.attributes)) ? schema.attributes : Object.keys(data)
if (Array.isArray(schema.extraAttributes)) {
for (let field of schema.extraAttributes) {
if (!attributes.includes(field)) {
attributes.push(field)
}
}
}
if (Array.isArray(schema.omit)) {
attributes = attributes.filter(attribute => !schema.omit.includes(attribute))
}
if (!_.isObject(options)) {
throw new WrongTypeError('options', 'object', options)
}
if (options.omit) {
if (!Array.isArray(options.omit)) {
throw new WrongTypeError('options.omit', 'array', options.omit)
}
attributes = attributes.filter(attribute => !options.omit.includes(attribute))
}
if (options.only) {
if (!Array.isArray(options.only)) {
throw new WrongTypeError('options.only', 'array', options.only)
}
attributes = options.only
}
if (schema.formatters) {
for (let key of Object.keys(schema.formatters)) {
if (attributes.includes(key)) {
const formatter = schema.formatters[key]
if (_.isFunction(formatter)) {
serialized[key] = formatter(data, options, serialized)
} else if (_.isObject(formatter)) {
if (!_.isString(formatter['$ref'])) {
throw new WrongRefTypeError(schema[key]['$ref'])
}
const fieldSerializer = this.get(formatter['$ref'])
if (!fieldSerializer.serialize) {
continue
}
if (_.isObject(formatter.options) && formatter.options.passParent) {
formatter.options.parent = data
}
const valueToSerialize = _.isFunction(formatter.getter) ? formatter.getter(data) : data[key]
serialized[key] = fieldSerializer.serialize(valueToSerialize, formatter.options)
} else {
throw new WrongFormatterError(schema[key])
}
}
}
}
for (let key of attributes) {
if (serialized[key] === undefined) {
if (data[key] !== undefined) {
serialized[key] = data[key]
} else {
let strict = _.isBoolean(options.strict) ? options.strict : schema.strict
if (strict) {
throw new AttributeNotFoundError(key)
}
}
}
}
this.stack.pop()
return serialized
}
this.stack.pop()
if (Array.isArray(data)) {
return data.map(el => serializeData(el))
} else {
return serializeData(data)
}
}
return {
serialize
}
}
}
module.exports = Serializer