@kaspernj/api-maker 1.0.126 → 1.0.129

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.
Files changed (53) hide show
  1. package/.eslintrc.js +33 -30
  2. package/__tests__/cable-connection-pool.test.js +4 -4
  3. package/__tests__/custom-error.test.js +13 -0
  4. package/__tests__/support/task.js +5 -5
  5. package/__tests__/support/user.js +5 -5
  6. package/package.json +6 -5
  7. package/src/api.cjs +15 -15
  8. package/src/base-model.cjs +82 -81
  9. package/src/cable-connection-pool.cjs +11 -11
  10. package/src/cable-subscription-pool.cjs +34 -34
  11. package/src/cable-subscription.cjs +2 -2
  12. package/src/can-can-loader.jsx +7 -7
  13. package/src/can-can.cjs +17 -16
  14. package/src/collection.cjs +36 -36
  15. package/src/command-submit-data.cjs +10 -11
  16. package/src/commands-pool.cjs +13 -13
  17. package/src/custom-error.cjs +20 -14
  18. package/src/deserializer.cjs +7 -7
  19. package/src/devise.cjs +15 -15
  20. package/src/error-logger.cjs +9 -9
  21. package/src/event-connection.jsx +6 -6
  22. package/src/event-created.jsx +8 -8
  23. package/src/event-destroyed.jsx +6 -6
  24. package/src/event-emitter-listener.jsx +6 -6
  25. package/src/event-listener.jsx +8 -8
  26. package/src/event-model-class.jsx +6 -6
  27. package/src/event-updated.jsx +10 -10
  28. package/src/instance-of-class-name.cjs +5 -7
  29. package/src/key-value-store.cjs +21 -18
  30. package/src/logger.cjs +4 -4
  31. package/src/merge.cjs +4 -4
  32. package/src/model-events.cjs +6 -5
  33. package/src/model-name.cjs +6 -5
  34. package/src/model-prop-type.cjs +11 -13
  35. package/src/model-recipes-loader.cjs +4 -4
  36. package/src/model-recipes-model-loader.cjs +39 -14
  37. package/src/models-response-reader.cjs +10 -10
  38. package/src/money-formatter.cjs +11 -11
  39. package/src/params.cjs +7 -7
  40. package/src/preloaded.cjs +6 -6
  41. package/src/resource-route.cjs +14 -12
  42. package/src/resource-routes.jsx +9 -4
  43. package/src/result.cjs +7 -7
  44. package/src/routes-native.cjs +6 -6
  45. package/src/routes.cjs +1 -1
  46. package/src/serializer.cjs +9 -9
  47. package/src/services.cjs +3 -3
  48. package/src/session-status-updater.cjs +14 -14
  49. package/src/source-maps-loader.cjs +11 -11
  50. package/src/translated-collections.cjs +2 -1
  51. package/src/updated-attribute.jsx +17 -14
  52. package/src/validation-error.cjs +5 -8
  53. package/src/validation-errors.cjs +16 -16
package/.eslintrc.js CHANGED
@@ -9,6 +9,7 @@ module.exports = {
9
9
  "plugin:react/recommended",
10
10
  "plugin:jest/recommended"
11
11
  ],
12
+ parser: "@babel/eslint-parser",
12
13
  parserOptions: {
13
14
  ecmaFeatures: {
14
15
  jsx: true
@@ -36,34 +37,35 @@ module.exports = {
36
37
  "block-scoped-var": "error",
37
38
  "block-spacing": "error",
38
39
  "brace-style": "error",
39
- "class-methods-use-this": "error",
40
+ "class-methods-use-this": "off",
40
41
  "comma-dangle": "error",
41
42
  "comma-spacing": "error",
42
43
  "comma-style": "error",
43
44
  "complexity": "error",
44
45
  "computed-property-spacing": "error",
45
- "consistent-return": "error",
46
+ "consistent-return": "off",
46
47
  "consistent-this": "error",
47
- "curly": ["error", "multi-or-nest"],
48
+ "curly": "off",
48
49
  "default-case": "error",
49
50
  "default-case-last": "error",
50
51
  "default-param-last": "error",
51
52
  "dot-location": ["error", "property"],
52
53
  "dot-notation": "error",
53
54
  "eol-last": "error",
54
- "eqeqeq": "error",
55
+ "eqeqeq": "off",
56
+ "function-call-argument-newline": "off",
55
57
  "func-call-spacing": "error",
56
58
  "func-name-matching": "error",
57
59
  "func-style": "error",
58
60
  "function-paren-newline": "error",
59
61
  "generator-star-spacing": "error",
60
62
  "grouped-accessor-pairs": "error",
61
- "guard-for-in": "error",
63
+ "guard-for-in": "off", // Disabled because insane?
62
64
  "id-denylist": "error",
63
65
  "id-match": "error",
64
66
  "implicit-arrow-linebreak": "error",
65
67
  "indent": ["error", 2, {"MemberExpression": "off"}],
66
- "init-declarations": "error",
68
+ "init-declarations": "off",
67
69
  "jest/max-nested-describe": "error",
68
70
  "jest/no-alias-methods": "error",
69
71
  "jest/no-duplicate-hooks": "error",
@@ -85,12 +87,12 @@ module.exports = {
85
87
  "jest/unbound-method": "error",
86
88
  "jsx-quotes": "error",
87
89
  "key-spacing": "error",
88
- "keyword-spacing": "error",
90
+ "keyword-spacing": "off",
89
91
  "linebreak-style": ["error", "unix"],
90
92
  "line-comment-position": "off",
91
93
  "lines-around-comment": "error",
92
- "lines-between-class-members": "error",
93
- "max-classes-per-file": "error",
94
+ "lines-between-class-members": "off",
95
+ "max-classes-per-file": "off",
94
96
  "max-depth": "error",
95
97
  "max-nested-callbacks": "error",
96
98
  "max-params": "error",
@@ -109,7 +111,7 @@ module.exports = {
109
111
  "no-continue": "error",
110
112
  "no-div-regex": "error",
111
113
  "no-duplicate-imports": "error",
112
- "no-else-return": "error",
114
+ "no-else-return": "off",
113
115
  "no-eq-null": "error",
114
116
  "no-eval": "error",
115
117
  "no-extra-bind": "error",
@@ -142,7 +144,7 @@ module.exports = {
142
144
  "no-nonoctal-decimal-escape": "error",
143
145
  "no-octal-escape": "error",
144
146
  "no-param-reassign": "error",
145
- "no-plusplus": "error",
147
+ "no-plusplus": "off",
146
148
  "no-promise-executor-return": "error",
147
149
  "no-proto": "error",
148
150
  "no-restricted-exports": "error",
@@ -167,6 +169,7 @@ module.exports = {
167
169
  "no-unused-private-class-members": "error",
168
170
  "no-unsafe-optional-chaining": "error",
169
171
  "no-unused-expressions": "error",
172
+ "no-unused-vars": ["error", {"argsIgnorePattern": "^_", "ignoreRestSiblings": true}],
170
173
  "no-use-before-define": "error",
171
174
  "no-useless-backreference": "error",
172
175
  "no-useless-call": "error",
@@ -178,14 +181,14 @@ module.exports = {
178
181
  "no-void": "error",
179
182
  "no-warning-comments": "off",
180
183
  "no-whitespace-before-property": "error",
181
- "nonblock-statement-body-position": "error",
184
+ "nonblock-statement-body-position": ["error", "any"],
182
185
  "object-curly-newline": "error",
183
186
  "object-curly-spacing": "error",
184
187
  "object-shorthand": "error",
185
188
  "one-var-declaration-per-line": "error",
186
189
  "operator-assignment": "error",
187
190
  "operator-linebreak": "error",
188
- "padded-blocks": "off",
191
+ "padded-blocks": ["error", "never"],
189
192
  "padding-line-between-statements": "error",
190
193
  "prefer-arrow-callback": "error",
191
194
  "prefer-exponentiation-operator": "error",
@@ -201,13 +204,13 @@ module.exports = {
201
204
  "radix": "error",
202
205
  "react/boolean-prop-naming": "error",
203
206
  "react/button-has-type": "error",
204
- "react/default-props-match-prop-types": "error",
205
- "react/destructuring-assignment": "error",
206
- "react/forbid-component-props": "error",
207
+ "react/default-props-match-prop-types": "off",
208
+ "react/destructuring-assignment": "off",
209
+ "react/forbid-component-props": "off",
207
210
  "react/forbid-dom-props": "error",
208
211
  "react/forbid-elements": "error",
209
212
  "react/forbid-foreign-prop-types": "error",
210
- "react/forbid-prop-types": "error",
213
+ "react/forbid-prop-types": "off",
211
214
  "react/function-component-definition": "error",
212
215
  "react/jsx-boolean-value": "error",
213
216
  "react/jsx-child-element-spacing": "error",
@@ -220,12 +223,12 @@ module.exports = {
220
223
  "react/jsx-filename-extension": "error",
221
224
  "react/jsx-first-prop-new-line": "error",
222
225
  "react/jsx-fragments": "error",
223
- "react/jsx-handler-names": "error",
224
- "react/jsx-indent": "error",
225
- "react/jsx-indent-props": "error",
226
+ "react/jsx-handler-names": "off",
227
+ "react/jsx-indent": ["error", 2],
228
+ "react/jsx-indent-props": ["error", 2],
226
229
  "react/jsx-max-depth": "error",
227
230
  "react/jsx-max-props-per-line": "error",
228
- "react/jsx-newline": "error",
231
+ "react/jsx-newline": "off",
229
232
  "react/jsx-no-bind": "error",
230
233
  "react/jsx-no-constructed-context-values": "error",
231
234
  "react/jsx-no-literals": "error",
@@ -234,7 +237,7 @@ module.exports = {
234
237
  "react/jsx-one-expression-per-line": "error",
235
238
  "react/jsx-pascal-case": "error",
236
239
  "react/jsx-props-no-multi-spaces": "error",
237
- "react/jsx-props-no-spreading": "error",
240
+ "react/jsx-props-no-spreading": "off",
238
241
  "react/jsx-sort-default-props": "error",
239
242
  "react/jsx-sort-props": "error",
240
243
  "react/jsx-tag-spacing": "error",
@@ -250,11 +253,11 @@ module.exports = {
250
253
  "react/no-multi-comp": "error",
251
254
  "react/no-namespace": "error",
252
255
  "react/no-redundant-should-component-update": "error",
253
- "react/no-set-state": "error",
256
+ "react/no-set-state": "off",
254
257
  "react/no-this-in-sfc": "error",
255
258
  "react/no-typos": "error",
256
259
  "react/no-unstable-nested-components": "error",
257
- "react/no-unused-class-component-methods": "error",
260
+ "react/no-unused-class-component-methods": "off",
258
261
  "react/no-unused-prop-types": "error",
259
262
  "react/no-unused-state": "error",
260
263
  "react/no-will-update-set-state": "error",
@@ -262,12 +265,13 @@ module.exports = {
262
265
  "react/prefer-exact-props": "error",
263
266
  "react/prefer-read-only-props": "error",
264
267
  "react/prefer-stateless-function": "error",
265
- "react/require-default-props": "error",
268
+ "react/prop-types": "off", // Disabled because it doesn't work with propTypesExact
269
+ "react/require-default-props": "off",
266
270
  "react/require-optimization": "error",
267
271
  "react/self-closing-comp": "error",
268
- "react/sort-comp": "error",
272
+ "react/sort-comp": "off",
269
273
  "react/sort-prop-types": "error",
270
- "react/state-in-constructor": "error",
274
+ "react/state-in-constructor": "off",
271
275
  "react/static-property-placement": "error",
272
276
  "react/style-prop-object": "error",
273
277
  "react/void-dom-elements-no-children": "error",
@@ -279,6 +283,7 @@ module.exports = {
279
283
  "sort-imports": "error",
280
284
  "sort-vars": "error",
281
285
  "space-before-blocks": "error",
286
+ "space-before-function-paren": "error",
282
287
  "space-in-parens": "error",
283
288
  "space-infix-ops": "error",
284
289
  "space-unary-ops": "error",
@@ -299,7 +304,6 @@ module.exports = {
299
304
  "camelcase": "off",
300
305
  "capitalized-comments": "off",
301
306
  "func-names": "off",
302
- "function-call-argument-newline": "off",
303
307
  "id-length": "off",
304
308
  "jest/consistent-test-it": "off",
305
309
  "jest/prefer-called-with": "off",
@@ -307,7 +311,7 @@ module.exports = {
307
311
  "jest/prefer-strict-equal": "off",
308
312
  "no-shadow": "off",
309
313
  "jest/prefer-to-have-length": "off",
310
- "max-len": "off",
314
+ "max-len": ["error", {"code": 160}],
311
315
  "max-lines": "off",
312
316
  "max-lines-per-function": "off",
313
317
  "max-statements": "off",
@@ -327,7 +331,6 @@ module.exports = {
327
331
  "require-await": "off",
328
332
  "require-unicode-regexp": "off",
329
333
  "sort-keys": "off",
330
- "space-before-function-paren": "off",
331
334
  "spaced-comment": "off"
332
335
  }
333
336
  }
@@ -75,7 +75,7 @@ describe("CableConnectionPool", () => {
75
75
  const cableSubscriptionPool = new CableSubscriptionPool()
76
76
 
77
77
  cableSubscriptionPool.connected = true
78
- cableSubscriptionPool.connectUnsubscriptionForSubscription = function() {
78
+ cableSubscriptionPool.connectUnsubscriptionForSubscription = function () {
79
79
  connectedUnsubscribeEvent = true
80
80
  }
81
81
  cableSubscriptionPool.subscriptions = {
@@ -109,7 +109,7 @@ describe("CableConnectionPool", () => {
109
109
  }
110
110
 
111
111
  cableConnectionPool.connected = false
112
- cableConnectionPool.connectUpcoming = function() {
112
+ cableConnectionPool.connectUpcoming = function () {
113
113
  const subscriptionData = this.upcomingSubscriptionData
114
114
  const subscriptions = this.upcomingSubscriptions
115
115
 
@@ -173,7 +173,7 @@ describe("CableConnectionPool", () => {
173
173
  const cableSubscriptionPool = new CableSubscriptionPool()
174
174
 
175
175
  cableSubscriptionPool.connected = true
176
- cableSubscriptionPool.connectUnsubscriptionForSubscription = function() {
176
+ cableSubscriptionPool.connectUnsubscriptionForSubscription = function () {
177
177
  connectedUnsubscribeEvent = true
178
178
  }
179
179
  cableSubscriptionPool.subscriptions = {
@@ -201,7 +201,7 @@ describe("CableConnectionPool", () => {
201
201
  const cableSubscriptionPool = new CableSubscriptionPool()
202
202
 
203
203
  cableSubscriptionPool.connected = true
204
- cableSubscriptionPool.connectUnsubscriptionForSubscription = function() {
204
+ cableSubscriptionPool.connectUnsubscriptionForSubscription = function () {
205
205
  connectedUnsubscribeEvent = true
206
206
  }
207
207
  cableSubscriptionPool.subscriptions = {
@@ -0,0 +1,13 @@
1
+ const CustomError = require("../src/custom-error.cjs")
2
+
3
+ describe("CustomError", () => {
4
+ it("handles when response is a string", () => {
5
+ const xhr = {status: 401}
6
+ const response = "An error happened"
7
+ const customError = new CustomError(`Request failed with code: ${xhr.status}`, {response, xhr})
8
+
9
+ expect(customError.message).toEqual("Request failed with code: 401")
10
+ expect(customError.errorMessages()).toEqual(undefined)
11
+ expect(customError.errorTypes()).toEqual(undefined)
12
+ })
13
+ })
@@ -1,27 +1,27 @@
1
1
  module.exports = class Task {
2
- static modelClassData() {
2
+ static modelClassData () {
3
3
  return {
4
4
  attributes: [],
5
5
  name: "Task"
6
6
  }
7
7
  }
8
8
 
9
- static modelName() {
9
+ static modelName () {
10
10
  return "Task"
11
11
  }
12
12
 
13
- constructor({a, b, isNewRecord = false, r} = {}) {
13
+ constructor ({a, b, isNewRecord = false, r} = {}) {
14
14
  this.abilities = b
15
15
  this._isNewRecord = isNewRecord
16
16
  this.modelData = a
17
17
  this.relationshipsCache = r
18
18
  }
19
19
 
20
- isNewRecord() {
20
+ isNewRecord () {
21
21
  return this._isNewRecord
22
22
  }
23
23
 
24
- isPersisted() {
24
+ isPersisted () {
25
25
  return !this._isNewRecord
26
26
  }
27
27
  }
@@ -1,7 +1,7 @@
1
1
  const BaseModel = require("../../src/base-model.cjs")
2
2
 
3
3
  module.exports = class User extends BaseModel {
4
- static modelClassData() {
4
+ static modelClassData () {
5
5
  return {
6
6
  attributes: [],
7
7
  name: "User",
@@ -9,11 +9,11 @@ module.exports = class User extends BaseModel {
9
9
  }
10
10
  }
11
11
 
12
- static modelName() {
12
+ static modelName () {
13
13
  return "User"
14
14
  }
15
15
 
16
- constructor({a, b, isNewRecord = false, r}) {
16
+ constructor ({a, b, isNewRecord = false, r}) {
17
17
  super({a})
18
18
 
19
19
  this.abilities = b
@@ -22,11 +22,11 @@ module.exports = class User extends BaseModel {
22
22
  this.relationshipsCache = r
23
23
  }
24
24
 
25
- isNewRecord() {
25
+ isNewRecord () {
26
26
  return this._isNewRecord
27
27
  }
28
28
 
29
- isPersisted() {
29
+ isPersisted () {
30
30
  return !this._isNewRecord
31
31
  }
32
32
  }
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  ]
17
17
  },
18
18
  "name": "@kaspernj/api-maker",
19
- "version": "1.0.126",
19
+ "version": "1.0.129",
20
20
  "description": "",
21
21
  "main": "index.js",
22
22
  "repository": {
@@ -30,7 +30,7 @@
30
30
  },
31
31
  "homepage": "https://github.com/kaspernj/api_maker",
32
32
  "scripts": {
33
- "eslint": "eslint",
33
+ "eslint": "eslint --ext .cjs,.js,.jsx",
34
34
  "eslint-find-rules": "eslint-find-rules",
35
35
  "test": "jest"
36
36
  },
@@ -44,22 +44,23 @@
44
44
  "stacktrace-parser": ">= 0.1.9"
45
45
  },
46
46
  "devDependencies": {
47
+ "@babel/eslint-parser": "^7.16.3",
47
48
  "@babel/preset-env": "^7.12.11",
48
49
  "@babel/preset-react": "^7.12.10",
49
50
  "babel-jest": "^27.0.6",
50
51
  "eslint": "^8.2.0",
51
52
  "eslint-find-rules": "^4.0.0",
52
- "eslint-plugin-jest": "^25.2.4",
53
+ "eslint-plugin-jest": "^26.0.0",
53
54
  "eslint-plugin-react": "^7.23.2",
54
55
  "i18n-on-steroids": "^1.0.2",
55
56
  "jest": "^27.0.6",
56
- "jsdom": "^18.0.0"
57
+ "jsdom": "^19.0.0"
57
58
  },
58
59
  "dependencies": {
59
60
  "@rails/actioncable": ">= 6.1.0",
60
61
  "clone-deep": ">= 4.0.1",
61
62
  "debounce": ">= 1.2.1",
62
- "diggerize": ">= 1.0.3",
63
+ "diggerize": ">= 1.0.4",
63
64
  "epic-locks": ">= 1.0.1",
64
65
  "form-data-objectizer": ">= 1.0.0",
65
66
  "form-serialize": ">= 0.7.2",
package/src/api.cjs CHANGED
@@ -3,23 +3,23 @@ const FormDataObjectizer = require("form-data-objectizer")
3
3
  const qs = require("qs")
4
4
 
5
5
  module.exports = class Api {
6
- static get(path, pathParams = null) {
6
+ static get (path, pathParams = null) {
7
7
  return this.requestLocal({path, pathParams, method: "GET"})
8
8
  }
9
9
 
10
- static delete(path, pathParams = null) {
10
+ static delete (path, pathParams = null) {
11
11
  return this.requestLocal({path, pathParams, method: "DELETE"})
12
12
  }
13
13
 
14
- static patch(path, data = {}) {
14
+ static patch (path, data = {}) {
15
15
  return this.requestLocal({path, data, method: "PATCH"})
16
16
  }
17
17
 
18
- static post(path, data = {}) {
18
+ static post (path, data = {}) {
19
19
  return this.requestLocal({path, data, method: "POST"})
20
20
  }
21
21
 
22
- static request(args) {
22
+ static request (args) {
23
23
  let path = args.path
24
24
 
25
25
  if (args.pathParams) {
@@ -32,7 +32,7 @@ module.exports = class Api {
32
32
  xhr.open(args.method, path, true)
33
33
 
34
34
  if (args.headers) {
35
- for(const headerName in args.headers) {
35
+ for (const headerName in args.headers) {
36
36
  xhr.setRequestHeader(headerName, args.headers[headerName])
37
37
  }
38
38
  }
@@ -59,41 +59,41 @@ module.exports = class Api {
59
59
  })
60
60
  }
61
61
 
62
- static requestLocal(args) {
62
+ static requestLocal (args) {
63
63
  if (!args.headers) {
64
- args["headers"] = {}
64
+ args.headers = {}
65
65
  }
66
66
 
67
67
  const token = this._token()
68
68
 
69
69
  if (token) {
70
- args["headers"]["X-CSRF-Token"] = token
70
+ args.headers["X-CSRF-Token"] = token
71
71
  }
72
72
 
73
73
  if (args.data) {
74
- args["headers"]["Content-Type"] = "application/json"
75
- args["data"] = JSON.stringify(args.data)
74
+ args.headers["Content-Type"] = "application/json"
75
+ args.data = JSON.stringify(args.data)
76
76
  }
77
77
 
78
78
  if (args.rawData) {
79
- args["data"] = args.rawData
79
+ args.data = args.rawData
80
80
  }
81
81
 
82
82
  return this.request(args)
83
83
  }
84
84
 
85
- static put(path, data = {}) {
85
+ static put (path, data = {}) {
86
86
  return this.requestLocal({path, data, method: "PUT"})
87
87
  }
88
88
 
89
- static _token() {
89
+ static _token () {
90
90
  const tokenElement = document.querySelector("meta[name='csrf-token']")
91
91
 
92
92
  if (tokenElement)
93
93
  return tokenElement.getAttribute("content")
94
94
  }
95
95
 
96
- static _parseResponse(xhr) {
96
+ static _parseResponse (xhr) {
97
97
  const responseType = xhr.getResponseHeader("content-type")
98
98
 
99
99
  if (responseType && responseType.startsWith("application/json")) {