ramdb2-client 2.0.0-alpha.4 → 2.0.0-alpha.6
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/CHANGELOG.md +10 -0
- package/README.md +16 -0
- package/package.json +1 -1
- package/src/auth.js +17 -2
- package/src/config.js +20 -0
- package/src/request.js +9 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# ramdb2-client
|
|
2
2
|
|
|
3
|
+
## 2.0.0-alpha.6
|
|
4
|
+
|
|
5
|
+
No changes in this release.
|
|
6
|
+
|
|
7
|
+
## 2.0.0-alpha.5
|
|
8
|
+
|
|
9
|
+
### Minor Changes
|
|
10
|
+
|
|
11
|
+
- e34f3bd: `configure({ onError })`: called for each error of an operation, which is still thrown to the caller (D21).
|
|
12
|
+
|
|
3
13
|
## 2.0.0-alpha.4
|
|
4
14
|
|
|
5
15
|
No changes in this release.
|
package/README.md
CHANGED
|
@@ -282,6 +282,22 @@ they check (`rule.required`, `rule.allowed`, `rule.pattern`).
|
|
|
282
282
|
| `auth.userType` | `User` | the type returned by `me` |
|
|
283
283
|
| `auth.standard` | `true` | the server has the authentication module of ramdb2-server |
|
|
284
284
|
| `auth.onUnauthenticated` | nothing | called when the session is over |
|
|
285
|
+
| `onError(error, { operation })` | nothing | called for each error of an operation, see below |
|
|
286
|
+
|
|
287
|
+
`onError` sees every error of an operation sent to the server (`request`, `list`, `byId`, `save`, `delete`,
|
|
288
|
+
`dataCheck`, `login`, `loadMe`), typed, with the operation name: one place to react, that no call can miss. It
|
|
289
|
+
observes only: the caller still receives the error.
|
|
290
|
+
|
|
291
|
+
```js
|
|
292
|
+
configure({
|
|
293
|
+
onError(error, { operation }) {
|
|
294
|
+
if (error instanceof ForbiddenError) reloadGame() // e.g. the turn is being resolved
|
|
295
|
+
},
|
|
296
|
+
})
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
It is not called for an aborted request nor for a draft refused by its local rules. An error thrown by `onError`
|
|
300
|
+
is written in the console and does not replace the error of the operation.
|
|
285
301
|
|
|
286
302
|
## 10. Testing
|
|
287
303
|
|
package/package.json
CHANGED
package/src/auth.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { config, operationName } from './config.js'
|
|
1
|
+
import { config, notifyError, operationName } from './config.js'
|
|
2
2
|
import { ForbiddenError, UnauthenticatedError } from './errors.js'
|
|
3
3
|
import { clearInstances } from './identity.js'
|
|
4
4
|
import { send } from './transport.js'
|
|
@@ -110,6 +110,15 @@ function sessionOver() {
|
|
|
110
110
|
* @returns {Promise<object | null>} the user
|
|
111
111
|
*/
|
|
112
112
|
export async function login(login, password, { name } = {}) {
|
|
113
|
+
try {
|
|
114
|
+
return await logIn(login, password, name)
|
|
115
|
+
} catch (error) {
|
|
116
|
+
notifyError(error, operationName('NewToken', name))
|
|
117
|
+
throw error
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function logIn(login, password, name) {
|
|
113
122
|
const data = await send('query NewToken($login: String!, $password: String!) { newToken(login: $login, password: $password) }',
|
|
114
123
|
{ login, password }, { name: operationName('NewToken', name) })
|
|
115
124
|
const previous = readRecord()?.userId ?? null
|
|
@@ -135,7 +144,13 @@ export function logout() {
|
|
|
135
144
|
* @returns {Promise<object | null>}
|
|
136
145
|
*/
|
|
137
146
|
export async function loadMe() {
|
|
138
|
-
|
|
147
|
+
let user
|
|
148
|
+
try {
|
|
149
|
+
user = await fetchMe()
|
|
150
|
+
} catch (error) {
|
|
151
|
+
notifyError(error, operationName('Me'))
|
|
152
|
+
throw error
|
|
153
|
+
}
|
|
139
154
|
session().user = user
|
|
140
155
|
return user
|
|
141
156
|
}
|
package/src/config.js
CHANGED
|
@@ -17,6 +17,9 @@ const NAME = /^[_A-Za-z][_0-9A-Za-z]*$/
|
|
|
17
17
|
* @property {Pick<Storage, 'getItem' | 'setItem' | 'removeItem'> | null} [storage] where the token is kept. Default: localStorage
|
|
18
18
|
* @property {Function[]} [classes] the client classes generated by ramdb2-cli
|
|
19
19
|
* @property {AuthOptions} [auth]
|
|
20
|
+
* @property {(error: Error, info: {operation: string | undefined}) => void} [onError] called for each error of an
|
|
21
|
+
* operation (request, list, byId, save, login…), with the typed error, before it is thrown to the caller: it
|
|
22
|
+
* observes, the caller still receives the error. Not called for an aborted request (D21)
|
|
20
23
|
*/
|
|
21
24
|
|
|
22
25
|
function defaults() {
|
|
@@ -27,6 +30,7 @@ function defaults() {
|
|
|
27
30
|
storage: globalThis.localStorage ?? memoryStorage(),
|
|
28
31
|
classes: new Map(),
|
|
29
32
|
operationPrefix: '',
|
|
33
|
+
onError: () => {},
|
|
30
34
|
auth: { header: 'authorization', prefix: 'Bearer', userType: 'User', standard: true, onUnauthenticated: () => {} },
|
|
31
35
|
}
|
|
32
36
|
}
|
|
@@ -102,3 +106,19 @@ export function memoryStorage() {
|
|
|
102
106
|
removeItem: key => values.delete(key),
|
|
103
107
|
}
|
|
104
108
|
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Gives the error of an operation to onError (D21), before it is thrown to the caller. An error of onError itself
|
|
112
|
+
* is written in the console: it does not replace the error of the operation.
|
|
113
|
+
* @param {unknown} error
|
|
114
|
+
* @param {string | undefined} operation the operation name
|
|
115
|
+
*/
|
|
116
|
+
export function notifyError(error, operation) {
|
|
117
|
+
if (error?.name === 'AbortError') return
|
|
118
|
+
try {
|
|
119
|
+
const result = config.onError(error, { operation })
|
|
120
|
+
if (typeof result?.then === 'function') result.then(undefined, failure => console.error('onError failed', failure))
|
|
121
|
+
} catch (failure) {
|
|
122
|
+
console.error('onError failed', failure)
|
|
123
|
+
}
|
|
124
|
+
}
|
package/src/request.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { afterError, authHeaders, beforeRequest } from './auth.js'
|
|
2
|
-
import { operationName } from './config.js'
|
|
2
|
+
import { notifyError, operationName } from './config.js'
|
|
3
3
|
import { send } from './transport.js'
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -14,11 +14,16 @@ import { send } from './transport.js'
|
|
|
14
14
|
*/
|
|
15
15
|
export async function request(document, variables = {}, { name, signal, auth = true } = {}) {
|
|
16
16
|
const operation = name ?? /^\s*(?:query|mutation)\s+([_A-Za-z][_0-9A-Za-z]*)/.exec(document)?.[1]
|
|
17
|
-
if (auth) await beforeRequest()
|
|
18
17
|
try {
|
|
19
|
-
|
|
18
|
+
if (auth) await beforeRequest()
|
|
19
|
+
try {
|
|
20
|
+
return await send(document, variables, { name: operation, signal, headers: auth ? authHeaders() : {} })
|
|
21
|
+
} catch (error) {
|
|
22
|
+
throw auth ? await afterError(error) : error
|
|
23
|
+
}
|
|
20
24
|
} catch (error) {
|
|
21
|
-
|
|
25
|
+
notifyError(error, operation)
|
|
26
|
+
throw error
|
|
22
27
|
}
|
|
23
28
|
}
|
|
24
29
|
|