@feathq/js-sdk 0.1.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/LICENSE +21 -0
- package/README.md +65 -0
- package/dist/index.cjs +195 -0
- package/dist/index.d.cts +45 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +189 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 feat HQ
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# @feathq/js-sdk
|
|
2
|
+
|
|
3
|
+
Server-side JavaScript / TypeScript SDK for [feat](https://feat.so) feature flags. Local flag evaluation against a polled datafile, OpenFeature provider included.
|
|
4
|
+
|
|
5
|
+
For browser code, use [`@feathq/web-sdk`](../web-sdk). For edge runtimes via service binding, use [`@feathq/worker-sdk`](../worker-sdk).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @feathq/js-sdk
|
|
11
|
+
# or
|
|
12
|
+
yarn add @feathq/js-sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Node.js 18+ (built-in `fetch`). Bun and Deno are supported.
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { FeatClient } from "@feathq/js-sdk";
|
|
21
|
+
|
|
22
|
+
const client = new FeatClient({
|
|
23
|
+
apiKey: process.env.FEAT_SERVER_KEY!, // feat_sdk_…
|
|
24
|
+
dataPlaneUrl: "https://data.feat.so",
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
await client.ready();
|
|
28
|
+
|
|
29
|
+
const result = await client.evaluate("checkout-v2", false, {
|
|
30
|
+
targetingKey: "user-123",
|
|
31
|
+
user: { plan: "pro", email: "alice@example.com" },
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
if (result.value) {
|
|
35
|
+
// …
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Use a **server** API key (`feat_sdk_…`). Mobile and client-side keys are for the mobile and browser SDKs respectively.
|
|
40
|
+
|
|
41
|
+
## OpenFeature
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { OpenFeature } from "@openfeature/server-sdk";
|
|
45
|
+
import { FeatClient, FeatProvider } from "@feathq/js-sdk";
|
|
46
|
+
|
|
47
|
+
const featClient = new FeatClient({ apiKey, dataPlaneUrl });
|
|
48
|
+
await OpenFeature.setProviderAndWait(new FeatProvider(featClient));
|
|
49
|
+
|
|
50
|
+
const client = OpenFeature.getClient();
|
|
51
|
+
const enabled = await client.getBooleanValue("checkout-v2", false, {
|
|
52
|
+
targetingKey: "user-123",
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## How it works
|
|
57
|
+
|
|
58
|
+
- The SDK fetches a per-environment **datafile** and keeps it in memory.
|
|
59
|
+
- Polls every 30 s by default (configurable down to 5 s). ETag-aware: unchanged polls are 304s.
|
|
60
|
+
- Evaluation is local; no per-flag network call.
|
|
61
|
+
- `dataPlaneUrl` must use `https://` (the constructor rejects plaintext URLs except `http://localhost` for tests).
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
|
|
65
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var featEval = require('@feathq/feat-eval');
|
|
4
|
+
var core = require('@openfeature/core');
|
|
5
|
+
|
|
6
|
+
// src/client.ts
|
|
7
|
+
var MIN_POLL_INTERVAL_MS = 5e3;
|
|
8
|
+
var DEFAULT_POLL_INTERVAL_MS = 3e4;
|
|
9
|
+
var MAX_DATAFILE_BYTES = 10 * 1024 * 1024;
|
|
10
|
+
var FeatClient = class {
|
|
11
|
+
constructor(config) {
|
|
12
|
+
this.config = config;
|
|
13
|
+
assertHttpsUrl(config.dataPlaneUrl);
|
|
14
|
+
this.fetchImpl = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
15
|
+
this.pollIntervalMs = Math.max(
|
|
16
|
+
config.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS,
|
|
17
|
+
MIN_POLL_INTERVAL_MS
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
config;
|
|
21
|
+
datafile = null;
|
|
22
|
+
etag = null;
|
|
23
|
+
timer = null;
|
|
24
|
+
readyPromise = null;
|
|
25
|
+
fetchImpl;
|
|
26
|
+
pollIntervalMs;
|
|
27
|
+
async ready() {
|
|
28
|
+
if (!this.readyPromise) {
|
|
29
|
+
this.readyPromise = this.bootstrap();
|
|
30
|
+
}
|
|
31
|
+
return this.readyPromise;
|
|
32
|
+
}
|
|
33
|
+
// Fetch once now. Returns true if the datafile changed.
|
|
34
|
+
async refresh() {
|
|
35
|
+
return this.fetchDatafile();
|
|
36
|
+
}
|
|
37
|
+
async evaluate(flagKey, defaultValue, context) {
|
|
38
|
+
if (!this.datafile) {
|
|
39
|
+
return {
|
|
40
|
+
value: defaultValue,
|
|
41
|
+
variationId: null,
|
|
42
|
+
reason: "ERROR",
|
|
43
|
+
errorMessage: "client not ready: call client.ready() before evaluate"
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const result = await featEval.evaluate(flagKey, defaultValue, context, this.datafile);
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
close() {
|
|
50
|
+
if (this.timer) {
|
|
51
|
+
clearInterval(this.timer);
|
|
52
|
+
this.timer = null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async bootstrap() {
|
|
56
|
+
await this.fetchDatafile();
|
|
57
|
+
this.timer = setInterval(() => {
|
|
58
|
+
void this.fetchDatafile().catch((err) => {
|
|
59
|
+
console.warn(
|
|
60
|
+
"feat: background poll failed:",
|
|
61
|
+
err instanceof Error ? err.message : String(err)
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
}, this.pollIntervalMs);
|
|
65
|
+
const t = this.timer;
|
|
66
|
+
t.unref?.();
|
|
67
|
+
}
|
|
68
|
+
async fetchDatafile() {
|
|
69
|
+
const url = `${this.config.dataPlaneUrl.replace(/\/$/, "")}/sdk/v1/datafile`;
|
|
70
|
+
const headers = {
|
|
71
|
+
Authorization: `Bearer ${this.config.apiKey}`
|
|
72
|
+
};
|
|
73
|
+
if (this.etag) headers["If-None-Match"] = this.etag;
|
|
74
|
+
const res = await this.fetchImpl(url, { method: "GET", headers });
|
|
75
|
+
if (res.status === 304) return false;
|
|
76
|
+
if (res.status === 404) return false;
|
|
77
|
+
if (!res.ok) {
|
|
78
|
+
throw new Error(`fetchDatafile failed: ${res.status}`);
|
|
79
|
+
}
|
|
80
|
+
const lengthHeader = res.headers.get("content-length");
|
|
81
|
+
if (lengthHeader && Number(lengthHeader) > MAX_DATAFILE_BYTES) {
|
|
82
|
+
throw new Error("datafile exceeds maximum allowed size");
|
|
83
|
+
}
|
|
84
|
+
const next = await res.json();
|
|
85
|
+
this.datafile = next;
|
|
86
|
+
this.etag = res.headers.get("etag");
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
function assertHttpsUrl(url) {
|
|
91
|
+
try {
|
|
92
|
+
const u = new URL(url);
|
|
93
|
+
if (u.protocol === "https:") return;
|
|
94
|
+
if (u.protocol === "http:" && (u.hostname === "localhost" || u.hostname === "127.0.0.1")) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
} catch {
|
|
98
|
+
}
|
|
99
|
+
throw new Error("dataPlaneUrl must use https:// (http://localhost allowed for tests)");
|
|
100
|
+
}
|
|
101
|
+
var FeatProvider = class {
|
|
102
|
+
constructor(client) {
|
|
103
|
+
this.client = client;
|
|
104
|
+
}
|
|
105
|
+
client;
|
|
106
|
+
metadata = { name: "feat" };
|
|
107
|
+
runsOn = "server";
|
|
108
|
+
hooks = [];
|
|
109
|
+
status = "NOT_READY";
|
|
110
|
+
async initialize() {
|
|
111
|
+
await this.client.ready();
|
|
112
|
+
this.status = "READY";
|
|
113
|
+
}
|
|
114
|
+
async onClose() {
|
|
115
|
+
this.client.close();
|
|
116
|
+
this.status = "NOT_READY";
|
|
117
|
+
}
|
|
118
|
+
async resolveBooleanEvaluation(flagKey, defaultValue, context, _logger) {
|
|
119
|
+
const result = await this.client.evaluate(
|
|
120
|
+
flagKey,
|
|
121
|
+
defaultValue,
|
|
122
|
+
toEvalContext(context)
|
|
123
|
+
);
|
|
124
|
+
if (typeof result.value !== "boolean") {
|
|
125
|
+
return wrongType(defaultValue, typeof result.value, "boolean");
|
|
126
|
+
}
|
|
127
|
+
return toResolution(result.value, result);
|
|
128
|
+
}
|
|
129
|
+
async resolveStringEvaluation(flagKey, defaultValue, context, _logger) {
|
|
130
|
+
const result = await this.client.evaluate(
|
|
131
|
+
flagKey,
|
|
132
|
+
defaultValue,
|
|
133
|
+
toEvalContext(context)
|
|
134
|
+
);
|
|
135
|
+
if (typeof result.value !== "string") {
|
|
136
|
+
return wrongType(defaultValue, typeof result.value, "string");
|
|
137
|
+
}
|
|
138
|
+
return toResolution(result.value, result);
|
|
139
|
+
}
|
|
140
|
+
async resolveNumberEvaluation(flagKey, defaultValue, context, _logger) {
|
|
141
|
+
const result = await this.client.evaluate(
|
|
142
|
+
flagKey,
|
|
143
|
+
defaultValue,
|
|
144
|
+
toEvalContext(context)
|
|
145
|
+
);
|
|
146
|
+
if (typeof result.value !== "number") {
|
|
147
|
+
return wrongType(defaultValue, typeof result.value, "number");
|
|
148
|
+
}
|
|
149
|
+
return toResolution(result.value, result);
|
|
150
|
+
}
|
|
151
|
+
async resolveObjectEvaluation(flagKey, defaultValue, context, _logger) {
|
|
152
|
+
const result = await this.client.evaluate(
|
|
153
|
+
flagKey,
|
|
154
|
+
defaultValue,
|
|
155
|
+
toEvalContext(context)
|
|
156
|
+
);
|
|
157
|
+
return toResolution(result.value, result);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
function toResolution(value, result) {
|
|
161
|
+
return {
|
|
162
|
+
value,
|
|
163
|
+
reason: result.reason,
|
|
164
|
+
...result.variationId ? { variant: result.variationId } : {},
|
|
165
|
+
...result.errorMessage ? { errorCode: core.ErrorCode.GENERAL, errorMessage: result.errorMessage } : {}
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
function wrongType(defaultValue, got, want) {
|
|
169
|
+
return {
|
|
170
|
+
value: defaultValue,
|
|
171
|
+
reason: "ERROR",
|
|
172
|
+
errorCode: core.ErrorCode.TYPE_MISMATCH,
|
|
173
|
+
errorMessage: `flag value type ${got} does not match requested ${want}`
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
function toEvalContext(ctx) {
|
|
177
|
+
const out = {};
|
|
178
|
+
for (const [k, v] of Object.entries(ctx)) {
|
|
179
|
+
if (k === "targetingKey" && typeof v === "string") {
|
|
180
|
+
out.targetingKey = v;
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (v && typeof v === "object" && !Array.isArray(v) && "key" in v) {
|
|
184
|
+
out[k] = v;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return out;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
Object.defineProperty(exports, "evaluate", {
|
|
191
|
+
enumerable: true,
|
|
192
|
+
get: function () { return featEval.evaluate; }
|
|
193
|
+
});
|
|
194
|
+
exports.FeatClient = FeatClient;
|
|
195
|
+
exports.FeatProvider = FeatProvider;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { EvalContext, EvaluationResult } from '@feathq/feat-eval';
|
|
2
|
+
export { ContextKindObject, EvalContext, EvaluationResult, Reason, evaluate } from '@feathq/feat-eval';
|
|
3
|
+
import { ProviderMetadata, EvaluationContext, Logger, ResolutionDetails, JsonValue } from '@openfeature/core';
|
|
4
|
+
import { Provider, Hook, ProviderStatus } from '@openfeature/server-sdk';
|
|
5
|
+
export { Datafile, Operator } from '@feathq/datafile-schema';
|
|
6
|
+
|
|
7
|
+
interface FeatClientConfig {
|
|
8
|
+
apiKey: string;
|
|
9
|
+
dataPlaneUrl: string;
|
|
10
|
+
pollIntervalMs?: number;
|
|
11
|
+
fetch?: typeof fetch;
|
|
12
|
+
}
|
|
13
|
+
declare class FeatClient {
|
|
14
|
+
private readonly config;
|
|
15
|
+
private datafile;
|
|
16
|
+
private etag;
|
|
17
|
+
private timer;
|
|
18
|
+
private readyPromise;
|
|
19
|
+
private readonly fetchImpl;
|
|
20
|
+
private readonly pollIntervalMs;
|
|
21
|
+
constructor(config: FeatClientConfig);
|
|
22
|
+
ready(): Promise<void>;
|
|
23
|
+
refresh(): Promise<boolean>;
|
|
24
|
+
evaluate<T = unknown>(flagKey: string, defaultValue: T, context: EvalContext): Promise<EvaluationResult<T>>;
|
|
25
|
+
close(): void;
|
|
26
|
+
private bootstrap;
|
|
27
|
+
private fetchDatafile;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
declare class FeatProvider implements Provider {
|
|
31
|
+
private readonly client;
|
|
32
|
+
readonly metadata: ProviderMetadata;
|
|
33
|
+
readonly runsOn: "server";
|
|
34
|
+
readonly hooks: Hook[];
|
|
35
|
+
status: ProviderStatus;
|
|
36
|
+
constructor(client: FeatClient);
|
|
37
|
+
initialize(): Promise<void>;
|
|
38
|
+
onClose(): Promise<void>;
|
|
39
|
+
resolveBooleanEvaluation(flagKey: string, defaultValue: boolean, context: EvaluationContext, _logger: Logger): Promise<ResolutionDetails<boolean>>;
|
|
40
|
+
resolveStringEvaluation(flagKey: string, defaultValue: string, context: EvaluationContext, _logger: Logger): Promise<ResolutionDetails<string>>;
|
|
41
|
+
resolveNumberEvaluation(flagKey: string, defaultValue: number, context: EvaluationContext, _logger: Logger): Promise<ResolutionDetails<number>>;
|
|
42
|
+
resolveObjectEvaluation<T extends JsonValue>(flagKey: string, defaultValue: T, context: EvaluationContext, _logger: Logger): Promise<ResolutionDetails<T>>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { FeatClient, type FeatClientConfig, FeatProvider };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { EvalContext, EvaluationResult } from '@feathq/feat-eval';
|
|
2
|
+
export { ContextKindObject, EvalContext, EvaluationResult, Reason, evaluate } from '@feathq/feat-eval';
|
|
3
|
+
import { ProviderMetadata, EvaluationContext, Logger, ResolutionDetails, JsonValue } from '@openfeature/core';
|
|
4
|
+
import { Provider, Hook, ProviderStatus } from '@openfeature/server-sdk';
|
|
5
|
+
export { Datafile, Operator } from '@feathq/datafile-schema';
|
|
6
|
+
|
|
7
|
+
interface FeatClientConfig {
|
|
8
|
+
apiKey: string;
|
|
9
|
+
dataPlaneUrl: string;
|
|
10
|
+
pollIntervalMs?: number;
|
|
11
|
+
fetch?: typeof fetch;
|
|
12
|
+
}
|
|
13
|
+
declare class FeatClient {
|
|
14
|
+
private readonly config;
|
|
15
|
+
private datafile;
|
|
16
|
+
private etag;
|
|
17
|
+
private timer;
|
|
18
|
+
private readyPromise;
|
|
19
|
+
private readonly fetchImpl;
|
|
20
|
+
private readonly pollIntervalMs;
|
|
21
|
+
constructor(config: FeatClientConfig);
|
|
22
|
+
ready(): Promise<void>;
|
|
23
|
+
refresh(): Promise<boolean>;
|
|
24
|
+
evaluate<T = unknown>(flagKey: string, defaultValue: T, context: EvalContext): Promise<EvaluationResult<T>>;
|
|
25
|
+
close(): void;
|
|
26
|
+
private bootstrap;
|
|
27
|
+
private fetchDatafile;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
declare class FeatProvider implements Provider {
|
|
31
|
+
private readonly client;
|
|
32
|
+
readonly metadata: ProviderMetadata;
|
|
33
|
+
readonly runsOn: "server";
|
|
34
|
+
readonly hooks: Hook[];
|
|
35
|
+
status: ProviderStatus;
|
|
36
|
+
constructor(client: FeatClient);
|
|
37
|
+
initialize(): Promise<void>;
|
|
38
|
+
onClose(): Promise<void>;
|
|
39
|
+
resolveBooleanEvaluation(flagKey: string, defaultValue: boolean, context: EvaluationContext, _logger: Logger): Promise<ResolutionDetails<boolean>>;
|
|
40
|
+
resolveStringEvaluation(flagKey: string, defaultValue: string, context: EvaluationContext, _logger: Logger): Promise<ResolutionDetails<string>>;
|
|
41
|
+
resolveNumberEvaluation(flagKey: string, defaultValue: number, context: EvaluationContext, _logger: Logger): Promise<ResolutionDetails<number>>;
|
|
42
|
+
resolveObjectEvaluation<T extends JsonValue>(flagKey: string, defaultValue: T, context: EvaluationContext, _logger: Logger): Promise<ResolutionDetails<T>>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { FeatClient, type FeatClientConfig, FeatProvider };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { evaluate } from '@feathq/feat-eval';
|
|
2
|
+
export { evaluate } from '@feathq/feat-eval';
|
|
3
|
+
import { ErrorCode } from '@openfeature/core';
|
|
4
|
+
|
|
5
|
+
// src/client.ts
|
|
6
|
+
var MIN_POLL_INTERVAL_MS = 5e3;
|
|
7
|
+
var DEFAULT_POLL_INTERVAL_MS = 3e4;
|
|
8
|
+
var MAX_DATAFILE_BYTES = 10 * 1024 * 1024;
|
|
9
|
+
var FeatClient = class {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.config = config;
|
|
12
|
+
assertHttpsUrl(config.dataPlaneUrl);
|
|
13
|
+
this.fetchImpl = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
14
|
+
this.pollIntervalMs = Math.max(
|
|
15
|
+
config.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS,
|
|
16
|
+
MIN_POLL_INTERVAL_MS
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
config;
|
|
20
|
+
datafile = null;
|
|
21
|
+
etag = null;
|
|
22
|
+
timer = null;
|
|
23
|
+
readyPromise = null;
|
|
24
|
+
fetchImpl;
|
|
25
|
+
pollIntervalMs;
|
|
26
|
+
async ready() {
|
|
27
|
+
if (!this.readyPromise) {
|
|
28
|
+
this.readyPromise = this.bootstrap();
|
|
29
|
+
}
|
|
30
|
+
return this.readyPromise;
|
|
31
|
+
}
|
|
32
|
+
// Fetch once now. Returns true if the datafile changed.
|
|
33
|
+
async refresh() {
|
|
34
|
+
return this.fetchDatafile();
|
|
35
|
+
}
|
|
36
|
+
async evaluate(flagKey, defaultValue, context) {
|
|
37
|
+
if (!this.datafile) {
|
|
38
|
+
return {
|
|
39
|
+
value: defaultValue,
|
|
40
|
+
variationId: null,
|
|
41
|
+
reason: "ERROR",
|
|
42
|
+
errorMessage: "client not ready: call client.ready() before evaluate"
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const result = await evaluate(flagKey, defaultValue, context, this.datafile);
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
close() {
|
|
49
|
+
if (this.timer) {
|
|
50
|
+
clearInterval(this.timer);
|
|
51
|
+
this.timer = null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async bootstrap() {
|
|
55
|
+
await this.fetchDatafile();
|
|
56
|
+
this.timer = setInterval(() => {
|
|
57
|
+
void this.fetchDatafile().catch((err) => {
|
|
58
|
+
console.warn(
|
|
59
|
+
"feat: background poll failed:",
|
|
60
|
+
err instanceof Error ? err.message : String(err)
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
}, this.pollIntervalMs);
|
|
64
|
+
const t = this.timer;
|
|
65
|
+
t.unref?.();
|
|
66
|
+
}
|
|
67
|
+
async fetchDatafile() {
|
|
68
|
+
const url = `${this.config.dataPlaneUrl.replace(/\/$/, "")}/sdk/v1/datafile`;
|
|
69
|
+
const headers = {
|
|
70
|
+
Authorization: `Bearer ${this.config.apiKey}`
|
|
71
|
+
};
|
|
72
|
+
if (this.etag) headers["If-None-Match"] = this.etag;
|
|
73
|
+
const res = await this.fetchImpl(url, { method: "GET", headers });
|
|
74
|
+
if (res.status === 304) return false;
|
|
75
|
+
if (res.status === 404) return false;
|
|
76
|
+
if (!res.ok) {
|
|
77
|
+
throw new Error(`fetchDatafile failed: ${res.status}`);
|
|
78
|
+
}
|
|
79
|
+
const lengthHeader = res.headers.get("content-length");
|
|
80
|
+
if (lengthHeader && Number(lengthHeader) > MAX_DATAFILE_BYTES) {
|
|
81
|
+
throw new Error("datafile exceeds maximum allowed size");
|
|
82
|
+
}
|
|
83
|
+
const next = await res.json();
|
|
84
|
+
this.datafile = next;
|
|
85
|
+
this.etag = res.headers.get("etag");
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
function assertHttpsUrl(url) {
|
|
90
|
+
try {
|
|
91
|
+
const u = new URL(url);
|
|
92
|
+
if (u.protocol === "https:") return;
|
|
93
|
+
if (u.protocol === "http:" && (u.hostname === "localhost" || u.hostname === "127.0.0.1")) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
} catch {
|
|
97
|
+
}
|
|
98
|
+
throw new Error("dataPlaneUrl must use https:// (http://localhost allowed for tests)");
|
|
99
|
+
}
|
|
100
|
+
var FeatProvider = class {
|
|
101
|
+
constructor(client) {
|
|
102
|
+
this.client = client;
|
|
103
|
+
}
|
|
104
|
+
client;
|
|
105
|
+
metadata = { name: "feat" };
|
|
106
|
+
runsOn = "server";
|
|
107
|
+
hooks = [];
|
|
108
|
+
status = "NOT_READY";
|
|
109
|
+
async initialize() {
|
|
110
|
+
await this.client.ready();
|
|
111
|
+
this.status = "READY";
|
|
112
|
+
}
|
|
113
|
+
async onClose() {
|
|
114
|
+
this.client.close();
|
|
115
|
+
this.status = "NOT_READY";
|
|
116
|
+
}
|
|
117
|
+
async resolveBooleanEvaluation(flagKey, defaultValue, context, _logger) {
|
|
118
|
+
const result = await this.client.evaluate(
|
|
119
|
+
flagKey,
|
|
120
|
+
defaultValue,
|
|
121
|
+
toEvalContext(context)
|
|
122
|
+
);
|
|
123
|
+
if (typeof result.value !== "boolean") {
|
|
124
|
+
return wrongType(defaultValue, typeof result.value, "boolean");
|
|
125
|
+
}
|
|
126
|
+
return toResolution(result.value, result);
|
|
127
|
+
}
|
|
128
|
+
async resolveStringEvaluation(flagKey, defaultValue, context, _logger) {
|
|
129
|
+
const result = await this.client.evaluate(
|
|
130
|
+
flagKey,
|
|
131
|
+
defaultValue,
|
|
132
|
+
toEvalContext(context)
|
|
133
|
+
);
|
|
134
|
+
if (typeof result.value !== "string") {
|
|
135
|
+
return wrongType(defaultValue, typeof result.value, "string");
|
|
136
|
+
}
|
|
137
|
+
return toResolution(result.value, result);
|
|
138
|
+
}
|
|
139
|
+
async resolveNumberEvaluation(flagKey, defaultValue, context, _logger) {
|
|
140
|
+
const result = await this.client.evaluate(
|
|
141
|
+
flagKey,
|
|
142
|
+
defaultValue,
|
|
143
|
+
toEvalContext(context)
|
|
144
|
+
);
|
|
145
|
+
if (typeof result.value !== "number") {
|
|
146
|
+
return wrongType(defaultValue, typeof result.value, "number");
|
|
147
|
+
}
|
|
148
|
+
return toResolution(result.value, result);
|
|
149
|
+
}
|
|
150
|
+
async resolveObjectEvaluation(flagKey, defaultValue, context, _logger) {
|
|
151
|
+
const result = await this.client.evaluate(
|
|
152
|
+
flagKey,
|
|
153
|
+
defaultValue,
|
|
154
|
+
toEvalContext(context)
|
|
155
|
+
);
|
|
156
|
+
return toResolution(result.value, result);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
function toResolution(value, result) {
|
|
160
|
+
return {
|
|
161
|
+
value,
|
|
162
|
+
reason: result.reason,
|
|
163
|
+
...result.variationId ? { variant: result.variationId } : {},
|
|
164
|
+
...result.errorMessage ? { errorCode: ErrorCode.GENERAL, errorMessage: result.errorMessage } : {}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function wrongType(defaultValue, got, want) {
|
|
168
|
+
return {
|
|
169
|
+
value: defaultValue,
|
|
170
|
+
reason: "ERROR",
|
|
171
|
+
errorCode: ErrorCode.TYPE_MISMATCH,
|
|
172
|
+
errorMessage: `flag value type ${got} does not match requested ${want}`
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
function toEvalContext(ctx) {
|
|
176
|
+
const out = {};
|
|
177
|
+
for (const [k, v] of Object.entries(ctx)) {
|
|
178
|
+
if (k === "targetingKey" && typeof v === "string") {
|
|
179
|
+
out.targetingKey = v;
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (v && typeof v === "object" && !Array.isArray(v) && "key" in v) {
|
|
183
|
+
out[k] = v;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export { FeatClient, FeatProvider };
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@feathq/js-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "feat feature-flag SDK for JavaScript and TypeScript (server-side, OpenFeature provider)",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"feature-flags",
|
|
7
|
+
"feature-toggles",
|
|
8
|
+
"openfeature",
|
|
9
|
+
"feat",
|
|
10
|
+
"feature-management"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "feat HQ <engineering@feat.so>",
|
|
14
|
+
"homepage": "https://feat.so",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/feathq/js-sdk"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/feathq/js-sdk/issues"
|
|
21
|
+
},
|
|
22
|
+
"type": "module",
|
|
23
|
+
"packageManager": "yarn@4.14.1",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"main": "./dist/index.cjs",
|
|
26
|
+
"module": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
|
+
"require": "./dist/index.cjs"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist"
|
|
37
|
+
],
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsup",
|
|
43
|
+
"typecheck": "tsc --noEmit",
|
|
44
|
+
"test": "vitest --run"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@feathq/datafile-schema": "^0.1.0",
|
|
48
|
+
"@feathq/feat-eval": "^0.1.0"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"@openfeature/server-sdk": "^1.0.0"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@openfeature/core": "^1.10.0",
|
|
55
|
+
"@openfeature/server-sdk": "^1.18.0",
|
|
56
|
+
"@types/node": "^25.6.2",
|
|
57
|
+
"tsup": "^8.3.5",
|
|
58
|
+
"typescript": "^6.0.3",
|
|
59
|
+
"vitest": "^4.1.6"
|
|
60
|
+
},
|
|
61
|
+
"resolutions": {
|
|
62
|
+
"@feathq/datafile-schema": "portal:../../packages/datafile-schema",
|
|
63
|
+
"@feathq/datafile-schema@workspace:*": "portal:../../packages/datafile-schema",
|
|
64
|
+
"@feathq/feat-eval": "portal:../../packages/feat-eval"
|
|
65
|
+
}
|
|
66
|
+
}
|