@onlyworlds/sdk 1.0.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
+ MIT License
2
+
3
+ Copyright (c) 2024 OnlyWorlds
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,262 @@
1
+ # OnlyWorlds TypeScript SDK
2
+
3
+ Type-safe SDK for building applications with the OnlyWorlds API. Access all 22 element types with full TypeScript support.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @onlyworlds/sdk
9
+ # or
10
+ yarn add @onlyworlds/sdk
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ```typescript
16
+ import { OnlyWorldsClient } from '@onlyworlds/sdk';
17
+
18
+ // Initialize the client
19
+ const client = new OnlyWorldsClient({
20
+ apiKey: 'your-api-key',
21
+ apiPin: 'your-api-pin'
22
+ });
23
+
24
+ // Create a character
25
+ const character = await client.characters.create({
26
+ name: 'Aragorn',
27
+ description: 'Heir of Isildur',
28
+ location_id: 'uuid-of-location',
29
+ abilities_ids: ['uuid1', 'uuid2']
30
+ });
31
+
32
+ // List locations with filtering
33
+ const locations = await client.locations.list({
34
+ search: 'tavern',
35
+ ordering: '-created_at',
36
+ limit: 10
37
+ });
38
+ ```
39
+
40
+ ## Features
41
+
42
+ - 🎯 **Full TypeScript Support** - Complete type definitions for all 22 element types
43
+ - 🔒 **Type Safety** - Separate input/output types handle the `_id`/`_ids` pattern automatically
44
+ - 📦 **All Element Types** - Characters, Locations, Objects, Species, Events, and more
45
+ - 🔗 **Relationship Handling** - Properly typed single and multi-link relationships
46
+ - 🚀 **Simple API** - Intuitive methods for CRUD operations
47
+ - 📄 **Auto-completion** - IDE support for all fields and methods
48
+
49
+ ## Element Types
50
+
51
+ The SDK supports all 22 OnlyWorlds element types:
52
+
53
+ - **Beings**: Character, Creature, Collective, Family, Species
54
+ - **Places**: Location, Zone, Map, Pin, Marker
55
+ - **Things**: Object, Construct, Phenomenon
56
+ - **Concepts**: Ability, Event, Institution, Language, Law, Narrative, Relation, Title, Trait
57
+
58
+ ## API Reference
59
+
60
+ ### Client Initialization
61
+
62
+ ```typescript
63
+ import { OnlyWorldsClient } from '@onlyworlds/sdk';
64
+
65
+ const client = new OnlyWorldsClient({
66
+ apiKey: 'your-api-key', // Required
67
+ apiPin: 'your-api-pin', // Required
68
+ baseUrl: 'custom-url' // Optional, defaults to onlyworlds.com
69
+ });
70
+ ```
71
+
72
+ ### CRUD Operations
73
+
74
+ All element types support the same operations:
75
+
76
+ ```typescript
77
+ // List with filtering
78
+ const items = await client.characters.list({
79
+ search: 'hero',
80
+ ordering: 'name',
81
+ limit: 20,
82
+ offset: 0
83
+ });
84
+
85
+ // Get single item
86
+ const character = await client.characters.get('uuid');
87
+
88
+ // Create new item
89
+ const newChar = await client.characters.create({
90
+ name: 'Gandalf',
91
+ description: 'A wizard'
92
+ });
93
+
94
+ // Update existing item
95
+ const updated = await client.characters.update('uuid', {
96
+ description: 'A wizard, also known as Mithrandir'
97
+ });
98
+
99
+ // Delete item
100
+ await client.characters.delete('uuid');
101
+ ```
102
+
103
+ ### Working with Relationships
104
+
105
+ The SDK handles OnlyWorlds' `_id`/`_ids` pattern automatically:
106
+
107
+ ```typescript
108
+ // Creating with relationships
109
+ const character = await client.characters.create({
110
+ name: 'Frodo',
111
+ birthplace_id: 'location-uuid', // Single relationship
112
+ abilities_ids: ['uuid1', 'uuid2'], // Multi relationship
113
+ friends_ids: ['sam-uuid', 'merry-uuid']
114
+ });
115
+
116
+ // The response includes nested objects
117
+ console.log(character.birthplace.name); // "The Shire"
118
+ console.log(character.abilities[0].name); // "Ring Bearer"
119
+ ```
120
+
121
+ ### Type Definitions
122
+
123
+ All types are exported for your use:
124
+
125
+ ```typescript
126
+ import {
127
+ Character,
128
+ CharacterInput,
129
+ Location,
130
+ ElementType
131
+ } from '@onlyworlds/sdk';
132
+
133
+ // Use in your functions
134
+ function createHero(data: CharacterInput): Promise<Character> {
135
+ return client.characters.create(data);
136
+ }
137
+
138
+ // Element type enum
139
+ console.log(ElementType.Character); // 'character'
140
+ ```
141
+
142
+ ### Advanced Examples
143
+
144
+ #### Batch Operations
145
+
146
+ ```typescript
147
+ // Create multiple related elements
148
+ async function createParty(partyData: any[]) {
149
+ const location = await client.locations.create({
150
+ name: 'Tavern',
151
+ description: 'Where the party meets'
152
+ });
153
+
154
+ const characters = await Promise.all(
155
+ partyData.map(data =>
156
+ client.characters.create({
157
+ ...data,
158
+ location_id: location.id
159
+ })
160
+ )
161
+ );
162
+
163
+ return { location, characters };
164
+ }
165
+ ```
166
+
167
+ #### Search and Filter
168
+
169
+ ```typescript
170
+ // Find all wizards in a specific location
171
+ const wizards = await client.characters.list({
172
+ search: 'wizard',
173
+ location: 'location-uuid',
174
+ ordering: '-level'
175
+ });
176
+
177
+ // Get recently updated items
178
+ const recent = await client.events.list({
179
+ ordering: '-updated_at',
180
+ limit: 5
181
+ });
182
+ ```
183
+
184
+ #### Error Handling
185
+
186
+ ```typescript
187
+ try {
188
+ const character = await client.characters.get('invalid-uuid');
189
+ } catch (error) {
190
+ if (error.message.includes('404')) {
191
+ console.log('Character not found');
192
+ }
193
+ }
194
+ ```
195
+
196
+ ## Input vs Output Types
197
+
198
+ OnlyWorlds uses different formats for requests and responses:
199
+
200
+ - **Input** (requests): Use `_id` for single relationships, `_ids` for multiple
201
+ - **Output** (responses): Nested objects with full data
202
+
203
+ The SDK provides separate types for each:
204
+
205
+ ```typescript
206
+ // CharacterInput for creating/updating
207
+ interface CharacterInput {
208
+ name: string;
209
+ location_id?: string; // UUID string
210
+ abilities_ids?: string[]; // Array of UUIDs
211
+ }
212
+
213
+ // Character for responses
214
+ interface Character {
215
+ name: string;
216
+ location?: Location; // Full nested object
217
+ abilities?: Ability[]; // Array of full objects
218
+ }
219
+ ```
220
+
221
+ ## Helper Methods
222
+
223
+ ```typescript
224
+ // Convert nested objects to _id/_ids format
225
+ const input = OnlyWorldsClient.prepareInput({
226
+ name: 'Test',
227
+ location: { id: 'uuid', name: 'Place' },
228
+ abilities: [{ id: 'uuid1' }, { id: 'uuid2' }]
229
+ });
230
+ // Result: { name: 'Test', location_id: 'uuid', abilities_ids: ['uuid1', 'uuid2'] }
231
+ ```
232
+
233
+ ## Requirements
234
+
235
+ - Node.js 18+ or modern browser with fetch API
236
+ - TypeScript 4.5+ (for TypeScript projects)
237
+
238
+ ## Browser Usage
239
+
240
+ This SDK works in browsers, but you'll need to handle CORS:
241
+
242
+ ```javascript
243
+ // For development, use a proxy or CORS-enabled server
244
+ // Never expose API credentials in client-side code!
245
+
246
+ // Better approach: Use a backend proxy
247
+ const response = await fetch('/api/proxy/onlyworlds', {
248
+ method: 'POST',
249
+ body: JSON.stringify({ /* your request */ })
250
+ });
251
+ ```
252
+
253
+ ## License
254
+
255
+ MIT
256
+
257
+ ## Links
258
+
259
+ - [OnlyWorlds Documentation](https://onlyworlds.github.io/)
260
+ - [API Reference](https://www.onlyworlds.com/api/docs)
261
+ - [NPM Package](https://www.npmjs.com/package/@onlyworlds/sdk)
262
+ - [GitHub](https://github.com/onlyworlds/sdk)