@laboratoria/sdk-js 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/core.js +8 -1
- package/lib/model.js +46 -11
- package/package.json +6 -6
- package/schemas/core.json +34 -0
package/lib/core.js
CHANGED
@@ -128,8 +128,15 @@ const extended = {
|
|
128
128
|
'hoursPerWeek',
|
129
129
|
'start',
|
130
130
|
'end',
|
131
|
+
'isTrial',
|
132
|
+
'checkPilot',
|
133
|
+
'source',
|
134
|
+
],
|
135
|
+
searchProps: [
|
136
|
+
'user.firstName',
|
137
|
+
'user.lastName',
|
138
|
+
'user.email',
|
131
139
|
],
|
132
|
-
searchProps: ['user.firstName'],
|
133
140
|
getOptionLabel: ({ id, user }) => `${user?.firstName} (id: ${id})`,
|
134
141
|
},
|
135
142
|
Gig: {
|
package/lib/model.js
CHANGED
@@ -15,10 +15,10 @@ const isOptionalOneToOneRelation = (schema, key) => (
|
|
15
15
|
&& schema.properties[key].anyOf[1]?.type === 'null'
|
16
16
|
);
|
17
17
|
|
18
|
-
const isOneToOneRelation = (schema, key) => (
|
19
|
-
|
20
|
-
|
21
|
-
);
|
18
|
+
// const isOneToOneRelation = (schema, key) => (
|
19
|
+
// isRequiredOneToOneRelation(schema, key)
|
20
|
+
// || isOptionalOneToOneRelation(schema, key)
|
21
|
+
// );
|
22
22
|
|
23
23
|
const isOneToManyRelation = (schema, key) => (
|
24
24
|
schema.properties
|
@@ -123,7 +123,7 @@ const serializeData = (data, schema) => {
|
|
123
123
|
};
|
124
124
|
|
125
125
|
|
126
|
-
export const createModel = (baseUrl, state, collectionName, schema = {}) => {
|
126
|
+
export const createModel = (baseUrl, state, collectionName, schema = {}, models = {}) => {
|
127
127
|
const primaryKey = schema.primaryKey || 'id';
|
128
128
|
const validator = createValidator(schema);
|
129
129
|
const buildURL = createBuildURL(collectionName);
|
@@ -141,9 +141,40 @@ export const createModel = (baseUrl, state, collectionName, schema = {}) => {
|
|
141
141
|
};
|
142
142
|
|
143
143
|
const parse = data => {
|
144
|
+
if (!data) {
|
145
|
+
return data;
|
146
|
+
}
|
147
|
+
|
144
148
|
const parsed = Object.keys(data).reduce(
|
145
149
|
(memo, key) => {
|
146
|
-
const
|
150
|
+
const propSchema = schema.properties[key] || {};
|
151
|
+
const { type, format, items } = propSchema;
|
152
|
+
if (items && isOneToManyRelation(schema, key)) {
|
153
|
+
const relationModelName = items.$ref.split('/').pop().toLowerCase();
|
154
|
+
return {
|
155
|
+
...memo,
|
156
|
+
[key]: (
|
157
|
+
typeof models[relationModelName]?.parse === 'function'
|
158
|
+
? data[key].map(obj => models[relationModelName].parse(obj))
|
159
|
+
: data[key]
|
160
|
+
),
|
161
|
+
};
|
162
|
+
}
|
163
|
+
const ref = (
|
164
|
+
isRequiredOneToOneRelation(schema, key)
|
165
|
+
? propSchema.$ref
|
166
|
+
: isOptionalOneToOneRelation(schema, key)
|
167
|
+
? propSchema.anyOf[0]?.$ref
|
168
|
+
: null
|
169
|
+
);
|
170
|
+
|
171
|
+
if (ref) {
|
172
|
+
const relationModelName = ref.split('/').pop().toLowerCase();
|
173
|
+
return {
|
174
|
+
...memo,
|
175
|
+
[key]: models[relationModelName].parse(data[key]),
|
176
|
+
};
|
177
|
+
}
|
147
178
|
return {
|
148
179
|
...memo,
|
149
180
|
[key]: (
|
@@ -199,12 +230,13 @@ export const createModel = (baseUrl, state, collectionName, schema = {}) => {
|
|
199
230
|
get relations() {
|
200
231
|
return relations;
|
201
232
|
},
|
233
|
+
parse,
|
202
234
|
validateAttr: validator.validateAttr,
|
203
235
|
validate: validator.validate,
|
204
236
|
findMany: q => req(buildURL(null, q))
|
205
237
|
.then(results => results.map(parse)),
|
206
238
|
findById: (id, q) => req(buildURL(id, q))
|
207
|
-
.then(
|
239
|
+
.then(parse),
|
208
240
|
create,
|
209
241
|
update: put,
|
210
242
|
upsert: opts => (
|
@@ -213,27 +245,30 @@ export const createModel = (baseUrl, state, collectionName, schema = {}) => {
|
|
213
245
|
: put({ where: opts.where, data: opts.update })
|
214
246
|
),
|
215
247
|
delete: id => req(buildURL(id), { method: 'DELETE' }),
|
248
|
+
stats: () => req(buildURL('_stats')),
|
216
249
|
};
|
217
250
|
};
|
218
251
|
|
252
|
+
|
219
253
|
export const createModels = (url, state, schema) => {
|
220
254
|
return Object.keys(schema.properties).reduce(
|
221
255
|
(prev, key) => {
|
222
256
|
const name = schema.properties[key].$ref.split('/').pop();
|
223
|
-
return {
|
224
|
-
...prev,
|
257
|
+
return Object.assign(prev, {
|
225
258
|
[key]: createModel(
|
226
259
|
url,
|
227
260
|
state,
|
228
261
|
schema.definitions[name].plural || `${key}s`,
|
229
262
|
schema.definitions[name],
|
263
|
+
prev,
|
230
264
|
),
|
231
|
-
};
|
265
|
+
});
|
232
266
|
},
|
233
267
|
{},
|
234
268
|
);
|
235
269
|
};
|
236
270
|
|
271
|
+
|
237
272
|
export const extendSchemaDefinitions = (schema, extended) => {
|
238
273
|
return Object.keys(schema.definitions).reduce(
|
239
274
|
(memo, key) => ({
|
@@ -259,4 +294,4 @@ export const extendSchemaDefinitions = (schema, extended) => {
|
|
259
294
|
}),
|
260
295
|
{},
|
261
296
|
);
|
262
|
-
};
|
297
|
+
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@laboratoria/sdk-js",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.4.0",
|
4
4
|
"description": "Laboratoria JavaScript (browser) SDK",
|
5
5
|
"scripts": {
|
6
6
|
"test": "jest --verbose --coverage",
|
@@ -12,21 +12,21 @@
|
|
12
12
|
"firebase": "^9.6.6"
|
13
13
|
},
|
14
14
|
"devDependencies": {
|
15
|
-
"@babel/core": "^7.17.
|
15
|
+
"@babel/core": "^7.17.4",
|
16
16
|
"@babel/plugin-transform-modules-commonjs": "^7.16.8",
|
17
17
|
"babel-jest": "^27.5.1",
|
18
18
|
"jest": "^27.5.1",
|
19
|
-
"webpack": "^5.
|
19
|
+
"webpack": "^5.69.0",
|
20
20
|
"webpack-cli": "^4.9.2"
|
21
21
|
},
|
22
22
|
"jest": {
|
23
23
|
"testEnvironment": "jsdom",
|
24
24
|
"coverageThreshold": {
|
25
25
|
"global": {
|
26
|
-
"statements":
|
27
|
-
"branches":
|
26
|
+
"statements": 96.9,
|
27
|
+
"branches": 94,
|
28
28
|
"functions": 98,
|
29
|
-
"lines":
|
29
|
+
"lines": 96.7
|
30
30
|
}
|
31
31
|
}
|
32
32
|
}
|
package/schemas/core.json
CHANGED
@@ -123,6 +123,12 @@
|
|
123
123
|
"$ref": "#/definitions/Contract"
|
124
124
|
}
|
125
125
|
},
|
126
|
+
"contractsAsCheckPilot": {
|
127
|
+
"type": "array",
|
128
|
+
"items": {
|
129
|
+
"$ref": "#/definitions/Contract"
|
130
|
+
}
|
131
|
+
},
|
126
132
|
"gigs": {
|
127
133
|
"type": "array",
|
128
134
|
"items": {
|
@@ -488,9 +494,37 @@
|
|
488
494
|
"feeAmount": {
|
489
495
|
"type": "integer"
|
490
496
|
},
|
497
|
+
"isTrial": {
|
498
|
+
"type": "boolean",
|
499
|
+
"default": false
|
500
|
+
},
|
501
|
+
"source": {
|
502
|
+
"type": [
|
503
|
+
"string",
|
504
|
+
"null"
|
505
|
+
],
|
506
|
+
"enum": [
|
507
|
+
"graduate",
|
508
|
+
"referral",
|
509
|
+
"jazzhr",
|
510
|
+
"web",
|
511
|
+
"linkedin",
|
512
|
+
"other"
|
513
|
+
]
|
514
|
+
},
|
491
515
|
"user": {
|
492
516
|
"$ref": "#/definitions/User"
|
493
517
|
},
|
518
|
+
"checkPilot": {
|
519
|
+
"anyOf": [
|
520
|
+
{
|
521
|
+
"$ref": "#/definitions/User"
|
522
|
+
},
|
523
|
+
{
|
524
|
+
"type": "null"
|
525
|
+
}
|
526
|
+
]
|
527
|
+
},
|
494
528
|
"country": {
|
495
529
|
"$ref": "#/definitions/Country"
|
496
530
|
},
|