@kaspernj/api-maker 1.0.190 → 1.0.193

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/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  ]
17
17
  },
18
18
  "name": "@kaspernj/api-maker",
19
- "version": "1.0.190",
19
+ "version": "1.0.193",
20
20
  "description": "",
21
21
  "main": "index.js",
22
22
  "repository": {
@@ -72,6 +72,6 @@
72
72
  "eslint-plugin-react": "^7.23.2",
73
73
  "i18n-on-steroids": "^1.0.2",
74
74
  "jest": "^28.0.3",
75
- "jsdom": "^19.0.0"
75
+ "jsdom": "^20.0.0"
76
76
  }
77
77
  }
@@ -14,6 +14,17 @@ const {ValidationErrors} = require("./validation-errors.cjs")
14
14
 
15
15
  const shared = {}
16
16
 
17
+ const newCustomEvent = (validationErrors) => {
18
+ return new CustomEvent("validation-errors", {detail: validationErrors})
19
+ }
20
+
21
+ const sendValidationErrorsEvent = (validationErrors, options) => {
22
+ if (options && options.form) {
23
+ const event = newCustomEvent(validationErrors)
24
+ options.form.dispatchEvent(event)
25
+ }
26
+ }
27
+
17
28
  class BaseModel {
18
29
  static modelClassData () {
19
30
  throw new Error("modelClassData should be overriden by child")
@@ -177,7 +188,7 @@ class BaseModel {
177
188
  {}
178
189
  )
179
190
  } catch (error) {
180
- this.parseValidationErrors(error, options)
191
+ BaseModel.parseValidationErrors({error, model: this, options})
181
192
  throw error
182
193
  }
183
194
 
@@ -190,7 +201,7 @@ class BaseModel {
190
201
  }
191
202
 
192
203
  async createRaw (rawData, options = {}) {
193
- const objectData = this._objectDataFromGivenRawData(rawData, options)
204
+ const objectData = BaseModel._objectDataFromGivenRawData(rawData, options)
194
205
 
195
206
  let response
196
207
 
@@ -208,7 +219,7 @@ class BaseModel {
208
219
  {}
209
220
  )
210
221
  } catch (error) {
211
- this.parseValidationErrors(error, options)
222
+ BaseModel.parseValidationErrors({error, model: this, options})
212
223
  throw error
213
224
  }
214
225
 
@@ -284,7 +295,7 @@ class BaseModel {
284
295
  }
285
296
 
286
297
  handleResponseError (response) {
287
- this.parseValidationErrors(response)
298
+ BaseModel.parseValidationErrors({model: this, response})
288
299
  throw new new CustomError("Response wasn't successful", {model: this, response})
289
300
  }
290
301
 
@@ -299,33 +310,22 @@ class BaseModel {
299
310
  return false
300
311
  }
301
312
 
302
- parseValidationErrors (error, options) {
313
+ static parseValidationErrors ({error, model, options}) {
303
314
  if (!(error instanceof CustomError)) return
304
315
  if (!error.args.response.validation_errors) return
305
316
 
306
317
  const validationErrors = new ValidationErrors({
307
- model: this,
318
+ model,
308
319
  validationErrors: digg(error, "args", "response", "validation_errors")
309
320
  })
310
321
 
311
- this.sendValidationErrorsEvent(validationErrors, options)
322
+ sendValidationErrorsEvent(validationErrors, options)
312
323
 
313
324
  if (!options || options.throwValidationError != false) {
314
325
  throw new ValidationError(validationErrors, digg(error, "args"))
315
326
  }
316
327
  }
317
328
 
318
- sendValidationErrorsEvent (validationErrors, options) {
319
- if (options && options.form) {
320
- const event = this.newCustomEvent(validationErrors)
321
- options.form.dispatchEvent(event)
322
- }
323
- }
324
-
325
- newCustomEvent (validationErrors) {
326
- return new CustomEvent("validation-errors", {detail: validationErrors})
327
- }
328
-
329
329
  static humanAttributeName (attributeName) {
330
330
  const keyName = digg(this.modelClassData(), "i18nKey")
331
331
 
@@ -517,7 +517,7 @@ class BaseModel {
517
517
  {}
518
518
  )
519
519
  } catch (error) {
520
- this.parseValidationErrors(error, options)
520
+ BaseModel.parseValidationErrors({error, model: this, options})
521
521
  throw error
522
522
  }
523
523
 
@@ -549,7 +549,7 @@ class BaseModel {
549
549
  this.setNewModelData(newModel)
550
550
  }
551
551
 
552
- _objectDataFromGivenRawData (rawData, options) {
552
+ static _objectDataFromGivenRawData (rawData, options) {
553
553
  if (rawData instanceof FormData || rawData.nodeName == "FORM") {
554
554
  const formData = FormDataObjectizer.formDataFromObject(rawData, options)
555
555
 
@@ -560,8 +560,7 @@ class BaseModel {
560
560
  }
561
561
 
562
562
  async updateRaw (rawData, options = {}) {
563
- const objectData = this._objectDataFromGivenRawData(rawData, options)
564
-
563
+ const objectData = BaseModel._objectDataFromGivenRawData(rawData, options)
565
564
  let response
566
565
 
567
566
  try {
@@ -579,7 +578,7 @@ class BaseModel {
579
578
  {}
580
579
  )
581
580
  } catch (error) {
582
- this.parseValidationErrors(error, options)
581
+ BaseModel.parseValidationErrors({error, model: this, options})
583
582
  throw error
584
583
  }
585
584
 
@@ -636,8 +635,18 @@ class BaseModel {
636
635
  return this.uniqueKeyValue
637
636
  }
638
637
 
639
- static _callCollectionCommand (args, commandArgs) {
640
- return CommandsPool.addCommand(args, commandArgs)
638
+ static async _callCollectionCommand (args, commandArgs) {
639
+ const formOrDataObject = args.args
640
+
641
+ try {
642
+ return await CommandsPool.addCommand(args, commandArgs)
643
+ } catch (error) {
644
+ if (formOrDataObject.nodeName == "FORM") {
645
+ BaseModel.parseValidationErrors({error, options: {form: formOrDataObject}})
646
+ }
647
+
648
+ throw error
649
+ }
641
650
  }
642
651
 
643
652
  _callMemberCommand (args, commandArgs) {
@@ -61,18 +61,17 @@ module.exports = class ApiMakerCommandsPool {
61
61
 
62
62
  this.pool[id] = {resolve, reject}
63
63
 
64
- if (!this.poolData[commandType])
65
- this.poolData[commandType] = {}
66
-
67
- if (!this.poolData[commandType][collectionName])
68
- this.poolData[commandType][collectionName] = {}
69
-
70
- if (!this.poolData[commandType][collectionName][commandName])
71
- this.poolData[commandType][collectionName][commandName] = {}
64
+ if (!this.poolData[commandType]) this.poolData[commandType] = {}
65
+ if (!this.poolData[commandType][collectionName]) this.poolData[commandType][collectionName] = {}
66
+ if (!this.poolData[commandType][collectionName][commandName]) this.poolData[commandType][collectionName][commandName] = {}
72
67
 
73
68
  let args
74
69
 
75
- if (data.args instanceof FormData) {
70
+ if (data.args?.nodeName == "FORM") {
71
+ const formData = new FormData(data.args)
72
+
73
+ args = FormDataObjectizer.toObject(formData)
74
+ } else if (data.args instanceof FormData) {
76
75
  args = FormDataObjectizer.toObject(data.args)
77
76
  } else {
78
77
  args = Serializer.serialize(data.args)
@@ -30,7 +30,7 @@ module.exports = class SourceMapsLoader {
30
30
 
31
31
  if (error) sources = sources.concat(this.getSourcesFromError(error))
32
32
 
33
- return uniqunize(sources)
33
+ return uniqunize(sources, (source) => source.originalUrl)
34
34
  }
35
35
 
36
36
  async loadSourceMaps (error) {
@@ -40,11 +40,11 @@ module.exports = class SourceMapsLoader {
40
40
  const promises = []
41
41
  const sources = this.getSources(error)
42
42
 
43
- for(const src of sources) {
44
- if (src && !this.srcLoaded[src]) {
45
- this.srcLoaded[src] = true
43
+ for(const source of sources) {
44
+ if (source.originalUrl && !this.srcLoaded[source.originalUrl]) {
45
+ this.srcLoaded[source.originalUrl] = true
46
46
 
47
- const promise = this.loadSourceMapForSource(src)
47
+ const promise = this.loadSourceMapForSource(source)
48
48
  promises.push(promise)
49
49
  }
50
50
  }
@@ -60,7 +60,13 @@ module.exports = class SourceMapsLoader {
60
60
  const sources = []
61
61
 
62
62
  for (const trace of stack) {
63
- sources.push(trace.file)
63
+ const file = trace.file
64
+
65
+ if (file != "\u003Canonymous>") {
66
+ const sourceMapUrl = this.getMapURL({src: file})
67
+
68
+ if (sourceMapUrl) sources.push({originalUrl: file, sourceMapUrl})
69
+ }
64
70
  }
65
71
 
66
72
  return sources
@@ -71,40 +77,41 @@ module.exports = class SourceMapsLoader {
71
77
  const sources = []
72
78
 
73
79
  for (const script of scripts) {
74
- const src = this.loadSourceMapsForScriptTagsCallback(script)
80
+ const sourceMapUrl = this.getMapURL({script, src: script.src})
75
81
 
76
- if (src) {
77
- this.srcLoaded[src] = true
78
- sources.push(src)
79
- }
82
+ if (sourceMapUrl) sources.push({originalUrl: script.src, sourceMapUrl})
80
83
  }
81
84
 
82
85
  return sources
83
86
  }
84
87
 
85
- async loadSourceMapForSource (src) {
88
+ getMapURL({script, src}) {
86
89
  const url = this.loadUrl(src)
87
90
  const originalUrl = `${url.origin}${url.pathname}`
88
91
 
89
- let mapUrl
90
-
91
92
  if (this.sourceMapForSourceCallback) {
92
93
  // Use custom callback to resolve which map-file to download
93
- mapUrl = this.sourceMapForSourceCallback({src, url})
94
- } else {
94
+ return this.sourceMapForSourceCallback({originalUrl, script, src, url})
95
+ } else if (this.includeMapURL(src)) {
95
96
  // Default to original URL with '.map' appended
96
- mapUrl = `${originalUrl}.map`
97
+ return `${originalUrl}.map`
97
98
  }
99
+ }
100
+
101
+ includeMapURL(src) {
102
+ return src.includes("/packs/") || src.includes("/assets/")
103
+ }
98
104
 
105
+ async loadSourceMapForSource ({originalUrl, sourceMapUrl}) {
99
106
  const xhr = new XMLHttpRequest()
100
107
 
101
- xhr.open("GET", mapUrl, true)
108
+ xhr.open("GET", sourceMapUrl, true)
102
109
 
103
110
  await this.loadXhr(xhr)
104
111
 
105
112
  const consumer = await new SourceMapConsumer(xhr.responseText)
106
113
 
107
- this.sourceMaps.push({consumer, originalUrl, src})
114
+ this.sourceMaps.push({consumer, originalUrl})
108
115
  }
109
116
 
110
117
  loadUrl (url) {