@testchimp/flags 0.0.0-stage → 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 +151 -2
- package/dist/openfeature-BIO-k-VW.d.mts +10 -0
- package/dist/openfeature-BIO-k-VW.d.ts +10 -0
- package/dist/server/index.d.mts +49 -0
- package/dist/server/index.d.ts +49 -0
- package/dist/server/index.js +306 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/index.mjs +272 -0
- package/dist/server/index.mjs.map +1 -0
- package/dist/web/index.d.mts +46 -0
- package/dist/web/index.d.ts +46 -0
- package/dist/web/index.js +132 -0
- package/dist/web/index.js.map +1 -0
- package/dist/web/index.mjs +100 -0
- package/dist/web/index.mjs.map +1 -0
- package/package.json +66 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/shared/variant.ts","../../src/shared/openfeature.ts","../../src/web/index.ts"],"sourcesContent":["/** Longest variant string kept. JSON and number values with no named variations can be long. */\nexport const MAX_VARIANT_LENGTH = 128;\n\n/**\n * Normalize a flag value to the string the coverage model stores, matching `@testchimp/playwright`:\n * booleans become \"true\"/\"false\", numbers become their decimal text, objects become JSON. Returns undefined\n * when the value has no representation (for example a function), so the caller skips it.\n */\nexport function normalizeVariant(value: unknown): string | undefined {\n if (value === undefined) return undefined;\n let text: string | undefined;\n if (value === null) {\n text = 'null';\n } else if (typeof value === 'string') {\n text = value;\n } else if (typeof value === 'number' || typeof value === 'boolean' || typeof value === 'bigint') {\n text = String(value);\n } else {\n try {\n text = JSON.stringify(value);\n } catch {\n return undefined;\n }\n }\n if (text === undefined) return undefined;\n return text.length > MAX_VARIANT_LENGTH ? text.slice(0, MAX_VARIANT_LENGTH) : text;\n}\n","/**\n * OpenFeature hook shared by the web and server entry points. It is structural: it does not import\n * `@openfeature/*`, so the package has no runtime dependency on OpenFeature. The hook object is what the\n * OpenFeature SDK expects: an object with an `after(hookContext, evaluationDetails)` method.\n */\n\nexport type ReportFlag = (flagKey: string, variant: unknown, providerId: string) => void;\n\nexport interface HookContextLike {\n flagKey?: string;\n}\n\nexport interface EvaluationDetailsLike {\n flagKey?: string;\n variant?: string;\n value?: unknown;\n}\n\nexport function openFeatureHook(report: ReportFlag) {\n return {\n after(hookContext: HookContextLike | undefined, details: EvaluationDetailsLike | undefined): void {\n const flagKey = details?.flagKey || hookContext?.flagKey;\n if (!flagKey) return;\n // Named variant when the provider gives one, otherwise the resolved value.\n const variant = details?.variant !== undefined && details.variant !== '' ? details.variant : details?.value;\n report(flagKey, variant, 'openfeature');\n },\n };\n}\n","/**\n * `@testchimp/flags/web`: report flag evaluations made in the browser to a TestChimp test.\n *\n * Safety: every function here does nothing unless `globalThis.__TESTCHIMP_FLAGS__` exists. The TestChimp\n * Playwright fixture defines that global, and only in tests. In production there is no global, so the hooks\n * install no wrappers, make no calls and throw nothing.\n */\nimport { normalizeVariant } from '../shared/variant';\nimport { openFeatureHook } from '../shared/openfeature';\n\ninterface FlagsGlobal {\n record?: (entry: { k: string; v: string; p: string }) => void;\n}\n\nconst instrumented = new WeakSet<object>();\n\nfunction flagsGlobal(): FlagsGlobal | undefined {\n const candidate = (globalThis as unknown as { __TESTCHIMP_FLAGS__?: FlagsGlobal }).__TESTCHIMP_FLAGS__;\n return candidate && typeof candidate.record === 'function' ? candidate : undefined;\n}\n\n/** True only inside a TestChimp test. Use it to skip any setup that is not needed in production. */\nexport function isTestChimpCaptureActive(): boolean {\n return flagsGlobal() !== undefined;\n}\n\n/**\n * Low-level: report one evaluation. `variant` is normalized (booleans become \"true\"/\"false\", objects JSON).\n * `providerId` is the provider the value came from (`launchdarkly`, `posthog`, `openfeature` or your own id).\n */\nexport function reportFlag(flagKey: string, variant: unknown, providerId = 'unknown'): void {\n try {\n const target = flagsGlobal();\n if (!target || !flagKey) return;\n const v = normalizeVariant(variant);\n if (v === undefined) return;\n target.record?.({ k: flagKey, v, p: providerId });\n } catch {\n /* reporting must never affect the app */\n }\n}\n\n/**\n * OpenFeature web SDK hook. Add it globally or per client:\n * OpenFeature.addHooks(createOpenFeatureHook());\n * Reports the flag key and variant after each successful evaluation, falling back to the resolved value.\n */\nexport function createOpenFeatureHook() {\n return openFeatureHook(reportFlag);\n}\n\ninterface LaunchDarklyFlagUsedInspector {\n type: 'flag-used';\n name: string;\n method: (flagKey: string, flagDetail: { value?: unknown } | undefined) => void;\n}\n\n/** The LaunchDarkly `flag-used` inspector. Use `instrumentLaunchDarkly` instead unless you build options yourself. */\nexport function launchDarklyInspector(): LaunchDarklyFlagUsedInspector {\n return {\n type: 'flag-used',\n name: 'testchimp-flags',\n method: (flagKey, flagDetail) => reportFlag(flagKey, flagDetail?.value, 'launchdarkly'),\n };\n}\n\n/**\n * LaunchDarkly JS client. Inspectors can only be set when the client is initialized, so pass the options you\n * give to `LDClient.initialize` through this function. It returns a new options object with the inspector\n * added, or the same options object unchanged when TestChimp is not capturing.\n *\n * const client = LDClient.initialize(id, context, instrumentLaunchDarkly({ bootstrap }));\n */\nexport function instrumentLaunchDarkly<T extends { inspectors?: unknown[] }>(options: T): T {\n if (!isTestChimpCaptureActive()) return options;\n const others = (options.inspectors ?? []).filter(\n (inspector) => (inspector as { name?: string } | undefined)?.name !== 'testchimp-flags'\n );\n return { ...options, inspectors: [...others, launchDarklyInspector()] };\n}\n\ntype CaptureFn = (...args: unknown[]) => unknown;\n\n/**\n * PostHog JS. PostHog reports each evaluation as a `$feature_flag_called` event, emitted through\n * `posthog.capture`. This wraps `capture` on the instance and reads the event; the original call and its\n * result are unchanged. Lookups made with `send_event: false` emit no event, so they are not captured.\n *\n * posthog.init(key, options); instrumentPostHog(posthog);\n */\nexport function instrumentPostHog<T extends object>(posthog: T): T {\n if (!isTestChimpCaptureActive() || instrumented.has(posthog)) return posthog;\n const target = posthog as unknown as { capture?: CaptureFn };\n const original = target.capture;\n if (typeof original !== 'function') return posthog;\n instrumented.add(posthog);\n target.capture = function (this: unknown, ...args: unknown[]) {\n try {\n const [event, properties] = args;\n if (event === '$feature_flag_called' && properties && typeof properties === 'object') {\n const p = properties as Record<string, unknown>;\n if (typeof p.$feature_flag === 'string') {\n reportFlag(p.$feature_flag, p.$feature_flag_response, 'posthog');\n }\n }\n } catch {\n /* never affects the app */\n }\n return original.apply(this, args);\n };\n return posthog;\n}\n"],"mappings":";AACO,IAAM,qBAAqB;AAO3B,SAAS,iBAAiB,OAAoC;AACnE,MAAI,UAAU,OAAW,QAAO;AAChC,MAAI;AACJ,MAAI,UAAU,MAAM;AAClB,WAAO;AAAA,EACT,WAAW,OAAO,UAAU,UAAU;AACpC,WAAO;AAAA,EACT,WAAW,OAAO,UAAU,YAAY,OAAO,UAAU,aAAa,OAAO,UAAU,UAAU;AAC/F,WAAO,OAAO,KAAK;AAAA,EACrB,OAAO;AACL,QAAI;AACF,aAAO,KAAK,UAAU,KAAK;AAAA,IAC7B,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;AACA,MAAI,SAAS,OAAW,QAAO;AAC/B,SAAO,KAAK,SAAS,qBAAqB,KAAK,MAAM,GAAG,kBAAkB,IAAI;AAChF;;;ACRO,SAAS,gBAAgB,QAAoB;AAClD,SAAO;AAAA,IACL,MAAM,aAA0C,SAAkD;AAChG,YAAM,UAAU,SAAS,WAAW,aAAa;AACjD,UAAI,CAAC,QAAS;AAEd,YAAM,UAAU,SAAS,YAAY,UAAa,QAAQ,YAAY,KAAK,QAAQ,UAAU,SAAS;AACtG,aAAO,SAAS,SAAS,aAAa;AAAA,IACxC;AAAA,EACF;AACF;;;ACdA,IAAM,eAAe,oBAAI,QAAgB;AAEzC,SAAS,cAAuC;AAC9C,QAAM,YAAa,WAAgE;AACnF,SAAO,aAAa,OAAO,UAAU,WAAW,aAAa,YAAY;AAC3E;AAGO,SAAS,2BAAoC;AAClD,SAAO,YAAY,MAAM;AAC3B;AAMO,SAAS,WAAW,SAAiB,SAAkB,aAAa,WAAiB;AAC1F,MAAI;AACF,UAAM,SAAS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
|
{
|
|
2
2
|
"name": "@testchimp/flags",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Feature flag evaluation hooks for apps under test. Reports which flag arms a TestChimp test ran, from the browser and from the backend.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "TestChimp",
|
|
7
|
+
"homepage": "https://github.com/TestChimp/testchimp-flags#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/TestChimp/testchimp-flags.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/TestChimp/testchimp-flags/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"testchimp",
|
|
17
|
+
"feature-flags",
|
|
18
|
+
"openfeature",
|
|
19
|
+
"launchdarkly",
|
|
20
|
+
"posthog",
|
|
21
|
+
"test-coverage"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=18.17.0"
|
|
28
|
+
},
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"exports": {
|
|
31
|
+
"./web": {
|
|
32
|
+
"import": {
|
|
33
|
+
"types": "./dist/web/index.d.mts",
|
|
34
|
+
"default": "./dist/web/index.mjs"
|
|
35
|
+
},
|
|
36
|
+
"require": {
|
|
37
|
+
"types": "./dist/web/index.d.ts",
|
|
38
|
+
"default": "./dist/web/index.js"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"./server": {
|
|
42
|
+
"import": {
|
|
43
|
+
"types": "./dist/server/index.d.mts",
|
|
44
|
+
"default": "./dist/server/index.mjs"
|
|
45
|
+
},
|
|
46
|
+
"require": {
|
|
47
|
+
"types": "./dist/server/index.d.ts",
|
|
48
|
+
"default": "./dist/server/index.js"
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"files": [
|
|
53
|
+
"dist/",
|
|
54
|
+
"README.md",
|
|
55
|
+
"LICENSE"
|
|
56
|
+
],
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "tsup",
|
|
59
|
+
"typecheck": "tsc -p . --noEmit",
|
|
60
|
+
"test": "npm run build && node --test test/*.test.js",
|
|
61
|
+
"prepublishOnly": "npm run typecheck && npm test"
|
|
62
|
+
},
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"@types/node": "^20.10.0",
|
|
65
|
+
"tsup": "^8.5.0",
|
|
66
|
+
"typescript": "^5.6.0"
|
|
67
|
+
}
|
|
68
|
+
}
|