@onlyworlds/sdk 2.0.0 → 2.0.2

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 CHANGED
@@ -1,21 +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.
1
+ MIT License
2
+
3
+ Copyright (c) 2024-2025 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 CHANGED
@@ -1,13 +1,14 @@
1
1
  # OnlyWorlds TypeScript SDK
2
2
 
3
- Type-safe SDK for building applications with the OnlyWorlds API. Access all 22 element types with full TypeScript support.
3
+ [![npm version](https://badge.fury.io/js/@onlyworlds%2Fsdk.svg)](https://www.npmjs.com/package/@onlyworlds/sdk)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ A type-safe TypeScript SDK for building world-building applications with the OnlyWorlds API.
4
7
 
5
8
  ## Installation
6
9
 
7
10
  ```bash
8
11
  npm install @onlyworlds/sdk
9
- # or
10
- yarn add @onlyworlds/sdk
11
12
  ```
12
13
 
13
14
  ## Quick Start
@@ -15,248 +16,109 @@ yarn add @onlyworlds/sdk
15
16
  ```typescript
16
17
  import { OnlyWorldsClient } from '@onlyworlds/sdk';
17
18
 
18
- // Initialize the client
19
19
  const client = new OnlyWorldsClient({
20
20
  apiKey: 'your-api-key',
21
- apiPin: 'your-api-pin'
21
+ apiPin: '1234',
22
+ baseUrl: 'https://onlyworlds.com'
22
23
  });
23
24
 
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
- });
25
+ // Fetch characters
26
+ const characters = await client.character.list();
31
27
 
32
- // List locations with filtering
33
- const locations = await client.locations.list({
34
- search: 'tavern',
35
- ordering: '-created_at',
36
- limit: 10
28
+ // Create a new location
29
+ const location = await client.location.create({
30
+ name: 'Dragon Peak',
31
+ description: 'A treacherous mountain peak where dragons nest',
32
+ supertype: 'mountain',
33
+ climate: 'cold'
37
34
  });
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
35
 
105
- The SDK handles OnlyWorlds' `_id`/`_ids` pattern automatically:
36
+ // Get a specific element
37
+ const character = await client.character.get('element-id');
106
38
 
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']
39
+ // Update an element
40
+ await client.character.update('element-id', {
41
+ name: 'Updated Name'
114
42
  });
115
43
 
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
- }
44
+ // Delete an element
45
+ await client.character.delete('element-id');
165
46
  ```
166
47
 
167
- #### Search and Filter
48
+ ## Features
168
49
 
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
- });
50
+ - ✅ **Full Type Safety** - Complete TypeScript definitions for all 22 OnlyWorlds element types
51
+ - ✅ **Auto-Generated Types** - Types synchronized with the latest OnlyWorlds schema
52
+ - ✅ **CRUD Operations** - Create, read, update, and delete operations for all elements
53
+ - ✅ **Branded Types** - Compile-time safety for element relationships
54
+ - ✅ **Zero Runtime Overhead** - Type system has no runtime cost
55
+ - ✅ **Modern ESM/CJS** - Supports both ES modules and CommonJS
176
56
 
177
- // Get recently updated items
178
- const recent = await client.events.list({
179
- ordering: '-updated_at',
180
- limit: 5
181
- });
182
- ```
57
+ ## Element Types
183
58
 
184
- #### Error Handling
59
+ The SDK provides full support for all OnlyWorlds elements:
60
+
61
+ - **Character** - People, NPCs, protagonists
62
+ - **Location** - Places, regions, buildings
63
+ - **Object** - Items, artifacts, tools
64
+ - **Creature** - Animals, monsters, beasts
65
+ - **Species** - Races, types of beings
66
+ - **Event** - Historical events, occurrences
67
+ - **Ability** - Skills, powers, magic
68
+ - **Trait** - Characteristics, attributes
69
+ - **Family** - Family groups, lineages
70
+ - **Institution** - Organizations, governments
71
+ - **Collective** - Groups, teams, parties
72
+ - **Construct** - Buildings, structures, concepts
73
+ - **Language** - Languages, dialects
74
+ - **Law** - Rules, regulations, customs
75
+ - **Narrative** - Stories, tales, myths
76
+ - **Phenomenon** - Natural phenomena, effects
77
+ - **Title** - Ranks, positions, honors
78
+ - **Zone** - Areas, territories, districts
79
+ - **Relation** - Relationships between elements
80
+ - **Map** - Visual maps with pins/markers
81
+ - **Pin** - Location markers on maps
82
+ - **Marker** - Additional map annotations
83
+
84
+ ## Type-Safe Relationships
185
85
 
186
86
  ```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:
87
+ import { ElementId, Character, Location } from '@onlyworlds/sdk';
204
88
 
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
- }
89
+ // Branded types ensure you can't mix up element IDs
90
+ const locationId: ElementId<'Location'> = 'some-location-id';
91
+ const character: Character = {
92
+ name: 'Aragorn',
93
+ location: locationId // Type-safe!
94
+ };
219
95
  ```
220
96
 
221
- ## Helper Methods
97
+ ## Documentation
222
98
 
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
- ```
99
+ For detailed documentation, examples, and API reference, visit:
232
100
 
233
- ## Requirements
101
+ 📚 **[https://onlyworlds.github.io/](https://onlyworlds.github.io/)**
234
102
 
235
- - Node.js 18+ or modern browser with fetch API
236
- - TypeScript 4.5+ (for TypeScript projects)
103
+ ## API Authentication
237
104
 
238
- ## Browser Usage
105
+ Get your API credentials from your OnlyWorlds account:
239
106
 
240
- This SDK works in browsers, but you'll need to handle CORS:
107
+ 1. Log in to [onlyworlds.com](https://onlyworlds.com)
108
+ 2. Go to your world settings
109
+ 3. Copy your API Key and PIN
241
110
 
242
- ```javascript
243
- // For development, use a proxy or CORS-enabled server
244
- // Never expose API credentials in client-side code!
111
+ ## Contributing
245
112
 
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
- ```
113
+ Contributions are welcome! Please feel free to submit a Pull Request.
252
114
 
253
115
  ## License
254
116
 
255
- MIT
117
+ MIT License - see [LICENSE](LICENSE) file for details
256
118
 
257
119
  ## Links
258
120
 
259
- - [OnlyWorlds Documentation](https://onlyworlds.github.io/)
260
- - [API Reference](https://www.onlyworlds.com/api/docs)
121
+ - [OnlyWorlds Website](https://onlyworlds.com)
122
+ - [Documentation](https://onlyworlds.github.io/)
261
123
  - [NPM Package](https://www.npmjs.com/package/@onlyworlds/sdk)
262
- - [GitHub](https://github.com/onlyworlds/sdk)
124
+ - [Report Issues](https://github.com/OnlyWorlds/sdk/issues)