@jsonfirst/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.
Files changed (3) hide show
  1. package/README.md +54 -0
  2. package/index.js +124 -0
  3. package/package.json +36 -0
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # @jsonfirst/sdk
2
+
3
+ Official JavaScript/Node.js SDK for the **JSONFIRST Protocol** — the Universal Intent Protocol for AI governance.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @jsonfirst/sdk
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```javascript
14
+ const JsonFirst = require('@jsonfirst/sdk');
15
+
16
+ const client = new JsonFirst({ apiKey: 'YOUR_API_KEY' });
17
+
18
+ // Parse a natural language intent
19
+ const result = await client.parse('Create a new order for John Smith, 2 units of product A');
20
+ console.log(result);
21
+
22
+ // Validate a JSONFIRST JSON
23
+ const { valid, errors } = client.validate(result);
24
+ console.log(valid, errors);
25
+ ```
26
+
27
+ ## Methods
28
+
29
+ | Method | Description |
30
+ |--------|-------------|
31
+ | `parse(text, options?)` | Convert natural language to JSONFIRST JSON |
32
+ | `validate(json)` | Validate a JSON against the JSONFIRST spec |
33
+ | `pull(schemaId)` | Fetch a publicly shared schema |
34
+ | `share(outputJson, title?)` | Share a schema and get a public link |
35
+ | `usage()` | Get your account usage stats |
36
+
37
+ ## Options
38
+
39
+ ```javascript
40
+ const client = new JsonFirst({
41
+ apiKey: 'YOUR_API_KEY',
42
+ baseUrl: 'https://jsonfirst.io' // optional override
43
+ });
44
+ ```
45
+
46
+ ## Links
47
+
48
+ - [Documentation](https://jsonfirst.io/developers)
49
+ - [Templates](https://jsonfirst.io/templates)
50
+ - [GitHub](https://github.com/jsonfirst/sdk)
51
+
52
+ ## License
53
+
54
+ MIT
package/index.js ADDED
@@ -0,0 +1,124 @@
1
+ /**
2
+ * @jsonfirst/sdk v1.0.0
3
+ * Official JavaScript/Node.js SDK for the JSONFIRST Protocol
4
+ *
5
+ * Usage:
6
+ * const JsonFirst = require('@jsonfirst/sdk');
7
+ * const client = new JsonFirst({ apiKey: 'YOUR_API_KEY' });
8
+ * const result = await client.parse('Create a new order for John Smith');
9
+ *
10
+ * Install:
11
+ * npm install @jsonfirst/sdk
12
+ */
13
+
14
+ (function(root, factory) {
15
+ if (typeof module === 'object' && module.exports) {
16
+ module.exports = factory();
17
+ } else {
18
+ root.JsonFirst = factory();
19
+ }
20
+ }(typeof globalThis !== 'undefined' ? globalThis : this, function() {
21
+
22
+ const DEFAULT_BASE_URL = 'https://jsonfirst.io';
23
+
24
+ class JsonFirstError extends Error {
25
+ constructor(message, status, body) {
26
+ super(message);
27
+ this.name = 'JsonFirstError';
28
+ this.status = status;
29
+ this.body = body;
30
+ }
31
+ }
32
+
33
+ class JsonFirst {
34
+ /**
35
+ * @param {Object} options
36
+ * @param {string} options.apiKey - Your JSONFIRST API key
37
+ * @param {string} [options.baseUrl] - Override base URL (default: https://jsonfirst.io)
38
+ */
39
+ constructor(options = {}) {
40
+ if (!options.apiKey) throw new Error('JsonFirst: apiKey is required');
41
+ this.apiKey = options.apiKey;
42
+ this.baseUrl = (options.baseUrl || DEFAULT_BASE_URL).replace(/\/$/, '');
43
+ }
44
+
45
+ _headers() {
46
+ return {
47
+ 'Content-Type': 'application/json',
48
+ 'Authorization': `Bearer ${this.apiKey}`,
49
+ };
50
+ }
51
+
52
+ async _request(method, path, body) {
53
+ const url = `${this.baseUrl}/api${path}`;
54
+ const res = await fetch(url, {
55
+ method,
56
+ headers: this._headers(),
57
+ body: body ? JSON.stringify(body) : undefined,
58
+ });
59
+ const data = await res.json().catch(() => ({}));
60
+ if (!res.ok) throw new JsonFirstError(data.detail || 'Request failed', res.status, data);
61
+ return data;
62
+ }
63
+
64
+ /**
65
+ * Parse a natural language intent into a JSONFIRST JSON.
66
+ * @param {string} text - The intent to parse
67
+ * @param {Object} [options] - Optional params: model, execution_mode
68
+ * @returns {Promise<Object>} JSONFIRST JSON output
69
+ */
70
+ async parse(text, options = {}) {
71
+ return this._request('POST', '/parse', { input: text, ...options });
72
+ }
73
+
74
+ /**
75
+ * Pull a publicly shared schema by its share ID.
76
+ * @param {string} schemaId - The share ID (e.g. "abc123xy")
77
+ * @returns {Promise<Object>} Schema object
78
+ */
79
+ async pull(schemaId) {
80
+ if (!schemaId) throw new Error('pull: schemaId is required');
81
+ const res = await fetch(`${this.baseUrl}/api/schemas/public/${schemaId}`);
82
+ const data = await res.json().catch(() => ({}));
83
+ if (!res.ok) throw new JsonFirstError(data.detail || 'Schema not found', res.status, data);
84
+ return data;
85
+ }
86
+
87
+ /**
88
+ * Validate a JSON object against the JSONFIRST spec.
89
+ * @param {Object} json - The JSON to validate
90
+ * @returns {{ valid: boolean, errors: string[] }}
91
+ */
92
+ validate(json) {
93
+ const errors = [];
94
+ if (!json || typeof json !== 'object') {
95
+ return { valid: false, errors: ['Input must be a JSON object'] };
96
+ }
97
+ if (json.spec !== 'JSONFIRST') errors.push('Missing or incorrect "spec" field (expected "JSONFIRST")');
98
+ if (!json.version) errors.push('Missing "version" field');
99
+ if (!json.jdons || !Array.isArray(json.jdons)) errors.push('Missing "jdons" array');
100
+ if (!json.execution || typeof json.execution !== 'object') errors.push('Missing "execution" object');
101
+ return { valid: errors.length === 0, errors };
102
+ }
103
+
104
+ /**
105
+ * Share a schema output and get a public link.
106
+ * @param {Object} outputJson - The JSONFIRST JSON to share
107
+ * @param {string} [title] - Optional title
108
+ * @returns {Promise<{ share_id: string, url: string }>}
109
+ */
110
+ async share(outputJson, title) {
111
+ return this._request('POST', '/schemas/share', { output_json: outputJson, title: title || 'Shared Schema' });
112
+ }
113
+
114
+ /**
115
+ * Get your account usage and remaining intents.
116
+ * @returns {Promise<Object>}
117
+ */
118
+ async usage() {
119
+ return this._request('GET', '/auth/me');
120
+ }
121
+ }
122
+
123
+ return JsonFirst;
124
+ }));
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@jsonfirst/sdk",
3
+ "version": "1.0.0",
4
+ "description": "Official JavaScript/Node.js SDK for the JSONFIRST Protocol — Universal Intent Protocol for AI governance",
5
+ "main": "index.js",
6
+ "files": [
7
+ "index.js",
8
+ "README.md"
9
+ ],
10
+ "scripts": {
11
+ "test": "node -e \"const sdk = require('.'); const jf = new sdk({apiKey:'test'}); console.log('SDK loaded OK'); const v = jf.validate({spec:'JSONFIRST',version:'2.0',jdons:[],execution:{}}); console.log('validate:', v);\""
12
+ },
13
+ "keywords": [
14
+ "jsonfirst",
15
+ "json",
16
+ "ai",
17
+ "governance",
18
+ "llm",
19
+ "intent",
20
+ "protocol",
21
+ "sdk",
22
+ "chatgpt",
23
+ "claude",
24
+ "gemini"
25
+ ],
26
+ "author": "JSONFIRST <contact@jsonfirst.io>",
27
+ "license": "MIT",
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/jsonfirst/sdk"
31
+ },
32
+ "homepage": "https://jsonfirst.io",
33
+ "engines": {
34
+ "node": ">=14"
35
+ }
36
+ }