@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
package/src/shared.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/** Shared constants + pure utilities used by both `client/` and `component/`. */
|
|
2
|
+
|
|
3
|
+
export const COMPONENT_NAME = "slugs";
|
|
4
|
+
|
|
5
|
+
/** Default namespace when the host does not scope a slug. */
|
|
6
|
+
export const DEFAULT_SCOPE = "global";
|
|
7
|
+
|
|
8
|
+
/** Default minimum slug length (after normalization). */
|
|
9
|
+
export const DEFAULT_MIN_LENGTH = 1;
|
|
10
|
+
|
|
11
|
+
/** Default maximum slug length (after normalization). */
|
|
12
|
+
export const DEFAULT_MAX_LENGTH = 256;
|
|
13
|
+
|
|
14
|
+
/** Opaque host-supplied resource reference. Never assume its shape or source. */
|
|
15
|
+
export type ResourceRef = string;
|
|
16
|
+
|
|
17
|
+
/** Stable code-tag for a rejected slug write. */
|
|
18
|
+
export type SlugReason =
|
|
19
|
+
| "SLUG_TAKEN"
|
|
20
|
+
| "SLUG_NOT_FOUND"
|
|
21
|
+
| "SLUG_INVALID"
|
|
22
|
+
| "SLUG_RESERVED";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Resolved validation config (no optionals) — what {@link validateSlug} checks
|
|
26
|
+
* a normalized slug against.
|
|
27
|
+
*/
|
|
28
|
+
export interface SlugConstraints {
|
|
29
|
+
minLength: number;
|
|
30
|
+
maxLength: number;
|
|
31
|
+
pattern?: RegExp;
|
|
32
|
+
reservedWords: ReadonlyArray<string>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Normalize a slug for storage/lookup. `foldCase` lowercases (the default —
|
|
37
|
+
* handles are case-insensitive); when false the raw case is preserved.
|
|
38
|
+
*
|
|
39
|
+
* Applies Unicode NFC normalization before trimming so visually-identical
|
|
40
|
+
* slugs with different compositions (e.g. precomposed vs. combining-character
|
|
41
|
+
* forms) are treated as the same string.
|
|
42
|
+
*
|
|
43
|
+
* @param slug - Raw, host-supplied slug.
|
|
44
|
+
* @param foldCase - Lowercase before comparison when `true`.
|
|
45
|
+
* @returns The NFC-normalized, trimmed (and optionally lowercased) slug.
|
|
46
|
+
*/
|
|
47
|
+
export function normalizeSlug(slug: string, foldCase: boolean): string {
|
|
48
|
+
const normalized = slug.normalize("NFC").trim();
|
|
49
|
+
return foldCase ? normalized.toLowerCase() : normalized;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Validate an already-normalized slug against {@link SlugConstraints}. Returns a
|
|
54
|
+
* {@link SlugReason} when the slug is rejected, or `null` when it is acceptable.
|
|
55
|
+
*
|
|
56
|
+
* Order: empty/length first (`SLUG_INVALID`), then charset (`SLUG_INVALID`),
|
|
57
|
+
* then reserved words (`SLUG_RESERVED`). Reserved-word matching is exact on the
|
|
58
|
+
* normalized form.
|
|
59
|
+
*
|
|
60
|
+
* @param slug - The normalized slug (output of {@link normalizeSlug}).
|
|
61
|
+
* @param c - Resolved validation constraints.
|
|
62
|
+
* @returns A rejection reason, or `null` when valid.
|
|
63
|
+
*/
|
|
64
|
+
export function validateSlug(slug: string, c: SlugConstraints): SlugReason | null {
|
|
65
|
+
if (slug.length < Math.max(1, c.minLength)) {
|
|
66
|
+
return "SLUG_INVALID";
|
|
67
|
+
}
|
|
68
|
+
if (slug.length > c.maxLength) {
|
|
69
|
+
return "SLUG_INVALID";
|
|
70
|
+
}
|
|
71
|
+
if (c.pattern !== undefined && !c.pattern.test(slug)) {
|
|
72
|
+
return "SLUG_INVALID";
|
|
73
|
+
}
|
|
74
|
+
if (c.reservedWords.includes(slug)) {
|
|
75
|
+
return "SLUG_RESERVED";
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
package/src/test.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { TestConvex } from "convex-test";
|
|
2
|
+
import schema from "./component/schema";
|
|
3
|
+
|
|
4
|
+
const modules = import.meta.glob("./component/**/*.ts");
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Register this component with a `convex-test` instance so consuming apps can
|
|
8
|
+
* test integration: `import { register } from "@vllnt/convex-slugs/test"`.
|
|
9
|
+
*/
|
|
10
|
+
export function register(t: TestConvex<typeof schema>, name = "slugs"): void {
|
|
11
|
+
t.registerComponent(name, schema, modules);
|
|
12
|
+
}
|