@jsontech/sdk-js 0.0.6 → 0.0.8

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 (2) hide show
  1. package/README.md +130 -0
  2. package/package.json +3 -2
package/README.md ADDED
@@ -0,0 +1,130 @@
1
+ # @jsontech/sdk-js
2
+
3
+ JavaScript/TypeScript SDK for ShipIt feature flags.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @jsontech/sdk-js
9
+ # or
10
+ pnpm add @jsontech/sdk-js
11
+ # or
12
+ yarn add @jsontech/sdk-js
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Basic Example
18
+
19
+ ```typescript
20
+ import { ShipItClient } from '@jsontech/sdk-js';
21
+
22
+ // SDK automatically uses production API URL
23
+ // Set SHIPIT_CLIENT_KEY or SHIPIT_SERVER_KEY env var, or pass sdkKey explicitly
24
+ const shipit = new ShipItClient({
25
+ sdkKey: 'your-sdk-key-here'
26
+ });
27
+
28
+ const enabled = await shipit.bool('new-nav', { id: 'user-123' }, false);
29
+ console.log(enabled);
30
+ ```
31
+
32
+ ### Environment Variables
33
+
34
+ The SDK automatically reads from environment variables if `sdkKey` is not provided:
35
+
36
+ - `SHIPIT_CLIENT_KEY` - Client SDK key (for browser/mobile)
37
+ - `SHIPIT_SERVER_KEY` - Server SDK key (for backend)
38
+
39
+ ```typescript
40
+ // In Node.js, this will use SHIPIT_CLIENT_KEY or SHIPIT_SERVER_KEY from env
41
+ const shipit = new ShipItClient();
42
+ ```
43
+
44
+ ### API Base URL
45
+
46
+ The SDK automatically determines the API base URL:
47
+
48
+ - **Browser**: Uses `window.location.origin` (assumes API is on same origin)
49
+ - **Node.js**: Uses the production ShipIt API endpoint
50
+
51
+ The API URL cannot be overridden.
52
+
53
+ ### User Payload
54
+
55
+ ```typescript
56
+ import { ShipItClient, type ShipItUserPayload } from '@jsontech/sdk-js';
57
+
58
+ const user: ShipItUserPayload = {
59
+ id: 'user-123', // Required: unique user identifier
60
+ email: 'user@example.com',
61
+ name: 'John Doe',
62
+ country: 'US',
63
+ meta: { // Custom attributes for targeting
64
+ companyId: 'acme',
65
+ plan: 'pro'
66
+ }
67
+ };
68
+
69
+ const enabled = await shipit.bool('feature-flag', user, false);
70
+ ```
71
+
72
+ ## API Reference
73
+
74
+ ### `ShipItClient`
75
+
76
+ #### Constructor
77
+
78
+ ```typescript
79
+ new ShipItClient(options?: ShipItClientOptions)
80
+ ```
81
+
82
+ **Options:**
83
+
84
+ - `sdkKey?: string` - SDK key (client or server). If not provided, reads from `SHIPIT_CLIENT_KEY` or `SHIPIT_SERVER_KEY` env vars.
85
+ - `projectKey?: string` - Legacy: project key (requires `envKey`). Not recommended.
86
+ - `envKey?: string` - Environment key (default: `'production'`). Only used with `projectKey`.
87
+
88
+ #### Methods
89
+
90
+ ##### `bool(flagKey: string, user: ShipItUserPayload, defaultValue?: boolean): Promise<boolean>`
91
+
92
+ Evaluates a boolean feature flag for a user.
93
+
94
+ - `flagKey`: The flag key to evaluate
95
+ - `user`: User payload with `id` or `key` (required)
96
+ - `defaultValue`: Default value if evaluation fails (default: `false`)
97
+
98
+ Returns `Promise<boolean>` - The flag value for the user.
99
+
100
+ **Example:**
101
+
102
+ ```typescript
103
+ const enabled = await shipit.bool('new-nav', { id: 'user-123' }, false);
104
+ ```
105
+
106
+ ## Error Handling
107
+
108
+ If the API request fails (network error, non-2xx status), the SDK returns the `defaultValue`. Network errors (DNS, timeout, connection refused) will throw; wrap calls in `try/catch` if you want to handle them.
109
+
110
+ ```typescript
111
+ try {
112
+ const enabled = await shipit.bool('flag', user, false);
113
+ } catch (error) {
114
+ // Handle network errors
115
+ console.error('Failed to evaluate flag:', error);
116
+ }
117
+ ```
118
+
119
+ ## SDK Keys
120
+
121
+ Each environment has two SDK keys:
122
+
123
+ - **Server key**: Secret. Use only in trusted server environments.
124
+ - **Client key**: Not a secret. Intended for browser/mobile SDKs.
125
+
126
+ Get your SDK keys from your ShipIt Console → Environments.
127
+
128
+ ## License
129
+
130
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsontech/sdk-js",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,7 +17,8 @@
17
17
  }
18
18
  },
19
19
  "files": [
20
- "dist"
20
+ "dist",
21
+ "README.md"
21
22
  ],
22
23
  "sideEffects": false,
23
24
  "scripts": {