@vllnt/convex-slugs 0.1.0-canary.2051641
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/LICENSE +21 -0
- package/README.md +138 -0
- package/dist/client/index.d.ts +90 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +102 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +27 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +3 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +38 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +67 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +133 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +7 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/mutations.d.ts +102 -0
- package/dist/component/mutations.d.ts.map +1 -0
- package/dist/component/mutations.js +191 -0
- package/dist/component/mutations.js.map +1 -0
- package/dist/component/queries.d.ts +44 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +75 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +36 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +27 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/validators.d.ts +30 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +25 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/react/index.d.ts +55 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +40 -0
- package/dist/react/index.js.map +1 -0
- package/dist/shared.d.ts +49 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +52 -0
- package/dist/shared.js.map +1 -0
- package/package.json +115 -0
- package/src/client/index.ts +213 -0
- package/src/client/types.ts +29 -0
- package/src/component/_generated/api.ts +54 -0
- package/src/component/_generated/component.ts +90 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +169 -0
- package/src/component/component.test.ts +115 -0
- package/src/component/convex.config.ts +9 -0
- package/src/component/mutations.ts +212 -0
- package/src/component/queries.ts +84 -0
- package/src/component/schema.ts +27 -0
- package/src/component/validators.ts +32 -0
- package/src/react/index.test.tsx +110 -0
- package/src/react/index.tsx +79 -0
- package/src/shared.ts +78 -0
- package/src/test.ts +12 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { convexTest } from "convex-test";
|
|
2
|
+
import { describe, expect, test } from "vitest";
|
|
3
|
+
import { api, internal } from "./_generated/api";
|
|
4
|
+
import schema from "./schema";
|
|
5
|
+
|
|
6
|
+
const modules = import.meta.glob("./**/*.ts");
|
|
7
|
+
const scope = "global";
|
|
8
|
+
const bounds = { minLength: 1, maxLength: 256 };
|
|
9
|
+
|
|
10
|
+
function setup() {
|
|
11
|
+
return convexTest(schema, modules);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function reserve(
|
|
15
|
+
t: ReturnType<typeof setup>,
|
|
16
|
+
slug: string,
|
|
17
|
+
resourceRef: string,
|
|
18
|
+
) {
|
|
19
|
+
return await t.mutation(api.mutations.reserve, {
|
|
20
|
+
scope,
|
|
21
|
+
slug,
|
|
22
|
+
resourceRef,
|
|
23
|
+
bounds,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function rename(
|
|
28
|
+
t: ReturnType<typeof setup>,
|
|
29
|
+
fromSlug: string,
|
|
30
|
+
toSlug: string,
|
|
31
|
+
) {
|
|
32
|
+
return await t.mutation(api.mutations.rename, {
|
|
33
|
+
scope,
|
|
34
|
+
fromSlug,
|
|
35
|
+
toSlug,
|
|
36
|
+
bounds,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe("slugs component — internal repair and inspection APIs", () => {
|
|
41
|
+
test("counts collapsed redirect chains", async () => {
|
|
42
|
+
const t = setup();
|
|
43
|
+
await reserve(t, "a", "doc");
|
|
44
|
+
await rename(t, "a", "b");
|
|
45
|
+
await rename(t, "b", "c");
|
|
46
|
+
expect(await t.query(internal.queries.countRedirects, { scope })).toBe(2);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("rename patches a pre-existing redirect row in place", async () => {
|
|
50
|
+
const t = setup();
|
|
51
|
+
await reserve(t, "src", "r");
|
|
52
|
+
await t.mutation(internal.mutations.insertRedirectRaw, {
|
|
53
|
+
scope,
|
|
54
|
+
fromSlug: "src",
|
|
55
|
+
toSlug: "stale",
|
|
56
|
+
});
|
|
57
|
+
expect(await rename(t, "src", "dst")).toEqual({ ok: true });
|
|
58
|
+
expect(
|
|
59
|
+
await t.query(api.queries.redirectFor, { scope, slug: "src" }),
|
|
60
|
+
).toBe("dst");
|
|
61
|
+
expect(await t.query(internal.queries.countRedirects, { scope })).toBe(1);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("duplicate slug rows degrade instead of throwing", async () => {
|
|
65
|
+
const t = setup();
|
|
66
|
+
await reserve(t, "twin", "first");
|
|
67
|
+
await t.mutation(internal.mutations.insertRaw, {
|
|
68
|
+
scope,
|
|
69
|
+
slug: "twin",
|
|
70
|
+
resourceRef: "first-dup",
|
|
71
|
+
});
|
|
72
|
+
expect(await t.query(api.queries.resolve, { scope, slug: "twin" })).toBe(
|
|
73
|
+
"first",
|
|
74
|
+
);
|
|
75
|
+
expect(
|
|
76
|
+
await t.query(api.queries.slugForResource, {
|
|
77
|
+
scope,
|
|
78
|
+
resourceRef: "first",
|
|
79
|
+
}),
|
|
80
|
+
).toBe("twin");
|
|
81
|
+
expect(await rename(t, "twin", "twin3")).toEqual({ ok: true });
|
|
82
|
+
expect(
|
|
83
|
+
await t.mutation(api.mutations.release, { scope, slug: "twin" }),
|
|
84
|
+
).toBeNull();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("repoints and then clears multiple inbound redirects", async () => {
|
|
88
|
+
const t = setup();
|
|
89
|
+
await reserve(t, "mb", "r1");
|
|
90
|
+
await t.mutation(internal.mutations.insertRedirectRaw, {
|
|
91
|
+
scope,
|
|
92
|
+
fromSlug: "ma",
|
|
93
|
+
toSlug: "mb",
|
|
94
|
+
});
|
|
95
|
+
await t.mutation(internal.mutations.insertRedirectRaw, {
|
|
96
|
+
scope,
|
|
97
|
+
fromSlug: "mx",
|
|
98
|
+
toSlug: "mb",
|
|
99
|
+
});
|
|
100
|
+
await rename(t, "mb", "mc");
|
|
101
|
+
expect(await t.query(api.queries.redirectFor, { scope, slug: "ma" })).toBe(
|
|
102
|
+
"mc",
|
|
103
|
+
);
|
|
104
|
+
expect(await t.query(api.queries.redirectFor, { scope, slug: "mx" })).toBe(
|
|
105
|
+
"mc",
|
|
106
|
+
);
|
|
107
|
+
await t.mutation(api.mutations.release, { scope, slug: "mc" });
|
|
108
|
+
expect(
|
|
109
|
+
await t.query(api.queries.redirectFor, { scope, slug: "ma" }),
|
|
110
|
+
).toBeNull();
|
|
111
|
+
expect(
|
|
112
|
+
await t.query(api.queries.redirectFor, { scope, slug: "mx" }),
|
|
113
|
+
).toBeNull();
|
|
114
|
+
});
|
|
115
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { defineComponent } from "convex/server";
|
|
2
|
+
|
|
3
|
+
const component = defineComponent("slugs");
|
|
4
|
+
|
|
5
|
+
// Nest official child components here, e.g.:
|
|
6
|
+
// import sharded from "@convex-dev/sharded-counter/convex.config";
|
|
7
|
+
// component.use(sharded);
|
|
8
|
+
|
|
9
|
+
export default component;
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { internalMutation, mutation } from "./_generated/server";
|
|
3
|
+
import { lengthBounds, reserveResult } from "./validators";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Reserve `slug` for `resourceRef` within `scope`. The slug arrives already
|
|
7
|
+
* normalized by the client; `bounds` re-guards empty/oversized input at the
|
|
8
|
+
* trust boundary. If the slug was a live redirect target's source, that stale
|
|
9
|
+
* redirect row is removed (the slug is a live name again).
|
|
10
|
+
*
|
|
11
|
+
* @returns `{ ok: true }`, or `{ ok: false, reason }` where reason is
|
|
12
|
+
* `SLUG_INVALID` (empty/over length) or `SLUG_TAKEN` (already held in scope).
|
|
13
|
+
*/
|
|
14
|
+
export const reserve = mutation({
|
|
15
|
+
args: {
|
|
16
|
+
scope: v.string(),
|
|
17
|
+
slug: v.string(),
|
|
18
|
+
resourceRef: v.string(),
|
|
19
|
+
bounds: lengthBounds,
|
|
20
|
+
},
|
|
21
|
+
returns: reserveResult,
|
|
22
|
+
handler: async (ctx, args) => {
|
|
23
|
+
if (args.slug.length < Math.max(1, args.bounds.minLength)) {
|
|
24
|
+
return { ok: false, reason: "SLUG_INVALID" as const };
|
|
25
|
+
}
|
|
26
|
+
if (args.slug.length > args.bounds.maxLength) {
|
|
27
|
+
return { ok: false, reason: "SLUG_INVALID" as const };
|
|
28
|
+
}
|
|
29
|
+
const existing = await ctx.db
|
|
30
|
+
.query("slugs")
|
|
31
|
+
.withIndex("by_scope_slug", (q) =>
|
|
32
|
+
q.eq("scope", args.scope).eq("slug", args.slug),
|
|
33
|
+
)
|
|
34
|
+
.first();
|
|
35
|
+
if (existing !== null) {
|
|
36
|
+
return { ok: false, reason: "SLUG_TAKEN" as const };
|
|
37
|
+
}
|
|
38
|
+
const dangling = await ctx.db
|
|
39
|
+
.query("redirects")
|
|
40
|
+
.withIndex("by_scope_from", (q) =>
|
|
41
|
+
q.eq("scope", args.scope).eq("fromSlug", args.slug),
|
|
42
|
+
)
|
|
43
|
+
.collect();
|
|
44
|
+
for (const row of dangling) {
|
|
45
|
+
await ctx.db.delete("redirects", row._id);
|
|
46
|
+
}
|
|
47
|
+
await ctx.db.insert("slugs", {
|
|
48
|
+
scope: args.scope,
|
|
49
|
+
slug: args.slug,
|
|
50
|
+
resourceRef: args.resourceRef,
|
|
51
|
+
});
|
|
52
|
+
return { ok: true };
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Release `slug` from `scope`. Idempotent — releasing an unheld slug is a no-op.
|
|
58
|
+
* Uses `.first()` so a stray duplicate row degrades to releasing one row instead
|
|
59
|
+
* of throwing. Also deletes all `redirects` rows whose `toSlug` equals the
|
|
60
|
+
* released slug so that `redirectFor(old)` returns `null` after release rather
|
|
61
|
+
* than pointing at a now-dead target.
|
|
62
|
+
*
|
|
63
|
+
* @returns `null`.
|
|
64
|
+
*/
|
|
65
|
+
export const release = mutation({
|
|
66
|
+
args: { scope: v.string(), slug: v.string() },
|
|
67
|
+
returns: v.null(),
|
|
68
|
+
handler: async (ctx, args) => {
|
|
69
|
+
const existing = await ctx.db
|
|
70
|
+
.query("slugs")
|
|
71
|
+
.withIndex("by_scope_slug", (q) =>
|
|
72
|
+
q.eq("scope", args.scope).eq("slug", args.slug),
|
|
73
|
+
)
|
|
74
|
+
.first();
|
|
75
|
+
if (existing !== null) {
|
|
76
|
+
await ctx.db.delete("slugs", existing._id);
|
|
77
|
+
}
|
|
78
|
+
const inbound = await ctx.db
|
|
79
|
+
.query("redirects")
|
|
80
|
+
.withIndex("by_scope_to", (q) =>
|
|
81
|
+
q.eq("scope", args.scope).eq("toSlug", args.slug),
|
|
82
|
+
)
|
|
83
|
+
.collect();
|
|
84
|
+
for (const row of inbound) {
|
|
85
|
+
await ctx.db.delete("redirects", row._id);
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Rename `fromSlug` to `toSlug` within `scope` and record a redirect. `toSlug`
|
|
93
|
+
* arrives normalized; `bounds` re-guards it. On success the slug row is patched,
|
|
94
|
+
* existing redirects that pointed at `fromSlug` are repointed to `toSlug` (so a
|
|
95
|
+
* chain A→B→C collapses to A→C), and a single redirect row per `(scope,
|
|
96
|
+
* fromSlug)` is upserted (patched if present, else inserted) to bound growth.
|
|
97
|
+
*
|
|
98
|
+
* Self-rename (`fromSlug === toSlug`) is rejected with `SLUG_TAKEN` before any
|
|
99
|
+
* DB read, preventing a self-loop redirect from being created.
|
|
100
|
+
*
|
|
101
|
+
* @returns `{ ok: true }`, or `{ ok: false, reason }` where reason is
|
|
102
|
+
* `SLUG_INVALID` (empty/over length `toSlug`), `SLUG_NOT_FOUND` (`fromSlug` not
|
|
103
|
+
* held), or `SLUG_TAKEN` (`toSlug` already held or equals `fromSlug`).
|
|
104
|
+
*/
|
|
105
|
+
export const rename = mutation({
|
|
106
|
+
args: {
|
|
107
|
+
scope: v.string(),
|
|
108
|
+
fromSlug: v.string(),
|
|
109
|
+
toSlug: v.string(),
|
|
110
|
+
bounds: lengthBounds,
|
|
111
|
+
},
|
|
112
|
+
returns: reserveResult,
|
|
113
|
+
handler: async (ctx, args) => {
|
|
114
|
+
if (args.fromSlug === args.toSlug) {
|
|
115
|
+
return { ok: false, reason: "SLUG_TAKEN" as const };
|
|
116
|
+
}
|
|
117
|
+
if (args.toSlug.length < Math.max(1, args.bounds.minLength)) {
|
|
118
|
+
return { ok: false, reason: "SLUG_INVALID" as const };
|
|
119
|
+
}
|
|
120
|
+
if (args.toSlug.length > args.bounds.maxLength) {
|
|
121
|
+
return { ok: false, reason: "SLUG_INVALID" as const };
|
|
122
|
+
}
|
|
123
|
+
const from = await ctx.db
|
|
124
|
+
.query("slugs")
|
|
125
|
+
.withIndex("by_scope_slug", (q) =>
|
|
126
|
+
q.eq("scope", args.scope).eq("slug", args.fromSlug),
|
|
127
|
+
)
|
|
128
|
+
.first();
|
|
129
|
+
if (from === null) {
|
|
130
|
+
return { ok: false, reason: "SLUG_NOT_FOUND" as const };
|
|
131
|
+
}
|
|
132
|
+
const taken = await ctx.db
|
|
133
|
+
.query("slugs")
|
|
134
|
+
.withIndex("by_scope_slug", (q) =>
|
|
135
|
+
q.eq("scope", args.scope).eq("slug", args.toSlug),
|
|
136
|
+
)
|
|
137
|
+
.first();
|
|
138
|
+
if (taken !== null) {
|
|
139
|
+
return { ok: false, reason: "SLUG_TAKEN" as const };
|
|
140
|
+
}
|
|
141
|
+
await ctx.db.patch("slugs", from._id, { slug: args.toSlug });
|
|
142
|
+
|
|
143
|
+
const inbound = await ctx.db
|
|
144
|
+
.query("redirects")
|
|
145
|
+
.withIndex("by_scope_to", (q) =>
|
|
146
|
+
q.eq("scope", args.scope).eq("toSlug", args.fromSlug),
|
|
147
|
+
)
|
|
148
|
+
.collect();
|
|
149
|
+
for (const row of inbound) {
|
|
150
|
+
await ctx.db.patch("redirects", row._id, { toSlug: args.toSlug });
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const existing = await ctx.db
|
|
154
|
+
.query("redirects")
|
|
155
|
+
.withIndex("by_scope_from", (q) =>
|
|
156
|
+
q.eq("scope", args.scope).eq("fromSlug", args.fromSlug),
|
|
157
|
+
)
|
|
158
|
+
.first();
|
|
159
|
+
if (existing !== null) {
|
|
160
|
+
await ctx.db.patch("redirects", existing._id, { toSlug: args.toSlug });
|
|
161
|
+
} else {
|
|
162
|
+
await ctx.db.insert("redirects", {
|
|
163
|
+
scope: args.scope,
|
|
164
|
+
fromSlug: args.fromSlug,
|
|
165
|
+
toSlug: args.toSlug,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
return { ok: true };
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Test/repair escape hatch: insert a `slugs` row with no uniqueness check.
|
|
174
|
+
* `internalMutation`, so it is never part of the public client API. Used to
|
|
175
|
+
* simulate a stray duplicate `(scope, slug)` row (Convex indexes are not unique
|
|
176
|
+
* constraints) and verify reads degrade via `.first()` instead of throwing.
|
|
177
|
+
*
|
|
178
|
+
* @returns `null`.
|
|
179
|
+
*/
|
|
180
|
+
export const insertRaw = internalMutation({
|
|
181
|
+
args: { scope: v.string(), slug: v.string(), resourceRef: v.string() },
|
|
182
|
+
returns: v.null(),
|
|
183
|
+
handler: async (ctx, args) => {
|
|
184
|
+
await ctx.db.insert("slugs", {
|
|
185
|
+
scope: args.scope,
|
|
186
|
+
slug: args.slug,
|
|
187
|
+
resourceRef: args.resourceRef,
|
|
188
|
+
});
|
|
189
|
+
return null;
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Test/repair escape hatch: insert a `redirects` row directly. `internalMutation`,
|
|
195
|
+
* never part of the public client API. Used to seed a `(scope, fromSlug)`
|
|
196
|
+
* redirect that coexists with a live slug so `rename`'s upsert-patch branch (one
|
|
197
|
+
* row per source) is exercised.
|
|
198
|
+
*
|
|
199
|
+
* @returns `null`.
|
|
200
|
+
*/
|
|
201
|
+
export const insertRedirectRaw = internalMutation({
|
|
202
|
+
args: { scope: v.string(), fromSlug: v.string(), toSlug: v.string() },
|
|
203
|
+
returns: v.null(),
|
|
204
|
+
handler: async (ctx, args) => {
|
|
205
|
+
await ctx.db.insert("redirects", {
|
|
206
|
+
scope: args.scope,
|
|
207
|
+
fromSlug: args.fromSlug,
|
|
208
|
+
toSlug: args.toSlug,
|
|
209
|
+
});
|
|
210
|
+
return null;
|
|
211
|
+
},
|
|
212
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { internalQuery, query } from "./_generated/server";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Resolve `slug` to the `resourceRef` currently holding it within `scope`, or
|
|
6
|
+
* `null`. Uses `.first()` so a stray duplicate row degrades to one answer
|
|
7
|
+
* instead of throwing.
|
|
8
|
+
*
|
|
9
|
+
* @returns The held `resourceRef`, or `null` when the slug is free.
|
|
10
|
+
*/
|
|
11
|
+
export const resolve = query({
|
|
12
|
+
args: { scope: v.string(), slug: v.string() },
|
|
13
|
+
returns: v.union(v.null(), v.string()),
|
|
14
|
+
handler: async (ctx, args) => {
|
|
15
|
+
const row = await ctx.db
|
|
16
|
+
.query("slugs")
|
|
17
|
+
.withIndex("by_scope_slug", (q) =>
|
|
18
|
+
q.eq("scope", args.scope).eq("slug", args.slug),
|
|
19
|
+
)
|
|
20
|
+
.first();
|
|
21
|
+
return row === null ? null : row.resourceRef;
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Where a renamed `slug` now points within `scope`. After chain repointing this
|
|
27
|
+
* is always the live slug (A→B→C resolves A to C), or `null` when `slug` was
|
|
28
|
+
* never renamed away.
|
|
29
|
+
*
|
|
30
|
+
* @returns The current target slug, or `null`.
|
|
31
|
+
*/
|
|
32
|
+
export const redirectFor = query({
|
|
33
|
+
args: { scope: v.string(), slug: v.string() },
|
|
34
|
+
returns: v.union(v.null(), v.string()),
|
|
35
|
+
handler: async (ctx, args) => {
|
|
36
|
+
const row = await ctx.db
|
|
37
|
+
.query("redirects")
|
|
38
|
+
.withIndex("by_scope_from", (q) =>
|
|
39
|
+
q.eq("scope", args.scope).eq("fromSlug", args.slug),
|
|
40
|
+
)
|
|
41
|
+
.first();
|
|
42
|
+
return row === null ? null : row.toSlug;
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The slug currently held by `resourceRef` within `scope`. Assumes one slug per
|
|
48
|
+
* `(scope, resourceRef)` (the host reserves at most one); returns the first if
|
|
49
|
+
* the host violated that, or `null` when the resource holds none.
|
|
50
|
+
*
|
|
51
|
+
* @returns The held slug, or `null`.
|
|
52
|
+
*/
|
|
53
|
+
export const slugForResource = query({
|
|
54
|
+
args: { scope: v.string(), resourceRef: v.string() },
|
|
55
|
+
returns: v.union(v.null(), v.string()),
|
|
56
|
+
handler: async (ctx, args) => {
|
|
57
|
+
const row = await ctx.db
|
|
58
|
+
.query("slugs")
|
|
59
|
+
.withIndex("by_scope_resource", (q) =>
|
|
60
|
+
q.eq("scope", args.scope).eq("resourceRef", args.resourceRef),
|
|
61
|
+
)
|
|
62
|
+
.first();
|
|
63
|
+
return row === null ? null : row.slug;
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Test/inspection escape hatch: count `redirects` rows in `scope`. `internalQuery`,
|
|
69
|
+
* never part of the public client API. Used to assert the one-row-per-source
|
|
70
|
+
* bound (redirect chains repoint in place rather than accumulate).
|
|
71
|
+
*
|
|
72
|
+
* @returns The number of redirect rows in the scope.
|
|
73
|
+
*/
|
|
74
|
+
export const countRedirects = internalQuery({
|
|
75
|
+
args: { scope: v.string() },
|
|
76
|
+
returns: v.number(),
|
|
77
|
+
handler: async (ctx, args) => {
|
|
78
|
+
const rows = await ctx.db
|
|
79
|
+
.query("redirects")
|
|
80
|
+
.withIndex("by_scope_from", (q) => q.eq("scope", args.scope))
|
|
81
|
+
.collect();
|
|
82
|
+
return rows.length;
|
|
83
|
+
},
|
|
84
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { defineSchema, defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sandboxed tables — the slug registry's own concern. `resourceRef` is an opaque
|
|
6
|
+
* host-owned reference (never assume its shape). Uniqueness is on `(scope, slug)`.
|
|
7
|
+
*
|
|
8
|
+
* `redirects` keeps at most one row per `(scope, fromSlug)`. The `by_scope_to`
|
|
9
|
+
* index lets `rename` repoint chains so `redirectFor` always lands on the live
|
|
10
|
+
* slug (A→B then B→C leaves A→C, not A→B).
|
|
11
|
+
*/
|
|
12
|
+
export default defineSchema({
|
|
13
|
+
slugs: defineTable({
|
|
14
|
+
scope: v.string(),
|
|
15
|
+
slug: v.string(),
|
|
16
|
+
resourceRef: v.string(),
|
|
17
|
+
})
|
|
18
|
+
.index("by_scope_slug", ["scope", "slug"])
|
|
19
|
+
.index("by_scope_resource", ["scope", "resourceRef"]),
|
|
20
|
+
redirects: defineTable({
|
|
21
|
+
scope: v.string(),
|
|
22
|
+
fromSlug: v.string(),
|
|
23
|
+
toSlug: v.string(),
|
|
24
|
+
})
|
|
25
|
+
.index("by_scope_from", ["scope", "fromSlug"])
|
|
26
|
+
.index("by_scope_to", ["scope", "toSlug"]),
|
|
27
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Stable code-tag for a rejected slug write, as a Convex validator union. Kept
|
|
5
|
+
* in lockstep with the `SlugReason` string-literal union in `src/shared.ts`.
|
|
6
|
+
*/
|
|
7
|
+
export const slugReason = v.union(
|
|
8
|
+
v.literal("SLUG_TAKEN"),
|
|
9
|
+
v.literal("SLUG_NOT_FOUND"),
|
|
10
|
+
v.literal("SLUG_INVALID"),
|
|
11
|
+
v.literal("SLUG_RESERVED"),
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Result of a write that may be rejected. `reason` carries a stable code-tag
|
|
16
|
+
* (one of {@link slugReason}) when `ok` is `false`, and is absent when `ok` is
|
|
17
|
+
* `true`.
|
|
18
|
+
*/
|
|
19
|
+
export const reserveResult = v.object({
|
|
20
|
+
ok: v.boolean(),
|
|
21
|
+
reason: v.optional(slugReason),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Numeric length backstop applied inside the component. The client owns the full
|
|
26
|
+
* config (charset, reserved words); these bounds re-guard empty/oversized slugs
|
|
27
|
+
* at the trust boundary even if a caller bypasses the client class.
|
|
28
|
+
*/
|
|
29
|
+
export const lengthBounds = v.object({
|
|
30
|
+
minLength: v.number(),
|
|
31
|
+
maxLength: v.number(),
|
|
32
|
+
});
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Unit tests for the optional React front-tooling hooks. `convex/react`'s
|
|
5
|
+
* `useQuery` is mocked so the hooks are exercised in isolation — no live Convex
|
|
6
|
+
* client. Runs under jsdom (per-file pragma above) while the global vitest env
|
|
7
|
+
* stays edge-runtime for the backend suite.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { renderHook } from "@testing-library/react";
|
|
11
|
+
import type { FunctionReference } from "convex/server";
|
|
12
|
+
import { useQuery } from "convex/react";
|
|
13
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
14
|
+
|
|
15
|
+
import { useResolve, useSlugAvailable } from "./index";
|
|
16
|
+
|
|
17
|
+
vi.mock("convex/react", () => ({
|
|
18
|
+
useQuery: vi.fn(),
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
const mockedUseQuery = vi.mocked(useQuery);
|
|
22
|
+
|
|
23
|
+
// A stand-in for the host's re-exported `resolve` query reference. The hooks
|
|
24
|
+
// only forward it to `useQuery`, so an opaque cast is sufficient for the test.
|
|
25
|
+
const resolveRef = "resolveRef" as unknown as FunctionReference<
|
|
26
|
+
"query",
|
|
27
|
+
"public",
|
|
28
|
+
{ slug: string; scope?: string },
|
|
29
|
+
string | null
|
|
30
|
+
>;
|
|
31
|
+
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
mockedUseQuery.mockReset();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe("useResolve", () => {
|
|
37
|
+
it("forwards (ref, args) to useQuery and returns the held resourceRef", () => {
|
|
38
|
+
mockedUseQuery.mockReturnValue("user_1");
|
|
39
|
+
const args = { slug: "my-handle", scope: "users" };
|
|
40
|
+
|
|
41
|
+
const { result } = renderHook(() => useResolve(resolveRef, args));
|
|
42
|
+
|
|
43
|
+
expect(mockedUseQuery).toHaveBeenCalledWith(resolveRef, args);
|
|
44
|
+
expect(result.current).toBe("user_1");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("returns undefined while the query is loading", () => {
|
|
48
|
+
mockedUseQuery.mockReturnValue(undefined);
|
|
49
|
+
|
|
50
|
+
const { result } = renderHook(() =>
|
|
51
|
+
useResolve(resolveRef, { slug: "loading" }),
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
expect(result.current).toBeUndefined();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("returns null when the slug is free", () => {
|
|
58
|
+
mockedUseQuery.mockReturnValue(null);
|
|
59
|
+
|
|
60
|
+
const { result } = renderHook(() =>
|
|
61
|
+
useResolve(resolveRef, { slug: "free" }),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
expect(result.current).toBeNull();
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe("useSlugAvailable", () => {
|
|
69
|
+
it("forwards (ref, args) to useQuery", () => {
|
|
70
|
+
mockedUseQuery.mockReturnValue(null);
|
|
71
|
+
const args = { slug: "candidate", scope: "users" };
|
|
72
|
+
|
|
73
|
+
renderHook(() => useSlugAvailable(resolveRef, args));
|
|
74
|
+
|
|
75
|
+
expect(mockedUseQuery).toHaveBeenCalledWith(resolveRef, args);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("loading: useQuery undefined -> available undefined, resourceRef null", () => {
|
|
79
|
+
mockedUseQuery.mockReturnValue(undefined);
|
|
80
|
+
|
|
81
|
+
const { result } = renderHook(() =>
|
|
82
|
+
useSlugAvailable(resolveRef, { slug: "loading" }),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
expect(result.current.available).toBeUndefined();
|
|
86
|
+
expect(result.current.resourceRef).toBeNull();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("free: useQuery null -> available true, resourceRef null", () => {
|
|
90
|
+
mockedUseQuery.mockReturnValue(null);
|
|
91
|
+
|
|
92
|
+
const { result } = renderHook(() =>
|
|
93
|
+
useSlugAvailable(resolveRef, { slug: "free" }),
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
expect(result.current.available).toBe(true);
|
|
97
|
+
expect(result.current.resourceRef).toBeNull();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("taken: useQuery string -> available false, resourceRef the holder", () => {
|
|
101
|
+
mockedUseQuery.mockReturnValue("user_42");
|
|
102
|
+
|
|
103
|
+
const { result } = renderHook(() =>
|
|
104
|
+
useSlugAvailable(resolveRef, { slug: "taken" }),
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
expect(result.current.available).toBe(false);
|
|
108
|
+
expect(result.current.resourceRef).toBe("user_42");
|
|
109
|
+
});
|
|
110
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional, tree-shakeable React front-tooling for `@vllnt/convex-slugs`.
|
|
3
|
+
*
|
|
4
|
+
* Thin reactive hooks over `useQuery` from `convex/react`. Each hook takes the
|
|
5
|
+
* HOST's re-exported slug function reference plus its args — the component never
|
|
6
|
+
* imports the host `api`. `react` and `convex/react` are optional peer deps: a
|
|
7
|
+
* backend-only consumer pulls none of this code.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { FunctionReference } from "convex/server";
|
|
11
|
+
import { useQuery } from "convex/react";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Args accepted by the slug-resolving hooks: a slug plus an optional scope.
|
|
15
|
+
* Declared as a type alias (not an interface) so it satisfies Convex's
|
|
16
|
+
* `DefaultFunctionArgs` index-signature constraint on {@link ResolveRef}.
|
|
17
|
+
*/
|
|
18
|
+
type ResolveArgs = {
|
|
19
|
+
slug: string;
|
|
20
|
+
scope?: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The host's re-exported `resolve` query reference: takes `{ slug, scope? }`
|
|
25
|
+
* (the host forwards the optional scope to the client) and returns the holding
|
|
26
|
+
* `resourceRef`, or `null` when the slug is free.
|
|
27
|
+
*/
|
|
28
|
+
type ResolveRef = FunctionReference<
|
|
29
|
+
"query",
|
|
30
|
+
"public",
|
|
31
|
+
ResolveArgs,
|
|
32
|
+
string | null
|
|
33
|
+
>;
|
|
34
|
+
|
|
35
|
+
/** Live handle-availability state for {@link useSlugAvailable}. */
|
|
36
|
+
interface SlugAvailability {
|
|
37
|
+
/** `true` when the slug is free, `false` when taken, `undefined` while loading. */
|
|
38
|
+
available: boolean | undefined;
|
|
39
|
+
/** The `resourceRef` holding the slug, or `null` when free / still loading. */
|
|
40
|
+
resourceRef: string | null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Reactive handle-availability check — live "is this slug free?" UX. Wraps the
|
|
45
|
+
* host's re-exported `resolve` query: a slug is available exactly when `resolve`
|
|
46
|
+
* returns `null` (no holder).
|
|
47
|
+
*
|
|
48
|
+
* @param resolveRef - The host's re-exported slug `resolve` query reference.
|
|
49
|
+
* @param args - `{ slug, scope? }` the candidate slug and optional scope.
|
|
50
|
+
* @returns `{ available, resourceRef }`. `available` is `undefined` while the
|
|
51
|
+
* query loads, `true` when the slug is free, `false` when held; `resourceRef`
|
|
52
|
+
* is the current holder, or `null` when free or loading.
|
|
53
|
+
*/
|
|
54
|
+
export function useSlugAvailable(
|
|
55
|
+
resolveRef: ResolveRef,
|
|
56
|
+
args: ResolveArgs,
|
|
57
|
+
): SlugAvailability {
|
|
58
|
+
const result = useQuery(resolveRef, args);
|
|
59
|
+
return {
|
|
60
|
+
available: result === undefined ? undefined : result === null,
|
|
61
|
+
resourceRef: result ?? null,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Reactive slug resolution — the `resourceRef` currently holding `slug`, or
|
|
67
|
+
* `null` when free. Wraps the host's re-exported `resolve` query.
|
|
68
|
+
*
|
|
69
|
+
* @param resolveRef - The host's re-exported slug `resolve` query reference.
|
|
70
|
+
* @param args - `{ slug, scope? }` the slug to resolve and optional scope.
|
|
71
|
+
* @returns The held `resourceRef`, `null` when the slug is free, or `undefined`
|
|
72
|
+
* while the query loads.
|
|
73
|
+
*/
|
|
74
|
+
export function useResolve(
|
|
75
|
+
resolveRef: ResolveRef,
|
|
76
|
+
args: ResolveArgs,
|
|
77
|
+
): string | null | undefined {
|
|
78
|
+
return useQuery(resolveRef, args);
|
|
79
|
+
}
|