@featureflare/sdk-js 0.0.1

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 ADDED
@@ -0,0 +1,130 @@
1
+ # @featureflare/sdk-js
2
+
3
+ JavaScript/TypeScript SDK for ShipIt feature flags.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @featureflare/sdk-js
9
+ # or
10
+ pnpm add @featureflare/sdk-js
11
+ # or
12
+ yarn add @featureflare/sdk-js
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Basic Example
18
+
19
+ ```typescript
20
+ import { ShipItClient } from '@featureflare/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 '@featureflare/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/dist/index.cjs ADDED
@@ -0,0 +1,73 @@
1
+ 'use strict';
2
+
3
+ // src/index.ts
4
+ function getApiBaseUrl() {
5
+ if (typeof window !== "undefined" && typeof window.location !== "undefined") {
6
+ return window.location.origin;
7
+ }
8
+ return "https://shipit-api-246728836834.us-central1.run.app";
9
+ }
10
+ function getSdkKeyFromEnv() {
11
+ if (typeof process !== "undefined" && process.env) {
12
+ return process.env.SHIPIT_CLIENT_KEY?.trim() || process.env.SHIPIT_SERVER_KEY?.trim() || null;
13
+ }
14
+ return null;
15
+ }
16
+ var ShipItClient = class {
17
+ apiBaseUrl;
18
+ sdkKey;
19
+ projectKey;
20
+ envKey;
21
+ constructor(options = {}) {
22
+ this.apiBaseUrl = getApiBaseUrl().replace(/\/$/, "");
23
+ this.sdkKey = options.sdkKey?.trim() || getSdkKeyFromEnv();
24
+ this.projectKey = options.projectKey?.trim() ? options.projectKey.trim() : null;
25
+ this.envKey = options.envKey ?? "production";
26
+ if (!this.sdkKey && !this.projectKey) {
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.");
28
+ }
29
+ }
30
+ normalizeUser(input) {
31
+ const key = (input.id ?? input.key ?? "").trim();
32
+ if (!key) throw new Error("ShipItClient requires user.id (or legacy user.key).");
33
+ return {
34
+ key,
35
+ email: input.email,
36
+ name: input.name,
37
+ country: input.country,
38
+ custom: input.meta ?? input.custom
39
+ };
40
+ }
41
+ async bool(flagKey, user, defaultValue = false) {
42
+ const normalizedUser = this.normalizeUser(user);
43
+ const res = this.sdkKey ? await fetch(`${this.apiBaseUrl}/api/v1/sdk/eval`, {
44
+ method: "POST",
45
+ headers: {
46
+ "content-type": "application/json",
47
+ "x-featureflare-sdk-key": this.sdkKey
48
+ },
49
+ body: JSON.stringify({
50
+ flagKey,
51
+ user: normalizedUser,
52
+ defaultValue
53
+ })
54
+ }) : await fetch(`${this.apiBaseUrl}/api/v1/eval`, {
55
+ method: "POST",
56
+ headers: { "content-type": "application/json" },
57
+ body: JSON.stringify({
58
+ projectKey: this.projectKey,
59
+ envKey: this.envKey,
60
+ flagKey,
61
+ user: normalizedUser,
62
+ defaultValue
63
+ })
64
+ });
65
+ if (!res.ok) return defaultValue;
66
+ const json = await res.json();
67
+ return json.value;
68
+ }
69
+ };
70
+
71
+ exports.ShipItClient = ShipItClient;
72
+ //# sourceMappingURL=index.cjs.map
73
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
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,IAAA,KAAS,OAAA,CAAQ,UAAA,CA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AgB;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"]}
@@ -0,0 +1,29 @@
1
+ type ShipItUserPayload = {
2
+ /** Unique user identifier (preferred). */
3
+ id?: string;
4
+ /** Legacy alias for id. */
5
+ key?: string;
6
+ email?: string;
7
+ name?: string;
8
+ country?: string;
9
+ /** Queryable attributes. */
10
+ meta?: Record<string, string | number | boolean | null>;
11
+ /** Legacy alias for meta. */
12
+ custom?: Record<string, string | number | boolean | null>;
13
+ };
14
+ type ShipItClientOptions = {
15
+ sdkKey?: string;
16
+ projectKey?: string;
17
+ envKey?: string;
18
+ };
19
+ declare class ShipItClient {
20
+ private readonly apiBaseUrl;
21
+ private readonly sdkKey;
22
+ private readonly projectKey;
23
+ private readonly envKey;
24
+ constructor(options?: ShipItClientOptions);
25
+ private normalizeUser;
26
+ bool(flagKey: string, user: ShipItUserPayload, defaultValue?: boolean): Promise<boolean>;
27
+ }
28
+
29
+ export { ShipItClient, type ShipItClientOptions, type ShipItUserPayload };
@@ -0,0 +1,29 @@
1
+ type ShipItUserPayload = {
2
+ /** Unique user identifier (preferred). */
3
+ id?: string;
4
+ /** Legacy alias for id. */
5
+ key?: string;
6
+ email?: string;
7
+ name?: string;
8
+ country?: string;
9
+ /** Queryable attributes. */
10
+ meta?: Record<string, string | number | boolean | null>;
11
+ /** Legacy alias for meta. */
12
+ custom?: Record<string, string | number | boolean | null>;
13
+ };
14
+ type ShipItClientOptions = {
15
+ sdkKey?: string;
16
+ projectKey?: string;
17
+ envKey?: string;
18
+ };
19
+ declare class ShipItClient {
20
+ private readonly apiBaseUrl;
21
+ private readonly sdkKey;
22
+ private readonly projectKey;
23
+ private readonly envKey;
24
+ constructor(options?: ShipItClientOptions);
25
+ private normalizeUser;
26
+ bool(flagKey: string, user: ShipItUserPayload, defaultValue?: boolean): Promise<boolean>;
27
+ }
28
+
29
+ export { ShipItClient, type ShipItClientOptions, type ShipItUserPayload };
package/dist/index.js ADDED
@@ -0,0 +1,71 @@
1
+ // src/index.ts
2
+ function getApiBaseUrl() {
3
+ if (typeof window !== "undefined" && typeof window.location !== "undefined") {
4
+ return window.location.origin;
5
+ }
6
+ return "https://shipit-api-246728836834.us-central1.run.app";
7
+ }
8
+ function getSdkKeyFromEnv() {
9
+ if (typeof process !== "undefined" && process.env) {
10
+ return process.env.SHIPIT_CLIENT_KEY?.trim() || process.env.SHIPIT_SERVER_KEY?.trim() || null;
11
+ }
12
+ return null;
13
+ }
14
+ var ShipItClient = class {
15
+ apiBaseUrl;
16
+ sdkKey;
17
+ projectKey;
18
+ envKey;
19
+ constructor(options = {}) {
20
+ this.apiBaseUrl = getApiBaseUrl().replace(/\/$/, "");
21
+ this.sdkKey = options.sdkKey?.trim() || getSdkKeyFromEnv();
22
+ this.projectKey = options.projectKey?.trim() ? options.projectKey.trim() : null;
23
+ this.envKey = options.envKey ?? "production";
24
+ if (!this.sdkKey && !this.projectKey) {
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.");
26
+ }
27
+ }
28
+ normalizeUser(input) {
29
+ const key = (input.id ?? input.key ?? "").trim();
30
+ if (!key) throw new Error("ShipItClient requires user.id (or legacy user.key).");
31
+ return {
32
+ key,
33
+ email: input.email,
34
+ name: input.name,
35
+ country: input.country,
36
+ custom: input.meta ?? input.custom
37
+ };
38
+ }
39
+ async bool(flagKey, user, defaultValue = false) {
40
+ const normalizedUser = this.normalizeUser(user);
41
+ const res = this.sdkKey ? await fetch(`${this.apiBaseUrl}/api/v1/sdk/eval`, {
42
+ method: "POST",
43
+ headers: {
44
+ "content-type": "application/json",
45
+ "x-featureflare-sdk-key": this.sdkKey
46
+ },
47
+ body: JSON.stringify({
48
+ flagKey,
49
+ user: normalizedUser,
50
+ defaultValue
51
+ })
52
+ }) : await fetch(`${this.apiBaseUrl}/api/v1/eval`, {
53
+ method: "POST",
54
+ headers: { "content-type": "application/json" },
55
+ body: JSON.stringify({
56
+ projectKey: this.projectKey,
57
+ envKey: this.envKey,
58
+ flagKey,
59
+ user: normalizedUser,
60
+ defaultValue
61
+ })
62
+ });
63
+ if (!res.ok) return defaultValue;
64
+ const json = await res.json();
65
+ return json.value;
66
+ }
67
+ };
68
+
69
+ export { ShipItClient };
70
+ //# sourceMappingURL=index.js.map
71
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAsBA,SAAS,aAAA,GAAwB;AAE/B,EAA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AA,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"]}
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@featureflare/sdk-js",
3
+ "version": "0.0.1",
4
+ "private": false,
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "type": "module",
9
+ "main": "./dist/index.cjs",
10
+ "module": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "require": "./dist/index.cjs"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist",
21
+ "README.md"
22
+ ],
23
+ "sideEffects": false,
24
+ "scripts": {
25
+ "dev": "tsup --watch",
26
+ "build": "tsup",
27
+ "typecheck": "tsc -p tsconfig.json --noEmit",
28
+ "lint": "eslint .",
29
+ "format": "prettier -w ."
30
+ },
31
+ "dependencies": {},
32
+ "devDependencies": {
33
+ "@types/node": "^22.10.2",
34
+ "eslint": "^9.16.0",
35
+ "prettier": "^3.4.2",
36
+ "tsup": "^8.3.5",
37
+ "typescript": "^5.7.2",
38
+ "typescript-eslint": "^8.17.0"
39
+ }
40
+ }