|
1 | | -# Hypercerts Lexicon Documentation |
| 1 | +# @hypercerts-org/lexicon |
2 | 2 |
|
3 | | -This repository contains ATProto lexicon definitions for the |
4 | | -Hypercerts protocol. Each lexicon defines a record type that can be |
5 | | -stored on the ATProto network. |
| 3 | +ATProto lexicon definitions and generated TypeScript types for the Hypercerts protocol. |
6 | 4 |
|
7 | 5 | ## Installation |
8 | 6 |
|
9 | | -``` |
10 | | -npm i @hypercerts-org/lexicon |
| 7 | +```bash |
| 8 | +pnpm add @hypercerts-org/lexicon |
11 | 9 | ``` |
12 | 10 |
|
13 | | -## Usage |
| 11 | +## Features |
14 | 12 |
|
15 | | -```typescript |
16 | | -import { AtpBaseClient } from '@hypercerts-org/lexicon' |
17 | | -import type { HypercertClaim } from '@hypercerts-org/lexicon' |
| 13 | +- **Lexicon JSON Definitions**: ATProto-compliant lexicon schemas for all Hypercerts record types |
| 14 | +- **Generated TypeScript Types**: Strongly-typed interfaces generated via `@atproto/lex-cli` |
| 15 | +- **Runtime Validation**: `isRecord` and `validateRecord` functions for each type |
| 16 | +- **Lexicon Registry**: Pre-configured registry for schema validation |
| 17 | + |
| 18 | +## Usage |
18 | 19 |
|
19 | | -const client = new AtpBaseClient({ |
20 | | - service: 'https://bsky.social', |
21 | | - headers: { Authorization: `Bearer ${token}` } |
22 | | -}) |
| 20 | +### Using Types |
23 | 21 |
|
24 | | -const hypercert: HypercertClaim = { |
| 22 | +```typescript |
| 23 | +import type { |
| 24 | + OrgHypercertsClaim, |
| 25 | + OrgHypercertsCollection, |
| 26 | + OrgHypercertsClaimRights |
| 27 | +} from '@hypercerts-org/lexicon'; |
| 28 | + |
| 29 | +// Use the Main type for full records (includes $type) |
| 30 | +const claim: OrgHypercertsClaim.Main = { |
25 | 31 | $type: 'org.hypercerts.claim', |
26 | 32 | title: 'My Impact Work', |
27 | 33 | shortDescription: 'Description here', |
28 | 34 | workScope: 'Scope of work', |
29 | 35 | workTimeFrameFrom: '2023-01-01T00:00:00Z', |
30 | 36 | workTimeFrameTo: '2023-12-31T23:59:59Z', |
31 | 37 | createdAt: new Date().toISOString() |
| 38 | +}; |
| 39 | +``` |
| 40 | + |
| 41 | +### Runtime Validation |
| 42 | + |
| 43 | +```typescript |
| 44 | +import { OrgHypercertsClaim } from '@hypercerts-org/lexicon'; |
| 45 | + |
| 46 | +// Check if a value matches the type |
| 47 | +if (OrgHypercertsClaim.isRecord(unknownValue)) { |
| 48 | + // unknownValue is now typed as OrgHypercertsClaim.Main |
| 49 | + console.log(unknownValue.title); |
32 | 50 | } |
33 | 51 |
|
34 | | -await client.org.hypercerts.claim.create( |
35 | | - { repo: 'did:plc:example' }, |
36 | | - hypercert |
37 | | -) |
| 52 | +// Validate with detailed error information |
| 53 | +const result = OrgHypercertsClaim.validateRecord(data); |
| 54 | +if (result.success) { |
| 55 | + // data is valid |
| 56 | +} else { |
| 57 | + console.error(result.error); |
| 58 | +} |
38 | 59 | ``` |
39 | 60 |
|
| 61 | +### Using Lexicon Constants |
| 62 | + |
| 63 | +```typescript |
| 64 | +import { HYPERCERT_LEXICONS, HYPERCERT_COLLECTIONS } from '@hypercerts-org/lexicon'; |
| 65 | + |
| 66 | +// Collection names for ATProto operations |
| 67 | +console.log(HYPERCERT_COLLECTIONS.CLAIM); // 'org.hypercerts.claim' |
| 68 | +console.log(HYPERCERT_COLLECTIONS.COLLECTION); // 'org.hypercerts.collection' |
| 69 | +console.log(HYPERCERT_COLLECTIONS.RIGHTS); // 'org.hypercerts.claim.rights' |
| 70 | + |
| 71 | +// Full lexicon documents for registry |
| 72 | +import { Lexicons } from '@atproto/lexicon'; |
| 73 | +const registry = new Lexicons(HYPERCERT_LEXICONS); |
| 74 | +``` |
| 75 | + |
| 76 | +## Type Inheritance |
| 77 | + |
| 78 | +This package is the source of truth for Hypercerts types. The SDK packages re-export these types with friendly aliases: |
| 79 | + |
| 80 | +``` |
| 81 | +@hypercerts-org/lexicon |
| 82 | +├── OrgHypercertsClaim.Main → sdk-core: HypercertClaim |
| 83 | +├── OrgHypercertsClaimRights.Main → sdk-core: HypercertRights |
| 84 | +├── OrgHypercertsCollection.Main → sdk-core: HypercertCollection |
| 85 | +└── ...etc |
| 86 | +``` |
| 87 | + |
| 88 | +**Recommendation**: Import types from `@hypercerts-org/sdk-core` for cleaner imports, unless you need direct access to validation functions. |
| 89 | + |
| 90 | +## Development |
| 91 | + |
| 92 | +### Regenerating Types |
| 93 | + |
| 94 | +Types are generated from the lexicon JSON files using `@atproto/lex-cli`: |
| 95 | + |
| 96 | +```bash |
| 97 | +pnpm generate # Regenerates src/types/ from lexicons/ |
| 98 | +pnpm build # Build the package |
| 99 | +``` |
| 100 | + |
| 101 | +### Adding New Lexicons |
| 102 | + |
| 103 | +1. Add lexicon JSON file to `lexicons/` directory |
| 104 | +2. Run `pnpm generate` to regenerate types |
| 105 | +3. Export new types from `src/index.ts` |
| 106 | +4. Update `HYPERCERT_COLLECTIONS` if adding a new collection |
| 107 | + |
40 | 108 | ## Certified Lexicons |
41 | 109 |
|
42 | 110 | Certified lexicons are common/shared lexicons that can be used across multiple protocols. |
|
0 commit comments