@kaspernj/api-maker 1.0.348 → 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 +2 -2
- package/src/devise.mjs +19 -13
- package/src/session-status-updater.mjs +2 -1
- package/src/use-current-user.mjs +21 -18
package/package.json
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
]
|
|
17
17
|
},
|
|
18
18
|
"name": "@kaspernj/api-maker",
|
|
19
|
-
"version": "1.0.
|
|
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.
|
|
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/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
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
|
111
|
-
if (!this.
|
|
116
|
+
loadCurrentScope(scope) {
|
|
117
|
+
if (!this.hasGlobalCurrentScope(scope)) {
|
|
112
118
|
return null
|
|
113
119
|
}
|
|
114
120
|
|
|
@@ -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 (
|
|
74
|
+
if (host) requestPath += host
|
|
74
75
|
|
|
75
76
|
requestPath += "/api_maker/session_statuses"
|
|
76
77
|
|
package/src/use-current-user.mjs
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
41
|
+
if (Devise.current().hasCurrentScope(s.m.scope)) {
|
|
42
|
+
current = Devise.current().getCurrentScope(scope)
|
|
47
43
|
|
|
48
|
-
|
|
44
|
+
logger.debug(() => `Setting ${scope} from current scope: ${current?.id()}`)
|
|
45
|
+
} else if (Devise.current().hasGlobalCurrentScope(scope)) {
|
|
46
|
+
current = Devise[scopeName]()
|
|
49
47
|
|
|
50
|
-
|
|
48
|
+
logger.debug(() => `Setting ${scope} from global current scope: ${current?.id()}`)
|
|
49
|
+
}
|
|
51
50
|
|
|
52
|
-
|
|
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
|
}, [])
|