@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/dist/index.js ADDED
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ ElementType: () => ElementType,
24
+ OnlyWorldsClient: () => OnlyWorldsClient
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+
28
+ // src/client.ts
29
+ var Resource = class {
30
+ constructor(client, elementType) {
31
+ this.client = client;
32
+ this.elementType = elementType;
33
+ }
34
+ async list(options) {
35
+ return this.client.request("GET", `/${this.elementType}/`, { params: options });
36
+ }
37
+ async get(id) {
38
+ return this.client.request("GET", `/${this.elementType}/${id}/`);
39
+ }
40
+ async create(data) {
41
+ return this.client.request("POST", `/${this.elementType}/`, { body: data });
42
+ }
43
+ async update(id, data) {
44
+ return this.client.request("PATCH", `/${this.elementType}/${id}/`, { body: data });
45
+ }
46
+ async delete(id) {
47
+ return this.client.request("DELETE", `/${this.elementType}/${id}/`);
48
+ }
49
+ };
50
+ var OnlyWorldsClient = class {
51
+ constructor(config) {
52
+ this.baseUrl = config.baseUrl || "https://www.onlyworlds.com/api/worldapi";
53
+ this.headers = {
54
+ "Content-Type": "application/json",
55
+ "API-Key": config.apiKey,
56
+ "API-Pin": config.apiPin
57
+ };
58
+ this.abilities = new Resource(this, "ability");
59
+ this.characters = new Resource(this, "character");
60
+ this.collectives = new Resource(this, "collective");
61
+ this.constructs = new Resource(this, "construct");
62
+ this.creatures = new Resource(this, "creature");
63
+ this.events = new Resource(this, "event");
64
+ this.families = new Resource(this, "family");
65
+ this.institutions = new Resource(this, "institution");
66
+ this.languages = new Resource(this, "language");
67
+ this.laws = new Resource(this, "law");
68
+ this.locations = new Resource(this, "location");
69
+ this.maps = new Resource(this, "map");
70
+ this.markers = new Resource(this, "marker");
71
+ this.narratives = new Resource(this, "narrative");
72
+ this.objects = new Resource(this, "object");
73
+ this.phenomena = new Resource(this, "phenomenon");
74
+ this.pins = new Resource(this, "pin");
75
+ this.relations = new Resource(this, "relation");
76
+ this.species = new Resource(this, "species");
77
+ this.titles = new Resource(this, "title");
78
+ this.traits = new Resource(this, "trait");
79
+ this.zones = new Resource(this, "zone");
80
+ }
81
+ /**
82
+ * Make a request to the OnlyWorlds API
83
+ */
84
+ async request(method, path, options) {
85
+ const url = new URL(`${this.baseUrl}${path}`);
86
+ if (options?.params) {
87
+ Object.entries(options.params).forEach(([key, value]) => {
88
+ if (value !== void 0 && value !== null) {
89
+ url.searchParams.append(key, String(value));
90
+ }
91
+ });
92
+ }
93
+ const fetchOptions = {
94
+ method,
95
+ headers: this.headers
96
+ };
97
+ if (options?.body && ["POST", "PATCH", "PUT"].includes(method)) {
98
+ fetchOptions.body = JSON.stringify(options.body);
99
+ }
100
+ const response = await fetch(url.toString(), fetchOptions);
101
+ if (!response.ok) {
102
+ let errorMessage = `API Error ${response.status}`;
103
+ try {
104
+ const errorText = await response.text();
105
+ if (errorText) {
106
+ try {
107
+ const errorJson = JSON.parse(errorText);
108
+ errorMessage += `: ${errorJson.detail || errorJson.error || errorText}`;
109
+ } catch {
110
+ errorMessage += `: ${errorText}`;
111
+ }
112
+ }
113
+ } catch {
114
+ }
115
+ throw new Error(errorMessage);
116
+ }
117
+ if (response.status === 204) {
118
+ return void 0;
119
+ }
120
+ return response.json();
121
+ }
122
+ /**
123
+ * Get all worlds accessible with current credentials
124
+ */
125
+ async getWorlds() {
126
+ return this.request("GET", "/worlds/");
127
+ }
128
+ /**
129
+ * Helper to convert nested objects to _id/_ids format
130
+ */
131
+ static prepareInput(data) {
132
+ const result = { ...data };
133
+ for (const [key, value] of Object.entries(result)) {
134
+ if (value && typeof value === "object" && "id" in value) {
135
+ delete result[key];
136
+ result[`${key}_id`] = value.id;
137
+ } else if (Array.isArray(value) && value.length > 0 && typeof value[0] === "object" && "id" in value[0]) {
138
+ delete result[key];
139
+ result[`${key}_ids`] = value.map((item) => item.id);
140
+ }
141
+ }
142
+ return result;
143
+ }
144
+ };
145
+
146
+ // src/types.ts
147
+ var ElementType = /* @__PURE__ */ ((ElementType2) => {
148
+ ElementType2["Ability"] = "ability";
149
+ ElementType2["Character"] = "character";
150
+ ElementType2["Collective"] = "collective";
151
+ ElementType2["Construct"] = "construct";
152
+ ElementType2["Creature"] = "creature";
153
+ ElementType2["Event"] = "event";
154
+ ElementType2["Family"] = "family";
155
+ ElementType2["Institution"] = "institution";
156
+ ElementType2["Language"] = "language";
157
+ ElementType2["Law"] = "law";
158
+ ElementType2["Location"] = "location";
159
+ ElementType2["Map"] = "map";
160
+ ElementType2["Marker"] = "marker";
161
+ ElementType2["Narrative"] = "narrative";
162
+ ElementType2["Object"] = "object";
163
+ ElementType2["Phenomenon"] = "phenomenon";
164
+ ElementType2["Pin"] = "pin";
165
+ ElementType2["Relation"] = "relation";
166
+ ElementType2["Species"] = "species";
167
+ ElementType2["Title"] = "title";
168
+ ElementType2["Trait"] = "trait";
169
+ ElementType2["Zone"] = "zone";
170
+ return ElementType2;
171
+ })(ElementType || {});
172
+ // Annotate the CommonJS export names for ESM import in node:
173
+ 0 && (module.exports = {
174
+ ElementType,
175
+ OnlyWorldsClient
176
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,148 @@
1
+ // src/client.ts
2
+ var Resource = class {
3
+ constructor(client, elementType) {
4
+ this.client = client;
5
+ this.elementType = elementType;
6
+ }
7
+ async list(options) {
8
+ return this.client.request("GET", `/${this.elementType}/`, { params: options });
9
+ }
10
+ async get(id) {
11
+ return this.client.request("GET", `/${this.elementType}/${id}/`);
12
+ }
13
+ async create(data) {
14
+ return this.client.request("POST", `/${this.elementType}/`, { body: data });
15
+ }
16
+ async update(id, data) {
17
+ return this.client.request("PATCH", `/${this.elementType}/${id}/`, { body: data });
18
+ }
19
+ async delete(id) {
20
+ return this.client.request("DELETE", `/${this.elementType}/${id}/`);
21
+ }
22
+ };
23
+ var OnlyWorldsClient = class {
24
+ constructor(config) {
25
+ this.baseUrl = config.baseUrl || "https://www.onlyworlds.com/api/worldapi";
26
+ this.headers = {
27
+ "Content-Type": "application/json",
28
+ "API-Key": config.apiKey,
29
+ "API-Pin": config.apiPin
30
+ };
31
+ this.abilities = new Resource(this, "ability");
32
+ this.characters = new Resource(this, "character");
33
+ this.collectives = new Resource(this, "collective");
34
+ this.constructs = new Resource(this, "construct");
35
+ this.creatures = new Resource(this, "creature");
36
+ this.events = new Resource(this, "event");
37
+ this.families = new Resource(this, "family");
38
+ this.institutions = new Resource(this, "institution");
39
+ this.languages = new Resource(this, "language");
40
+ this.laws = new Resource(this, "law");
41
+ this.locations = new Resource(this, "location");
42
+ this.maps = new Resource(this, "map");
43
+ this.markers = new Resource(this, "marker");
44
+ this.narratives = new Resource(this, "narrative");
45
+ this.objects = new Resource(this, "object");
46
+ this.phenomena = new Resource(this, "phenomenon");
47
+ this.pins = new Resource(this, "pin");
48
+ this.relations = new Resource(this, "relation");
49
+ this.species = new Resource(this, "species");
50
+ this.titles = new Resource(this, "title");
51
+ this.traits = new Resource(this, "trait");
52
+ this.zones = new Resource(this, "zone");
53
+ }
54
+ /**
55
+ * Make a request to the OnlyWorlds API
56
+ */
57
+ async request(method, path, options) {
58
+ const url = new URL(`${this.baseUrl}${path}`);
59
+ if (options?.params) {
60
+ Object.entries(options.params).forEach(([key, value]) => {
61
+ if (value !== void 0 && value !== null) {
62
+ url.searchParams.append(key, String(value));
63
+ }
64
+ });
65
+ }
66
+ const fetchOptions = {
67
+ method,
68
+ headers: this.headers
69
+ };
70
+ if (options?.body && ["POST", "PATCH", "PUT"].includes(method)) {
71
+ fetchOptions.body = JSON.stringify(options.body);
72
+ }
73
+ const response = await fetch(url.toString(), fetchOptions);
74
+ if (!response.ok) {
75
+ let errorMessage = `API Error ${response.status}`;
76
+ try {
77
+ const errorText = await response.text();
78
+ if (errorText) {
79
+ try {
80
+ const errorJson = JSON.parse(errorText);
81
+ errorMessage += `: ${errorJson.detail || errorJson.error || errorText}`;
82
+ } catch {
83
+ errorMessage += `: ${errorText}`;
84
+ }
85
+ }
86
+ } catch {
87
+ }
88
+ throw new Error(errorMessage);
89
+ }
90
+ if (response.status === 204) {
91
+ return void 0;
92
+ }
93
+ return response.json();
94
+ }
95
+ /**
96
+ * Get all worlds accessible with current credentials
97
+ */
98
+ async getWorlds() {
99
+ return this.request("GET", "/worlds/");
100
+ }
101
+ /**
102
+ * Helper to convert nested objects to _id/_ids format
103
+ */
104
+ static prepareInput(data) {
105
+ const result = { ...data };
106
+ for (const [key, value] of Object.entries(result)) {
107
+ if (value && typeof value === "object" && "id" in value) {
108
+ delete result[key];
109
+ result[`${key}_id`] = value.id;
110
+ } else if (Array.isArray(value) && value.length > 0 && typeof value[0] === "object" && "id" in value[0]) {
111
+ delete result[key];
112
+ result[`${key}_ids`] = value.map((item) => item.id);
113
+ }
114
+ }
115
+ return result;
116
+ }
117
+ };
118
+
119
+ // src/types.ts
120
+ var ElementType = /* @__PURE__ */ ((ElementType2) => {
121
+ ElementType2["Ability"] = "ability";
122
+ ElementType2["Character"] = "character";
123
+ ElementType2["Collective"] = "collective";
124
+ ElementType2["Construct"] = "construct";
125
+ ElementType2["Creature"] = "creature";
126
+ ElementType2["Event"] = "event";
127
+ ElementType2["Family"] = "family";
128
+ ElementType2["Institution"] = "institution";
129
+ ElementType2["Language"] = "language";
130
+ ElementType2["Law"] = "law";
131
+ ElementType2["Location"] = "location";
132
+ ElementType2["Map"] = "map";
133
+ ElementType2["Marker"] = "marker";
134
+ ElementType2["Narrative"] = "narrative";
135
+ ElementType2["Object"] = "object";
136
+ ElementType2["Phenomenon"] = "phenomenon";
137
+ ElementType2["Pin"] = "pin";
138
+ ElementType2["Relation"] = "relation";
139
+ ElementType2["Species"] = "species";
140
+ ElementType2["Title"] = "title";
141
+ ElementType2["Trait"] = "trait";
142
+ ElementType2["Zone"] = "zone";
143
+ return ElementType2;
144
+ })(ElementType || {});
145
+ export {
146
+ ElementType,
147
+ OnlyWorldsClient
148
+ };
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@onlyworlds/sdk",
3
+ "version": "1.0.0",
4
+ "description": "TypeScript SDK for the OnlyWorlds API - build world-building applications with type safety",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "README.md"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
14
+ "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
15
+ "prepublishOnly": "npm run build"
16
+ },
17
+ "keywords": [
18
+ "onlyworlds",
19
+ "worldbuilding",
20
+ "api",
21
+ "sdk",
22
+ "typescript",
23
+ "rpg",
24
+ "ttrpg",
25
+ "game-development"
26
+ ],
27
+ "author": "OnlyWorlds",
28
+ "license": "MIT",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/onlyworlds/sdk"
32
+ },
33
+ "devDependencies": {
34
+ "tsup": "^8.0.1",
35
+ "typescript": "^5.3.3"
36
+ },
37
+ "peerDependencies": {
38
+ "typescript": ">=4.5.0"
39
+ },
40
+ "engines": {
41
+ "node": ">=18.0.0"
42
+ }
43
+ }