@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.
Files changed (71) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +138 -0
  3. package/dist/client/index.d.ts +90 -0
  4. package/dist/client/index.d.ts.map +1 -0
  5. package/dist/client/index.js +102 -0
  6. package/dist/client/index.js.map +1 -0
  7. package/dist/client/types.d.ts +27 -0
  8. package/dist/client/types.d.ts.map +1 -0
  9. package/dist/client/types.js +3 -0
  10. package/dist/client/types.js.map +1 -0
  11. package/dist/component/_generated/api.d.ts +38 -0
  12. package/dist/component/_generated/api.d.ts.map +1 -0
  13. package/dist/component/_generated/api.js +31 -0
  14. package/dist/component/_generated/api.js.map +1 -0
  15. package/dist/component/_generated/component.d.ts +67 -0
  16. package/dist/component/_generated/component.d.ts.map +1 -0
  17. package/dist/component/_generated/component.js +11 -0
  18. package/dist/component/_generated/component.js.map +1 -0
  19. package/dist/component/_generated/dataModel.d.ts +46 -0
  20. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  21. package/dist/component/_generated/dataModel.js +11 -0
  22. package/dist/component/_generated/dataModel.js.map +1 -0
  23. package/dist/component/_generated/server.d.ts +133 -0
  24. package/dist/component/_generated/server.d.ts.map +1 -0
  25. package/dist/component/_generated/server.js +80 -0
  26. package/dist/component/_generated/server.js.map +1 -0
  27. package/dist/component/convex.config.d.ts +3 -0
  28. package/dist/component/convex.config.d.ts.map +1 -0
  29. package/dist/component/convex.config.js +7 -0
  30. package/dist/component/convex.config.js.map +1 -0
  31. package/dist/component/mutations.d.ts +102 -0
  32. package/dist/component/mutations.d.ts.map +1 -0
  33. package/dist/component/mutations.js +191 -0
  34. package/dist/component/mutations.js.map +1 -0
  35. package/dist/component/queries.d.ts +44 -0
  36. package/dist/component/queries.d.ts.map +1 -0
  37. package/dist/component/queries.js +75 -0
  38. package/dist/component/queries.js.map +1 -0
  39. package/dist/component/schema.d.ts +36 -0
  40. package/dist/component/schema.d.ts.map +1 -0
  41. package/dist/component/schema.js +27 -0
  42. package/dist/component/schema.js.map +1 -0
  43. package/dist/component/validators.d.ts +30 -0
  44. package/dist/component/validators.d.ts.map +1 -0
  45. package/dist/component/validators.js +25 -0
  46. package/dist/component/validators.js.map +1 -0
  47. package/dist/react/index.d.ts +55 -0
  48. package/dist/react/index.d.ts.map +1 -0
  49. package/dist/react/index.js +40 -0
  50. package/dist/react/index.js.map +1 -0
  51. package/dist/shared.d.ts +49 -0
  52. package/dist/shared.d.ts.map +1 -0
  53. package/dist/shared.js +52 -0
  54. package/dist/shared.js.map +1 -0
  55. package/package.json +115 -0
  56. package/src/client/index.ts +213 -0
  57. package/src/client/types.ts +29 -0
  58. package/src/component/_generated/api.ts +54 -0
  59. package/src/component/_generated/component.ts +90 -0
  60. package/src/component/_generated/dataModel.ts +60 -0
  61. package/src/component/_generated/server.ts +169 -0
  62. package/src/component/component.test.ts +115 -0
  63. package/src/component/convex.config.ts +9 -0
  64. package/src/component/mutations.ts +212 -0
  65. package/src/component/queries.ts +84 -0
  66. package/src/component/schema.ts +27 -0
  67. package/src/component/validators.ts +32 -0
  68. package/src/react/index.test.tsx +110 -0
  69. package/src/react/index.tsx +79 -0
  70. package/src/shared.ts +78 -0
  71. package/src/test.ts +12 -0
@@ -0,0 +1,75 @@
1
+ import { v } from "convex/values";
2
+ import { internalQuery, query } from "./_generated/server";
3
+ /**
4
+ * Resolve `slug` to the `resourceRef` currently holding it within `scope`, or
5
+ * `null`. Uses `.first()` so a stray duplicate row degrades to one answer
6
+ * instead of throwing.
7
+ *
8
+ * @returns The held `resourceRef`, or `null` when the slug is free.
9
+ */
10
+ export const resolve = query({
11
+ args: { scope: v.string(), slug: v.string() },
12
+ returns: v.union(v.null(), v.string()),
13
+ handler: async (ctx, args) => {
14
+ const row = await ctx.db
15
+ .query("slugs")
16
+ .withIndex("by_scope_slug", (q) => q.eq("scope", args.scope).eq("slug", args.slug))
17
+ .first();
18
+ return row === null ? null : row.resourceRef;
19
+ },
20
+ });
21
+ /**
22
+ * Where a renamed `slug` now points within `scope`. After chain repointing this
23
+ * is always the live slug (A→B→C resolves A to C), or `null` when `slug` was
24
+ * never renamed away.
25
+ *
26
+ * @returns The current target slug, or `null`.
27
+ */
28
+ export const redirectFor = query({
29
+ args: { scope: v.string(), slug: v.string() },
30
+ returns: v.union(v.null(), v.string()),
31
+ handler: async (ctx, args) => {
32
+ const row = await ctx.db
33
+ .query("redirects")
34
+ .withIndex("by_scope_from", (q) => q.eq("scope", args.scope).eq("fromSlug", args.slug))
35
+ .first();
36
+ return row === null ? null : row.toSlug;
37
+ },
38
+ });
39
+ /**
40
+ * The slug currently held by `resourceRef` within `scope`. Assumes one slug per
41
+ * `(scope, resourceRef)` (the host reserves at most one); returns the first if
42
+ * the host violated that, or `null` when the resource holds none.
43
+ *
44
+ * @returns The held slug, or `null`.
45
+ */
46
+ export const slugForResource = query({
47
+ args: { scope: v.string(), resourceRef: v.string() },
48
+ returns: v.union(v.null(), v.string()),
49
+ handler: async (ctx, args) => {
50
+ const row = await ctx.db
51
+ .query("slugs")
52
+ .withIndex("by_scope_resource", (q) => q.eq("scope", args.scope).eq("resourceRef", args.resourceRef))
53
+ .first();
54
+ return row === null ? null : row.slug;
55
+ },
56
+ });
57
+ /**
58
+ * Test/inspection escape hatch: count `redirects` rows in `scope`. `internalQuery`,
59
+ * never part of the public client API. Used to assert the one-row-per-source
60
+ * bound (redirect chains repoint in place rather than accumulate).
61
+ *
62
+ * @returns The number of redirect rows in the scope.
63
+ */
64
+ export const countRedirects = internalQuery({
65
+ args: { scope: v.string() },
66
+ returns: v.number(),
67
+ handler: async (ctx, args) => {
68
+ const rows = await ctx.db
69
+ .query("redirects")
70
+ .withIndex("by_scope_from", (q) => q.eq("scope", args.scope))
71
+ .collect();
72
+ return rows.length;
73
+ },
74
+ });
75
+ //# sourceMappingURL=queries.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queries.js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,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE;IAC3B,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE;IACnB,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QAC3B,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,EAAE;aACtB,KAAK,CAAC,WAAW,CAAC;aAClB,SAAS,CAAC,eAAe,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;aAC5D,OAAO,EAAE,CAAC;QACb,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF,CAAC,CAAC"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Sandboxed tables — the slug registry's own concern. `resourceRef` is an opaque
3
+ * host-owned reference (never assume its shape). Uniqueness is on `(scope, slug)`.
4
+ *
5
+ * `redirects` keeps at most one row per `(scope, fromSlug)`. The `by_scope_to`
6
+ * index lets `rename` repoint chains so `redirectFor` always lands on the live
7
+ * slug (A→B then B→C leaves A→C, not A→B).
8
+ */
9
+ declare const _default: import("convex/server").SchemaDefinition<{
10
+ slugs: import("convex/server").TableDefinition<import("convex/values").VObject<{
11
+ resourceRef: string;
12
+ scope: string;
13
+ slug: string;
14
+ }, {
15
+ scope: import("convex/values").VString<string, "required">;
16
+ slug: import("convex/values").VString<string, "required">;
17
+ resourceRef: import("convex/values").VString<string, "required">;
18
+ }, "required", "resourceRef" | "scope" | "slug">, {
19
+ by_scope_resource: ["scope", "resourceRef", "_creationTime"];
20
+ by_scope_slug: ["scope", "slug", "_creationTime"];
21
+ }, {}, {}>;
22
+ redirects: import("convex/server").TableDefinition<import("convex/values").VObject<{
23
+ fromSlug: string;
24
+ scope: string;
25
+ toSlug: string;
26
+ }, {
27
+ scope: import("convex/values").VString<string, "required">;
28
+ fromSlug: import("convex/values").VString<string, "required">;
29
+ toSlug: import("convex/values").VString<string, "required">;
30
+ }, "required", "fromSlug" | "scope" | "toSlug">, {
31
+ by_scope_from: ["scope", "fromSlug", "_creationTime"];
32
+ by_scope_to: ["scope", "toSlug", "_creationTime"];
33
+ }, {}, {}>;
34
+ }, true>;
35
+ export default _default;
36
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG"}
@@ -0,0 +1,27 @@
1
+ import { defineSchema, defineTable } from "convex/server";
2
+ import { v } from "convex/values";
3
+ /**
4
+ * Sandboxed tables — the slug registry's own concern. `resourceRef` is an opaque
5
+ * host-owned reference (never assume its shape). Uniqueness is on `(scope, slug)`.
6
+ *
7
+ * `redirects` keeps at most one row per `(scope, fromSlug)`. The `by_scope_to`
8
+ * index lets `rename` repoint chains so `redirectFor` always lands on the live
9
+ * slug (A→B then B→C leaves A→C, not A→B).
10
+ */
11
+ export default defineSchema({
12
+ slugs: defineTable({
13
+ scope: v.string(),
14
+ slug: v.string(),
15
+ resourceRef: v.string(),
16
+ })
17
+ .index("by_scope_slug", ["scope", "slug"])
18
+ .index("by_scope_resource", ["scope", "resourceRef"]),
19
+ redirects: defineTable({
20
+ scope: v.string(),
21
+ fromSlug: v.string(),
22
+ toSlug: v.string(),
23
+ })
24
+ .index("by_scope_from", ["scope", "fromSlug"])
25
+ .index("by_scope_to", ["scope", "toSlug"]),
26
+ });
27
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC;;;;;;;GAOG;AACH,eAAe,YAAY,CAAC;IAC1B,KAAK,EAAE,WAAW,CAAC;QACjB,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;QAChB,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE;KACxB,CAAC;SACC,KAAK,CAAC,eAAe,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;SACzC,KAAK,CAAC,mBAAmB,EAAE,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;IACvD,SAAS,EAAE,WAAW,CAAC;QACrB,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;QACpB,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE;KACnB,CAAC;SACC,KAAK,CAAC,eAAe,EAAE,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;SAC7C,KAAK,CAAC,aAAa,EAAE,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;CAC7C,CAAC,CAAC"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Stable code-tag for a rejected slug write, as a Convex validator union. Kept
3
+ * in lockstep with the `SlugReason` string-literal union in `src/shared.ts`.
4
+ */
5
+ export declare const slugReason: import("convex/values").VUnion<"SLUG_INVALID" | "SLUG_NOT_FOUND" | "SLUG_RESERVED" | "SLUG_TAKEN", [import("convex/values").VLiteral<"SLUG_TAKEN", "required">, import("convex/values").VLiteral<"SLUG_NOT_FOUND", "required">, import("convex/values").VLiteral<"SLUG_INVALID", "required">, import("convex/values").VLiteral<"SLUG_RESERVED", "required">], "required", never>;
6
+ /**
7
+ * Result of a write that may be rejected. `reason` carries a stable code-tag
8
+ * (one of {@link slugReason}) when `ok` is `false`, and is absent when `ok` is
9
+ * `true`.
10
+ */
11
+ export declare const reserveResult: import("convex/values").VObject<{
12
+ ok: boolean;
13
+ reason?: "SLUG_INVALID" | "SLUG_NOT_FOUND" | "SLUG_RESERVED" | "SLUG_TAKEN" | undefined;
14
+ }, {
15
+ ok: import("convex/values").VBoolean<boolean, "required">;
16
+ reason: import("convex/values").VUnion<"SLUG_INVALID" | "SLUG_NOT_FOUND" | "SLUG_RESERVED" | "SLUG_TAKEN" | undefined, [import("convex/values").VLiteral<"SLUG_TAKEN", "required">, import("convex/values").VLiteral<"SLUG_NOT_FOUND", "required">, import("convex/values").VLiteral<"SLUG_INVALID", "required">, import("convex/values").VLiteral<"SLUG_RESERVED", "required">], "optional", never>;
17
+ }, "required", "ok" | "reason">;
18
+ /**
19
+ * Numeric length backstop applied inside the component. The client owns the full
20
+ * config (charset, reserved words); these bounds re-guard empty/oversized slugs
21
+ * at the trust boundary even if a caller bypasses the client class.
22
+ */
23
+ export declare const lengthBounds: import("convex/values").VObject<{
24
+ maxLength: number;
25
+ minLength: number;
26
+ }, {
27
+ minLength: import("convex/values").VFloat64<number, "required">;
28
+ maxLength: import("convex/values").VFloat64<number, "required">;
29
+ }, "required", "maxLength" | "minLength">;
30
+ //# sourceMappingURL=validators.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../../src/component/validators.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,eAAO,MAAM,UAAU,kXAKtB,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,aAAa;;;;;;+BAGxB,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,YAAY;;;;;;yCAGvB,CAAC"}
@@ -0,0 +1,25 @@
1
+ import { v } from "convex/values";
2
+ /**
3
+ * Stable code-tag for a rejected slug write, as a Convex validator union. Kept
4
+ * in lockstep with the `SlugReason` string-literal union in `src/shared.ts`.
5
+ */
6
+ export const slugReason = v.union(v.literal("SLUG_TAKEN"), v.literal("SLUG_NOT_FOUND"), v.literal("SLUG_INVALID"), v.literal("SLUG_RESERVED"));
7
+ /**
8
+ * Result of a write that may be rejected. `reason` carries a stable code-tag
9
+ * (one of {@link slugReason}) when `ok` is `false`, and is absent when `ok` is
10
+ * `true`.
11
+ */
12
+ export const reserveResult = v.object({
13
+ ok: v.boolean(),
14
+ reason: v.optional(slugReason),
15
+ });
16
+ /**
17
+ * Numeric length backstop applied inside the component. The client owns the full
18
+ * config (charset, reserved words); these bounds re-guard empty/oversized slugs
19
+ * at the trust boundary even if a caller bypasses the client class.
20
+ */
21
+ export const lengthBounds = v.object({
22
+ minLength: v.number(),
23
+ maxLength: v.number(),
24
+ });
25
+ //# sourceMappingURL=validators.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validators.js","sourceRoot":"","sources":["../../src/component/validators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC;;;GAGG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAC/B,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,EACvB,CAAC,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAC3B,CAAC,CAAC,OAAO,CAAC,cAAc,CAAC,EACzB,CAAC,CAAC,OAAO,CAAC,eAAe,CAAC,CAC3B,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC;IACpC,EAAE,EAAE,CAAC,CAAC,OAAO,EAAE;IACf,MAAM,EAAE,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC;CAC/B,CAAC,CAAC;AAEH;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,CAAC,MAAM,CAAC;IACnC,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;IACrB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;CACtB,CAAC,CAAC"}
@@ -0,0 +1,55 @@
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
+ import type { FunctionReference } from "convex/server";
10
+ /**
11
+ * Args accepted by the slug-resolving hooks: a slug plus an optional scope.
12
+ * Declared as a type alias (not an interface) so it satisfies Convex's
13
+ * `DefaultFunctionArgs` index-signature constraint on {@link ResolveRef}.
14
+ */
15
+ type ResolveArgs = {
16
+ slug: string;
17
+ scope?: string;
18
+ };
19
+ /**
20
+ * The host's re-exported `resolve` query reference: takes `{ slug, scope? }`
21
+ * (the host forwards the optional scope to the client) and returns the holding
22
+ * `resourceRef`, or `null` when the slug is free.
23
+ */
24
+ type ResolveRef = FunctionReference<"query", "public", ResolveArgs, string | null>;
25
+ /** Live handle-availability state for {@link useSlugAvailable}. */
26
+ interface SlugAvailability {
27
+ /** `true` when the slug is free, `false` when taken, `undefined` while loading. */
28
+ available: boolean | undefined;
29
+ /** The `resourceRef` holding the slug, or `null` when free / still loading. */
30
+ resourceRef: string | null;
31
+ }
32
+ /**
33
+ * Reactive handle-availability check — live "is this slug free?" UX. Wraps the
34
+ * host's re-exported `resolve` query: a slug is available exactly when `resolve`
35
+ * returns `null` (no holder).
36
+ *
37
+ * @param resolveRef - The host's re-exported slug `resolve` query reference.
38
+ * @param args - `{ slug, scope? }` the candidate slug and optional scope.
39
+ * @returns `{ available, resourceRef }`. `available` is `undefined` while the
40
+ * query loads, `true` when the slug is free, `false` when held; `resourceRef`
41
+ * is the current holder, or `null` when free or loading.
42
+ */
43
+ export declare function useSlugAvailable(resolveRef: ResolveRef, args: ResolveArgs): SlugAvailability;
44
+ /**
45
+ * Reactive slug resolution — the `resourceRef` currently holding `slug`, or
46
+ * `null` when free. Wraps the host's re-exported `resolve` query.
47
+ *
48
+ * @param resolveRef - The host's re-exported slug `resolve` query reference.
49
+ * @param args - `{ slug, scope? }` the slug to resolve and optional scope.
50
+ * @returns The held `resourceRef`, `null` when the slug is free, or `undefined`
51
+ * while the query loads.
52
+ */
53
+ export declare function useResolve(resolveRef: ResolveRef, args: ResolveArgs): string | null | undefined;
54
+ export {};
55
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAGvD;;;;GAIG;AACH,KAAK,WAAW,GAAG;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;;;GAIG;AACH,KAAK,UAAU,GAAG,iBAAiB,CACjC,OAAO,EACP,QAAQ,EACR,WAAW,EACX,MAAM,GAAG,IAAI,CACd,CAAC;AAEF,mEAAmE;AACnE,UAAU,gBAAgB;IACxB,mFAAmF;IACnF,SAAS,EAAE,OAAO,GAAG,SAAS,CAAC;IAC/B,+EAA+E;IAC/E,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5B;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAC9B,UAAU,EAAE,UAAU,EACtB,IAAI,EAAE,WAAW,GAChB,gBAAgB,CAMlB;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CACxB,UAAU,EAAE,UAAU,EACtB,IAAI,EAAE,WAAW,GAChB,MAAM,GAAG,IAAI,GAAG,SAAS,CAE3B"}
@@ -0,0 +1,40 @@
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
+ import { useQuery } from "convex/react";
10
+ /**
11
+ * Reactive handle-availability check — live "is this slug free?" UX. Wraps the
12
+ * host's re-exported `resolve` query: a slug is available exactly when `resolve`
13
+ * returns `null` (no holder).
14
+ *
15
+ * @param resolveRef - The host's re-exported slug `resolve` query reference.
16
+ * @param args - `{ slug, scope? }` the candidate slug and optional scope.
17
+ * @returns `{ available, resourceRef }`. `available` is `undefined` while the
18
+ * query loads, `true` when the slug is free, `false` when held; `resourceRef`
19
+ * is the current holder, or `null` when free or loading.
20
+ */
21
+ export function useSlugAvailable(resolveRef, args) {
22
+ const result = useQuery(resolveRef, args);
23
+ return {
24
+ available: result === undefined ? undefined : result === null,
25
+ resourceRef: result ?? null,
26
+ };
27
+ }
28
+ /**
29
+ * Reactive slug resolution — the `resourceRef` currently holding `slug`, or
30
+ * `null` when free. Wraps the host's re-exported `resolve` query.
31
+ *
32
+ * @param resolveRef - The host's re-exported slug `resolve` query reference.
33
+ * @param args - `{ slug, scope? }` the slug to resolve and optional scope.
34
+ * @returns The held `resourceRef`, `null` when the slug is free, or `undefined`
35
+ * while the query loads.
36
+ */
37
+ export function useResolve(resolveRef, args) {
38
+ return useQuery(resolveRef, args);
39
+ }
40
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAgCxC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,gBAAgB,CAC9B,UAAsB,EACtB,IAAiB;IAEjB,MAAM,MAAM,GAAG,QAAQ,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;IAC1C,OAAO;QACL,SAAS,EAAE,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,KAAK,IAAI;QAC7D,WAAW,EAAE,MAAM,IAAI,IAAI;KAC5B,CAAC;AACJ,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,UAAU,CACxB,UAAsB,EACtB,IAAiB;IAEjB,OAAO,QAAQ,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;AACpC,CAAC"}
@@ -0,0 +1,49 @@
1
+ /** Shared constants + pure utilities used by both `client/` and `component/`. */
2
+ export declare const COMPONENT_NAME = "slugs";
3
+ /** Default namespace when the host does not scope a slug. */
4
+ export declare const DEFAULT_SCOPE = "global";
5
+ /** Default minimum slug length (after normalization). */
6
+ export declare const DEFAULT_MIN_LENGTH = 1;
7
+ /** Default maximum slug length (after normalization). */
8
+ export declare const DEFAULT_MAX_LENGTH = 256;
9
+ /** Opaque host-supplied resource reference. Never assume its shape or source. */
10
+ export type ResourceRef = string;
11
+ /** Stable code-tag for a rejected slug write. */
12
+ export type SlugReason = "SLUG_TAKEN" | "SLUG_NOT_FOUND" | "SLUG_INVALID" | "SLUG_RESERVED";
13
+ /**
14
+ * Resolved validation config (no optionals) — what {@link validateSlug} checks
15
+ * a normalized slug against.
16
+ */
17
+ export interface SlugConstraints {
18
+ minLength: number;
19
+ maxLength: number;
20
+ pattern?: RegExp;
21
+ reservedWords: ReadonlyArray<string>;
22
+ }
23
+ /**
24
+ * Normalize a slug for storage/lookup. `foldCase` lowercases (the default —
25
+ * handles are case-insensitive); when false the raw case is preserved.
26
+ *
27
+ * Applies Unicode NFC normalization before trimming so visually-identical
28
+ * slugs with different compositions (e.g. precomposed vs. combining-character
29
+ * forms) are treated as the same string.
30
+ *
31
+ * @param slug - Raw, host-supplied slug.
32
+ * @param foldCase - Lowercase before comparison when `true`.
33
+ * @returns The NFC-normalized, trimmed (and optionally lowercased) slug.
34
+ */
35
+ export declare function normalizeSlug(slug: string, foldCase: boolean): string;
36
+ /**
37
+ * Validate an already-normalized slug against {@link SlugConstraints}. Returns a
38
+ * {@link SlugReason} when the slug is rejected, or `null` when it is acceptable.
39
+ *
40
+ * Order: empty/length first (`SLUG_INVALID`), then charset (`SLUG_INVALID`),
41
+ * then reserved words (`SLUG_RESERVED`). Reserved-word matching is exact on the
42
+ * normalized form.
43
+ *
44
+ * @param slug - The normalized slug (output of {@link normalizeSlug}).
45
+ * @param c - Resolved validation constraints.
46
+ * @returns A rejection reason, or `null` when valid.
47
+ */
48
+ export declare function validateSlug(slug: string, c: SlugConstraints): SlugReason | null;
49
+ //# sourceMappingURL=shared.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,iFAAiF;AAEjF,eAAO,MAAM,cAAc,UAAU,CAAC;AAEtC,6DAA6D;AAC7D,eAAO,MAAM,aAAa,WAAW,CAAC;AAEtC,yDAAyD;AACzD,eAAO,MAAM,kBAAkB,IAAI,CAAC;AAEpC,yDAAyD;AACzD,eAAO,MAAM,kBAAkB,MAAM,CAAC;AAEtC,iFAAiF;AACjF,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC;AAEjC,iDAAiD;AACjD,MAAM,MAAM,UAAU,GAClB,YAAY,GACZ,gBAAgB,GAChB,cAAc,GACd,eAAe,CAAC;AAEpB;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,aAAa,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;CACtC;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,GAAG,MAAM,CAGrE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,CAAC,EAAE,eAAe,GAAG,UAAU,GAAG,IAAI,CAchF"}
package/dist/shared.js ADDED
@@ -0,0 +1,52 @@
1
+ /** Shared constants + pure utilities used by both `client/` and `component/`. */
2
+ export const COMPONENT_NAME = "slugs";
3
+ /** Default namespace when the host does not scope a slug. */
4
+ export const DEFAULT_SCOPE = "global";
5
+ /** Default minimum slug length (after normalization). */
6
+ export const DEFAULT_MIN_LENGTH = 1;
7
+ /** Default maximum slug length (after normalization). */
8
+ export const DEFAULT_MAX_LENGTH = 256;
9
+ /**
10
+ * Normalize a slug for storage/lookup. `foldCase` lowercases (the default —
11
+ * handles are case-insensitive); when false the raw case is preserved.
12
+ *
13
+ * Applies Unicode NFC normalization before trimming so visually-identical
14
+ * slugs with different compositions (e.g. precomposed vs. combining-character
15
+ * forms) are treated as the same string.
16
+ *
17
+ * @param slug - Raw, host-supplied slug.
18
+ * @param foldCase - Lowercase before comparison when `true`.
19
+ * @returns The NFC-normalized, trimmed (and optionally lowercased) slug.
20
+ */
21
+ export function normalizeSlug(slug, foldCase) {
22
+ const normalized = slug.normalize("NFC").trim();
23
+ return foldCase ? normalized.toLowerCase() : normalized;
24
+ }
25
+ /**
26
+ * Validate an already-normalized slug against {@link SlugConstraints}. Returns a
27
+ * {@link SlugReason} when the slug is rejected, or `null` when it is acceptable.
28
+ *
29
+ * Order: empty/length first (`SLUG_INVALID`), then charset (`SLUG_INVALID`),
30
+ * then reserved words (`SLUG_RESERVED`). Reserved-word matching is exact on the
31
+ * normalized form.
32
+ *
33
+ * @param slug - The normalized slug (output of {@link normalizeSlug}).
34
+ * @param c - Resolved validation constraints.
35
+ * @returns A rejection reason, or `null` when valid.
36
+ */
37
+ export function validateSlug(slug, c) {
38
+ if (slug.length < Math.max(1, c.minLength)) {
39
+ return "SLUG_INVALID";
40
+ }
41
+ if (slug.length > c.maxLength) {
42
+ return "SLUG_INVALID";
43
+ }
44
+ if (c.pattern !== undefined && !c.pattern.test(slug)) {
45
+ return "SLUG_INVALID";
46
+ }
47
+ if (c.reservedWords.includes(slug)) {
48
+ return "SLUG_RESERVED";
49
+ }
50
+ return null;
51
+ }
52
+ //# sourceMappingURL=shared.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.js","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,iFAAiF;AAEjF,MAAM,CAAC,MAAM,cAAc,GAAG,OAAO,CAAC;AAEtC,6DAA6D;AAC7D,MAAM,CAAC,MAAM,aAAa,GAAG,QAAQ,CAAC;AAEtC,yDAAyD;AACzD,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,CAAC;AAEpC,yDAAyD;AACzD,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,CAAC;AAuBtC;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,aAAa,CAAC,IAAY,EAAE,QAAiB;IAC3D,MAAM,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC;IAChD,OAAO,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC;AAC1D,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,YAAY,CAAC,IAAY,EAAE,CAAkB;IAC3D,IAAI,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,EAAE,CAAC;QAC3C,OAAO,cAAc,CAAC;IACxB,CAAC;IACD,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC;QAC9B,OAAO,cAAc,CAAC;IACxB,CAAC;IACD,IAAI,CAAC,CAAC,OAAO,KAAK,SAAS,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACrD,OAAO,cAAc,CAAC;IACxB,CAAC;IACD,IAAI,CAAC,CAAC,aAAa,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QACnC,OAAO,eAAe,CAAC;IACzB,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
package/package.json ADDED
@@ -0,0 +1,115 @@
1
+ {
2
+ "name": "@vllnt/convex-slugs",
3
+ "version": "0.1.0-canary.2051641",
4
+ "description": "Unique slug and handle registry with rename redirects, as a Convex component",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "packageManager": "pnpm@9.15.4",
8
+ "files": [
9
+ "dist",
10
+ "src"
11
+ ],
12
+ "exports": {
13
+ "./package.json": "./package.json",
14
+ ".": {
15
+ "types": "./dist/client/index.d.ts",
16
+ "default": "./dist/client/index.js"
17
+ },
18
+ "./react": {
19
+ "types": "./dist/react/index.d.ts",
20
+ "default": "./dist/react/index.js"
21
+ },
22
+ "./test": "./src/test.ts",
23
+ "./_generated/component.js": {
24
+ "types": "./dist/component/_generated/component.d.ts"
25
+ },
26
+ "./_generated/component": {
27
+ "types": "./dist/component/_generated/component.d.ts"
28
+ },
29
+ "./convex.config": {
30
+ "types": "./dist/component/convex.config.d.ts",
31
+ "default": "./dist/component/convex.config.js"
32
+ },
33
+ "./convex.config.js": {
34
+ "types": "./dist/component/convex.config.d.ts",
35
+ "default": "./dist/component/convex.config.js"
36
+ }
37
+ },
38
+ "types": "./dist/client/index.d.ts",
39
+ "module": "./dist/client/index.js",
40
+ "scripts": {
41
+ "build": "tsc --project ./tsconfig.build.json",
42
+ "build:codegen": "pnpm codegen && pnpm build",
43
+ "build:clean": "rm -rf dist *.tsbuildinfo && pnpm build:codegen",
44
+ "typecheck": "tsc --noEmit",
45
+ "typecheck:ci": "tsc --noEmit --project tsconfig.ci.json",
46
+ "lint": "pnpm check:agents && eslint .",
47
+ "test": "vitest run --passWithNoTests",
48
+ "test:watch": "vitest --typecheck --clearScreen false",
49
+ "test:coverage": "vitest run --coverage --coverage.reporter=text",
50
+ "generate:llms": "node scripts/generate-llms.mjs",
51
+ "preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test:coverage && pnpm typecheck && pnpm generate:llms",
52
+ "prepublishOnly": "npm whoami || npm login",
53
+ "alpha": "npm version prerelease --preid alpha && npm publish --tag alpha && git push --follow-tags",
54
+ "release": "npm version patch && npm publish && git push --follow-tags",
55
+ "check:agents": "node -e \"const f=require('node:fs');if(!f.existsSync('AGENTS.md')||f.existsSync('CLAUDE.md')||f.existsSync('.claude'))process.exit(1)\"",
56
+ "codegen": "pnpm convex codegen --component-dir ./src/component"
57
+ },
58
+ "peerDependencies": {
59
+ "convex": "^1.45.0",
60
+ "react": ">=18"
61
+ },
62
+ "devDependencies": {
63
+ "@edge-runtime/vm": "^5.0.0",
64
+ "@testing-library/react": "^16.3.3",
65
+ "@types/react": "^19.2.18",
66
+ "@types/react-dom": "^19.2.5",
67
+ "@typescript/native": "npm:typescript@7.0.2",
68
+ "@vitest/coverage-v8": "^4.1.11",
69
+ "@vllnt/eslint-config": "2.0.0",
70
+ "@vllnt/typescript": "2.0.0",
71
+ "convex": "^1.45.0",
72
+ "convex-test": "^0.0.56",
73
+ "eslint": "^9.39.5",
74
+ "jsdom": "^29.1.1",
75
+ "prettier": "^3.9.6",
76
+ "react": "^19.2.8",
77
+ "react-dom": "^19.2.8",
78
+ "typescript": "npm:@typescript/typescript6@^6.0.2",
79
+ "vite": "^8.2.2",
80
+ "vitest": "^4.1.11"
81
+ },
82
+ "homepage": "https://github.com/vllnt/convex-slugs#readme",
83
+ "bugs": {
84
+ "url": "https://github.com/vllnt/convex-slugs/issues"
85
+ },
86
+ "author": {
87
+ "name": "bntvllnt",
88
+ "url": "https://bntvllnt.com"
89
+ },
90
+ "funding": {
91
+ "type": "github",
92
+ "url": "https://github.com/sponsors/bntvllnt"
93
+ },
94
+ "repository": {
95
+ "type": "git",
96
+ "url": "https://github.com/vllnt/convex-slugs"
97
+ },
98
+ "engines": {
99
+ "node": ">=18"
100
+ },
101
+ "publishConfig": {
102
+ "access": "public"
103
+ },
104
+ "sideEffects": false,
105
+ "keywords": [
106
+ "convex",
107
+ "convex-component",
108
+ "slugs"
109
+ ],
110
+ "peerDependenciesMeta": {
111
+ "react": {
112
+ "optional": true
113
+ }
114
+ }
115
+ }