@laboratoria/sdk-js 3.2.1 → 3.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 CHANGED
@@ -39,6 +39,9 @@ const extended = {
39
39
  email: {
40
40
  inputType: 'email',
41
41
  },
42
+ mobileNumber: {
43
+ inputType: 'phone',
44
+ },
42
45
  bio: {
43
46
  inputType: 'textarea',
44
47
  },
@@ -179,8 +182,8 @@ const extended = {
179
182
  'householdIncome',
180
183
  'educationLevel',
181
184
  'educationSubject',
185
+ 'university',
182
186
  'isStudying',
183
- 'isDegreeFromListedInstitutions',
184
187
  'workStatus',
185
188
  'isLookingForWork',
186
189
  'monthsLookingForWork',
@@ -189,9 +192,13 @@ const extended = {
189
192
  'workFormal',
190
193
  'workIncome',
191
194
  'isReady',
192
- // 'isReturningApplicant',
193
- // 'legacyAdmissionCohortSlug',
195
+ 'bypassFilters',
194
196
  ],
197
+ properties: {
198
+ locality: {
199
+ required: true,
200
+ },
201
+ },
195
202
  },
196
203
  IscedLevel: {
197
204
  parse: (props) => {
@@ -208,6 +215,10 @@ const extended = {
208
215
  'label',
209
216
  ],
210
217
  },
218
+ University: {
219
+ plural: 'universities',
220
+ searchProps: ['name'],
221
+ },
211
222
  };
212
223
 
213
224
  export const createAPI = (url, state) => ({
package/lib/model.js CHANGED
@@ -39,43 +39,38 @@ const createValidator = (schema) => {
39
39
 
40
40
  const validateAttr = (key, value) => {
41
41
  const attrSchema = properties[key] || {};
42
- const type = (
43
- Array.isArray(attrSchema.type)
44
- ? attrSchema.type[0]
45
- : attrSchema.type
46
- );
47
- const isRequired = (
48
- !Array.isArray(attrSchema.type)
49
- || !attrSchema.type.includes('null')
50
- );
51
42
 
52
43
  if (attrSchema.enum) {
53
- if (!isRequired && !value) {
44
+ if (!attrSchema.required && !value) {
54
45
  return;
55
46
  }
56
47
  if (!attrSchema.enum.includes(value)) {
57
- return `${value} is not one of: ${attrSchema.enum.join(',')}`;
48
+ return {
49
+ id: 'enum-validation-error',
50
+ values: { enum: attrSchema.enum, value },
51
+ };
58
52
  }
59
53
  }
60
54
 
61
- switch (type) {
62
- case 'string':
63
- if (isRequired && !value) {
64
- return 'Field is required';
65
- }
66
- break;
55
+ switch (attrSchema.$type) {
67
56
  case 'integer':
68
57
  const parsedValue = parseInt(value, 10);
69
- if (isRequired && Number.isNaN(parsedValue)) {
70
- return 'Invalid number format';
58
+ if (attrSchema.required && Number.isNaN(parsedValue)) {
59
+ return { id: 'invalid-number-validation-error' };
71
60
  }
72
61
  break;
73
62
  case 'date':
74
63
  break;
75
64
  case 'boolean':
76
65
  break;
66
+ case 'json':
67
+ case 'string':
68
+ case 'ref':
77
69
  default:
78
- // ...
70
+ if (attrSchema.required && !value) {
71
+ return { id: 'required-validation-error' };
72
+ }
73
+ break;
79
74
  }
80
75
  };
81
76
 
@@ -138,6 +133,54 @@ const serializeData = (data, schema) => {
138
133
  };
139
134
 
140
135
 
136
+ const parseSchema = ({ primaryKey, properties, ...rest }) => {
137
+ return {
138
+ primaryKey: primaryKey || 'id',
139
+ properties: Object.keys(properties || {}).reduce(
140
+ (memo, key) => {
141
+ const propSchema = properties[key] || {};
142
+ const $type = (
143
+ !propSchema.type
144
+ ? propSchema.$ref
145
+ ? 'ref'
146
+ : undefined
147
+ : !Array.isArray(propSchema.type)
148
+ ? propSchema.type
149
+ : propSchema.type.length < 3
150
+ ? propSchema.type[0]
151
+ : 'json'
152
+ );
153
+ const inputType = propSchema.inputType || (
154
+ $type === 'integer'
155
+ ? 'number'
156
+ : $type === 'string' && propSchema.format === 'date-time'
157
+ ? 'date'
158
+ : $type
159
+ );
160
+ return {
161
+ ...memo,
162
+ [key]: {
163
+ ...properties[key],
164
+ $type,
165
+ inputType,
166
+ required: (
167
+ typeof propSchema.required === 'boolean'
168
+ ? propSchema.required
169
+ : (
170
+ !Array.isArray(propSchema.type)
171
+ || !propSchema.type.includes('null')
172
+ )
173
+ ),
174
+ },
175
+ };
176
+ },
177
+ {},
178
+ ),
179
+ ...rest,
180
+ };
181
+ };
182
+
183
+
141
184
  export const createModel = (
142
185
  baseUrl,
143
186
  state,
@@ -145,19 +188,20 @@ export const createModel = (
145
188
  schema = {},
146
189
  models = {},
147
190
  ) => {
148
- const primaryKey = schema.primaryKey || 'id';
149
- const validator = createValidator(schema);
191
+ const parsedSchema = parseSchema(schema);
192
+ const { primaryKey, properties } = parsedSchema;
193
+ const validator = createValidator(parsedSchema);
150
194
  const buildURL = createBuildURL(collectionName);
151
195
  const req = (...args) => createClient(baseUrl, state.authUser)(...args);
152
196
  const create = ({ data, ...q }) => req(buildURL(null, q), {
153
197
  method: 'POST',
154
- body: serializeData(data, schema),
198
+ body: serializeData(data, parsedSchema),
155
199
  });
156
200
  const put = ({ data, ...q }) => {
157
201
  const { where, ...rest } = q;
158
202
  return req(buildURL(where[primaryKey], rest), {
159
203
  method: 'PUT',
160
- body: serializeData(data, schema),
204
+ body: serializeData(data, parsedSchema),
161
205
  });
162
206
  };
163
207
 
@@ -168,9 +212,9 @@ export const createModel = (
168
212
 
169
213
  const parsed = Object.keys(data).reduce(
170
214
  (memo, key) => {
171
- const propSchema = schema.properties[key] || {};
215
+ const propSchema = properties[key] || {};
172
216
  const { type, format, items } = propSchema;
173
- if (items && isOneToManyRelation(schema, key)) {
217
+ if (items && isOneToManyRelation(parsedSchema, key)) {
174
218
  const relationModelName = lcFirst(items.$ref.split('/').pop());
175
219
  return {
176
220
  ...memo,
@@ -182,9 +226,9 @@ export const createModel = (
182
226
  };
183
227
  }
184
228
  const ref = (
185
- isRequiredOneToOneRelation(schema, key)
229
+ isRequiredOneToOneRelation(parsedSchema, key)
186
230
  ? propSchema.$ref
187
- : isOptionalOneToOneRelation(schema, key)
231
+ : isOptionalOneToOneRelation(parsedSchema, key)
188
232
  ? propSchema.anyOf[0]?.$ref
189
233
  : null
190
234
  );
@@ -208,27 +252,27 @@ export const createModel = (
208
252
  {},
209
253
  );
210
254
  return (
211
- typeof schema.parse === 'function'
212
- ? schema.parse(parsed)
255
+ typeof parsedSchema.parse === 'function'
256
+ ? parsedSchema.parse(parsed)
213
257
  : parsed
214
258
  );
215
259
  };
216
260
 
217
- const relations = Object.keys(schema.properties || {}).reduce(
261
+ const relations = Object.keys(properties || {}).reduce(
218
262
  (memo, key) => (
219
- isRequiredOneToOneRelation(schema, key)
263
+ isRequiredOneToOneRelation(parsedSchema, key)
220
264
  ? Object.assign(memo, {
221
265
  all: memo.all.concat(key),
222
266
  oneToOne: memo.oneToOne.concat(key),
223
267
  requiredOneToOne: memo.requiredOneToOne.concat(key),
224
268
  })
225
- : isOptionalOneToOneRelation(schema, key)
269
+ : isOptionalOneToOneRelation(parsedSchema, key)
226
270
  ? Object.assign(memo, {
227
271
  all: memo.all.concat(key),
228
272
  oneToOne: memo.oneToOne.concat(key),
229
273
  optionalOneToOne: memo.optionalOneToOne.concat(key),
230
274
  })
231
- : isOneToManyRelation(schema, key)
275
+ : isOneToManyRelation(parsedSchema, key)
232
276
  ? Object.assign(memo, {
233
277
  all: memo.all.concat(key),
234
278
  oneToMany: memo.oneToMany.concat(key),
@@ -246,7 +290,7 @@ export const createModel = (
246
290
 
247
291
  return {
248
292
  get schema() {
249
- return { ...schema, primaryKey };
293
+ return parsedSchema;
250
294
  },
251
295
  get relations() {
252
296
  return relations;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@laboratoria/sdk-js",
3
- "version": "3.2.1",
3
+ "version": "3.4.0",
4
4
  "description": "Laboratoria JavaScript (browser) SDK",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -11,14 +11,14 @@
11
11
  },
12
12
  "dependencies": {
13
13
  "blueimp-md5": "^2.19.0",
14
- "firebase": "^9.16.0"
14
+ "firebase": "^9.17.1"
15
15
  },
16
16
  "devDependencies": {
17
17
  "@babel/core": "^7.20.12",
18
18
  "@babel/plugin-transform-modules-commonjs": "^7.20.11",
19
- "babel-jest": "^29.4.0",
20
- "jest": "^29.4.0",
21
- "jest-environment-jsdom": "^29.4.0",
19
+ "babel-jest": "^29.4.2",
20
+ "jest": "^29.4.2",
21
+ "jest-environment-jsdom": "^29.4.2",
22
22
  "webpack": "^5.75.0",
23
23
  "webpack-cli": "^5.0.1"
24
24
  },
package/schemas/core.json CHANGED
@@ -645,6 +645,45 @@
645
645
  }
646
646
  }
647
647
  },
648
+ "University": {
649
+ "type": "object",
650
+ "properties": {
651
+ "id": {
652
+ "type": "integer"
653
+ },
654
+ "createdAt": {
655
+ "type": "string",
656
+ "format": "date-time"
657
+ },
658
+ "updatedAt": {
659
+ "type": "string",
660
+ "format": "date-time"
661
+ },
662
+ "countryCode": {
663
+ "type": "string"
664
+ },
665
+ "lang": {
666
+ "type": "string",
667
+ "enum": [
668
+ "en",
669
+ "es",
670
+ "pt"
671
+ ]
672
+ },
673
+ "name": {
674
+ "type": "string"
675
+ },
676
+ "filter": {
677
+ "type": "boolean"
678
+ },
679
+ "applications": {
680
+ "type": "array",
681
+ "items": {
682
+ "$ref": "#/definitions/Application"
683
+ }
684
+ }
685
+ }
686
+ },
648
687
  "Application": {
649
688
  "type": "object",
650
689
  "properties": {
@@ -718,9 +757,6 @@
718
757
  "isStudying": {
719
758
  "type": "boolean"
720
759
  },
721
- "isDegreeFromListedInstitutions": {
722
- "type": "boolean"
723
- },
724
760
  "workStatus": {
725
761
  "type": "string",
726
762
  "enum": [
@@ -781,6 +817,16 @@
781
817
  "null"
782
818
  ]
783
819
  },
820
+ "failedFilters": {
821
+ "type": "array",
822
+ "items": {
823
+ "type": "string"
824
+ }
825
+ },
826
+ "bypassFilters": {
827
+ "type": "boolean",
828
+ "default": false
829
+ },
784
830
  "isReady": {
785
831
  "type": "boolean",
786
832
  "default": false
@@ -812,6 +858,16 @@
812
858
  "educationLevel": {
813
859
  "$ref": "#/definitions/EducationLevel"
814
860
  },
861
+ "university": {
862
+ "anyOf": [
863
+ {
864
+ "$ref": "#/definitions/University"
865
+ },
866
+ {
867
+ "type": "null"
868
+ }
869
+ ]
870
+ },
815
871
  "challengeAssignments": {
816
872
  "type": "array",
817
873
  "items": {
@@ -911,7 +967,8 @@
911
967
  "type": {
912
968
  "type": "string",
913
969
  "enum": [
914
- "challenge"
970
+ "challenge",
971
+ "application_filter"
915
972
  ]
916
973
  },
917
974
  "data": {
@@ -1208,6 +1265,9 @@
1208
1265
  "educationLevel": {
1209
1266
  "$ref": "#/definitions/EducationLevel"
1210
1267
  },
1268
+ "university": {
1269
+ "$ref": "#/definitions/University"
1270
+ },
1211
1271
  "application": {
1212
1272
  "$ref": "#/definitions/Application"
1213
1273
  },