@supatype/client 0.7.0 → 0.9.0
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/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +69 -56
- package/.turbo/turbo-typecheck.log +1 -1
- package/CHANGELOG.md +28 -0
- package/bin/supatype-prepare.js +68 -0
- package/dist/auth.d.ts +17 -0
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +41 -10
- package/dist/auth.js.map +1 -1
- package/dist/bindings.d.ts +1 -1
- package/dist/bindings.d.ts.map +1 -1
- package/dist/graphql-transport.d.ts +2 -2
- package/dist/graphql-transport.d.ts.map +1 -1
- package/dist/graphql-transport.js.map +1 -1
- package/dist/index.d.ts +16 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +33 -21
- package/dist/index.js.map +1 -1
- package/dist/mutation-request.d.ts +17 -4
- package/dist/mutation-request.d.ts.map +1 -1
- package/dist/mutation-request.js +16 -14
- package/dist/mutation-request.js.map +1 -1
- package/dist/query.d.ts +40 -16
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +81 -21
- package/dist/query.js.map +1 -1
- package/dist/realtime.d.ts +38 -1
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +201 -82
- package/dist/realtime.js.map +1 -1
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +10 -3
- package/dist/storage.js.map +1 -1
- package/dist/types.d.ts +14 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -2
- package/src/auth.ts +43 -10
- package/src/bindings.ts +1 -1
- package/src/graphql-transport.ts +2 -2
- package/src/index.ts +40 -26
- package/src/mutation-request.ts +32 -18
- package/src/query.ts +102 -37
- package/src/realtime.ts +222 -82
- package/src/storage.ts +11 -3
- package/src/types.ts +14 -0
- package/tests/auth.test.ts +65 -0
- package/tests/data-plane-auth.test.ts +1 -6
- package/tests/draft-insert.test.ts +9 -9
- package/tests/draft-update.test.ts +5 -5
- package/tests/helpers/socket-swap.ts +3 -0
- package/tests/minimal-writes.test.ts +118 -0
- package/tests/postgrest-error-codes.test.ts +1 -1
- package/tests/query.test.ts +3 -3
- package/tests/realtime-ack-ref.test.ts +112 -0
- package/tests/realtime-session-auth.test.ts +1 -4
- package/tests/realtime-shared-channel.test.ts +209 -0
- package/tests/rpc-resend.test.ts +33 -8
- package/tests/server-bodies.test.ts +48 -0
- package/tests/storage-quota-integration.test.ts +4 -7
- package/tests/storage-upload-limit-integration.test.ts +5 -9
- package/tests/storage-url-encoding.test.ts +52 -0
- package/tests/supatype-prepare.test.ts +139 -0
- package/tests/support/blob.ts +10 -0
- package/tests/support/jwt.ts +4 -0
- package/tests/table-subscribe.test.ts +28 -1
- package/tests/unauthorized-resend.test.ts +107 -0
- package/tsconfig.tsbuildinfo +1 -1
package/.turbo/turbo-build.log
CHANGED
package/.turbo/turbo-test.log
CHANGED
|
@@ -1,66 +1,79 @@
|
|
|
1
1
|
|
|
2
|
-
> @supatype/client@0.
|
|
3
|
-
> vitest run
|
|
2
|
+
> @supatype/client@0.9.0 test /home/runner/work/supatype/supatype/packages/client
|
|
3
|
+
> vitest run --maxWorkers=50%
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
[1m[46m RUN [49m[22m [36mv4.0.18 [39m[90m/home/runner/work/supatype/supatype/packages/client[39m
|
|
7
7
|
|
|
8
|
-
[32m✓[39m tests/auth.test.ts [2m([22m[
|
|
9
|
-
[32m✓[39m tests/query.test.ts [2m([22m[2m36 tests[22m[2m)[22m[33m
|
|
10
|
-
[32m✓[39m tests/
|
|
11
|
-
[32m✓[39m tests/
|
|
12
|
-
[32m✓[39m tests/draft-profile.test.ts [2m([22m[2m18 tests[22m[2m)[22m[33m
|
|
13
|
-
[32m✓[39m tests/
|
|
14
|
-
[32m✓[39m tests/
|
|
15
|
-
|
|
16
|
-
[32m✓[39m tests/
|
|
17
|
-
[32m✓[39m tests/
|
|
18
|
-
[32m✓[39m tests/
|
|
19
|
-
[32m✓[39m tests/
|
|
20
|
-
[32m✓[39m tests/
|
|
21
|
-
[32m✓[39m tests/
|
|
22
|
-
[32m✓[39m tests/
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
[32m✓[39m tests/
|
|
26
|
-
|
|
27
|
-
[32m✓[39m tests/realtime-
|
|
28
|
-
[32m✓[39m tests/
|
|
29
|
-
[32m✓[39m tests/retry-policy.test.ts [2m([22m[2m17 tests[22m[2m)[22m[33m 1823[2mms[22m[39m
|
|
8
|
+
[32m✓[39m tests/auth.test.ts [2m([22m[2m46 tests[22m[2m)[22m[33m 622[2mms[22m[39m
|
|
9
|
+
[32m✓[39m tests/query.test.ts [2m([22m[2m36 tests[22m[2m)[22m[33m 495[2mms[22m[39m
|
|
10
|
+
[32m✓[39m tests/mfa-totp-integration.test.ts [2m([22m[2m12 tests[22m[2m)[22m[33m 423[2mms[22m[39m
|
|
11
|
+
[32m✓[39m tests/types.test.ts [2m([22m[2m30 tests[22m[2m)[22m[32m 286[2mms[22m[39m
|
|
12
|
+
[32m✓[39m tests/draft-profile.test.ts [2m([22m[2m18 tests[22m[2m)[22m[33m 419[2mms[22m[39m
|
|
13
|
+
[32m✓[39m tests/table-subscribe.test.ts [2m([22m[2m12 tests[22m[2m)[22m[32m 298[2mms[22m[39m
|
|
14
|
+
[32m✓[39m tests/realtime-multi-tenant-integration.test.ts [2m([22m[2m7 tests[22m[2m)[22m[33m 476[2mms[22m[39m
|
|
15
|
+
[33m[2m✓[22m[39m receives INSERT events on the subscribed channel [33m 377[2mms[22m[39m
|
|
16
|
+
[32m✓[39m tests/draft-insert.test.ts [2m([22m[2m18 tests[22m[2m)[22m[32m 276[2mms[22m[39m
|
|
17
|
+
[32m✓[39m tests/graphql-json.test.ts [2m([22m[2m12 tests[22m[2m)[22m[33m 547[2mms[22m[39m
|
|
18
|
+
[32m✓[39m tests/realtime-shared-channel.test.ts [2m([22m[2m8 tests[22m[2m)[22m[32m 197[2mms[22m[39m
|
|
19
|
+
[32m✓[39m tests/auth-url-session.test.ts [2m([22m[2m10 tests[22m[2m)[22m[33m 445[2mms[22m[39m
|
|
20
|
+
[32m✓[39m tests/draft-update.test.ts [2m([22m[2m11 tests[22m[2m)[22m[33m 448[2mms[22m[39m
|
|
21
|
+
[32m✓[39m tests/server-bodies.test.ts [2m([22m[2m20 tests[22m[2m)[22m[32m 285[2mms[22m[39m
|
|
22
|
+
[32m✓[39m tests/retry-integration.test.ts [2m([22m[2m12 tests[22m[2m)[22m[33m 457[2mms[22m[39m
|
|
23
|
+
[32m✓[39m tests/exact-values.test.ts [2m([22m[2m24 tests[22m[2m)[22m[32m 36[2mms[22m[39m
|
|
24
|
+
[32m✓[39m tests/storage-quota-integration.test.ts [2m([22m[2m10 tests[22m[2m)[22m[32m 225[2mms[22m[39m
|
|
25
|
+
[32m✓[39m tests/rate-limit-integration.test.ts [2m([22m[2m8 tests[22m[2m)[22m[32m 196[2mms[22m[39m
|
|
26
|
+
[32m✓[39m tests/storage-upload-limit-integration.test.ts [2m([22m[2m11 tests[22m[2m)[22m[32m 252[2mms[22m[39m
|
|
27
|
+
[32m✓[39m tests/realtime-exact-values.test.ts [2m([22m[2m8 tests[22m[2m)[22m[33m 303[2mms[22m[39m
|
|
28
|
+
[32m✓[39m tests/retry-policy.test.ts [2m([22m[2m17 tests[22m[2m)[22m[33m 1873[2mms[22m[39m
|
|
30
29
|
[2m[90m↓[39m[22m tests/draft-update.integration.test.ts [2m([22m[2m3 tests[22m[2m | [22m[33m3 skipped[39m[2m)[22m
|
|
31
|
-
[32m✓[39m tests/
|
|
32
|
-
[32m✓[39m tests/subscribe-frame.test.ts [2m([22m[2m6 tests[22m[2m)[22m[33m
|
|
30
|
+
[32m✓[39m tests/realtime-session-auth.test.ts [2m([22m[2m7 tests[22m[2m)[22m[33m 401[2mms[22m[39m
|
|
31
|
+
[32m✓[39m tests/subscribe-frame.test.ts [2m([22m[2m6 tests[22m[2m)[22m[33m 340[2mms[22m[39m
|
|
32
|
+
[32m✓[39m tests/minimal-writes.test.ts [2m([22m[2m8 tests[22m[2m)[22m[32m 230[2mms[22m[39m
|
|
33
33
|
[2m[90m↓[39m[22m tests/draft-insert.integration.test.ts [2m([22m[2m4 tests[22m[2m | [22m[33m4 skipped[39m[2m)[22m
|
|
34
|
-
[32m✓[39m tests/
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
[32m✓[39m tests/
|
|
46
|
-
[32m✓[39m tests/
|
|
47
|
-
[32m✓[39m tests/
|
|
48
|
-
|
|
49
|
-
[32m✓[39m tests/
|
|
50
|
-
[32m✓[39m tests/
|
|
51
|
-
[32m✓[39m tests/
|
|
52
|
-
[32m✓[39m tests/
|
|
53
|
-
[32m✓[39m tests/
|
|
54
|
-
[32m✓[39m tests/
|
|
34
|
+
[32m✓[39m tests/supatype-prepare.test.ts [2m([22m[2m12 tests[22m[2m)[22m[33m 4388[2mms[22m[39m
|
|
35
|
+
[33m[2m✓[22m[39m succeeds, and generates nothing, where the CLI is not installed [33m 317[2mms[22m[39m
|
|
36
|
+
[33m[2m✓[22m[39m ignores a CLI on NODE_PATH [33m 311[2mms[22m[39m
|
|
37
|
+
[33m[2m✓[22m[39m ignores a CLI in the home folder's .node_modules [33m 335[2mms[22m[39m
|
|
38
|
+
[33m[2m✓[22m[39m runs supatype generate where the CLI is installed, its bin named [33m 384[2mms[22m[39m
|
|
39
|
+
[33m[2m✓[22m[39m runs supatype generate where the CLI is installed, its bin given as a string [33m 460[2mms[22m[39m
|
|
40
|
+
[33m[2m✓[22m[39m finds a CLI hoisted into a parent directory [33m 484[2mms[22m[39m
|
|
41
|
+
[33m[2m✓[22m[39m runs the nearest CLI [33m 397[2mms[22m[39m
|
|
42
|
+
[33m[2m✓[22m[39m fails the install when the CLI folder is an empty folder [33m 356[2mms[22m[39m
|
|
43
|
+
[33m[2m✓[22m[39m fails the install when the CLI folder is a CLI naming no supatype command [33m 302[2mms[22m[39m
|
|
44
|
+
[33m[2m✓[22m[39m fails the install when generate fails [33m 488[2mms[22m[39m
|
|
45
|
+
[32m✓[39m tests/filters.test.ts [2m([22m[2m11 tests[22m[2m)[22m[32m 166[2mms[22m[39m
|
|
46
|
+
[32m✓[39m tests/realtime-ack-ref.test.ts [2m([22m[2m4 tests[22m[2m)[22m[32m 248[2mms[22m[39m
|
|
47
|
+
[32m✓[39m tests/unauthorized-resend.test.ts [2m([22m[2m5 tests[22m[2m)[22m[32m 178[2mms[22m[39m
|
|
48
|
+
[32m✓[39m tests/storage-object-shape.test.ts [2m([22m[2m8 tests[22m[2m)[22m[32m 183[2mms[22m[39m
|
|
49
|
+
[32m✓[39m tests/postgrest-error-codes.test.ts [2m([22m[2m28 tests[22m[2m)[22m[32m 161[2mms[22m[39m
|
|
50
|
+
[32m✓[39m tests/auth-events.test.ts [2m([22m[2m6 tests[22m[2m)[22m[32m 191[2mms[22m[39m
|
|
51
|
+
[32m✓[39m tests/serverless.test.ts [2m([22m[2m9 tests[22m[2m)[22m[32m 10[2mms[22m[39m
|
|
52
|
+
[32m✓[39m tests/data-plane-auth.test.ts [2m([22m[2m2 tests[22m[2m)[22m[32m 115[2mms[22m[39m
|
|
53
|
+
[32m✓[39m tests/exact-response.test.ts [2m([22m[2m8 tests[22m[2m)[22m[32m 10[2mms[22m[39m
|
|
54
|
+
[32m✓[39m tests/auth-errors.test.ts [2m([22m[2m14 tests[22m[2m)[22m[32m 128[2mms[22m[39m
|
|
55
|
+
[32m✓[39m tests/query-cache.test.ts [2m([22m[2m8 tests[22m[2m)[22m[32m 106[2mms[22m[39m
|
|
56
|
+
[32m✓[39m tests/table-subscription.test.ts [2m([22m[2m8 tests[22m[2m)[22m[32m 110[2mms[22m[39m
|
|
57
|
+
[32m✓[39m tests/identity-change.test.ts [2m([22m[2m6 tests[22m[2m)[22m[32m 31[2mms[22m[39m
|
|
58
|
+
[32m✓[39m tests/signed-url-absolute.test.ts [2m([22m[2m5 tests[22m[2m)[22m[32m 101[2mms[22m[39m
|
|
59
|
+
[32m✓[39m tests/storage-upload-browser.test.ts [2m([22m[2m3 tests[22m[2m)[22m[32m 62[2mms[22m[39m
|
|
60
|
+
[32m✓[39m tests/table-transport.test.ts [2m([22m[2m8 tests[22m[2m)[22m[33m 464[2mms[22m[39m
|
|
61
|
+
[32m✓[39m tests/field-kinds-registry.test.ts [2m([22m[2m7 tests[22m[2m)[22m[32m 17[2mms[22m[39m
|
|
62
|
+
[32m✓[39m tests/realtime-presence.test.ts [2m([22m[2m3 tests[22m[2m)[22m[32m 120[2mms[22m[39m
|
|
63
|
+
[32m✓[39m tests/storage-reference-url.test.ts [2m([22m[2m5 tests[22m[2m)[22m[32m 6[2mms[22m[39m
|
|
64
|
+
[32m✓[39m tests/auth-error-codes.test.ts [2m([22m[2m26 tests[22m[2m)[22m[32m 16[2mms[22m[39m
|
|
65
|
+
[32m✓[39m tests/realtime-swap-status.test.ts [2m([22m[2m4 tests[22m[2m)[22m[32m 139[2mms[22m[39m
|
|
66
|
+
[32m✓[39m tests/rpc-resend.test.ts [2m([22m[2m1 test[22m[2m)[22m[32m 57[2mms[22m[39m
|
|
67
|
+
[32m✓[39m tests/masked-fields.test.ts [2m([22m[2m6 tests[22m[2m)[22m[32m 6[2mms[22m[39m
|
|
55
68
|
[32m✓[39m tests/count.test.ts [2m([22m[2m11 tests[22m[2m)[22m[32m 10[2mms[22m[39m
|
|
56
|
-
[32m✓[39m tests/
|
|
57
|
-
[32m✓[39m tests/
|
|
58
|
-
[32m✓[39m tests/
|
|
59
|
-
[32m✓[39m tests/pkce.test.ts [2m([22m[2m4 tests[22m[2m)[22m[32m
|
|
60
|
-
[32m✓[39m tests/sha256-hex.test.ts [2m([22m[2m1 test[22m[2m)[22m[32m
|
|
69
|
+
[32m✓[39m tests/storage-url-encoding.test.ts [2m([22m[2m4 tests[22m[2m)[22m[32m 61[2mms[22m[39m
|
|
70
|
+
[32m✓[39m tests/dynamic-headers.test.ts [2m([22m[2m2 tests[22m[2m)[22m[32m 44[2mms[22m[39m
|
|
71
|
+
[32m✓[39m tests/entry-points.test.ts [2m([22m[2m3 tests[22m[2m)[22m[32m 9[2mms[22m[39m
|
|
72
|
+
[32m✓[39m tests/pkce.test.ts [2m([22m[2m4 tests[22m[2m)[22m[32m 8[2mms[22m[39m
|
|
73
|
+
[32m✓[39m tests/sha256-hex.test.ts [2m([22m[2m1 test[22m[2m)[22m[32m 5[2mms[22m[39m
|
|
61
74
|
|
|
62
|
-
[2m Test Files [22m [1m[
|
|
63
|
-
[2m Tests [22m [1m[
|
|
64
|
-
[2m Start at [22m
|
|
65
|
-
[2m Duration [22m
|
|
75
|
+
[2m Test Files [22m [1m[32m53 passed[39m[22m[2m | [22m[33m2 skipped[39m[90m (55)[39m
|
|
76
|
+
[2m Tests [22m [1m[32m573 passed[39m[22m[2m | [22m[33m7 skipped[39m[90m (580)[39m
|
|
77
|
+
[2m Start at [22m 22:36:03
|
|
78
|
+
[2m Duration [22m 39.27s[2m (transform 6.28s, setup 0ms, import 20.46s, tests 16.68s, environment 32ms)[22m
|
|
66
79
|
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
### Breaking Changes
|
|
6
|
+
|
|
7
|
+
* **query:** `insert`, `update`, `upsert` and `delete` ask for no rows back and resolve to
|
|
8
|
+
`data: null`, as postgrest-js does. Chain `.select()` (or `.select(columns)`) to get the written
|
|
9
|
+
rows. Returning a row needs the right to read it, so a table whose rules let a caller create but
|
|
10
|
+
not read (an anon order form, say) refused every insert with 401 `42501`. A plain `delete()`
|
|
11
|
+
resolves to `null` rather than `[]`. On a model that keeps drafts, the drafts are read back only
|
|
12
|
+
after `.select()`.
|
|
13
|
+
* **query:** `.single()` and `.maybeSingle()` on a write are a type error until `.select()` is
|
|
14
|
+
chained. Called without it from untyped code, they answer an error and send nothing.
|
|
15
|
+
* **query:** `fetchWithOptional401Retry`'s `onUnauthorized` (and `GraphqlTransport.onUnauthorized`)
|
|
16
|
+
is an `UnauthorizedHandler`: `(refusedToken: string) => Promise<boolean>`, answering whether the
|
|
17
|
+
session now holds a different token. It was `() => Promise<void>`.
|
|
18
|
+
* **bindings:** `runMutation(client, table, operation, data, request)` takes one `MutationRequest`
|
|
19
|
+
(`filter`, `publish`, `select`) in place of the positional `filter` and update options, and returns
|
|
20
|
+
rows only when `request.select` asks for them (`true`, or the columns as `.select()` takes them).
|
|
21
|
+
|
|
22
|
+
### Bug Fixes
|
|
23
|
+
|
|
24
|
+
* **auth:** a request refused with 401 is sent again only when the server said the token was
|
|
25
|
+
expired or invalid (`PGRST301`, `PGRST303`, or "JWT expired"), a session was there to refresh, and
|
|
26
|
+
the refresh produced a different token. Every 401 used to be sent twice, an anon write a row rule
|
|
27
|
+
refused among them. The refresh now runs even when the local clock still calls the token valid,
|
|
28
|
+
since the server has said otherwise.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawnSync } from "node:child_process"
|
|
3
|
+
import { lstatSync, readFileSync } from "node:fs"
|
|
4
|
+
import { basename, dirname, join } from "node:path"
|
|
5
|
+
|
|
6
|
+
// A Supatype project's `prepare`: `supatype generate` where the CLI is installed, and nothing where
|
|
7
|
+
// it is not.
|
|
8
|
+
//
|
|
9
|
+
// The generated files are gitignored, so a fresh clone has to produce them before anything can
|
|
10
|
+
// import them. A production install leaves out development dependencies, the CLI among them, but
|
|
11
|
+
// npm still runs `prepare`, and a bare `supatype generate` failed it with `supatype: not found`.
|
|
12
|
+
// Nothing needs generating there: the build that imports the files ran with everything installed.
|
|
13
|
+
// Prisma's client skips its generate the same way when the Prisma CLI is not installed.
|
|
14
|
+
//
|
|
15
|
+
// It ships here, in a package a project installs for production too, rather than as a script
|
|
16
|
+
// written into each project's package.json, so a fix to it reaches every project with an upgrade.
|
|
17
|
+
// Yarn 2 and later do not run a project's own `prepare` on install, so there `supatype generate`
|
|
18
|
+
// is run by hand.
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The node_modules folders Node looks in for a package from `dir`, nearest first: beside it and in
|
|
22
|
+
* each folder above it, where a workspace hoists the CLI. Not NODE_PATH or the global folders,
|
|
23
|
+
* which belong to no project.
|
|
24
|
+
*/
|
|
25
|
+
function nodeModulesFolders(dir) {
|
|
26
|
+
const folders = []
|
|
27
|
+
for (let p = dir; ; p = dirname(p)) {
|
|
28
|
+
if (basename(p) !== "node_modules") folders.push(join(p, "node_modules"))
|
|
29
|
+
if (dirname(p) === p) return folders
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A folder that cannot be looked at is passed over, as Node passes it over. */
|
|
34
|
+
function exists(path) {
|
|
35
|
+
try {
|
|
36
|
+
lstatSync(path)
|
|
37
|
+
return true
|
|
38
|
+
} catch {
|
|
39
|
+
return false
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The `supatype` bin the CLI's own package.json names. Throws when the CLI cannot be read. */
|
|
44
|
+
function cliBin(cli) {
|
|
45
|
+
const { bin } = JSON.parse(readFileSync(join(cli, "package.json"), "utf8"))
|
|
46
|
+
const entry = typeof bin === "string" ? bin : bin?.supatype
|
|
47
|
+
if (typeof entry !== "string") throw new Error("its package.json names no supatype bin")
|
|
48
|
+
return join(cli, entry)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const cli = nodeModulesFolders(process.cwd())
|
|
52
|
+
.map((folder) => join(folder, "@supatype", "cli"))
|
|
53
|
+
.find(exists)
|
|
54
|
+
if (cli === undefined) process.exit(0)
|
|
55
|
+
|
|
56
|
+
// A CLI folder that is there but broken, such as a link into a pruned pnpm store, fails the
|
|
57
|
+
// install: skipping it would pass the install and fail the build later, on files never generated.
|
|
58
|
+
let bin
|
|
59
|
+
try {
|
|
60
|
+
bin = cliBin(cli)
|
|
61
|
+
} catch (e) {
|
|
62
|
+
console.error(`supatype-prepare: cannot read the Supatype CLI at ${cli}: ${e.message}`)
|
|
63
|
+
process.exit(1)
|
|
64
|
+
}
|
|
65
|
+
const result = spawnSync(process.execPath, [bin, "generate"], { stdio: "inherit" })
|
|
66
|
+
if (result.error) console.error(`supatype-prepare: ${result.error.message}`)
|
|
67
|
+
if (result.signal) console.error(`supatype-prepare: supatype generate was stopped by ${result.signal}`)
|
|
68
|
+
process.exit(result.status ?? 1)
|
package/dist/auth.d.ts
CHANGED
|
@@ -4,6 +4,10 @@ export type AuthListener = (event: AuthChangeEvent, session: Session | null) =>
|
|
|
4
4
|
interface AuthClientOptions {
|
|
5
5
|
initialSession?: Session | undefined;
|
|
6
6
|
persistSession?: boolean | undefined;
|
|
7
|
+
/** Refresh on a timer before the token expires, and at once when it starts expired. Default: true. */
|
|
8
|
+
autoRefreshToken?: boolean | undefined;
|
|
9
|
+
/** Refresh an expired access token when a request or `getSession` finds it. Default: true. */
|
|
10
|
+
refreshExpiredToken?: boolean | undefined;
|
|
7
11
|
storageKey?: string | undefined;
|
|
8
12
|
cookiePrefix?: string | undefined;
|
|
9
13
|
/** Custom session store (replaces browser localStorage/cookies when set). */
|
|
@@ -20,6 +24,10 @@ export declare class AuthClient {
|
|
|
20
24
|
private readonly listeners;
|
|
21
25
|
private listenerIdCounter;
|
|
22
26
|
private readonly persistSession;
|
|
27
|
+
/** False leaves every refresh to `ensureValidSession`, which each request calls first. */
|
|
28
|
+
private readonly autoRefreshToken;
|
|
29
|
+
/** False makes `ensureValidSession` leave an expired session as it is, for the server to refuse. */
|
|
30
|
+
private readonly refreshExpiredToken;
|
|
23
31
|
private readonly storageKey;
|
|
24
32
|
private readonly cookieName;
|
|
25
33
|
private readonly storage;
|
|
@@ -72,6 +80,15 @@ export declare class AuthClient {
|
|
|
72
80
|
*/
|
|
73
81
|
ensureValidSession(): Promise<void>;
|
|
74
82
|
private ensureValidSessionInternal;
|
|
83
|
+
/**
|
|
84
|
+
* Refresh after the server refused `refusedToken` as expired or invalid, whatever the local clock
|
|
85
|
+
* says of it. True when the session now holds a different token, so a request refused with that
|
|
86
|
+
* one is worth sending again; false when signed out, when the refresh failed or handed back the
|
|
87
|
+
* same token, or when this client leaves expired sessions for the server to refuse.
|
|
88
|
+
*/
|
|
89
|
+
refreshRefusedSession(refusedToken: string): Promise<boolean>;
|
|
90
|
+
/** One refresh at a time: a second caller waits for the one already in flight. */
|
|
91
|
+
private refreshOnce;
|
|
75
92
|
/** Schedule a token refresh ~60s before the current session expires. */
|
|
76
93
|
private scheduleAutoRefresh;
|
|
77
94
|
signUp(credentials: {
|
package/dist/auth.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,YAAY,EACZ,iBAAiB,EACjB,wBAAwB,EACxB,qBAAqB,EACrB,0BAA0B,EAC1B,WAAW,EAEX,OAAO,EACP,OAAO,EACP,aAAa,EACb,IAAI,EAEL,MAAM,YAAY,CAAA;AAMnB,OAAO,EAAc,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAqBzE,MAAM,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI,KAAK,IAAI,CAAA;AAEpF,UAAU,iBAAiB;IACzB,cAAc,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;IACpC,cAAc,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;IACpC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC/B,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACjC,6EAA6E;IAC7E,OAAO,CAAC,EAAE,WAAW,GAAG,SAAS,CAAA;IACjC,uGAAuG;IACvG,SAAS,CAAC,EAAE,gBAAgB,GAAG,SAAS,CAAA;IACxC,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cAAc;IAS5B,4EAA4E;YAC9D,YAAY;YAMZ,SAAS;YAMT,gBAAgB;YAMhB,iBAAiB;IAK/B,8DAA8D;IAC9D,OAAO,CAAC,YAAY;YAON,eAAe;YAKf,yBAAyB;IASvC,OAAO,CAAC,oBAAoB;IAmC5B,OAAO,CAAC,qBAAqB;IAQ7B,oGAAoG;IACpG,OAAO,CAAC,yBAAyB;IAgBjC,OAAO,CAAC,oBAAoB;YAKd,qBAAqB;IA+CnC,OAAO,CAAC,sBAAsB;IAa9B,OAAO,CAAC,UAAU;IAkBlB,OAAO,CAAC,WAAW;IAgBnB,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,GAAG,IAAI;IAqB1C,OAAO,CAAC,UAAU;IAMlB,4FAA4F;YAC9E,WAAW;IAIzB,yFAAyF;YAC3E,SAAS;YAIT,kBAAkB;IA4BhC,OAAO,CAAC,UAAU;CAkBnB"}
|
package/dist/auth.js
CHANGED
|
@@ -73,6 +73,10 @@ export class AuthClient {
|
|
|
73
73
|
listeners = new Map();
|
|
74
74
|
listenerIdCounter = 0;
|
|
75
75
|
persistSession;
|
|
76
|
+
/** False leaves every refresh to `ensureValidSession`, which each request calls first. */
|
|
77
|
+
autoRefreshToken;
|
|
78
|
+
/** False makes `ensureValidSession` leave an expired session as it is, for the server to refuse. */
|
|
79
|
+
refreshExpiredToken;
|
|
76
80
|
storageKey;
|
|
77
81
|
cookieName;
|
|
78
82
|
storage;
|
|
@@ -94,6 +98,8 @@ export class AuthClient {
|
|
|
94
98
|
this.url = url;
|
|
95
99
|
this.baseHeaders = baseHeaders;
|
|
96
100
|
this.persistSession = opts.persistSession ?? true;
|
|
101
|
+
this.autoRefreshToken = opts.autoRefreshToken ?? true;
|
|
102
|
+
this.refreshExpiredToken = opts.refreshExpiredToken ?? true;
|
|
97
103
|
this.storageKey = opts.storageKey ?? DEFAULT_STORAGE_KEY;
|
|
98
104
|
const cookiePrefix = opts.cookiePrefix ?? DEFAULT_COOKIE_PREFIX;
|
|
99
105
|
this.cookieName = `${cookiePrefix}-auth-token`;
|
|
@@ -108,7 +114,7 @@ export class AuthClient {
|
|
|
108
114
|
// Keep the access token fresh while the app is open (and refresh immediately
|
|
109
115
|
// if a persisted session loaded already expired).
|
|
110
116
|
this.scheduleAutoRefresh();
|
|
111
|
-
if (this.currentSession !== null && this.isAccessTokenExpired(this.currentSession)) {
|
|
117
|
+
if (this.autoRefreshToken && this.currentSession !== null && this.isAccessTokenExpired(this.currentSession)) {
|
|
112
118
|
void this.ensureValidSession();
|
|
113
119
|
}
|
|
114
120
|
}
|
|
@@ -239,19 +245,44 @@ export class AuthClient {
|
|
|
239
245
|
const session = this.currentSession;
|
|
240
246
|
if (session === null || !this.isAccessTokenExpired(session))
|
|
241
247
|
return;
|
|
248
|
+
// Sent as it is: a refresh nobody can store spends a refresh token someone else holds.
|
|
249
|
+
if (!this.refreshExpiredToken)
|
|
250
|
+
return;
|
|
242
251
|
if (!session.refreshToken?.trim()) {
|
|
243
252
|
this._setSession(null);
|
|
244
253
|
return;
|
|
245
254
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
255
|
+
await this.refreshOnce();
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Refresh after the server refused `refusedToken` as expired or invalid, whatever the local clock
|
|
259
|
+
* says of it. True when the session now holds a different token, so a request refused with that
|
|
260
|
+
* one is worth sending again; false when signed out, when the refresh failed or handed back the
|
|
261
|
+
* same token, or when this client leaves expired sessions for the server to refuse.
|
|
262
|
+
*/
|
|
263
|
+
async refreshRefusedSession(refusedToken) {
|
|
264
|
+
await this.ready;
|
|
265
|
+
const session = this.currentSession;
|
|
266
|
+
if (session === null)
|
|
267
|
+
return false;
|
|
268
|
+
// Another request's refresh has already replaced it.
|
|
269
|
+
if (session.accessToken !== refusedToken)
|
|
270
|
+
return true;
|
|
271
|
+
if (!this.refreshExpiredToken)
|
|
272
|
+
return false;
|
|
273
|
+
await this.refreshOnce();
|
|
274
|
+
const token = this.currentAccessToken;
|
|
275
|
+
return token !== null && token !== refusedToken;
|
|
276
|
+
}
|
|
277
|
+
/** One refresh at a time: a second caller waits for the one already in flight. */
|
|
278
|
+
async refreshOnce() {
|
|
279
|
+
if (this.refreshInFlight === null) {
|
|
280
|
+
this.refreshInFlight = this.refreshSession()
|
|
281
|
+
.then(() => undefined)
|
|
282
|
+
.finally(() => {
|
|
283
|
+
this.refreshInFlight = null;
|
|
284
|
+
});
|
|
249
285
|
}
|
|
250
|
-
this.refreshInFlight = this.refreshSession()
|
|
251
|
-
.then(() => undefined)
|
|
252
|
-
.finally(() => {
|
|
253
|
-
this.refreshInFlight = null;
|
|
254
|
-
});
|
|
255
286
|
await this.refreshInFlight;
|
|
256
287
|
}
|
|
257
288
|
/** Schedule a token refresh ~60s before the current session expires. */
|
|
@@ -260,7 +291,7 @@ export class AuthClient {
|
|
|
260
291
|
clearTimeout(this.refreshTimer);
|
|
261
292
|
this.refreshTimer = null;
|
|
262
293
|
}
|
|
263
|
-
if (typeof setTimeout === "undefined")
|
|
294
|
+
if (!this.autoRefreshToken || typeof setTimeout === "undefined")
|
|
264
295
|
return;
|
|
265
296
|
const session = this.currentSession;
|
|
266
297
|
if (session === null || !session.refreshToken)
|