@kaspernj/api-maker 1.0.347 → 1.0.349

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.347",
19
+ "version": "1.0.349",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -52,7 +52,7 @@
52
52
  "on-location-changed": ">= 1.0.10",
53
53
  "qs": ">= 6.9.3",
54
54
  "replaceall": ">= 0.1.6",
55
- "set-state-compare": ">= 1.0.28",
55
+ "set-state-compare": ">= 1.0.30",
56
56
  "spark-md5": "^3.0.2",
57
57
  "strftime": ">= 0.10.0",
58
58
  "uniqunize": "^1.0.1",
package/src/api.mjs CHANGED
@@ -1,9 +1,14 @@
1
1
  import config from "./config.mjs"
2
2
  import CustomError from "./custom-error.mjs"
3
3
  import FormDataObjectizer from "form-data-objectizer"
4
+ import Logger from "./logger.mjs"
4
5
  import qs from "qs"
5
6
  import SessionStatusUpdater from "./session-status-updater.mjs"
6
7
 
8
+ const logger = new Logger({name: "ApiMaker / Api"})
9
+
10
+ // logger.setDebug(true)
11
+
7
12
  export default class Api {
8
13
  static get(path, pathParams = null) {
9
14
  return Api.requestLocal({path, pathParams, method: "GET"})
@@ -70,6 +75,8 @@ export default class Api {
70
75
 
71
76
  const token = await this._token()
72
77
 
78
+ logger.debug(() => `Got token: ${token}`)
79
+
73
80
  if (token) {
74
81
  args.headers["X-CSRF-Token"] = token
75
82
  }
@@ -14,7 +14,7 @@ import {ValidationErrors} from "./validation-errors.mjs"
14
14
  const shared = {}
15
15
 
16
16
  export default class ApiMakerCommandsPool {
17
- static addCommand (data, args = {}) {
17
+ static addCommand(data, args = {}) {
18
18
  let pool
19
19
 
20
20
  if (args.instant) {
@@ -34,17 +34,17 @@ export default class ApiMakerCommandsPool {
34
34
  return promiseResult
35
35
  }
36
36
 
37
- static current () {
37
+ static current() {
38
38
  if (!shared.currentApiMakerCommandsPool) shared.currentApiMakerCommandsPool = new ApiMakerCommandsPool()
39
39
 
40
40
  return shared.currentApiMakerCommandsPool
41
41
  }
42
42
 
43
- static flush () {
43
+ static flush() {
44
44
  ApiMakerCommandsPool.current().flush()
45
45
  }
46
46
 
47
- constructor () {
47
+ constructor() {
48
48
  this.flushCount = 0
49
49
  this.pool = {}
50
50
  this.poolData = {}
@@ -52,7 +52,7 @@ export default class ApiMakerCommandsPool {
52
52
  this.globalRequestData = {}
53
53
  }
54
54
 
55
- addCommand (data) {
55
+ addCommand(data) {
56
56
  return new Promise((resolve, reject) => {
57
57
  const id = this.currentId
58
58
  this.currentId += 1
@@ -87,11 +87,11 @@ export default class ApiMakerCommandsPool {
87
87
  })
88
88
  }
89
89
 
90
- commandsCount () {
90
+ commandsCount() {
91
91
  return Object.keys(this.pool)
92
92
  }
93
93
 
94
- async sendRequest ({commandSubmitData, url}) {
94
+ async sendRequest({commandSubmitData, url}) {
95
95
  let response
96
96
 
97
97
  for (let i = 0; i < 3; i++) {
@@ -113,7 +113,7 @@ export default class ApiMakerCommandsPool {
113
113
  throw new Error("Couldnt successfully execute request")
114
114
  }
115
115
 
116
- async flush () {
116
+ async flush() {
117
117
  if (this.commandsCount() == 0) {
118
118
  return
119
119
  }
@@ -166,7 +166,7 @@ export default class ApiMakerCommandsPool {
166
166
  }
167
167
  }
168
168
 
169
- handleFailedResponse (commandData, commandResponseData) {
169
+ handleFailedResponse(commandData, commandResponseData) {
170
170
  let error
171
171
 
172
172
  if (commandResponseData.error_type == "destroy_error") {
@@ -190,13 +190,13 @@ export default class ApiMakerCommandsPool {
190
190
  commandData.reject(error)
191
191
  }
192
192
 
193
- clearTimeout () {
193
+ clearTimeout() {
194
194
  if (this.flushTimeout) {
195
195
  clearTimeout(this.flushTimeout)
196
196
  }
197
197
  }
198
198
 
199
- isActive () {
199
+ isActive() {
200
200
  if (this.commandsCount() > 0) {
201
201
  return true
202
202
  }
@@ -208,7 +208,7 @@ export default class ApiMakerCommandsPool {
208
208
  return false
209
209
  }
210
210
 
211
- setFlushTimeout () {
211
+ setFlushTimeout() {
212
212
  this.clearTimeout()
213
213
  this.flushTimeout = setTimeout(() => this.flush(), 0)
214
214
  }
package/src/devise.mjs CHANGED
@@ -9,11 +9,11 @@ import Services from "./services.mjs"
9
9
  const shared = {}
10
10
 
11
11
  export default class ApiMakerDevise {
12
- static callSignOutEvent (args) {
12
+ static callSignOutEvent(args) {
13
13
  events.emit("onDeviseSignOut", {args})
14
14
  }
15
15
 
16
- static current () {
16
+ static current() {
17
17
  if (!shared.currentApiMakerDevise) {
18
18
  shared.currentApiMakerDevise = new ApiMakerDevise()
19
19
  }
@@ -21,11 +21,11 @@ export default class ApiMakerDevise {
21
21
  return shared.currentApiMakerDevise
22
22
  }
23
23
 
24
- static events () {
24
+ static events() {
25
25
  return events
26
26
  }
27
27
 
28
- static addUserScope (scope) {
28
+ static addUserScope(scope) {
29
29
  const currentMethodName = `current${inflection.camelize(scope)}`
30
30
 
31
31
  ApiMakerDevise[currentMethodName] = () => ApiMakerDevise.current().getCurrentScope(scope)
@@ -35,7 +35,7 @@ export default class ApiMakerDevise {
35
35
  ApiMakerDevise[isSignedInMethodName] = () => Boolean(ApiMakerDevise.current().getCurrentScope(scope))
36
36
  }
37
37
 
38
- static async signIn (username, password, args = {}) {
38
+ static async signIn(username, password, args = {}) {
39
39
  if (!args.scope) args.scope = "user"
40
40
 
41
41
  const postData = {username, password, args}
@@ -53,7 +53,7 @@ export default class ApiMakerDevise {
53
53
  return {model, response}
54
54
  }
55
55
 
56
- static updateSession (model, args = {}) {
56
+ static updateSession(model, args = {}) {
57
57
  if (!args.scope) {
58
58
  args.scope = digg(model.modelClassData(), "name")
59
59
  }
@@ -63,11 +63,17 @@ export default class ApiMakerDevise {
63
63
  ApiMakerDevise.current().currents[camelizedScopeName] = model
64
64
  }
65
65
 
66
- static setSignedOut (args) {
66
+ hasCurrentScope(scope) {
67
+ const camelizedScopeName = inflection.camelize(scope, true)
68
+
69
+ return camelizedScopeName in ApiMakerDevise.current().currents
70
+ }
71
+
72
+ static setSignedOut(args) {
67
73
  ApiMakerDevise.current().currents[inflection.camelize(args.scope, true)] = null
68
74
  }
69
75
 
70
- static async signOut (args = {}) {
76
+ static async signOut(args = {}) {
71
77
  if (!args.scope) {
72
78
  args.scope = "user"
73
79
  }
@@ -87,11 +93,11 @@ export default class ApiMakerDevise {
87
93
  return response
88
94
  }
89
95
 
90
- constructor () {
96
+ constructor() {
91
97
  this.currents = {}
92
98
  }
93
99
 
94
- getCurrentScope (scope) {
100
+ getCurrentScope(scope) {
95
101
  if (!(scope in this.currents)) {
96
102
  this.currents[scope] = this.loadCurrentScope(scope)
97
103
  }
@@ -99,7 +105,7 @@ export default class ApiMakerDevise {
99
105
  return this.currents[scope]
100
106
  }
101
107
 
102
- hasCurrentScope(scope) {
108
+ hasGlobalCurrentScope(scope) {
103
109
  if (globalThis.apiMakerDeviseCurrent && scope in globalThis.apiMakerDeviseCurrent) {
104
110
  return true
105
111
  }
@@ -107,8 +113,8 @@ export default class ApiMakerDevise {
107
113
  return false
108
114
  }
109
115
 
110
- loadCurrentScope (scope) {
111
- if (!this.hasCurrentScope(scope)) {
116
+ loadCurrentScope(scope) {
117
+ if (!this.hasGlobalCurrentScope(scope)) {
112
118
  return null
113
119
  }
114
120
 
package/src/logger.mjs CHANGED
@@ -12,7 +12,13 @@ export default class ApiMakerLogger {
12
12
  }
13
13
 
14
14
  debug(message) {
15
- if (this.getDebug()) this.log(message)
15
+ if (this.getDebug()) {
16
+ this.log(message)
17
+ }
18
+ }
19
+
20
+ error(message) {
21
+ console.error(message)
16
22
  }
17
23
 
18
24
  log(message) {
@@ -37,7 +37,7 @@ export default class ApiMakerSessionStatusUpdater {
37
37
 
38
38
  async getCsrfToken() {
39
39
  if (this.csrfToken) {
40
- logger.debug("Get CSRF token from set variable")
40
+ logger.debug(`Get CSRF token from set variable: ${this.csrfToken}`)
41
41
 
42
42
  return this.csrfToken
43
43
  }
@@ -46,7 +46,7 @@ export default class ApiMakerSessionStatusUpdater {
46
46
  const csrfTokenElement = document.querySelector("meta[name='csrf-token']")
47
47
 
48
48
  if (csrfTokenElement) {
49
- logger.debug("Get CSRF token from meta element")
49
+ logger.debug(() => `Get CSRF token from meta element: ${csrfTokenElement.getAttribute("content")}`)
50
50
 
51
51
  this.csrfToken = csrfTokenElement.getAttribute("content")
52
52
 
@@ -58,7 +58,7 @@ export default class ApiMakerSessionStatusUpdater {
58
58
  await this.updateSessionStatus()
59
59
 
60
60
  if (this.csrfToken) {
61
- logger.debug("Returning CSRF token after updating session status")
61
+ logger.debug(() => `Returning CSRF token after updating session status: ${this.csrfToken}`)
62
62
 
63
63
  return this.csrfToken
64
64
  }
@@ -68,9 +68,10 @@ export default class ApiMakerSessionStatusUpdater {
68
68
 
69
69
  sessionStatus() {
70
70
  return new Promise((resolve) => {
71
+ const host = config.getHost()
71
72
  let requestPath = ""
72
73
 
73
- if (config.getHost()) requestPath += config.getHost()
74
+ if (host) requestPath += host
74
75
 
75
76
  requestPath += "/api_maker/session_statuses"
76
77
 
@@ -1,10 +1,15 @@
1
1
  import {useCallback, useEffect, useMemo} from "react"
2
2
  import {camelize} from "inflection"
3
3
  import Devise from "./devise.mjs"
4
+ import Logger from "./logger.mjs"
4
5
  import Services from "./services.mjs"
5
6
  import useEventEmitter from "./use-event-emitter.mjs"
6
7
  import useShape from "set-state-compare/src/use-shape.js"
7
8
 
9
+ const logger = new Logger({name: "ApiMaker / useCurrentUser"})
10
+
11
+ // logger.setDebug(true)
12
+
8
13
  const useCurrentUser = (args) => {
9
14
  const s = useShape(args || {})
10
15
  const scope = args?.scope || "user"
@@ -13,20 +18,10 @@ const useCurrentUser = (args) => {
13
18
  s.meta.scope = scope
14
19
  s.meta.scopeName = scopeName
15
20
 
16
- const debugs = useCallback((debugCallback) => {
17
- if (s.props.debug) {
18
- let debugArgs = debugCallback()
19
-
20
- if (!Array.isArray(debugArgs)) debugArgs = [debugArgs]
21
-
22
- console.log("useCurrentUser", ...debugArgs)
23
- }
24
- })
25
-
26
21
  const loadCurrentUserFromRequest = useCallback(async () => {
27
22
  const {scope, scopeName} = s.m
28
23
 
29
- debugs(() => `Loading ${scope} with request`)
24
+ logger.debug(() => `Loading ${scope} with request`)
30
25
 
31
26
  const result = await Services.current().sendRequest("Devise::Current", {scope})
32
27
  const current = digg(result, "current")
@@ -41,21 +36,28 @@ const useCurrentUser = (args) => {
41
36
 
42
37
  const defaultCurrentUser = useCallback(() => {
43
38
  const {scope, scopeName} = s.m
39
+ let current
44
40
 
45
- if (Devise.current().hasCurrentScope(scope)) {
46
- const current = Devise[scopeName]()
41
+ if (Devise.current().hasCurrentScope(s.m.scope)) {
42
+ current = Devise.current().getCurrentScope(scope)
47
43
 
48
- debugs(() => `Setting ${scope} from current scope: ${current?.id()}`)
44
+ logger.debug(() => `Setting ${scope} from current scope: ${current?.id()}`)
45
+ } else if (Devise.current().hasGlobalCurrentScope(scope)) {
46
+ current = Devise[scopeName]()
49
47
 
50
- if (s.props.onCurrentUserLoaded) setTimeout(() => s.props.onCurrentUserLoaded(current), 0)
48
+ logger.debug(() => `Setting ${scope} from global current scope: ${current?.id()}`)
49
+ }
51
50
 
52
- return current
51
+ if (current && s.props.onCurrentUserLoaded) {
52
+ setTimeout(() => s.props.onCurrentUserLoaded(current), 0)
53
53
  }
54
+
55
+ return current
54
56
  }, [])
55
57
 
56
58
  const useStatesArgument = {}
57
59
 
58
- useStatesArgument[scopeName] = defaultCurrentUser()
60
+ useStatesArgument[scopeName] = () => defaultCurrentUser()
59
61
 
60
62
  s.useStates(useStatesArgument)
61
63
 
@@ -68,7 +70,8 @@ const useCurrentUser = (args) => {
68
70
  }, [])
69
71
 
70
72
  useEffect(() => {
71
- if (!Devise.current().hasCurrentScope(s.m.scope)) {
73
+ if (!Devise.current().hasGlobalCurrentScope(s.m.scope) && !Devise.current().hasCurrentScope(s.m.scope)) {
74
+ logger.debug(() => `Devise hasn't got current scope ${s.m.scope} so loading from request`)
72
75
  loadCurrentUserFromRequest()
73
76
  }
74
77
  }, [])