@adland/data 0.14.2 → 0.14.3
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 +6 -0
- package/README.md +45 -177
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,217 +1,85 @@
|
|
|
1
1
|
# @adland/data
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Type-safe data schemas, validation, and metadata enrichment for [0xSlots](https://0xslots.org) ad types.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Install
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
npm install @adland/data
|
|
9
|
-
# or
|
|
10
|
-
pnpm add @adland/data
|
|
11
|
-
# or
|
|
12
|
-
yarn add @adland/data
|
|
13
9
|
```
|
|
14
10
|
|
|
15
|
-
##
|
|
16
|
-
|
|
17
|
-
### TypeScript Types
|
|
11
|
+
## Ad Types
|
|
18
12
|
|
|
19
|
-
|
|
13
|
+
| Type | Data | Metadata |
|
|
14
|
+
|------|------|----------|
|
|
15
|
+
| `link` | `{ url }` | title, description, image, icon |
|
|
16
|
+
| `cast` | `{ hash }` | username, pfpUrl, text, timestamp |
|
|
17
|
+
| `miniapp` | `{ url }` | icon, title, description, imageUrl |
|
|
18
|
+
| `token` | `{ address, chainId }` | name, symbol, decimals, logoURI |
|
|
19
|
+
| `farcasterProfile` | `{ fid }` | username, displayName, bio, pfpUrl |
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
import type { AdData, LinkAd, SwapAd } from "@adland/data";
|
|
21
|
+
## Usage
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
const linkAd: LinkAd = {
|
|
26
|
-
type: "link",
|
|
27
|
-
url: "https://example.com",
|
|
28
|
-
};
|
|
23
|
+
### Types
|
|
29
24
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
fromToken: "USDC",
|
|
33
|
-
toToken: "ETH",
|
|
34
|
-
amount: "100",
|
|
35
|
-
};
|
|
25
|
+
```ts
|
|
26
|
+
import type { AdData, AdType, LinkAd, CastAd } from "@adland/data";
|
|
36
27
|
```
|
|
37
28
|
|
|
38
|
-
###
|
|
29
|
+
### Form Validation
|
|
39
30
|
|
|
40
|
-
|
|
31
|
+
Each ad definition exposes a Zod schema — works directly with form libraries:
|
|
41
32
|
|
|
42
|
-
```
|
|
43
|
-
import { linkAdSchema, swapAdSchema, validateAdData } from "@adland/data";
|
|
44
|
-
import { useForm } from "react-hook-form";
|
|
33
|
+
```ts
|
|
45
34
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
35
|
+
import { linkAd } from "@adland/data";
|
|
46
36
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const form = useForm({
|
|
50
|
-
resolver: zodResolver(swapAdSchema),
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// ... rest of form
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Direct validation
|
|
57
|
-
const result = validateAdData(someData);
|
|
58
|
-
if (result.success) {
|
|
59
|
-
console.log("Valid ad data:", result.data);
|
|
60
|
-
} else {
|
|
61
|
-
console.error("Validation errors:", result.error);
|
|
62
|
-
}
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### Combining Schemas
|
|
66
|
-
|
|
67
|
-
Zod makes it easy to extend and combine schemas:
|
|
68
|
-
|
|
69
|
-
```tsx
|
|
70
|
-
import { linkAdSchema } from "@adland/data";
|
|
71
|
-
import { z } from "zod";
|
|
72
|
-
|
|
73
|
-
// Extend a schema
|
|
74
|
-
const customLinkSchema = linkAdSchema.extend({
|
|
75
|
-
title: z.string(),
|
|
76
|
-
description: z.string().optional(),
|
|
37
|
+
const form = useForm({
|
|
38
|
+
resolver: zodResolver(linkAd.data),
|
|
77
39
|
});
|
|
78
|
-
|
|
79
|
-
// Combine schemas
|
|
80
|
-
const adWithMetadata = z.intersection(
|
|
81
|
-
linkAdSchema,
|
|
82
|
-
z.object({ metadata: z.record(z.unknown()) })
|
|
83
|
-
);
|
|
84
40
|
```
|
|
85
41
|
|
|
86
|
-
|
|
42
|
+
### Processing Pipeline
|
|
87
43
|
|
|
88
|
-
|
|
89
|
-
Basic link ad type.
|
|
44
|
+
Validate, verify against external sources, and enrich with metadata in one call:
|
|
90
45
|
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
type: "link";
|
|
94
|
-
url: string;
|
|
95
|
-
}
|
|
96
|
-
```
|
|
46
|
+
```ts
|
|
47
|
+
import { linkAd } from "@adland/data";
|
|
97
48
|
|
|
98
|
-
|
|
99
|
-
Farcaster cast link ad type.
|
|
49
|
+
const result = await linkAd.safeProcess({ url: "https://example.com" });
|
|
100
50
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
url: string;
|
|
51
|
+
if (result.success) {
|
|
52
|
+
console.log(result.data); // validated input
|
|
53
|
+
console.log(result.metadata); // { title, description, image, icon }
|
|
105
54
|
}
|
|
106
55
|
```
|
|
107
56
|
|
|
108
|
-
###
|
|
109
|
-
Farcaster mini app link ad type.
|
|
110
|
-
|
|
111
|
-
```typescript
|
|
112
|
-
type MiniAppAd = {
|
|
113
|
-
type: "miniapp";
|
|
114
|
-
url: string;
|
|
115
|
-
}
|
|
116
|
-
```
|
|
57
|
+
### Registry
|
|
117
58
|
|
|
118
|
-
|
|
119
|
-
|
|
59
|
+
```ts
|
|
60
|
+
import { ads, getAd, validateAdData } from "@adland/data";
|
|
120
61
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
type: "token";
|
|
124
|
-
token: string; // Token address or symbol
|
|
125
|
-
}
|
|
62
|
+
const castDef = getAd("cast");
|
|
63
|
+
const result = validateAdData({ type: "cast", data: { hash: "0x..." } });
|
|
126
64
|
```
|
|
127
65
|
|
|
128
|
-
###
|
|
129
|
-
Token swap ad type.
|
|
66
|
+
### Farcaster Utilities
|
|
130
67
|
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
type: "swap";
|
|
134
|
-
fromToken: string; // Token address or symbol
|
|
135
|
-
toToken: string; // Token address or symbol
|
|
136
|
-
amount?: string; // Optional amount
|
|
137
|
-
}
|
|
138
|
-
```
|
|
68
|
+
```ts
|
|
69
|
+
import { parseAccountAssociation, FarcasterAPI } from "@adland/data";
|
|
139
70
|
|
|
140
|
-
|
|
71
|
+
const parsed = parseAccountAssociation(accountAssociation);
|
|
72
|
+
// => { fid, address, type, domain }
|
|
73
|
+
```
|
|
141
74
|
|
|
142
|
-
|
|
75
|
+
## How It Works
|
|
143
76
|
|
|
144
|
-
|
|
145
|
-
- `AdData` - Union type of all ad data objects (discriminated union)
|
|
146
|
-
- `LinkAd` - Link ad type
|
|
147
|
-
- `CastAd` - Cast ad type
|
|
148
|
-
- `MiniAppAd` - MiniApp ad type
|
|
149
|
-
- `TokenAd` - Token ad type
|
|
150
|
-
- `SwapAd` - Swap ad type
|
|
151
|
-
|
|
152
|
-
### Schemas
|
|
153
|
-
|
|
154
|
-
- `linkAdSchema` - Zod schema for link ads
|
|
155
|
-
- `castAdSchema` - Zod schema for cast ads
|
|
156
|
-
- `miniappAdSchema` - Zod schema for mini app ads
|
|
157
|
-
- `tokenAdSchema` - Zod schema for token ads
|
|
158
|
-
- `swapAdSchema` - Zod schema for swap ads
|
|
159
|
-
- `adDataSchema` - Discriminated union schema for all ad types
|
|
160
|
-
- `adSchemas` - Object containing all schemas
|
|
161
|
-
- `getAdSchema(type)` - Get schema for a specific ad type
|
|
162
|
-
- `getAllAdSchemas()` - Get all ad schemas
|
|
163
|
-
- `validateAdData(data)` - Validate ad data and return result
|
|
164
|
-
|
|
165
|
-
## Integration Examples
|
|
166
|
-
|
|
167
|
-
### React Hook Form
|
|
168
|
-
|
|
169
|
-
```tsx
|
|
170
|
-
import { useForm } from "react-hook-form";
|
|
171
|
-
import { zodResolver } from "@hookform/resolvers/zod";
|
|
172
|
-
import { swapAdSchema, type SwapAd } from "@adland/data";
|
|
173
|
-
|
|
174
|
-
function SwapForm() {
|
|
175
|
-
const form = useForm<SwapAd>({
|
|
176
|
-
resolver: zodResolver(swapAdSchema),
|
|
177
|
-
defaultValues: {
|
|
178
|
-
type: "swap",
|
|
179
|
-
fromToken: "",
|
|
180
|
-
toToken: "",
|
|
181
|
-
},
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
return (
|
|
185
|
-
<form onSubmit={form.handleSubmit(onSubmit)}>
|
|
186
|
-
{/* form fields */}
|
|
187
|
-
</form>
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
```
|
|
77
|
+
Each ad definition created with `defineAd()` provides:
|
|
191
78
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
import { swapAdSchema } from "@adland/data";
|
|
197
|
-
import { toFormikValidationSchema } from "zod-formik-adapter";
|
|
198
|
-
|
|
199
|
-
function SwapForm() {
|
|
200
|
-
return (
|
|
201
|
-
<Formik
|
|
202
|
-
validationSchema={toFormikValidationSchema(swapAdSchema)}
|
|
203
|
-
initialValues={{
|
|
204
|
-
type: "swap" as const,
|
|
205
|
-
fromToken: "",
|
|
206
|
-
toToken: "",
|
|
207
|
-
}}
|
|
208
|
-
onSubmit={handleSubmit}
|
|
209
|
-
>
|
|
210
|
-
{/* form */}
|
|
211
|
-
</Formik>
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
```
|
|
79
|
+
1. **Zod schema** — runtime data validation
|
|
80
|
+
2. **`verify(data)`** — checks validity against external sources (API, blockchain)
|
|
81
|
+
3. **`getMetadata(data)`** — fetches rich metadata (titles, images, etc.)
|
|
82
|
+
4. **`process(data)` / `safeProcess(data)`** — runs the full pipeline
|
|
215
83
|
|
|
216
84
|
## License
|
|
217
85
|
|