@vllnt/convex-slugs 0.2.0-canary.eb878e4 → 0.2.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/CHANGELOG.md +54 -35
- package/README.md +309 -125
- package/dist/client/index.d.ts +50 -4
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +50 -4
- package/dist/client/index.js.map +1 -1
- package/dist/client/types.d.ts +5 -1
- package/dist/client/types.d.ts.map +1 -1
- package/dist/component/convex.config.d.ts +1 -0
- package/dist/component/convex.config.d.ts.map +1 -1
- package/dist/component/convex.config.js +1 -0
- package/dist/component/convex.config.js.map +1 -1
- package/dist/component/mutations.d.ts +11 -3
- package/dist/component/mutations.d.ts.map +1 -1
- package/dist/component/mutations.js +35 -15
- package/dist/component/mutations.js.map +1 -1
- package/docs/API.md +247 -100
- package/llms.txt +49 -13
- package/package.json +12 -13
- package/src/client/index.ts +50 -4
- package/src/client/types.ts +5 -1
- package/src/component/convex.config.ts +1 -0
- package/src/component/mutations.ts +43 -15
package/docs/API.md
CHANGED
|
@@ -1,151 +1,298 @@
|
|
|
1
1
|
# API Reference — @vllnt/convex-slugs
|
|
2
2
|
|
|
3
|
-
**Compatibility:** `convex@^1.45.0`; optional `react@>=18` for `./react
|
|
3
|
+
**Compatibility:** `convex@^1.45.0`; optional `react@>=18` for `./react`;
|
|
4
|
+
Node.js 20 or later (`engines.node` `>=20`).
|
|
4
5
|
|
|
5
6
|
## Entry points
|
|
6
7
|
|
|
7
|
-
| Import
|
|
8
|
-
|
|
|
9
|
-
| `@vllnt/convex-slugs`
|
|
10
|
-
| `@vllnt/convex-slugs/convex.config` |
|
|
11
|
-
| `@vllnt/convex-slugs/react`
|
|
12
|
-
| `@vllnt/convex-slugs/test`
|
|
13
|
-
| `@vllnt/convex-slugs/_generated/component` |
|
|
8
|
+
| Import | Exports |
|
|
9
|
+
| ----------------------------------------------------- | ------------------------------------------------------------------------ |
|
|
10
|
+
| `@vllnt/convex-slugs` | `Slugs` class; types `SlugsComponent`, `SlugsOptions`, `SlugWriteResult` |
|
|
11
|
+
| `@vllnt/convex-slugs/convex.config` (or `.js`) | default component config for `app.use(...)` |
|
|
12
|
+
| `@vllnt/convex-slugs/react` | `useSlugAvailable`, `useResolve` |
|
|
13
|
+
| `@vllnt/convex-slugs/test` | `register(t, name = "slugs")` for `convex-test` (TypeScript source) |
|
|
14
|
+
| `@vllnt/convex-slugs/_generated/component` (or `.js`) | type `ComponentApi` |
|
|
14
15
|
|
|
15
|
-
`
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
that `new Slugs(...)` accepts.
|
|
16
|
+
`resourceRef` and `scope` are opaque host strings; slugs are normalized (see
|
|
17
|
+
[Normalization](#normalization)). Every method's
|
|
18
|
+
`scope` defaults to the client's `defaultScope`. `resourceRef` is stored and
|
|
19
|
+
returned as given; the component never reads its meaning.
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
## Client
|
|
22
|
+
|
|
23
|
+
### `Slugs`
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
new Slugs(component: SlugsComponent, options?: SlugsOptions)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`component` is the mounted component, `components.slugs` (or the name passed to
|
|
30
|
+
`app.use`). Call the methods from host mutations (`ctx.runMutation`) and
|
|
31
|
+
queries (`ctx.runQuery`); actions work too. The constructor throws
|
|
32
|
+
`INVALID_OPTIONS` for invalid [options](#slugsoptions).
|
|
22
33
|
|
|
23
34
|
```ts
|
|
24
35
|
import { Slugs } from "@vllnt/convex-slugs";
|
|
25
|
-
|
|
36
|
+
import type { SlugsComponent } from "@vllnt/convex-slugs";
|
|
37
|
+
|
|
38
|
+
declare const component: SlugsComponent;
|
|
39
|
+
|
|
40
|
+
const slugs = new Slugs(component, {
|
|
26
41
|
defaultScope: "global", // namespace applied when a call omits `scope`
|
|
27
|
-
foldCase: true, // lowercase slugs before store
|
|
28
|
-
minLength: 1, //
|
|
29
|
-
maxLength: 256, //
|
|
30
|
-
pattern: /^[a-z0-9-]+$/, // optional
|
|
31
|
-
reservedWords: ["admin"], //
|
|
42
|
+
foldCase: true, // lowercase slugs before store and lookup
|
|
43
|
+
minLength: 1, // shorter (after normalization) is SLUG_INVALID
|
|
44
|
+
maxLength: 256, // longer (after normalization) is SLUG_INVALID
|
|
45
|
+
pattern: /^[a-z0-9-]+$/, // optional; failing it is SLUG_INVALID
|
|
46
|
+
reservedWords: ["admin"], // normalized like slugs; a match is SLUG_RESERVED
|
|
32
47
|
});
|
|
33
48
|
```
|
|
34
49
|
|
|
35
|
-
|
|
36
|
-
|
|
50
|
+
Every method normalizes its slug arguments first (see
|
|
51
|
+
[Normalization](#normalization)). `reserve` and `rename` validate the
|
|
52
|
+
normalized slug against the options before any database call; an invalid slug
|
|
53
|
+
returns `{ ok: false, reason }` without calling the component.
|
|
37
54
|
|
|
38
|
-
|
|
39
|
-
`.trim()` → optional `.toLowerCase()` (when `foldCase: true`). This ensures
|
|
40
|
-
visually-identical slugs with different Unicode compositions (e.g. precomposed
|
|
41
|
-
vs. combining-character forms) are treated as the same string.
|
|
55
|
+
### `reserve`
|
|
42
56
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
restrict the alphabet. Reserved words are normalized the same way as slugs, so
|
|
47
|
-
`reservedWords: ["Admin"]` blocks `admin`.
|
|
57
|
+
```ts
|
|
58
|
+
reserve(ctx, slug: string, resourceRef: string, scope?: string): Promise<SlugWriteResult>
|
|
59
|
+
```
|
|
48
60
|
|
|
49
|
-
|
|
61
|
+
Claims `slug` for `resourceRef`. Returns `{ ok: true }`, or `{ ok: false, reason }`
|
|
62
|
+
with `SLUG_INVALID`, `SLUG_RESERVED` or `SLUG_TAKEN`. `SLUG_TAKEN` is returned
|
|
63
|
+
whenever the slug is held in the scope, also when the same `resourceRef` holds
|
|
64
|
+
it. Reserving a slug that was renamed away deletes the redirect that started at
|
|
65
|
+
it. Throws `INVALID_ARGUMENT` for `scope: ""` or `resourceRef: ""`, or a
|
|
66
|
+
`scope` or `resourceRef` over 1024 UTF-16 code units, unless the slug already
|
|
67
|
+
failed the client checks: then it returns `SLUG_INVALID` (or `SLUG_RESERVED`)
|
|
68
|
+
without calling the component. A slug over 1024 UTF-16 code units returns
|
|
69
|
+
`SLUG_INVALID` from the component even when `maxLength` is larger (see
|
|
70
|
+
[Limits](#limits)). One `resourceRef` may hold several slugs.
|
|
50
71
|
|
|
51
|
-
|
|
52
|
-
does not cover every failure: invalid regular-expression configuration, Convex
|
|
53
|
-
validation, authorization in host wrappers, and runtime failures can throw.
|
|
54
|
-
`reason` is a typed string-literal union:
|
|
72
|
+
### `release`
|
|
55
73
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
| `SLUG_RESERVED` | The normalized slug is in `reservedWords`. |
|
|
60
|
-
| `SLUG_TAKEN` | The slug is already held in the scope. |
|
|
61
|
-
| `SLUG_NOT_FOUND` | `rename` was called for a `fromSlug` that is not held. |
|
|
74
|
+
```ts
|
|
75
|
+
release(ctx, slug: string, scope?: string): Promise<null>
|
|
76
|
+
```
|
|
62
77
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
78
|
+
Deletes the slug row, if any, and every redirect whose target is `slug`, so
|
|
79
|
+
`redirectFor` of an older name returns `null` afterwards. Idempotent: releasing
|
|
80
|
+
a slug that is not held does nothing. It does not validate the slug and does not
|
|
81
|
+
check who holds it. Throws `INVALID_ARGUMENT` for `scope: ""`.
|
|
66
82
|
|
|
67
|
-
|
|
83
|
+
### `rename`
|
|
68
84
|
|
|
69
|
-
|
|
85
|
+
```ts
|
|
86
|
+
rename(ctx, fromSlug: string, toSlug: string, scope?: string): Promise<SlugWriteResult>
|
|
87
|
+
```
|
|
70
88
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
| `INVALID_ARGUMENT` | component mutations | Empty `scope` or `resourceRef`, or `bounds` that are not integers with `minLength <= maxLength`. |
|
|
89
|
+
Moves the slug row from `fromSlug` to `toSlug` (same `resourceRef`) and records
|
|
90
|
+
a redirect `fromSlug` -> `toSlug`. Only `toSlug` is validated against the
|
|
91
|
+
options. Checks, in order:
|
|
75
92
|
|
|
76
|
-
|
|
93
|
+
1. `toSlug` fails the options: `SLUG_INVALID` or `SLUG_RESERVED` (client).
|
|
94
|
+
2. `fromSlug === toSlug` after normalization: `SLUG_TAKEN`, before any read.
|
|
95
|
+
3. `toSlug` outside the length bounds or over 1024 UTF-16 code units:
|
|
96
|
+
`SLUG_INVALID` (component).
|
|
97
|
+
4. `fromSlug` not held: `SLUG_NOT_FOUND`.
|
|
98
|
+
5. `toSlug` held: `SLUG_TAKEN`.
|
|
77
99
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
100
|
+
On success, redirects that pointed at `fromSlug` are repointed to `toSlug`, so a
|
|
101
|
+
chain A -> B -> C leaves A -> C and B -> C. At most one redirect row exists per
|
|
102
|
+
`(scope, fromSlug)`. A redirect that started at `toSlug` is deleted, so renaming
|
|
103
|
+
back (A -> B, then B -> A) leaves only B -> A and no redirect starts at a live
|
|
104
|
+
slug. Throws `INVALID_ARGUMENT` for `scope: ""` or a `scope` over 1024 UTF-16
|
|
105
|
+
code units. `fromSlug` is not capped, so a slug an older version stored over
|
|
106
|
+
the cap can be renamed to a shorter one.
|
|
107
|
+
|
|
108
|
+
### `resolve`
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
resolve(ctx, slug: string, scope?: string): Promise<string | null>
|
|
112
|
+
```
|
|
82
113
|
|
|
83
|
-
|
|
114
|
+
The `resourceRef` holding `slug`, or `null`. It does not follow redirects.
|
|
84
115
|
|
|
85
|
-
### `
|
|
116
|
+
### `redirectFor`
|
|
86
117
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
redirect (the slug is a live name again).
|
|
118
|
+
```ts
|
|
119
|
+
redirectFor(ctx, slug: string, scope?: string): Promise<string | null>
|
|
120
|
+
```
|
|
91
121
|
|
|
92
|
-
|
|
122
|
+
When `slug` was renamed away, the live slug it now points to; otherwise `null`.
|
|
123
|
+
Chains are collapsed, so the result is the live slug: no second lookup is needed.
|
|
93
124
|
|
|
94
|
-
|
|
95
|
-
all `redirects` rows whose `toSlug` equals the released slug, so
|
|
96
|
-
`redirectFor(old)` returns `null` after release instead of pointing at a dead
|
|
97
|
-
target.
|
|
125
|
+
### `slugForResource`
|
|
98
126
|
|
|
99
|
-
|
|
127
|
+
```ts
|
|
128
|
+
slugForResource(ctx, resourceRef: string, scope?: string): Promise<string | null>
|
|
129
|
+
```
|
|
100
130
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
`SLUG_NOT_FOUND` (`fromSlug` not held), or `SLUG_TAKEN` (`toSlug` already held
|
|
104
|
-
or equals `fromSlug`). Self-rename (`fromSlug === toSlug`) is rejected with
|
|
105
|
-
`SLUG_TAKEN` before any DB read. Existing redirects that pointed at `fromSlug`
|
|
106
|
-
are repointed to `toSlug`, so a chain A→B→C collapses to A→C; at most one
|
|
107
|
-
redirect row is kept per `(scope, fromSlug)`. Renaming back to an earlier name
|
|
108
|
-
(A→B, then B→A) removes the redirect that left A, so a live slug never has a
|
|
109
|
-
redirect and redirects never loop.
|
|
131
|
+
The slug held by `resourceRef` in the scope, or `null`. When one resource holds
|
|
132
|
+
several slugs it returns one of them.
|
|
110
133
|
|
|
111
|
-
##
|
|
134
|
+
## Types
|
|
112
135
|
|
|
113
|
-
### `
|
|
136
|
+
### `SlugsComponent`
|
|
114
137
|
|
|
115
|
-
The
|
|
138
|
+
The type of `components.slugs`: `mutations.reserve`, `mutations.release`,
|
|
139
|
+
`mutations.rename`, `queries.resolve`, `queries.redirectFor` and
|
|
140
|
+
`queries.slugForResource` as internal function references. `ComponentApi`
|
|
141
|
+
matches it.
|
|
142
|
+
|
|
143
|
+
### `SlugsOptions`
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
interface SlugsOptions {
|
|
147
|
+
defaultScope?: string; // default "global"
|
|
148
|
+
foldCase?: boolean; // default true
|
|
149
|
+
minLength?: number; // default 1
|
|
150
|
+
maxLength?: number; // default 256
|
|
151
|
+
pattern?: RegExp; // default: none
|
|
152
|
+
reservedWords?: string[]; // default []
|
|
153
|
+
}
|
|
154
|
+
```
|
|
116
155
|
|
|
117
|
-
|
|
156
|
+
`new Slugs` throws `INVALID_OPTIONS` unless `defaultScope` is non-empty,
|
|
157
|
+
`minLength` and `maxLength` are integers, `minLength >= 0`, and
|
|
158
|
+
`maxLength >= max(1, minLength)`. A `maxLength` over 1024 is accepted, but the
|
|
159
|
+
component still returns `SLUG_INVALID` for a slug over 1024 UTF-16 code units
|
|
160
|
+
(see [Limits](#limits)). A `pattern` with the `g` or `y` flag is reset
|
|
161
|
+
before each test, so the same slug always gets the same verdict.
|
|
118
162
|
|
|
119
|
-
|
|
120
|
-
otherwise `null`.
|
|
163
|
+
### `SlugWriteResult`
|
|
121
164
|
|
|
122
|
-
|
|
165
|
+
```ts
|
|
166
|
+
interface SlugWriteResult {
|
|
167
|
+
ok: boolean;
|
|
168
|
+
reason?: "SLUG_TAKEN" | "SLUG_NOT_FOUND" | "SLUG_INVALID" | "SLUG_RESERVED";
|
|
169
|
+
}
|
|
170
|
+
```
|
|
123
171
|
|
|
124
|
-
|
|
125
|
-
`(scope, resourceRef)` (the host reserves at most one).
|
|
172
|
+
`reason` is set when `ok` is `false`.
|
|
126
173
|
|
|
127
174
|
## React hooks
|
|
128
175
|
|
|
129
176
|
`@vllnt/convex-slugs/react` wraps `useQuery` from `convex/react`. Each hook
|
|
130
|
-
takes the host's own public query
|
|
131
|
-
`
|
|
132
|
-
|
|
177
|
+
takes the host's own public query reference with args `{ slug, scope? }` (a
|
|
178
|
+
query that takes only `{ slug }` also fits; then pass `scope` only if your query
|
|
179
|
+
accepts it, because Convex rejects unknown args at runtime) and return type `string | null`,
|
|
180
|
+
usually a query that returns `slugs.resolve(...)`. The component's own queries
|
|
181
|
+
are internal and cannot be called from a browser. The hooks need a
|
|
182
|
+
`ConvexProvider`.
|
|
183
|
+
|
|
184
|
+
### `useSlugAvailable`
|
|
133
185
|
|
|
134
|
-
|
|
186
|
+
```ts
|
|
187
|
+
useSlugAvailable(resolveRef, args: { slug: string; scope?: string }): { available: boolean | undefined; resourceRef: string | null }
|
|
188
|
+
```
|
|
135
189
|
|
|
136
|
-
`available` is `undefined` while loading, `true` when the
|
|
137
|
-
`false` when
|
|
138
|
-
|
|
139
|
-
words, and
|
|
190
|
+
`available` is `undefined` while loading, `true` when the query returns `null`,
|
|
191
|
+
and `false` when it returns a holder. `resourceRef` is the holder, or `null`
|
|
192
|
+
when free or loading. This is a hint: it does not check length, `pattern` or
|
|
193
|
+
reserved words, and another caller can claim the slug before `reserve` runs.
|
|
140
194
|
|
|
141
|
-
### `useResolve
|
|
195
|
+
### `useResolve`
|
|
142
196
|
|
|
143
|
-
|
|
197
|
+
```ts
|
|
198
|
+
useResolve(resolveRef, args: { slug: string; scope?: string }): string | null | undefined
|
|
199
|
+
```
|
|
144
200
|
|
|
145
|
-
|
|
201
|
+
The query's result: the holding `resourceRef`, `null` when free, or `undefined`
|
|
202
|
+
while loading.
|
|
146
203
|
|
|
147
|
-
|
|
204
|
+
## Other entry points
|
|
205
|
+
|
|
206
|
+
### `convex.config`
|
|
207
|
+
|
|
208
|
+
The default export of `@vllnt/convex-slugs/convex.config` is the component
|
|
209
|
+
definition, named `slugs`. Mount it with `app.use(config)`, or
|
|
210
|
+
`app.use(config, { name })` for several isolated mounts.
|
|
211
|
+
|
|
212
|
+
### `register`
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
register<TSchema extends GenericSchema>(t: TestConvex<SchemaDefinition<TSchema, boolean>>, name = "slugs"): void
|
|
216
|
+
```
|
|
148
217
|
|
|
149
218
|
From `@vllnt/convex-slugs/test`. Mounts the component in a `convex-test`
|
|
150
|
-
instance under `name
|
|
151
|
-
|
|
219
|
+
instance under `name` (default `"slugs"`). `t` may be built from the host app's
|
|
220
|
+
own schema. Shipped as TypeScript source that uses Vite's `import.meta.glob`, so
|
|
221
|
+
run it under Vitest.
|
|
222
|
+
|
|
223
|
+
### `ComponentApi`
|
|
224
|
+
|
|
225
|
+
From `@vllnt/convex-slugs/_generated/component`. The Convex-generated type of
|
|
226
|
+
the mounted component, the same shape as `SlugsComponent`.
|
|
227
|
+
|
|
228
|
+
## Reason codes
|
|
229
|
+
|
|
230
|
+
`reserve` and `rename` return `{ ok: false, reason }` instead of throwing:
|
|
231
|
+
|
|
232
|
+
| Reason | When |
|
|
233
|
+
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
234
|
+
| `SLUG_INVALID` | After normalization the slug is empty, outside `minLength`..`maxLength`, fails `pattern`, or is over 1024 UTF-16 code units (component cap) |
|
|
235
|
+
| `SLUG_RESERVED` | The normalized slug is in the normalized `reservedWords` |
|
|
236
|
+
| `SLUG_TAKEN` | The slug is held in the scope, or `rename` got `fromSlug === toSlug` after normalization |
|
|
237
|
+
| `SLUG_NOT_FOUND` | `rename` got a `fromSlug` that is not held in the scope |
|
|
238
|
+
|
|
239
|
+
Causes and fixes are in the README's
|
|
240
|
+
[Errors and fixes](../README.md#errors-and-fixes) table.
|
|
241
|
+
|
|
242
|
+
## Errors
|
|
243
|
+
|
|
244
|
+
Invalid input throws a `ConvexError` whose `data` is `{ code, message }`:
|
|
245
|
+
|
|
246
|
+
| Code | Thrown by | When |
|
|
247
|
+
| ------------------ | -------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
248
|
+
| `INVALID_OPTIONS` | `new Slugs(...)` | Empty `defaultScope`, or `minLength`/`maxLength` not integers with `minLength >= 0` and `maxLength >= max(1, minLength)` |
|
|
249
|
+
| `INVALID_ARGUMENT` | component mutations `reserve`, `release`, `rename` | Empty `scope`, empty `resourceRef` (`reserve`), a `scope` or `resourceRef` over 1024 UTF-16 code units (`reserve`, `rename`), or `bounds` that are not integers with `minLength <= maxLength` |
|
|
250
|
+
|
|
251
|
+
The client always sends integer bounds from its validated options, so through
|
|
252
|
+
`Slugs` only an empty `scope` or `resourceRef`, or one over 1024 UTF-16 code
|
|
253
|
+
units, raises `INVALID_ARGUMENT`. The
|
|
254
|
+
queries do not throw for an empty scope. Arguments of the wrong type are
|
|
255
|
+
rejected by Convex argument validation.
|
|
256
|
+
|
|
257
|
+
## Limits
|
|
258
|
+
|
|
259
|
+
`reserve` and `rename` cap what they write at 1024 UTF-16 code units
|
|
260
|
+
(`string.length`), the same counting as `minLength` and `maxLength`. The cap
|
|
261
|
+
holds whatever `maxLength` or `bounds` say:
|
|
262
|
+
|
|
263
|
+
| Input | Over 1024 code units |
|
|
264
|
+
| --------------------------------------- | ------------------------- |
|
|
265
|
+
| `slug` (`reserve`), `toSlug` (`rename`) | returns `SLUG_INVALID` |
|
|
266
|
+
| `scope` (`reserve`, `rename`) | throws `INVALID_ARGUMENT` |
|
|
267
|
+
| `resourceRef` (`reserve`) | throws `INVALID_ARGUMENT` |
|
|
268
|
+
|
|
269
|
+
A value of exactly 1024 passes. 😀 counts 2, so 512 😀 pass and 513 are
|
|
270
|
+
rejected, although that is only 513 code points. `release`, `fromSlug` and the
|
|
271
|
+
queries are not capped. Rows a version before 0.2.0 stored over the cap stay
|
|
272
|
+
readable through `resolve`, `redirectFor` and `slugForResource`, and `release`
|
|
273
|
+
removes them. Renaming such a slug to a shorter one works; `reserve` or
|
|
274
|
+
`rename` onto it returns `SLUG_INVALID`, and writes in such a scope throw
|
|
275
|
+
`INVALID_ARGUMENT`.
|
|
276
|
+
|
|
277
|
+
## Normalization
|
|
278
|
+
|
|
279
|
+
Every slug argument goes through `String.prototype.normalize("NFC")`, then
|
|
280
|
+
`.trim()`, then `.toLowerCase()` when `foldCase` is `true`. Reserved words are
|
|
281
|
+
normalized the same way, so `reservedWords: ["Admin"]` blocks `admin`.
|
|
282
|
+
|
|
283
|
+
- NFC makes precomposed and combining-character spellings of the same letter
|
|
284
|
+
one slug. It does not apply compatibility folding (NFKC), confusable
|
|
285
|
+
detection or zero-width-character stripping; use `pattern` to restrict the
|
|
286
|
+
alphabet.
|
|
287
|
+
- Lengths count UTF-16 code units of the normalized slug (a single emoji such
|
|
288
|
+
as 😀 counts 2; flags and joined emoji count more).
|
|
289
|
+
- `toLowerCase()` is not full Unicode case folding: `"STRASSE"` and `"straße"`
|
|
290
|
+
stay different slugs.
|
|
291
|
+
|
|
292
|
+
## Concurrency
|
|
293
|
+
|
|
294
|
+
`reserve` and `rename` each run as one Convex mutation, which is serializable.
|
|
295
|
+
When several callers claim the same slug at once, exactly one succeeds and the
|
|
296
|
+
others receive `SLUG_TAKEN`. Convex indexes are not unique constraints, so
|
|
297
|
+
always write through these mutations. Reads use `.first()`, so a duplicate row
|
|
298
|
+
created outside them returns one value instead of throwing.
|
package/llms.txt
CHANGED
|
@@ -1,31 +1,67 @@
|
|
|
1
1
|
# @vllnt/convex-slugs
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> Convex component for unique slugs, handles and usernames per scope, with rename redirects, a TypeScript client and React hooks.
|
|
4
4
|
|
|
5
|
-
`@vllnt/convex-slugs`
|
|
6
|
-
|
|
5
|
+
Install `@vllnt/convex-slugs` (unreleased builds from `main` use the `canary`
|
|
6
|
+
dist-tag). Peer dependency `convex@^1.45.0`; optional `react@>=18` for
|
|
7
|
+
`@vllnt/convex-slugs/react`. Mount it with `app.use(config)` from
|
|
8
|
+
`@vllnt/convex-slugs/convex.config` and call it through
|
|
9
|
+
`new Slugs(components.slugs, options?)` from host mutations and queries.
|
|
10
|
+
Methods: `reserve`, `release`, `rename`, `resolve`, `redirectFor`,
|
|
11
|
+
`slugForResource`. Uniqueness is per `(scope, slug)` and rides the Convex
|
|
12
|
+
mutation transaction. Slugs are normalized with NFC, `trim()` and (by default)
|
|
13
|
+
`toLowerCase()`. `rename` records an old -> new redirect and collapses chains.
|
|
14
|
+
`reserve` and `rename` return `{ ok: false, reason }` (`SLUG_INVALID`,
|
|
15
|
+
`SLUG_RESERVED`, `SLUG_TAKEN`, `SLUG_NOT_FOUND`) instead of throwing. Invalid
|
|
16
|
+
input throws `ConvexError` with `data.code` `INVALID_OPTIONS` or
|
|
17
|
+
`INVALID_ARGUMENT`. Defaults: scope `"global"`, `minLength` 1, `maxLength` 256.
|
|
18
|
+
`reserve` and `rename` cap writes at 1024 UTF-16 code units (`string.length`):
|
|
19
|
+
a longer slug returns `SLUG_INVALID`, a longer `scope` or `resourceRef` throws
|
|
20
|
+
`INVALID_ARGUMENT`; `release` and the queries are not capped.
|
|
21
|
+
The host owns auth and the meaning of the opaque `resourceRef`. React hooks
|
|
22
|
+
`useSlugAvailable` and `useResolve` take the host's own public query.
|
|
7
23
|
|
|
8
|
-
|
|
24
|
+
## When to use
|
|
25
|
+
|
|
26
|
+
- Use it when each public name (article slug, `@handle`, username, workspace
|
|
27
|
+
slug) must be unique inside a scope, even under concurrent claims.
|
|
28
|
+
- Use it when renaming must keep old URLs working through a redirect.
|
|
29
|
+
- Use it when you need lookups both ways: slug to resource and resource to slug.
|
|
30
|
+
- Do not use it when the key must stay secret (share links, magic links,
|
|
31
|
+
invites): a slug is meant to be public (it appears in URLs). Use
|
|
32
|
+
[`@vllnt/convex-tokens`](https://oss.vllnt.com/packages/convex-tokens).
|
|
33
|
+
|
|
34
|
+
## For coding agents
|
|
35
|
+
|
|
36
|
+
The docs are bundled in the package at `node_modules/@vllnt/convex-slugs/docs/`
|
|
37
|
+
and match the installed version. Read them, and the shipped `src/`, before
|
|
38
|
+
writing code. The links below to `./` paths are files inside the installed package.
|
|
9
39
|
|
|
10
40
|
## Docs
|
|
11
41
|
|
|
12
|
-
- [README](./README.md):
|
|
13
|
-
- [API Reference](./docs/API.md):
|
|
42
|
+
- [README](./README.md): Install, quick start, common tasks, limits
|
|
43
|
+
- [API Reference](./docs/API.md): Every export, signature, return shape and validation rule
|
|
44
|
+
- [Errors and fixes](./README.md#errors-and-fixes): Every reason and error code, its cause and the fix
|
|
14
45
|
- [Changelog](./CHANGELOG.md): Version history and release notes
|
|
15
|
-
- [Contributing](https://github.com/vllnt/convex-slugs/blob/main/CONTRIBUTING.md): Development setup, validation commands, release workflow
|
|
46
|
+
- [Contributing](https://github.com/vllnt/convex-slugs/blob/main/CONTRIBUTING.md): Development setup, validation commands, release workflow (repository only)
|
|
16
47
|
|
|
17
48
|
## Examples
|
|
18
49
|
|
|
19
50
|
- [Example component wrapper](https://github.com/vllnt/convex-slugs/blob/main/example/convex/example.ts): Host-app wrapper functions
|
|
20
|
-
- [Example tests](https://github.com/vllnt/convex-slugs/blob/main/example/convex/example.test.ts):
|
|
21
|
-
|
|
22
|
-
## Ecosystem
|
|
23
|
-
|
|
24
|
-
- [vllnt oss](https://oss.vllnt.com/llms.txt): Index of vllnt open-source packages
|
|
51
|
+
- [Example tests](https://github.com/vllnt/convex-slugs/blob/main/example/convex/example.test.ts): Simulated usage coverage with `convex-test`
|
|
25
52
|
|
|
26
53
|
## Optional
|
|
27
54
|
|
|
28
|
-
- [AGENTS](https://github.com/vllnt/convex-slugs/blob/main/AGENTS.md):
|
|
55
|
+
- [AGENTS](https://github.com/vllnt/convex-slugs/blob/main/AGENTS.md): Contributor instructions for working in this repository (repository only)
|
|
29
56
|
- [Security Policy](https://github.com/vllnt/convex-slugs/blob/main/SECURITY.md): Vulnerability reporting policy
|
|
30
57
|
- [Code of Conduct](https://github.com/vllnt/convex-slugs/blob/main/CODE_OF_CONDUCT.md): Community participation guidelines
|
|
31
58
|
- [License](./LICENSE): MIT license
|
|
59
|
+
|
|
60
|
+
## Ecosystem
|
|
61
|
+
|
|
62
|
+
- [Package page](https://oss.vllnt.com/packages/convex-slugs)
|
|
63
|
+
- [Release feed](https://oss.vllnt.com/packages/convex-slugs/rss.xml)
|
|
64
|
+
- [All @vllnt packages](https://oss.vllnt.com/llms.txt)
|
|
65
|
+
- [Search vllnt packages and docs](https://index.vllnt.com)
|
|
66
|
+
- [vllnt](https://vllnt.com/llms.txt)
|
|
67
|
+
- [Author](https://bntvllnt.com)
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vllnt/convex-slugs",
|
|
3
|
-
"version": "0.2.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Convex component for unique slugs, handles and usernames per scope, with rename redirects, a TypeScript client and React hooks.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"packageManager": "pnpm@10.34.8",
|
|
@@ -58,6 +58,7 @@
|
|
|
58
58
|
"preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test:coverage && pnpm typecheck && pnpm generate:llms",
|
|
59
59
|
"check:agents": "node -e \"const f=require('node:fs');if(!f.existsSync('AGENTS.md')||f.existsSync('CLAUDE.md')||f.existsSync('.claude'))process.exit(1)\"",
|
|
60
60
|
"check:docs": "node scripts/test-check-documentation.mjs && node scripts/check-documentation.mjs",
|
|
61
|
+
"check:links": "node scripts/check-links.mjs",
|
|
61
62
|
"codegen": "pnpm convex codegen --component-dir ./src/component"
|
|
62
63
|
},
|
|
63
64
|
"peerDependencies": {
|
|
@@ -66,17 +67,15 @@
|
|
|
66
67
|
},
|
|
67
68
|
"devDependencies": {
|
|
68
69
|
"@edge-runtime/vm": "^5.0.0",
|
|
69
|
-
"@testing-library/react": "^16.3.3",
|
|
70
70
|
"@types/react": "^19.3.0",
|
|
71
71
|
"@types/react-dom": "^19.3.0",
|
|
72
72
|
"@typescript/native": "npm:typescript@7.0.2",
|
|
73
73
|
"@vitest/coverage-v8": "^5.1.1",
|
|
74
|
-
"@vllnt/eslint-config": "2.
|
|
75
|
-
"@vllnt/typescript": "3.0.
|
|
74
|
+
"@vllnt/eslint-config": "2.2.0",
|
|
75
|
+
"@vllnt/typescript": "3.0.1",
|
|
76
76
|
"convex": "^1.51.0",
|
|
77
77
|
"convex-test": "^0.0.73",
|
|
78
78
|
"eslint": "^10.15.0",
|
|
79
|
-
"jsdom": "^31.0.1",
|
|
80
79
|
"prettier": "^3.10.1",
|
|
81
80
|
"react": "^19.3.1",
|
|
82
81
|
"react-dom": "^19.3.1",
|
|
@@ -101,7 +100,7 @@
|
|
|
101
100
|
"url": "https://github.com/vllnt/convex-slugs"
|
|
102
101
|
},
|
|
103
102
|
"engines": {
|
|
104
|
-
"node": ">=
|
|
103
|
+
"node": ">=20"
|
|
105
104
|
},
|
|
106
105
|
"publishConfig": {
|
|
107
106
|
"access": "public"
|
|
@@ -110,11 +109,15 @@
|
|
|
110
109
|
"keywords": [
|
|
111
110
|
"convex",
|
|
112
111
|
"convex-component",
|
|
113
|
-
"
|
|
112
|
+
"typescript",
|
|
114
113
|
"slug",
|
|
114
|
+
"slugs",
|
|
115
115
|
"handle",
|
|
116
116
|
"username",
|
|
117
|
-
"
|
|
117
|
+
"unique",
|
|
118
|
+
"redirect",
|
|
119
|
+
"permalink",
|
|
120
|
+
"react"
|
|
118
121
|
],
|
|
119
122
|
"peerDependenciesMeta": {
|
|
120
123
|
"react": {
|
|
@@ -127,10 +130,6 @@
|
|
|
127
130
|
"deepmerge-ts@<8.0.0": "^8.0.0",
|
|
128
131
|
"handlebars@<4.7.10": "^4.7.10"
|
|
129
132
|
},
|
|
130
|
-
"minimumReleaseAge": 10080,
|
|
131
|
-
"minimumReleaseAgeExclude": [
|
|
132
|
-
"@vllnt/*"
|
|
133
|
-
],
|
|
134
133
|
"ignoredBuiltDependencies": [
|
|
135
134
|
"esbuild"
|
|
136
135
|
]
|