pubky-app-specs 0.3.0-rc.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024
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
13
+ all 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
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,167 @@
1
+ # ๐Ÿฆ„ Pubky App Specs (WASM) ยท `pubky-app-specs`
2
+
3
+ A WASM library for building and validating structured JSON models compatible with Pubky.App social powered by [`@synonymdev/pubky`](https://www.npmjs.com/package/@synonymdev/pubky). It handles domain objects like **Users**, **Posts**, **Feeds**, **Bookmarks**, **Tags**, and more. Each object is:
4
+
5
+ - **Sanitized** and **Validated** via Rust logic.
6
+ - **Auto-IDโ€™ed** and **Auto-Pathed** according to your domain rules.
7
+ - **Exported** to JavaScript/TypeScript with minimal overhead.
8
+
9
+ ## ๐Ÿค” Why Use This Crate Instead of [Manual JSONs](https://github.com/pubky/pubky-app-specs?tab=readme-ov-file#data-models)?
10
+
11
+ - **Validation Consistency**: Ensures your app uses the same sanitization and validation rules as [Pubky indexers](https://github.com/pubky/pubky-nexus), avoiding errors.
12
+ - **Schema Versioning**: Automatically stay up-to-date with schema changes, reducing maintenance overhead.
13
+ - **Auto IDs & Paths**: Generates unique IDs, paths, and URLs according to Pubky standards.
14
+ - **Rust-to-JavaScript Compatibility**: Type-safe models that work seamlessly across Rust and JavaScript/TypeScript.
15
+ - **Future-Proof**: Easily adapt to new Pubky object types without rewriting JSON manually.
16
+
17
+ ---
18
+
19
+ ## โš™๏ธ Installation
20
+
21
+ ```bash
22
+ npm install pubky-app-specs
23
+ # or
24
+ yarn add pubky-app-specs
25
+ ```
26
+
27
+ > **Note**: This package uses WASM. Ensure your bundler or environment supports loading WASM modules (e.g. Next.js, Vite, etc.).
28
+
29
+ ---
30
+
31
+ ## ๐Ÿš€ Quick Start
32
+
33
+ 1. **Initialize** the WASM module.
34
+ 2. **Construct** a `PubkySpecsBuilder(pubkyId)` object.
35
+ 3. **Create** validated domain objects (User, Post, Tag, etc.).
36
+ 4. **Store** them on the [PubKy homeserver](https://github.com/synonymdev/pubky) or any distributed storage solution you prefer.
37
+
38
+ ### Import & Initialize
39
+
40
+ ```js
41
+ import init, { PubkySpecsBuilder } from "pubky-app-specs";
42
+
43
+ async function loadSpecs(pubkyId) {
44
+ // 1. Initialize WASM
45
+ await init();
46
+
47
+ // 2. Create a specs builder instance
48
+ const specs = new PubkySpecsBuilder(pubkyId);
49
+ return specs;
50
+ }
51
+ ```
52
+
53
+ ---
54
+
55
+ ## ๐ŸŽจ Example Usage
56
+
57
+ Below are **succinct** examples to illustrate how to create or update data using **`pubky-app-specs`** and then **store** it with [`@synonymdev/pubky`](https://www.npmjs.com/package/@synonymdev/pubky).
58
+
59
+ ### 1) Creating a New User
60
+
61
+ ```js
62
+ import { Client, PublicKey } from "@synonymdev/pubky";
63
+ import { PubkySpecsBuilder } from "pubky-app-specs";
64
+
65
+ async function createUser(pubkyId) {
66
+ const client = new Client();
67
+ const specs = new PubkySpecsBuilder(pubkyId);
68
+
69
+ // Create user object with minimal fields
70
+ const userResult = specs.createUser(
71
+ "Alice", // Name
72
+ "Hello from WASM", // Bio
73
+ null, // Image URL or File
74
+ null, // Links
75
+ "active" // Status
76
+ );
77
+
78
+ // userResult.meta contains { id, path, url }.
79
+ // userResult.user is the Rust "PubkyAppUser" object.
80
+
81
+ // We bring the Rust object to JS using the .toJson() method.
82
+ const userJson = userResult.user.toJson();
83
+
84
+ // Store in homeserver via pubky
85
+ const response = await client.fetch(userResult.meta.url, {
86
+ method: "PUT",
87
+ body: JSON.stringify(userJson),
88
+ credentials: "include",
89
+ });
90
+
91
+ if (!response.ok) {
92
+ throw new Error(`Failed to store user: ${response.statusText}`);
93
+ }
94
+
95
+ console.log("User stored at:", userResult.meta.url);
96
+ return userResult;
97
+ }
98
+ ```
99
+
100
+ ### 2) Creating a Post
101
+
102
+ ```js
103
+ import { Client } from "@synonymdev/pubky";
104
+ import { PubkySpecsBuilder, PubkyAppPostKind } from "pubky-app-specs";
105
+
106
+ async function createPost(pubkyId, content) {
107
+ // fileData can be a File (browser) or a raw Blob/Buffer (Node).
108
+ const client = new Client();
109
+ const specs = new PubkySpecsBuilder(pubkyId);
110
+
111
+ // Create the Post object referencing your (optional) attachment
112
+ const postResult = specs.createPost(
113
+ content,
114
+ PubkyAppPostKind.Short,
115
+ null, // parent post
116
+ null, // embed
117
+ null // attachments list of urls
118
+ );
119
+
120
+ // Store the post
121
+ const postJson = postResult.post.toJson();
122
+ await client.fetch(postResult.meta.url, {
123
+ method: "PUT",
124
+ body: JSON.stringify(postJson),
125
+ });
126
+
127
+ console.log("Post stored at:", postResult.meta.url);
128
+ return postResult;
129
+ }
130
+ ```
131
+
132
+ ### 3) Following a User
133
+
134
+ ```js
135
+ import { Client } from "@synonymdev/pubky";
136
+ import { PubkySpecsBuilder } from "pubky-app-specs";
137
+
138
+ async function followUser(myPubkyId, userToFollow) {
139
+ const client = new Client();
140
+ const specs = new PubkySpecsBuilder(myPubkyId);
141
+
142
+ const followResult = specs.createFollow(userToFollow);
143
+
144
+ // We only need to store the JSON in the homeserver
145
+ await client.fetch(followResult.meta.url, {
146
+ method: "PUT",
147
+ body: JSON.stringify(followResult.follow.toJson()),
148
+ });
149
+
150
+ console.log(`Successfully followed: ${userToFollow}`);
151
+ }
152
+ ```
153
+
154
+ ---
155
+
156
+ ## ๐Ÿ“ Additional Models
157
+
158
+ This library supports many more domain objects beyond `User` and `Post`. Here are a few more you can explore:
159
+
160
+ - **Feeds**: `createFeed(...)`
161
+ - **Bookmarks**: `createBookmark(...)`
162
+ - **Tags**: `createTag(...)`
163
+ - **Mutes**: `createMute(...)`
164
+ - **Follows**: `createFollow(...)`
165
+ - **LastRead**: `createLastRead(...)`
166
+
167
+ Each has a `meta` field for storing relevant IDs/paths and a typed data object.
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "pubky-app-specs",
3
+ "type": "module",
4
+ "description": "Pubky.app Data Model Specifications",
5
+ "version": "0.3.0-rc.0",
6
+ "license": "MIT",
7
+ "collaborators": [
8
+ "SHAcollision"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/pubky/pubky-app-specs.git"
13
+ },
14
+ "files": [
15
+ "pubky_app_specs_bg.wasm",
16
+ "pubky_app_specs.js",
17
+ "pubky_app_specs.d.ts"
18
+ ],
19
+ "main": "pubky_app_specs.js",
20
+ "homepage": "https://pubky.app",
21
+ "types": "pubky_app_specs.d.ts",
22
+ "sideEffects": [
23
+ "./snippets/*"
24
+ ]
25
+ }