@featureflare/sdk-js 0.0.5 → 0.0.6
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/README.md +22 -31
- package/dist/index.cjs +10 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -7
- package/dist/index.d.ts +6 -7
- package/dist/index.js +10 -20
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @featureflare/sdk-js
|
|
2
2
|
|
|
3
|
-
JavaScript/TypeScript SDK for
|
|
3
|
+
JavaScript/TypeScript SDK for ShipIt feature flags.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -17,15 +17,15 @@ yarn add @featureflare/sdk-js
|
|
|
17
17
|
### Basic Example
|
|
18
18
|
|
|
19
19
|
```typescript
|
|
20
|
-
import {
|
|
20
|
+
import { ShipItClient } from '@featureflare/sdk-js';
|
|
21
21
|
|
|
22
22
|
// SDK automatically uses production API URL
|
|
23
|
-
// Set
|
|
24
|
-
const
|
|
23
|
+
// Set SHIPIT_CLIENT_KEY or SHIPIT_SERVER_KEY env var, or pass sdkKey explicitly
|
|
24
|
+
const shipit = new ShipItClient({
|
|
25
25
|
sdkKey: 'your-sdk-key-here'
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
-
const enabled = await
|
|
28
|
+
const enabled = await shipit.bool('new-nav', { id: 'user-123' }, false);
|
|
29
29
|
console.log(enabled);
|
|
30
30
|
```
|
|
31
31
|
|
|
@@ -33,37 +33,29 @@ console.log(enabled);
|
|
|
33
33
|
|
|
34
34
|
The SDK automatically reads from environment variables if `sdkKey` is not provided:
|
|
35
35
|
|
|
36
|
-
- `
|
|
37
|
-
- `
|
|
36
|
+
- `SHIPIT_CLIENT_KEY` - Client SDK key (for browser/mobile)
|
|
37
|
+
- `SHIPIT_SERVER_KEY` - Server SDK key (for backend)
|
|
38
38
|
|
|
39
39
|
```typescript
|
|
40
|
-
// In Node.js, this will use
|
|
41
|
-
const
|
|
40
|
+
// In Node.js, this will use SHIPIT_CLIENT_KEY or SHIPIT_SERVER_KEY from env
|
|
41
|
+
const shipit = new ShipItClient();
|
|
42
42
|
```
|
|
43
43
|
|
|
44
44
|
### API Base URL
|
|
45
45
|
|
|
46
46
|
The SDK automatically determines the API base URL:
|
|
47
47
|
|
|
48
|
-
-
|
|
49
|
-
- **
|
|
50
|
-
- **Default fallback (all runtimes)**: `https://shipit-api-392444455847.us-central1.run.app`.
|
|
48
|
+
- **Browser**: Uses `window.location.origin` (assumes API is on same origin)
|
|
49
|
+
- **Node.js**: Uses the production ShipIt API endpoint
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
```typescript
|
|
55
|
-
const featureflare = new FeatureFlareClient({
|
|
56
|
-
apiBaseUrl: 'https://flags.your-company.com',
|
|
57
|
-
sdkKey: 'featureflare_cli_...'
|
|
58
|
-
});
|
|
59
|
-
```
|
|
51
|
+
The API URL cannot be overridden.
|
|
60
52
|
|
|
61
53
|
### User Payload
|
|
62
54
|
|
|
63
55
|
```typescript
|
|
64
|
-
import {
|
|
56
|
+
import { ShipItClient, type ShipItUserPayload } from '@featureflare/sdk-js';
|
|
65
57
|
|
|
66
|
-
const user:
|
|
58
|
+
const user: ShipItUserPayload = {
|
|
67
59
|
id: 'user-123', // Required: unique user identifier
|
|
68
60
|
email: 'user@example.com',
|
|
69
61
|
name: 'John Doe',
|
|
@@ -74,29 +66,28 @@ const user: FeatureFlareUserPayload = {
|
|
|
74
66
|
}
|
|
75
67
|
};
|
|
76
68
|
|
|
77
|
-
const enabled = await
|
|
69
|
+
const enabled = await shipit.bool('feature-flag', user, false);
|
|
78
70
|
```
|
|
79
71
|
|
|
80
72
|
## API Reference
|
|
81
73
|
|
|
82
|
-
### `
|
|
74
|
+
### `ShipItClient`
|
|
83
75
|
|
|
84
76
|
#### Constructor
|
|
85
77
|
|
|
86
78
|
```typescript
|
|
87
|
-
new
|
|
79
|
+
new ShipItClient(options?: ShipItClientOptions)
|
|
88
80
|
```
|
|
89
81
|
|
|
90
82
|
**Options:**
|
|
91
83
|
|
|
92
|
-
- `
|
|
93
|
-
- `sdkKey?: string` - SDK key (client or server). If not provided, reads from `FEATUREFLARE_CLIENT_KEY` or `FEATUREFLARE_SERVER_KEY` env vars.
|
|
84
|
+
- `sdkKey?: string` - SDK key (client or server). If not provided, reads from `SHIPIT_CLIENT_KEY` or `SHIPIT_SERVER_KEY` env vars.
|
|
94
85
|
- `projectKey?: string` - Legacy: project key (requires `envKey`). Not recommended.
|
|
95
86
|
- `envKey?: string` - Environment key (default: `'production'`). Only used with `projectKey`.
|
|
96
87
|
|
|
97
88
|
#### Methods
|
|
98
89
|
|
|
99
|
-
##### `bool(flagKey: string, user:
|
|
90
|
+
##### `bool(flagKey: string, user: ShipItUserPayload, defaultValue?: boolean): Promise<boolean>`
|
|
100
91
|
|
|
101
92
|
Evaluates a boolean feature flag for a user.
|
|
102
93
|
|
|
@@ -109,7 +100,7 @@ Returns `Promise<boolean>` - The flag value for the user.
|
|
|
109
100
|
**Example:**
|
|
110
101
|
|
|
111
102
|
```typescript
|
|
112
|
-
const enabled = await
|
|
103
|
+
const enabled = await shipit.bool('new-nav', { id: 'user-123' }, false);
|
|
113
104
|
```
|
|
114
105
|
|
|
115
106
|
## Error Handling
|
|
@@ -118,7 +109,7 @@ If the API request fails (network error, non-2xx status), the SDK returns the `d
|
|
|
118
109
|
|
|
119
110
|
```typescript
|
|
120
111
|
try {
|
|
121
|
-
const enabled = await
|
|
112
|
+
const enabled = await shipit.bool('flag', user, false);
|
|
122
113
|
} catch (error) {
|
|
123
114
|
// Handle network errors
|
|
124
115
|
console.error('Failed to evaluate flag:', error);
|
|
@@ -132,7 +123,7 @@ Each environment has two SDK keys:
|
|
|
132
123
|
- **Server key**: Secret. Use only in trusted server environments.
|
|
133
124
|
- **Client key**: Not a secret. Intended for browser/mobile SDKs.
|
|
134
125
|
|
|
135
|
-
Get your SDK keys from your
|
|
126
|
+
Get your SDK keys from your ShipIt Console → Environments.
|
|
136
127
|
|
|
137
128
|
## License
|
|
138
129
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,45 +1,35 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
// src/index.ts
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return process.env.FEATUREFLARE_API_BASE_URL?.trim() || process.env.SHIPIT_API_BASE_URL?.trim() || null;
|
|
4
|
+
function getApiBaseUrl() {
|
|
5
|
+
if (typeof window !== "undefined" && typeof window.location !== "undefined") {
|
|
6
|
+
return window.location.origin;
|
|
8
7
|
}
|
|
9
|
-
return
|
|
10
|
-
}
|
|
11
|
-
function getApiBaseUrl(explicit) {
|
|
12
|
-
const fromOptions = explicit?.trim();
|
|
13
|
-
if (fromOptions) return fromOptions;
|
|
14
|
-
const fromEnv = getApiBaseUrlFromEnv();
|
|
15
|
-
if (fromEnv) return fromEnv;
|
|
16
|
-
return DEFAULT_FEATUREFLARE_API_BASE_URL;
|
|
8
|
+
return "https://shipit-api-246728836834.us-central1.run.app";
|
|
17
9
|
}
|
|
18
10
|
function getSdkKeyFromEnv() {
|
|
19
11
|
if (typeof process !== "undefined" && process.env) {
|
|
20
|
-
return process.env.
|
|
12
|
+
return process.env.SHIPIT_CLIENT_KEY?.trim() || process.env.SHIPIT_SERVER_KEY?.trim() || null;
|
|
21
13
|
}
|
|
22
14
|
return null;
|
|
23
15
|
}
|
|
24
|
-
var
|
|
16
|
+
var ShipItClient = class {
|
|
25
17
|
apiBaseUrl;
|
|
26
18
|
sdkKey;
|
|
27
19
|
projectKey;
|
|
28
20
|
envKey;
|
|
29
21
|
constructor(options = {}) {
|
|
30
|
-
this.apiBaseUrl = getApiBaseUrl(
|
|
22
|
+
this.apiBaseUrl = getApiBaseUrl().replace(/\/$/, "");
|
|
31
23
|
this.sdkKey = options.sdkKey?.trim() || getSdkKeyFromEnv();
|
|
32
24
|
this.projectKey = options.projectKey?.trim() ? options.projectKey.trim() : null;
|
|
33
25
|
this.envKey = options.envKey ?? "production";
|
|
34
26
|
if (!this.sdkKey && !this.projectKey) {
|
|
35
|
-
throw new Error(
|
|
36
|
-
"FeatureFlareClient requires either sdkKey (recommended) or projectKey (legacy). Set FEATUREFLARE_CLIENT_KEY or FEATUREFLARE_SERVER_KEY env var, or pass sdkKey in options."
|
|
37
|
-
);
|
|
27
|
+
throw new Error("ShipItClient requires either sdkKey (recommended) or projectKey (legacy). Set SHIPIT_CLIENT_KEY or SHIPIT_SERVER_KEY env var, or pass sdkKey in options.");
|
|
38
28
|
}
|
|
39
29
|
}
|
|
40
30
|
normalizeUser(input) {
|
|
41
31
|
const key = (input.id ?? input.key ?? "").trim();
|
|
42
|
-
if (!key) throw new Error("
|
|
32
|
+
if (!key) throw new Error("ShipItClient requires user.id (or legacy user.key).");
|
|
43
33
|
return {
|
|
44
34
|
key,
|
|
45
35
|
email: input.email,
|
|
@@ -78,6 +68,6 @@ var FeatureFlareClient = class {
|
|
|
78
68
|
}
|
|
79
69
|
};
|
|
80
70
|
|
|
81
|
-
exports.
|
|
71
|
+
exports.ShipItClient = ShipItClient;
|
|
82
72
|
//# sourceMappingURL=index.cjs.map
|
|
83
73
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAsBA,
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAsBA,SAAS,aAAA,GAAwB;AAE/B,EAAA,IAAI,OAAO,MAAA,KAAW,WAAA,IAAe,OAAQ,MAAA,CAAe,aAAa,WAAA,EAAa;AACpF,IAAA,OAAQ,OAAe,QAAA,CAAS,MAAA;AAAA,EAClC;AAEA,EAAA,OAAO,qDAAA;AACT;AAQA,SAAS,gBAAA,GAAkC;AACzC,EAAA,IAAI,OAAO,OA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,KAAA,CAAM;AAAA,KAC9B;AAAA,EACF;AAAA,EAEA,MAAM,IAAA,CAAK,OAAA,EAAiB,IAAA,EAAyB,eAAe,KAAA,EAAyB;AAC3F,IAAA,MAAM,cAAA,GAAiB,IAAA,CAAK,aAAA,CAAc,IAAI,CAAA;AAC9C,IAAA,MAAM,GAAA,GAAM,KAAK,MAAA,GACb,MAAM,MAAM,CAAA,EAAG,IAAA,CAAK,UAAU,CAAA,gBAAA,CAAA,EAAoB;AAAA,MAChD,MAAA,EAAQ,MAAA;AAAA,MACR,OAAA,EAAS;AAAA,QACP,cAAA,EAAgB,kBAAA;AAAA,QAChB,0BAA0B,IAAA,CAAK;AAAA,OACjC;AAAA,MACA,IAAA,EAAM,KAAK,SAAA,CAAU;AAAA,QACnB,OAAA;AAAA,QACA,IAAA,EAAM,cAAA;AAAA,QACN;AAAA,OACD;AAAA,KACF,CAAA,GACD,MAAM,MAAM,CAAA,EAAG,IAAA,CAAK,UAAU,CAAA,YAAA,CAAA,EAAgB;AAAA,MAC5C,MAAA,EAAQ,MAAA;AAAA,MACR,OAAA,EAAS,EAAE,cAAA,EAAgB,kBAAA,EAAmB;AAAA,MAC9C,IAAA,EAAM,KAAK,SAAA,CAAU;AAAA,QACnB,YAAY,IAAA,CAAK,UAAA;AAAA,QACjB,QAAQ,IAAA,CAAK,MAAA;AAAA,QACb,OAAA;AAAA,QACA,IAAA,EAAM,cAAA;AAAA,QACN;AAAA,OACD;AAAA,KACF,CAAA;AAEL,IAAA,IAAI,CAAC,GAAA,CAAI,EAAA,EAAI,OAAO,YAAA;AACpB,IAAA,MAAM,IAAA,GAAQ,MAAM,GAAA,CAAI,IAAA,EAAK;AAC7B,IAAA,OAAO,IAAA,CAAK,KAAA;AAAA,EACd;AACF","file":"index.cjs","sourcesContent":["export type ShipItUserPayload = {\n /** Unique user identifier (preferred). */\n id?: string;\n /** Legacy alias for id. */\n key?: string;\n email?: string;\n name?: string;\n country?: string;\n /** Queryable attributes. */\n meta?: Record<string, string | number | boolean | null>;\n /** Legacy alias for meta. */\n custom?: Record<string, string | number | boolean | null>;\n};\n\ntype ShipItUser = {\n key: string;\n email?: string;\n name?: string;\n country?: string;\n custom?: Record<string, string | number | boolean | null>;\n};\n\nfunction getApiBaseUrl(): string {\n // Browser: use current origin (assumes API is on same origin)\n if (typeof window !== 'undefined' && typeof (window as any).location !== 'undefined') {\n return (window as any).location.origin;\n }\n // Node.js: always use production API\n return 'https://shipit-api-246728836834.us-central1.run.app';\n}\n\nexport type ShipItClientOptions = {\n sdkKey?: string;\n projectKey?: string;\n envKey?: string;\n};\n\nfunction getSdkKeyFromEnv(): string | null {\n if (typeof process !== 'undefined' && process.env) {\n return process.env.SHIPIT_CLIENT_KEY?.trim() || process.env.SHIPIT_SERVER_KEY?.trim() || null;\n }\n return null;\n}\n\nexport class ShipItClient {\n private readonly apiBaseUrl: string;\n private readonly sdkKey: string | null;\n private readonly projectKey: string | null;\n private readonly envKey: string;\n\n constructor(options: ShipItClientOptions = {}) {\n this.apiBaseUrl = getApiBaseUrl().replace(/\\/$/, '');\n this.sdkKey = options.sdkKey?.trim() || getSdkKeyFromEnv();\n this.projectKey = options.projectKey?.trim() ? options.projectKey.trim() : null;\n this.envKey = options.envKey ?? 'production';\n\n if (!this.sdkKey && !this.projectKey) {\n throw new Error('ShipItClient requires either sdkKey (recommended) or projectKey (legacy). Set SHIPIT_CLIENT_KEY or SHIPIT_SERVER_KEY env var, or pass sdkKey in options.');\n }\n }\n\n private normalizeUser(input: ShipItUserPayload): ShipItUser {\n const key = (input.id ?? input.key ?? '').trim();\n if (!key) throw new Error('ShipItClient requires user.id (or legacy user.key).');\n return {\n key,\n email: input.email,\n name: input.name,\n country: input.country,\n custom: input.meta ?? input.custom\n };\n }\n\n async bool(flagKey: string, user: ShipItUserPayload, defaultValue = false): Promise<boolean> {\n const normalizedUser = this.normalizeUser(user);\n const res = this.sdkKey\n ? await fetch(`${this.apiBaseUrl}/api/v1/sdk/eval`, {\n method: 'POST',\n headers: {\n 'content-type': 'application/json',\n 'x-featureflare-sdk-key': this.sdkKey\n },\n body: JSON.stringify({\n flagKey,\n user: normalizedUser,\n defaultValue\n })\n })\n : await fetch(`${this.apiBaseUrl}/api/v1/eval`, {\n method: 'POST',\n headers: { 'content-type': 'application/json' },\n body: JSON.stringify({\n projectKey: this.projectKey,\n envKey: this.envKey,\n flagKey,\n user: normalizedUser,\n defaultValue\n })\n });\n\n if (!res.ok) return defaultValue;\n const json = (await res.json()) as { value: boolean };\n return json.value;\n }\n}\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
type
|
|
1
|
+
type ShipItUserPayload = {
|
|
2
2
|
/** Unique user identifier (preferred). */
|
|
3
3
|
id?: string;
|
|
4
4
|
/** Legacy alias for id. */
|
|
@@ -11,20 +11,19 @@ type FeatureFlareUserPayload = {
|
|
|
11
11
|
/** Legacy alias for meta. */
|
|
12
12
|
custom?: Record<string, string | number | boolean | null>;
|
|
13
13
|
};
|
|
14
|
-
type
|
|
15
|
-
apiBaseUrl?: string;
|
|
14
|
+
type ShipItClientOptions = {
|
|
16
15
|
sdkKey?: string;
|
|
17
16
|
projectKey?: string;
|
|
18
17
|
envKey?: string;
|
|
19
18
|
};
|
|
20
|
-
declare class
|
|
19
|
+
declare class ShipItClient {
|
|
21
20
|
private readonly apiBaseUrl;
|
|
22
21
|
private readonly sdkKey;
|
|
23
22
|
private readonly projectKey;
|
|
24
23
|
private readonly envKey;
|
|
25
|
-
constructor(options?:
|
|
24
|
+
constructor(options?: ShipItClientOptions);
|
|
26
25
|
private normalizeUser;
|
|
27
|
-
bool(flagKey: string, user:
|
|
26
|
+
bool(flagKey: string, user: ShipItUserPayload, defaultValue?: boolean): Promise<boolean>;
|
|
28
27
|
}
|
|
29
28
|
|
|
30
|
-
export {
|
|
29
|
+
export { ShipItClient, type ShipItClientOptions, type ShipItUserPayload };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
type
|
|
1
|
+
type ShipItUserPayload = {
|
|
2
2
|
/** Unique user identifier (preferred). */
|
|
3
3
|
id?: string;
|
|
4
4
|
/** Legacy alias for id. */
|
|
@@ -11,20 +11,19 @@ type FeatureFlareUserPayload = {
|
|
|
11
11
|
/** Legacy alias for meta. */
|
|
12
12
|
custom?: Record<string, string | number | boolean | null>;
|
|
13
13
|
};
|
|
14
|
-
type
|
|
15
|
-
apiBaseUrl?: string;
|
|
14
|
+
type ShipItClientOptions = {
|
|
16
15
|
sdkKey?: string;
|
|
17
16
|
projectKey?: string;
|
|
18
17
|
envKey?: string;
|
|
19
18
|
};
|
|
20
|
-
declare class
|
|
19
|
+
declare class ShipItClient {
|
|
21
20
|
private readonly apiBaseUrl;
|
|
22
21
|
private readonly sdkKey;
|
|
23
22
|
private readonly projectKey;
|
|
24
23
|
private readonly envKey;
|
|
25
|
-
constructor(options?:
|
|
24
|
+
constructor(options?: ShipItClientOptions);
|
|
26
25
|
private normalizeUser;
|
|
27
|
-
bool(flagKey: string, user:
|
|
26
|
+
bool(flagKey: string, user: ShipItUserPayload, defaultValue?: boolean): Promise<boolean>;
|
|
28
27
|
}
|
|
29
28
|
|
|
30
|
-
export {
|
|
29
|
+
export { ShipItClient, type ShipItClientOptions, type ShipItUserPayload };
|
package/dist/index.js
CHANGED
|
@@ -1,43 +1,33 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return process.env.FEATUREFLARE_API_BASE_URL?.trim() || process.env.SHIPIT_API_BASE_URL?.trim() || null;
|
|
2
|
+
function getApiBaseUrl() {
|
|
3
|
+
if (typeof window !== "undefined" && typeof window.location !== "undefined") {
|
|
4
|
+
return window.location.origin;
|
|
6
5
|
}
|
|
7
|
-
return
|
|
8
|
-
}
|
|
9
|
-
function getApiBaseUrl(explicit) {
|
|
10
|
-
const fromOptions = explicit?.trim();
|
|
11
|
-
if (fromOptions) return fromOptions;
|
|
12
|
-
const fromEnv = getApiBaseUrlFromEnv();
|
|
13
|
-
if (fromEnv) return fromEnv;
|
|
14
|
-
return DEFAULT_FEATUREFLARE_API_BASE_URL;
|
|
6
|
+
return "https://shipit-api-246728836834.us-central1.run.app";
|
|
15
7
|
}
|
|
16
8
|
function getSdkKeyFromEnv() {
|
|
17
9
|
if (typeof process !== "undefined" && process.env) {
|
|
18
|
-
return process.env.
|
|
10
|
+
return process.env.SHIPIT_CLIENT_KEY?.trim() || process.env.SHIPIT_SERVER_KEY?.trim() || null;
|
|
19
11
|
}
|
|
20
12
|
return null;
|
|
21
13
|
}
|
|
22
|
-
var
|
|
14
|
+
var ShipItClient = class {
|
|
23
15
|
apiBaseUrl;
|
|
24
16
|
sdkKey;
|
|
25
17
|
projectKey;
|
|
26
18
|
envKey;
|
|
27
19
|
constructor(options = {}) {
|
|
28
|
-
this.apiBaseUrl = getApiBaseUrl(
|
|
20
|
+
this.apiBaseUrl = getApiBaseUrl().replace(/\/$/, "");
|
|
29
21
|
this.sdkKey = options.sdkKey?.trim() || getSdkKeyFromEnv();
|
|
30
22
|
this.projectKey = options.projectKey?.trim() ? options.projectKey.trim() : null;
|
|
31
23
|
this.envKey = options.envKey ?? "production";
|
|
32
24
|
if (!this.sdkKey && !this.projectKey) {
|
|
33
|
-
throw new Error(
|
|
34
|
-
"FeatureFlareClient requires either sdkKey (recommended) or projectKey (legacy). Set FEATUREFLARE_CLIENT_KEY or FEATUREFLARE_SERVER_KEY env var, or pass sdkKey in options."
|
|
35
|
-
);
|
|
25
|
+
throw new Error("ShipItClient requires either sdkKey (recommended) or projectKey (legacy). Set SHIPIT_CLIENT_KEY or SHIPIT_SERVER_KEY env var, or pass sdkKey in options.");
|
|
36
26
|
}
|
|
37
27
|
}
|
|
38
28
|
normalizeUser(input) {
|
|
39
29
|
const key = (input.id ?? input.key ?? "").trim();
|
|
40
|
-
if (!key) throw new Error("
|
|
30
|
+
if (!key) throw new Error("ShipItClient requires user.id (or legacy user.key).");
|
|
41
31
|
return {
|
|
42
32
|
key,
|
|
43
33
|
email: input.email,
|
|
@@ -76,6 +66,6 @@ var FeatureFlareClient = class {
|
|
|
76
66
|
}
|
|
77
67
|
};
|
|
78
68
|
|
|
79
|
-
export {
|
|
69
|
+
export { ShipItClient };
|
|
80
70
|
//# sourceMappingURL=index.js.map
|
|
81
71
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAsBA,
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAsBA,SAAS,aAAA,GAAwB;AAE/B,EAAA,IAAI,OAAO,MAAA,KAAW,WAAA,IAAe,OAAQ,MAAA,CAAe,aAAa,WAAA,EAAa;AACpF,IAAA,OAAQ,OAAe,QAAA,CAAS,MAAA;AAAA,EAClC;AAEA,EAAA,OAAO,qDAAA;AACT;AAQA,SAAS,gBAAA,GAAkC;AACzC,EAAA,IAAI,OAAO,OAAA,KAAY,WAAA,IAAe,OAAA,CAAQ,GAAA,EAAK;AACjD,IAAA,OAAO,OAAA,CAAQ,IAAI,iBAAA,EAAmB,IAAA,MAAU,OAAA,CAAQ,GAAA,CAAI,iBAAA,EAAmB,IAAA,EAAK,IAAK,IAAA;AAAA,EAC3F;AACA,EAAA,OAAO,IAAA;AACT;AAEO,IAAM,eAAN,MAAmB;AAAA,EACP,UAAA;AAAA,EACA,MAAA;AAAA,EACA,UAAA;AAAA,EACA,MAAA;AAAA,EAEjB,WAAA,CAAY,OAAA,GAA+B,EAAC,EAAG;AAC7C,IAAA,IAAA,CAAK,UAAA,GAAa,aAAA,EAAc,CAAE,OAAA,CAAQ,OAAO,EAAE,CAAA;AACnD,IAAA,IAAA,CAAK,MAAA,GAAS,OAAA,CAAQ,MAAA,EAAQ,IAAA,MAAU,gBAAA,EAAiB;AACzD,IAAA,IAAA,CAAK,UAAA,GAAa,QAAQ,UAAA,EAAY,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,CAAK,UAAU,CAAA,YAAA,CAAA,EAAgB;AAAA,MAC5C,MAAA,EAAQ,MAAA;AAAA,MACR,OAAA,EAAS,EAAE,cAAA,EAAgB,kBAAA,EAAmB;AAAA,MAC9C,IAAA,EAAM,KAAK,SAAA,CAAU;AAAA,QACnB,YAAY,IAAA,CAAK,UAAA;AAAA,QACjB,QAAQ,IAAA,CAAK,MAAA;AAAA,QACb,OAAA;AAAA,QACA,IAAA,EAAM,cAAA;AAAA,QACN;AAAA,OACD;AAAA,KACF,CAAA;AAEL,IAAA,IAAI,CAAC,GAAA,CAAI,EAAA,EAAI,OAAO,YAAA;AACpB,IAAA,MAAM,IAAA,GAAQ,MAAM,GAAA,CAAI,IAAA,EAAK;AAC7B,IAAA,OAAO,IAAA,CAAK,KAAA;AAAA,EACd;AACF","file":"index.js","sourcesContent":["export type ShipItUserPayload = {\n /** Unique user identifier (preferred). */\n id?: string;\n /** Legacy alias for id. */\n key?: string;\n email?: string;\n name?: string;\n country?: string;\n /** Queryable attributes. */\n meta?: Record<string, string | number | boolean | null>;\n /** Legacy alias for meta. */\n custom?: Record<string, string | number | boolean | null>;\n};\n\ntype ShipItUser = {\n key: string;\n email?: string;\n name?: string;\n country?: string;\n custom?: Record<string, string | number | boolean | null>;\n};\n\nfunction getApiBaseUrl(): string {\n // Browser: use current origin (assumes API is on same origin)\n if (typeof window !== 'undefined' && typeof (window as any).location !== 'undefined') {\n return (window as any).location.origin;\n }\n // Node.js: always use production API\n return 'https://shipit-api-246728836834.us-central1.run.app';\n}\n\nexport type ShipItClientOptions = {\n sdkKey?: string;\n projectKey?: string;\n envKey?: string;\n};\n\nfunction getSdkKeyFromEnv(): string | null {\n if (typeof process !== 'undefined' && process.env) {\n return process.env.SHIPIT_CLIENT_KEY?.trim() || process.env.SHIPIT_SERVER_KEY?.trim() || null;\n }\n return null;\n}\n\nexport class ShipItClient {\n private readonly apiBaseUrl: string;\n private readonly sdkKey: string | null;\n private readonly projectKey: string | null;\n private readonly envKey: string;\n\n constructor(options: ShipItClientOptions = {}) {\n this.apiBaseUrl = getApiBaseUrl().replace(/\\/$/, '');\n this.sdkKey = options.sdkKey?.trim() || getSdkKeyFromEnv();\n this.projectKey = options.projectKey?.trim() ? options.projectKey.trim() : null;\n this.envKey = options.envKey ?? 'production';\n\n if (!this.sdkKey && !this.projectKey) {\n throw new Error('ShipItClient requires either sdkKey (recommended) or projectKey (legacy). Set SHIPIT_CLIENT_KEY or SHIPIT_SERVER_KEY env var, or pass sdkKey in options.');\n }\n }\n\n private normalizeUser(input: ShipItUserPayload): ShipItUser {\n const key = (input.id ?? input.key ?? '').trim();\n if (!key) throw new Error('ShipItClient requires user.id (or legacy user.key).');\n return {\n key,\n email: input.email,\n name: input.name,\n country: input.country,\n custom: input.meta ?? input.custom\n };\n }\n\n async bool(flagKey: string, user: ShipItUserPayload, defaultValue = false): Promise<boolean> {\n const normalizedUser = this.normalizeUser(user);\n const res = this.sdkKey\n ? await fetch(`${this.apiBaseUrl}/api/v1/sdk/eval`, {\n method: 'POST',\n headers: {\n 'content-type': 'application/json',\n 'x-featureflare-sdk-key': this.sdkKey\n },\n body: JSON.stringify({\n flagKey,\n user: normalizedUser,\n defaultValue\n })\n })\n : await fetch(`${this.apiBaseUrl}/api/v1/eval`, {\n method: 'POST',\n headers: { 'content-type': 'application/json' },\n body: JSON.stringify({\n projectKey: this.projectKey,\n envKey: this.envKey,\n flagKey,\n user: normalizedUser,\n defaultValue\n })\n });\n\n if (!res.ok) return defaultValue;\n const json = (await res.json()) as { value: boolean };\n return json.value;\n }\n}\n"]}
|