@adland/data 0.2.0

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/.eslintrc.js ADDED
@@ -0,0 +1,35 @@
1
+ const { resolve } = require("node:path");
2
+
3
+ const project = resolve(process.cwd(), "tsconfig.json");
4
+
5
+ /** @type {import("eslint").Linter.Config} */
6
+ module.exports = {
7
+ extends: ["eslint:recommended", "turbo"],
8
+ plugins: ["only-warn"],
9
+ globals: {
10
+ React: true,
11
+ JSX: true,
12
+ },
13
+ env: {
14
+ node: true,
15
+ },
16
+ settings: {
17
+ "import/resolver": {
18
+ typescript: {
19
+ project,
20
+ },
21
+ },
22
+ },
23
+ ignorePatterns: [
24
+ // Ignore dotfiles
25
+ ".*.js",
26
+ "node_modules/",
27
+ "dist/",
28
+ ],
29
+ overrides: [
30
+ {
31
+ files: ["*.js?(x)", "*.ts?(x)"],
32
+ },
33
+ ],
34
+ };
35
+
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @adland/data
2
+
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 5f947df: initial push of data package
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 dan5py (git@dan5py.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,218 @@
1
+ # @adland/data
2
+
3
+ TypeScript types and Zod schemas for AdLand ad types. Perfect for building forms with react-hook-form, validating data, and type-safe ad handling.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @adland/data
9
+ # or
10
+ pnpm add @adland/data
11
+ # or
12
+ yarn add @adland/data
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### TypeScript Types
18
+
19
+ Types are automatically derived from Zod schemas, ensuring type safety:
20
+
21
+ ```tsx
22
+ import type { AdData, LinkAd, SwapAd } from "@adland/data";
23
+
24
+ // Use the types
25
+ const linkAd: LinkAd = {
26
+ type: "link",
27
+ url: "https://example.com",
28
+ };
29
+
30
+ const swapAd: SwapAd = {
31
+ type: "swap",
32
+ fromToken: "USDC",
33
+ toToken: "ETH",
34
+ amount: "100",
35
+ };
36
+ ```
37
+
38
+ ### Zod Schemas
39
+
40
+ Use Zod schemas for validation and form integration:
41
+
42
+ ```tsx
43
+ import { linkAdSchema, swapAdSchema, validateAdData } from "@adland/data";
44
+ import { useForm } from "react-hook-form";
45
+ import { zodResolver } from "@hookform/resolvers/zod";
46
+
47
+ // With react-hook-form
48
+ function MyForm() {
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(),
77
+ });
78
+
79
+ // Combine schemas
80
+ const adWithMetadata = z.intersection(
81
+ linkAdSchema,
82
+ z.object({ metadata: z.record(z.unknown()) })
83
+ );
84
+ ```
85
+
86
+ ## Ad Types
87
+
88
+ ### Link Ad
89
+ Basic link ad type.
90
+
91
+ ```typescript
92
+ type LinkAd = {
93
+ type: "link";
94
+ url: string;
95
+ }
96
+ ```
97
+
98
+ ### Cast Ad
99
+ Farcaster cast link ad type.
100
+
101
+ ```typescript
102
+ type CastAd = {
103
+ type: "cast";
104
+ url: string;
105
+ }
106
+ ```
107
+
108
+ ### MiniApp Ad
109
+ Farcaster mini app link ad type.
110
+
111
+ ```typescript
112
+ type MiniAppAd = {
113
+ type: "miniapp";
114
+ url: string;
115
+ }
116
+ ```
117
+
118
+ ### Token Ad
119
+ Token information ad type.
120
+
121
+ ```typescript
122
+ type TokenAd = {
123
+ type: "token";
124
+ token: string; // Token address or symbol
125
+ }
126
+ ```
127
+
128
+ ### Swap Ad
129
+ Token swap ad type.
130
+
131
+ ```typescript
132
+ type SwapAd = {
133
+ type: "swap";
134
+ fromToken: string; // Token address or symbol
135
+ toToken: string; // Token address or symbol
136
+ amount?: string; // Optional amount
137
+ }
138
+ ```
139
+
140
+ ## API
141
+
142
+ ### Types
143
+
144
+ - `AdType` - Union type of all ad type strings
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
+ ```
191
+
192
+ ### Formik
193
+
194
+ ```tsx
195
+ import { Formik } from "formik";
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
+ ```
215
+
216
+ ## License
217
+
218
+ MIT
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@adland/data",
3
+ "version": "0.2.0",
4
+ "private": false,
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "type": "module",
9
+ "main": "./dist/index.cjs",
10
+ "module": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "require": "./dist/index.cjs"
17
+ },
18
+ "./package.json": "./package.json"
19
+ },
20
+ "dependencies": {
21
+ "zod": "^3.24.2"
22
+ },
23
+ "devDependencies": {
24
+ "@types/node": "^20",
25
+ "@typescript-eslint/eslint-plugin": "^7.13.1",
26
+ "@typescript-eslint/parser": "^7.13.1",
27
+ "@vercel/style-guide": "^6.0.0",
28
+ "eslint": "^8",
29
+ "eslint-config-turbo": "2.0.6",
30
+ "eslint-plugin-only-warn": "^1.1.0",
31
+ "tsup": "^8.0.1",
32
+ "typescript": "^5.4.5"
33
+ },
34
+ "scripts": {
35
+ "build": "tsup",
36
+ "dev": "tsup --watch",
37
+ "lint": "eslint ."
38
+ }
39
+ }
package/src/index.ts ADDED
@@ -0,0 +1,24 @@
1
+ // Export types (derived from Zod schemas)
2
+ export type {
3
+ AdType,
4
+ AdData,
5
+ LinkAd,
6
+ CastAd,
7
+ MiniAppAd,
8
+ TokenAd,
9
+ SwapAd,
10
+ } from "./types";
11
+
12
+ // Export Zod schemas
13
+ export {
14
+ linkAdSchema,
15
+ castAdSchema,
16
+ miniappAdSchema,
17
+ tokenAdSchema,
18
+ swapAdSchema,
19
+ adDataSchema,
20
+ adSchemas,
21
+ getAdSchema,
22
+ getAllAdSchemas,
23
+ validateAdData,
24
+ } from "./schemas";
@@ -0,0 +1,94 @@
1
+ import { z } from "zod";
2
+
3
+ /**
4
+ * Link ad schema - basic link
5
+ */
6
+ export const linkAdSchema = z.object({
7
+ type: z.literal("link"),
8
+ url: z.string().url("Must be a valid URL"),
9
+ });
10
+
11
+ /**
12
+ * Cast ad schema - link to a Farcaster cast
13
+ */
14
+ export const castAdSchema = z.object({
15
+ type: z.literal("cast"),
16
+ url: z.string().url("Must be a valid URL"),
17
+ });
18
+
19
+ /**
20
+ * MiniApp ad schema - link to a Farcaster mini app
21
+ */
22
+ export const miniappAdSchema = z.object({
23
+ type: z.literal("miniapp"),
24
+ url: z.string().url("Must be a valid URL"),
25
+ });
26
+
27
+ /**
28
+ * Token ad schema - token information
29
+ */
30
+ export const tokenAdSchema = z.object({
31
+ type: z.literal("token"),
32
+ token: z.string().min(1, "Token is required"),
33
+ });
34
+
35
+ /**
36
+ * Swap ad schema - token swap information
37
+ */
38
+ export const swapAdSchema = z.object({
39
+ type: z.literal("swap"),
40
+ fromToken: z.string().min(1, "From token is required"),
41
+ toToken: z.string().min(1, "To token is required"),
42
+ amount: z.string().optional(),
43
+ });
44
+
45
+ /**
46
+ * Union schema for all ad types
47
+ */
48
+ export const adDataSchema = z.discriminatedUnion("type", [
49
+ linkAdSchema,
50
+ castAdSchema,
51
+ miniappAdSchema,
52
+ tokenAdSchema,
53
+ swapAdSchema,
54
+ ]);
55
+
56
+ /**
57
+ * All ad schemas as an object
58
+ */
59
+ export const adSchemas = {
60
+ link: linkAdSchema,
61
+ cast: castAdSchema,
62
+ miniapp: miniappAdSchema,
63
+ token: tokenAdSchema,
64
+ swap: swapAdSchema,
65
+ } as const;
66
+
67
+ /**
68
+ * Get schema for a specific ad type
69
+ */
70
+ export function getAdSchema(type: keyof typeof adSchemas) {
71
+ return adSchemas[type];
72
+ }
73
+
74
+ /**
75
+ * Get all ad schemas
76
+ */
77
+ export function getAllAdSchemas() {
78
+ return adSchemas;
79
+ }
80
+
81
+ /**
82
+ * Validate ad data
83
+ */
84
+ export function validateAdData(data: unknown): {
85
+ success: boolean;
86
+ data?: z.infer<typeof adDataSchema>;
87
+ error?: z.ZodError;
88
+ } {
89
+ const result = adDataSchema.safeParse(data);
90
+ if (result.success) {
91
+ return { success: true, data: result.data };
92
+ }
93
+ return { success: false, error: result.error };
94
+ }
@@ -0,0 +1,44 @@
1
+ import { z } from "zod";
2
+ import {
3
+ linkAdSchema,
4
+ castAdSchema,
5
+ miniappAdSchema,
6
+ tokenAdSchema,
7
+ swapAdSchema,
8
+ adDataSchema,
9
+ } from "../schemas";
10
+
11
+ /**
12
+ * Base ad type
13
+ */
14
+ export type AdType = "link" | "cast" | "miniapp" | "token" | "swap";
15
+
16
+ /**
17
+ * Link ad type - basic link
18
+ */
19
+ export type LinkAd = z.infer<typeof linkAdSchema>;
20
+
21
+ /**
22
+ * Cast ad type - link to a Farcaster cast
23
+ */
24
+ export type CastAd = z.infer<typeof castAdSchema>;
25
+
26
+ /**
27
+ * MiniApp ad type - link to a Farcaster mini app
28
+ */
29
+ export type MiniAppAd = z.infer<typeof miniappAdSchema>;
30
+
31
+ /**
32
+ * Token ad type - token information
33
+ */
34
+ export type TokenAd = z.infer<typeof tokenAdSchema>;
35
+
36
+ /**
37
+ * Swap ad type - token swap information
38
+ */
39
+ export type SwapAd = z.infer<typeof swapAdSchema>;
40
+
41
+ /**
42
+ * Union type of all ad types
43
+ */
44
+ export type AdData = z.infer<typeof adDataSchema>;
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "declarationMap": true,
6
+ "esModuleInterop": true,
7
+ "incremental": false,
8
+ "isolatedModules": true,
9
+ "lib": ["es2022"],
10
+ "module": "ESNext",
11
+ "moduleDetection": "force",
12
+ "moduleResolution": "bundler",
13
+ "noUncheckedIndexedAccess": true,
14
+ "resolveJsonModule": true,
15
+ "skipLibCheck": true,
16
+ "strict": true,
17
+ "target": "ES2022",
18
+ "baseUrl": ".",
19
+ "paths": {
20
+ "@adland/data/*": ["./src/*"]
21
+ },
22
+ "outDir": "./dist"
23
+ },
24
+ "include": ["src"],
25
+ "exclude": ["node_modules", "dist"]
26
+ }
27
+
package/tsup.config.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { defineConfig } from "tsup";
2
+
3
+ export default defineConfig({
4
+ entry: ["src/index.ts"],
5
+ format: ["cjs", "esm"],
6
+ dts: true,
7
+ splitting: false,
8
+ sourcemap: true,
9
+ clean: true,
10
+ treeshake: true,
11
+ });
12
+