@ops-ai/toggly-fastify 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/dist/index.cjs +201 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +113 -0
- package/dist/index.d.ts +113 -0
- package/dist/index.js +162 -0
- package/dist/index.js.map +1 -0
- package/package.json +75 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
closeFastifyToggly: () => closeFastifyToggly,
|
|
34
|
+
closeToggly: () => import_toggly_node_core2.closeToggly,
|
|
35
|
+
createFileCache: () => import_toggly_node_core2.createFileCache,
|
|
36
|
+
createLoggingHook: () => import_toggly_node_core2.createLoggingHook,
|
|
37
|
+
createMemoryCache: () => import_toggly_node_core2.createMemoryCache,
|
|
38
|
+
createTogglyClient: () => import_toggly_node_core2.createTogglyClient,
|
|
39
|
+
featureGate: () => featureGate,
|
|
40
|
+
featureRoutes: () => featureRoutes,
|
|
41
|
+
featuresHandler: () => featuresHandler,
|
|
42
|
+
getFastifyToggly: () => getFastifyToggly,
|
|
43
|
+
getToggly: () => import_toggly_node_core2.getToggly,
|
|
44
|
+
initToggly: () => import_toggly_node_core2.initToggly,
|
|
45
|
+
togglyPlugin: () => togglyPlugin,
|
|
46
|
+
useToggly: () => import_toggly_node_core2.useToggly,
|
|
47
|
+
withFeature: () => withFeature
|
|
48
|
+
});
|
|
49
|
+
module.exports = __toCommonJS(index_exports);
|
|
50
|
+
|
|
51
|
+
// src/plugin.ts
|
|
52
|
+
var import_fastify_plugin = __toESM(require("fastify-plugin"), 1);
|
|
53
|
+
var import_toggly_node_core = require("@ops-ai/toggly-node-core");
|
|
54
|
+
var fastifyClient = null;
|
|
55
|
+
function getFastifyToggly() {
|
|
56
|
+
return fastifyClient;
|
|
57
|
+
}
|
|
58
|
+
async function extractIdentity(request, config) {
|
|
59
|
+
if (config.getIdentity) {
|
|
60
|
+
return config.getIdentity(request);
|
|
61
|
+
}
|
|
62
|
+
const headerIdentity = request.headers["x-toggly-identity"];
|
|
63
|
+
if (typeof headerIdentity === "string") {
|
|
64
|
+
return headerIdentity;
|
|
65
|
+
}
|
|
66
|
+
return void 0;
|
|
67
|
+
}
|
|
68
|
+
async function extractContext(request, config) {
|
|
69
|
+
if (config.getContext) {
|
|
70
|
+
return config.getContext(request);
|
|
71
|
+
}
|
|
72
|
+
const identity = await extractIdentity(request, config);
|
|
73
|
+
return {
|
|
74
|
+
identity,
|
|
75
|
+
traits: {
|
|
76
|
+
ip: request.ip,
|
|
77
|
+
userAgent: request.headers["user-agent"],
|
|
78
|
+
path: request.url,
|
|
79
|
+
method: request.method
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
var togglyPluginAsync = async (fastify, config) => {
|
|
84
|
+
if (!fastifyClient) {
|
|
85
|
+
fastifyClient = (0, import_toggly_node_core.createTogglyClient)(config);
|
|
86
|
+
await fastifyClient.init();
|
|
87
|
+
}
|
|
88
|
+
fastify.addHook("preHandler", async (request) => {
|
|
89
|
+
if (!fastifyClient) {
|
|
90
|
+
throw new Error("Toggly client not initialized");
|
|
91
|
+
}
|
|
92
|
+
const context = await extractContext(request, config);
|
|
93
|
+
const identity = context.identity;
|
|
94
|
+
request.toggly = {
|
|
95
|
+
client: fastifyClient,
|
|
96
|
+
features: fastifyClient.state.features,
|
|
97
|
+
identity,
|
|
98
|
+
context,
|
|
99
|
+
isFeatureOn: (featureKey) => fastifyClient.isFeatureOn(featureKey, context),
|
|
100
|
+
isFeatureOff: (featureKey) => fastifyClient.isFeatureOff(featureKey, context),
|
|
101
|
+
evaluateFeatureGate: (featureKeys, requirement, negate) => fastifyClient.evaluateFeatureGate(featureKeys, requirement, negate, context)
|
|
102
|
+
};
|
|
103
|
+
});
|
|
104
|
+
fastify.addHook("onClose", async () => {
|
|
105
|
+
closeFastifyToggly();
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
var togglyPlugin = (0, import_fastify_plugin.default)(togglyPluginAsync, {
|
|
109
|
+
name: "@ops-ai/toggly-fastify",
|
|
110
|
+
fastify: ">=4.0.0"
|
|
111
|
+
});
|
|
112
|
+
function featureGate(options) {
|
|
113
|
+
const {
|
|
114
|
+
featureKey,
|
|
115
|
+
requirement = "all",
|
|
116
|
+
negate = false,
|
|
117
|
+
onDisabled,
|
|
118
|
+
redirectTo,
|
|
119
|
+
redirectStatus = 302
|
|
120
|
+
} = options;
|
|
121
|
+
const featureKeys = (0, import_toggly_node_core.normalizeFeatureKeys)(featureKey);
|
|
122
|
+
return async (request, reply) => {
|
|
123
|
+
if (!request.toggly) {
|
|
124
|
+
throw new Error("Toggly plugin must be registered before using featureGate");
|
|
125
|
+
}
|
|
126
|
+
const isEnabled = await request.toggly.evaluateFeatureGate(featureKeys, requirement, negate);
|
|
127
|
+
if (isEnabled) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (redirectTo) {
|
|
131
|
+
return reply.redirect(redirectTo, redirectStatus);
|
|
132
|
+
}
|
|
133
|
+
if (onDisabled) {
|
|
134
|
+
await onDisabled(request);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
return reply.status(404).send({
|
|
138
|
+
error: "Not Found",
|
|
139
|
+
message: "The requested resource is not available"
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function featureRoutes(routes) {
|
|
144
|
+
return async (request, reply) => {
|
|
145
|
+
for (const route of routes) {
|
|
146
|
+
const pathMatches = typeof route.path === "string" ? request.url === route.path || request.url.startsWith(route.path) : route.path.test(request.url);
|
|
147
|
+
if (!pathMatches) {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (route.methods && !route.methods.includes(request.method.toUpperCase())) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const gate = featureGate(route);
|
|
154
|
+
return gate(request, reply);
|
|
155
|
+
}
|
|
156
|
+
return;
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function withFeature(featureKey, options = {}) {
|
|
160
|
+
return featureGate({ featureKey, ...options });
|
|
161
|
+
}
|
|
162
|
+
async function featuresHandler(request, reply) {
|
|
163
|
+
if (!request.toggly) {
|
|
164
|
+
return reply.status(500).send({
|
|
165
|
+
error: "Internal Server Error",
|
|
166
|
+
message: "Toggly plugin not configured"
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
return reply.send({
|
|
170
|
+
features: request.toggly.features,
|
|
171
|
+
identity: request.toggly.identity
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
function closeFastifyToggly() {
|
|
175
|
+
if (fastifyClient) {
|
|
176
|
+
fastifyClient.close();
|
|
177
|
+
fastifyClient = null;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// src/index.ts
|
|
182
|
+
var import_toggly_node_core2 = require("@ops-ai/toggly-node-core");
|
|
183
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
184
|
+
0 && (module.exports = {
|
|
185
|
+
closeFastifyToggly,
|
|
186
|
+
closeToggly,
|
|
187
|
+
createFileCache,
|
|
188
|
+
createLoggingHook,
|
|
189
|
+
createMemoryCache,
|
|
190
|
+
createTogglyClient,
|
|
191
|
+
featureGate,
|
|
192
|
+
featureRoutes,
|
|
193
|
+
featuresHandler,
|
|
194
|
+
getFastifyToggly,
|
|
195
|
+
getToggly,
|
|
196
|
+
initToggly,
|
|
197
|
+
togglyPlugin,
|
|
198
|
+
useToggly,
|
|
199
|
+
withFeature
|
|
200
|
+
});
|
|
201
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/plugin.ts"],"sourcesContent":["// Plugin\nexport {\n togglyPlugin,\n featureGate,\n featureRoutes,\n withFeature,\n featuresHandler,\n getFastifyToggly,\n closeFastifyToggly,\n} from './plugin.js'\n\n// Types\nexport type {\n TogglyFastifyConfig,\n TogglyRequestData,\n FeatureGateOptions,\n FeatureRouteOptions,\n} from './types.js'\n\n// Re-export core types for convenience\nexport type {\n TogglyClient,\n TogglyConfig,\n TogglyServerConfig,\n TogglyState,\n FeatureDefinitions,\n FeatureRequirement,\n EvaluationContext,\n Hook,\n HookMetadata,\n} from '@ops-ai/toggly-node-core'\n\n// Re-export core utilities\nexport {\n createTogglyClient,\n initToggly,\n getToggly,\n useToggly,\n closeToggly,\n createLoggingHook,\n createMemoryCache,\n createFileCache,\n} from '@ops-ai/toggly-node-core'\n","import type {\n FastifyInstance,\n FastifyRequest,\n FastifyReply,\n FastifyPluginAsync,\n} from 'fastify'\n\n// Custom type for preHandler hooks (simpler than Fastify's complex generic type)\ntype PreHandlerHook = (\n request: FastifyRequest,\n reply: FastifyReply\n) => Promise<unknown>\nimport fp from 'fastify-plugin'\nimport {\n createTogglyClient,\n normalizeFeatureKeys,\n type TogglyClient,\n type EvaluationContext,\n} from '@ops-ai/toggly-node-core'\nimport type {\n TogglyFastifyConfig,\n FeatureGateOptions,\n FeatureRouteOptions,\n} from './types.js'\n\n// Module-level client singleton\nlet fastifyClient: TogglyClient | null = null\n\n/**\n * Get the Fastify Toggly client\n */\nexport function getFastifyToggly(): TogglyClient | null {\n return fastifyClient\n}\n\n/**\n * Get identity from request\n */\nasync function extractIdentity(\n request: FastifyRequest,\n config: TogglyFastifyConfig\n): Promise<string | undefined> {\n // Use custom extractor if provided\n if (config.getIdentity) {\n return config.getIdentity(request)\n }\n\n // Default: check header\n const headerIdentity = request.headers['x-toggly-identity']\n if (typeof headerIdentity === 'string') {\n return headerIdentity\n }\n\n return undefined\n}\n\n/**\n * Get evaluation context from request\n */\nasync function extractContext(\n request: FastifyRequest,\n config: TogglyFastifyConfig\n): Promise<EvaluationContext> {\n // Use custom extractor if provided\n if (config.getContext) {\n return config.getContext(request)\n }\n\n // Default: extract basic context\n const identity = await extractIdentity(request, config)\n\n return {\n identity,\n traits: {\n ip: request.ip,\n userAgent: request.headers['user-agent'],\n path: request.url,\n method: request.method,\n },\n }\n}\n\n/**\n * Toggly Fastify plugin\n *\n * This plugin:\n * 1. Initializes the Toggly client\n * 2. Attaches feature flag helpers to the request object\n * 3. Extracts identity and context from the request\n */\nconst togglyPluginAsync: FastifyPluginAsync<TogglyFastifyConfig> = async (\n fastify: FastifyInstance,\n config: TogglyFastifyConfig\n) => {\n // Initialize client\n if (!fastifyClient) {\n fastifyClient = createTogglyClient(config)\n await fastifyClient.init()\n }\n\n // Add hook to attach toggly to request\n fastify.addHook('preHandler', async (request: FastifyRequest) => {\n if (!fastifyClient) {\n throw new Error('Toggly client not initialized')\n }\n\n // Extract context from request\n const context = await extractContext(request, config)\n const identity = context.identity\n\n // Attach Toggly helpers to request\n request.toggly = {\n client: fastifyClient,\n features: fastifyClient.state.features,\n identity,\n context,\n isFeatureOn: (featureKey: string) => fastifyClient!.isFeatureOn(featureKey, context),\n isFeatureOff: (featureKey: string) => fastifyClient!.isFeatureOff(featureKey, context),\n evaluateFeatureGate: (featureKeys, requirement, negate) =>\n fastifyClient!.evaluateFeatureGate(featureKeys, requirement, negate, context),\n }\n })\n\n // Close client when fastify closes\n fastify.addHook('onClose', async () => {\n closeFastifyToggly()\n })\n}\n\n/**\n * Toggly Fastify plugin with proper encapsulation\n */\nexport const togglyPlugin = fp(togglyPluginAsync, {\n name: '@ops-ai/toggly-fastify',\n fastify: '>=4.0.0',\n})\n\n/**\n * Create a feature gate preHandler hook\n *\n * Protects routes by checking if specified features are enabled\n */\nexport function featureGate(options: FeatureGateOptions): PreHandlerHook {\n const {\n featureKey,\n requirement = 'all',\n negate = false,\n onDisabled,\n redirectTo,\n redirectStatus = 302,\n } = options\n\n const featureKeys = normalizeFeatureKeys(featureKey)\n\n return async (request: FastifyRequest, reply: FastifyReply) => {\n if (!request.toggly) {\n throw new Error('Toggly plugin must be registered before using featureGate')\n }\n\n const isEnabled = await request.toggly.evaluateFeatureGate(featureKeys, requirement, negate)\n\n if (isEnabled) {\n return // Continue to handler\n }\n\n // Feature is disabled\n if (redirectTo) {\n return reply.redirect(redirectTo, redirectStatus)\n }\n\n if (onDisabled) {\n await onDisabled(request)\n return\n }\n\n // Default: 404\n return reply.status(404).send({\n error: 'Not Found',\n message: 'The requested resource is not available',\n })\n }\n}\n\n/**\n * Create preHandler that applies feature gates based on route patterns\n */\nexport function featureRoutes(routes: FeatureRouteOptions[]): PreHandlerHook {\n return async (request: FastifyRequest, reply: FastifyReply) => {\n for (const route of routes) {\n // Check if route matches\n const pathMatches =\n typeof route.path === 'string'\n ? request.url === route.path || request.url.startsWith(route.path)\n : route.path.test(request.url)\n\n if (!pathMatches) {\n continue\n }\n\n // Check if method matches\n if (route.methods && !route.methods.includes(request.method.toUpperCase())) {\n continue\n }\n\n // Apply feature gate\n const gate = featureGate(route)\n return gate(request, reply)\n }\n\n // No matching route, continue\n return\n }\n}\n\n/**\n * Create a decorated route handler that checks features\n */\nexport function withFeature(\n featureKey: string | string[],\n options: Omit<FeatureGateOptions, 'featureKey'> = {}\n): PreHandlerHook {\n return featureGate({ featureKey, ...options })\n}\n\n/**\n * Get features route handler\n */\nexport async function featuresHandler(\n request: FastifyRequest,\n reply: FastifyReply\n): Promise<void> {\n if (!request.toggly) {\n return reply.status(500).send({\n error: 'Internal Server Error',\n message: 'Toggly plugin not configured',\n })\n }\n\n return reply.send({\n features: request.toggly.features,\n identity: request.toggly.identity,\n })\n}\n\n/**\n * Close the Fastify Toggly client\n */\nexport function closeFastifyToggly(): void {\n if (fastifyClient) {\n fastifyClient.close()\n fastifyClient = null\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACYA,4BAAe;AACf,8BAKO;AAQP,IAAI,gBAAqC;AAKlC,SAAS,mBAAwC;AACtD,SAAO;AACT;AAKA,eAAe,gBACb,SACA,QAC6B;AAE7B,MAAI,OAAO,aAAa;AACtB,WAAO,OAAO,YAAY,OAAO;AAAA,EACnC;AAGA,QAAM,iBAAiB,QAAQ,QAAQ,mBAAmB;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,MAC5B,OAAO;AAAA,MACP,SAAS;AAAA,IACX,CAAC;AAAA,EACH;AAEA,SAAO,MAAM,KAAK;AAAA,IAChB,UAAU,QAAQ,OAAO;AAAA,IACzB,UAAU,QAAQ,OAAO;AAAA,EAC3B,CAAC;AACH;AAKO,SAAS,qBAA2B;AACzC,MAAI,eAAe;AACjB,kBAAc,MAAM;AACpB,oBAAgB;AAAA,EAClB;AACF;;;AD3NA,IAAAC,2BASO;","names":["fp","import_toggly_node_core"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { FastifyRequest, FastifyReply, FastifyPluginAsync } from 'fastify';
|
|
2
|
+
import { TogglyClient, FeatureDefinitions, EvaluationContext, FeatureRequirement, TogglyServerConfig } from '@ops-ai/toggly-node-core';
|
|
3
|
+
export { EvaluationContext, FeatureDefinitions, FeatureRequirement, Hook, HookMetadata, TogglyClient, TogglyConfig, TogglyServerConfig, TogglyState, closeToggly, createFileCache, createLoggingHook, createMemoryCache, createTogglyClient, getToggly, initToggly, useToggly } from '@ops-ai/toggly-node-core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Fastify-specific configuration
|
|
7
|
+
*/
|
|
8
|
+
interface TogglyFastifyConfig extends TogglyServerConfig {
|
|
9
|
+
/**
|
|
10
|
+
* Function to extract identity from request
|
|
11
|
+
* Default: Uses x-toggly-identity header
|
|
12
|
+
*/
|
|
13
|
+
getIdentity?: (request: FastifyRequest) => string | undefined | Promise<string | undefined>;
|
|
14
|
+
/**
|
|
15
|
+
* Function to extract evaluation context from request
|
|
16
|
+
*/
|
|
17
|
+
getContext?: (request: FastifyRequest) => EvaluationContext | Promise<EvaluationContext>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Toggly data attached to Fastify request
|
|
21
|
+
*/
|
|
22
|
+
interface TogglyRequestData {
|
|
23
|
+
client: TogglyClient;
|
|
24
|
+
features: FeatureDefinitions;
|
|
25
|
+
identity?: string;
|
|
26
|
+
context: EvaluationContext;
|
|
27
|
+
isFeatureOn: (featureKey: string) => Promise<boolean>;
|
|
28
|
+
isFeatureOff: (featureKey: string) => Promise<boolean>;
|
|
29
|
+
evaluateFeatureGate: (featureKeys: string[], requirement?: FeatureRequirement, negate?: boolean) => Promise<boolean>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Feature gate options for protecting routes
|
|
33
|
+
*/
|
|
34
|
+
interface FeatureGateOptions {
|
|
35
|
+
/**
|
|
36
|
+
* Feature key(s) to check
|
|
37
|
+
*/
|
|
38
|
+
featureKey: string | string[];
|
|
39
|
+
/**
|
|
40
|
+
* Requirement type for multiple features
|
|
41
|
+
*/
|
|
42
|
+
requirement?: FeatureRequirement;
|
|
43
|
+
/**
|
|
44
|
+
* Negate the result
|
|
45
|
+
*/
|
|
46
|
+
negate?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Custom handler when feature is disabled
|
|
49
|
+
* Default: Returns 404
|
|
50
|
+
*/
|
|
51
|
+
onDisabled?: (request: FastifyRequest) => void | Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Custom redirect URL when feature is disabled
|
|
54
|
+
*/
|
|
55
|
+
redirectTo?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Redirect status code
|
|
58
|
+
*/
|
|
59
|
+
redirectStatus?: 301 | 302 | 303 | 307 | 308;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Feature route options for protecting routes
|
|
63
|
+
*/
|
|
64
|
+
interface FeatureRouteOptions extends FeatureGateOptions {
|
|
65
|
+
/**
|
|
66
|
+
* Path pattern to match
|
|
67
|
+
*/
|
|
68
|
+
path: string | RegExp;
|
|
69
|
+
/**
|
|
70
|
+
* HTTP methods to match (default: all)
|
|
71
|
+
*/
|
|
72
|
+
methods?: string[];
|
|
73
|
+
}
|
|
74
|
+
declare module 'fastify' {
|
|
75
|
+
interface FastifyRequest {
|
|
76
|
+
toggly?: TogglyRequestData;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
type PreHandlerHook = (request: FastifyRequest, reply: FastifyReply) => Promise<unknown>;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Get the Fastify Toggly client
|
|
84
|
+
*/
|
|
85
|
+
declare function getFastifyToggly(): TogglyClient | null;
|
|
86
|
+
/**
|
|
87
|
+
* Toggly Fastify plugin with proper encapsulation
|
|
88
|
+
*/
|
|
89
|
+
declare const togglyPlugin: FastifyPluginAsync<TogglyFastifyConfig>;
|
|
90
|
+
/**
|
|
91
|
+
* Create a feature gate preHandler hook
|
|
92
|
+
*
|
|
93
|
+
* Protects routes by checking if specified features are enabled
|
|
94
|
+
*/
|
|
95
|
+
declare function featureGate(options: FeatureGateOptions): PreHandlerHook;
|
|
96
|
+
/**
|
|
97
|
+
* Create preHandler that applies feature gates based on route patterns
|
|
98
|
+
*/
|
|
99
|
+
declare function featureRoutes(routes: FeatureRouteOptions[]): PreHandlerHook;
|
|
100
|
+
/**
|
|
101
|
+
* Create a decorated route handler that checks features
|
|
102
|
+
*/
|
|
103
|
+
declare function withFeature(featureKey: string | string[], options?: Omit<FeatureGateOptions, 'featureKey'>): PreHandlerHook;
|
|
104
|
+
/**
|
|
105
|
+
* Get features route handler
|
|
106
|
+
*/
|
|
107
|
+
declare function featuresHandler(request: FastifyRequest, reply: FastifyReply): Promise<void>;
|
|
108
|
+
/**
|
|
109
|
+
* Close the Fastify Toggly client
|
|
110
|
+
*/
|
|
111
|
+
declare function closeFastifyToggly(): void;
|
|
112
|
+
|
|
113
|
+
export { type FeatureGateOptions, type FeatureRouteOptions, type TogglyFastifyConfig, type TogglyRequestData, closeFastifyToggly, featureGate, featureRoutes, featuresHandler, getFastifyToggly, togglyPlugin, withFeature };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { FastifyRequest, FastifyReply, FastifyPluginAsync } from 'fastify';
|
|
2
|
+
import { TogglyClient, FeatureDefinitions, EvaluationContext, FeatureRequirement, TogglyServerConfig } from '@ops-ai/toggly-node-core';
|
|
3
|
+
export { EvaluationContext, FeatureDefinitions, FeatureRequirement, Hook, HookMetadata, TogglyClient, TogglyConfig, TogglyServerConfig, TogglyState, closeToggly, createFileCache, createLoggingHook, createMemoryCache, createTogglyClient, getToggly, initToggly, useToggly } from '@ops-ai/toggly-node-core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Fastify-specific configuration
|
|
7
|
+
*/
|
|
8
|
+
interface TogglyFastifyConfig extends TogglyServerConfig {
|
|
9
|
+
/**
|
|
10
|
+
* Function to extract identity from request
|
|
11
|
+
* Default: Uses x-toggly-identity header
|
|
12
|
+
*/
|
|
13
|
+
getIdentity?: (request: FastifyRequest) => string | undefined | Promise<string | undefined>;
|
|
14
|
+
/**
|
|
15
|
+
* Function to extract evaluation context from request
|
|
16
|
+
*/
|
|
17
|
+
getContext?: (request: FastifyRequest) => EvaluationContext | Promise<EvaluationContext>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Toggly data attached to Fastify request
|
|
21
|
+
*/
|
|
22
|
+
interface TogglyRequestData {
|
|
23
|
+
client: TogglyClient;
|
|
24
|
+
features: FeatureDefinitions;
|
|
25
|
+
identity?: string;
|
|
26
|
+
context: EvaluationContext;
|
|
27
|
+
isFeatureOn: (featureKey: string) => Promise<boolean>;
|
|
28
|
+
isFeatureOff: (featureKey: string) => Promise<boolean>;
|
|
29
|
+
evaluateFeatureGate: (featureKeys: string[], requirement?: FeatureRequirement, negate?: boolean) => Promise<boolean>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Feature gate options for protecting routes
|
|
33
|
+
*/
|
|
34
|
+
interface FeatureGateOptions {
|
|
35
|
+
/**
|
|
36
|
+
* Feature key(s) to check
|
|
37
|
+
*/
|
|
38
|
+
featureKey: string | string[];
|
|
39
|
+
/**
|
|
40
|
+
* Requirement type for multiple features
|
|
41
|
+
*/
|
|
42
|
+
requirement?: FeatureRequirement;
|
|
43
|
+
/**
|
|
44
|
+
* Negate the result
|
|
45
|
+
*/
|
|
46
|
+
negate?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Custom handler when feature is disabled
|
|
49
|
+
* Default: Returns 404
|
|
50
|
+
*/
|
|
51
|
+
onDisabled?: (request: FastifyRequest) => void | Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Custom redirect URL when feature is disabled
|
|
54
|
+
*/
|
|
55
|
+
redirectTo?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Redirect status code
|
|
58
|
+
*/
|
|
59
|
+
redirectStatus?: 301 | 302 | 303 | 307 | 308;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Feature route options for protecting routes
|
|
63
|
+
*/
|
|
64
|
+
interface FeatureRouteOptions extends FeatureGateOptions {
|
|
65
|
+
/**
|
|
66
|
+
* Path pattern to match
|
|
67
|
+
*/
|
|
68
|
+
path: string | RegExp;
|
|
69
|
+
/**
|
|
70
|
+
* HTTP methods to match (default: all)
|
|
71
|
+
*/
|
|
72
|
+
methods?: string[];
|
|
73
|
+
}
|
|
74
|
+
declare module 'fastify' {
|
|
75
|
+
interface FastifyRequest {
|
|
76
|
+
toggly?: TogglyRequestData;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
type PreHandlerHook = (request: FastifyRequest, reply: FastifyReply) => Promise<unknown>;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Get the Fastify Toggly client
|
|
84
|
+
*/
|
|
85
|
+
declare function getFastifyToggly(): TogglyClient | null;
|
|
86
|
+
/**
|
|
87
|
+
* Toggly Fastify plugin with proper encapsulation
|
|
88
|
+
*/
|
|
89
|
+
declare const togglyPlugin: FastifyPluginAsync<TogglyFastifyConfig>;
|
|
90
|
+
/**
|
|
91
|
+
* Create a feature gate preHandler hook
|
|
92
|
+
*
|
|
93
|
+
* Protects routes by checking if specified features are enabled
|
|
94
|
+
*/
|
|
95
|
+
declare function featureGate(options: FeatureGateOptions): PreHandlerHook;
|
|
96
|
+
/**
|
|
97
|
+
* Create preHandler that applies feature gates based on route patterns
|
|
98
|
+
*/
|
|
99
|
+
declare function featureRoutes(routes: FeatureRouteOptions[]): PreHandlerHook;
|
|
100
|
+
/**
|
|
101
|
+
* Create a decorated route handler that checks features
|
|
102
|
+
*/
|
|
103
|
+
declare function withFeature(featureKey: string | string[], options?: Omit<FeatureGateOptions, 'featureKey'>): PreHandlerHook;
|
|
104
|
+
/**
|
|
105
|
+
* Get features route handler
|
|
106
|
+
*/
|
|
107
|
+
declare function featuresHandler(request: FastifyRequest, reply: FastifyReply): Promise<void>;
|
|
108
|
+
/**
|
|
109
|
+
* Close the Fastify Toggly client
|
|
110
|
+
*/
|
|
111
|
+
declare function closeFastifyToggly(): void;
|
|
112
|
+
|
|
113
|
+
export { type FeatureGateOptions, type FeatureRouteOptions, type TogglyFastifyConfig, type TogglyRequestData, closeFastifyToggly, featureGate, featureRoutes, featuresHandler, getFastifyToggly, togglyPlugin, withFeature };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// src/plugin.ts
|
|
2
|
+
import fp from "fastify-plugin";
|
|
3
|
+
import {
|
|
4
|
+
createTogglyClient,
|
|
5
|
+
normalizeFeatureKeys
|
|
6
|
+
} from "@ops-ai/toggly-node-core";
|
|
7
|
+
var fastifyClient = null;
|
|
8
|
+
function getFastifyToggly() {
|
|
9
|
+
return fastifyClient;
|
|
10
|
+
}
|
|
11
|
+
async function extractIdentity(request, config) {
|
|
12
|
+
if (config.getIdentity) {
|
|
13
|
+
return config.getIdentity(request);
|
|
14
|
+
}
|
|
15
|
+
const headerIdentity = request.headers["x-toggly-identity"];
|
|
16
|
+
if (typeof headerIdentity === "string") {
|
|
17
|
+
return headerIdentity;
|
|
18
|
+
}
|
|
19
|
+
return void 0;
|
|
20
|
+
}
|
|
21
|
+
async function extractContext(request, config) {
|
|
22
|
+
if (config.getContext) {
|
|
23
|
+
return config.getContext(request);
|
|
24
|
+
}
|
|
25
|
+
const identity = await extractIdentity(request, config);
|
|
26
|
+
return {
|
|
27
|
+
identity,
|
|
28
|
+
traits: {
|
|
29
|
+
ip: request.ip,
|
|
30
|
+
userAgent: request.headers["user-agent"],
|
|
31
|
+
path: request.url,
|
|
32
|
+
method: request.method
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
var togglyPluginAsync = async (fastify, config) => {
|
|
37
|
+
if (!fastifyClient) {
|
|
38
|
+
fastifyClient = createTogglyClient(config);
|
|
39
|
+
await fastifyClient.init();
|
|
40
|
+
}
|
|
41
|
+
fastify.addHook("preHandler", async (request) => {
|
|
42
|
+
if (!fastifyClient) {
|
|
43
|
+
throw new Error("Toggly client not initialized");
|
|
44
|
+
}
|
|
45
|
+
const context = await extractContext(request, config);
|
|
46
|
+
const identity = context.identity;
|
|
47
|
+
request.toggly = {
|
|
48
|
+
client: fastifyClient,
|
|
49
|
+
features: fastifyClient.state.features,
|
|
50
|
+
identity,
|
|
51
|
+
context,
|
|
52
|
+
isFeatureOn: (featureKey) => fastifyClient.isFeatureOn(featureKey, context),
|
|
53
|
+
isFeatureOff: (featureKey) => fastifyClient.isFeatureOff(featureKey, context),
|
|
54
|
+
evaluateFeatureGate: (featureKeys, requirement, negate) => fastifyClient.evaluateFeatureGate(featureKeys, requirement, negate, context)
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
fastify.addHook("onClose", async () => {
|
|
58
|
+
closeFastifyToggly();
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
var togglyPlugin = fp(togglyPluginAsync, {
|
|
62
|
+
name: "@ops-ai/toggly-fastify",
|
|
63
|
+
fastify: ">=4.0.0"
|
|
64
|
+
});
|
|
65
|
+
function featureGate(options) {
|
|
66
|
+
const {
|
|
67
|
+
featureKey,
|
|
68
|
+
requirement = "all",
|
|
69
|
+
negate = false,
|
|
70
|
+
onDisabled,
|
|
71
|
+
redirectTo,
|
|
72
|
+
redirectStatus = 302
|
|
73
|
+
} = options;
|
|
74
|
+
const featureKeys = normalizeFeatureKeys(featureKey);
|
|
75
|
+
return async (request, reply) => {
|
|
76
|
+
if (!request.toggly) {
|
|
77
|
+
throw new Error("Toggly plugin must be registered before using featureGate");
|
|
78
|
+
}
|
|
79
|
+
const isEnabled = await request.toggly.evaluateFeatureGate(featureKeys, requirement, negate);
|
|
80
|
+
if (isEnabled) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (redirectTo) {
|
|
84
|
+
return reply.redirect(redirectTo, redirectStatus);
|
|
85
|
+
}
|
|
86
|
+
if (onDisabled) {
|
|
87
|
+
await onDisabled(request);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
return reply.status(404).send({
|
|
91
|
+
error: "Not Found",
|
|
92
|
+
message: "The requested resource is not available"
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function featureRoutes(routes) {
|
|
97
|
+
return async (request, reply) => {
|
|
98
|
+
for (const route of routes) {
|
|
99
|
+
const pathMatches = typeof route.path === "string" ? request.url === route.path || request.url.startsWith(route.path) : route.path.test(request.url);
|
|
100
|
+
if (!pathMatches) {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (route.methods && !route.methods.includes(request.method.toUpperCase())) {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const gate = featureGate(route);
|
|
107
|
+
return gate(request, reply);
|
|
108
|
+
}
|
|
109
|
+
return;
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function withFeature(featureKey, options = {}) {
|
|
113
|
+
return featureGate({ featureKey, ...options });
|
|
114
|
+
}
|
|
115
|
+
async function featuresHandler(request, reply) {
|
|
116
|
+
if (!request.toggly) {
|
|
117
|
+
return reply.status(500).send({
|
|
118
|
+
error: "Internal Server Error",
|
|
119
|
+
message: "Toggly plugin not configured"
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return reply.send({
|
|
123
|
+
features: request.toggly.features,
|
|
124
|
+
identity: request.toggly.identity
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function closeFastifyToggly() {
|
|
128
|
+
if (fastifyClient) {
|
|
129
|
+
fastifyClient.close();
|
|
130
|
+
fastifyClient = null;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/index.ts
|
|
135
|
+
import {
|
|
136
|
+
createTogglyClient as createTogglyClient2,
|
|
137
|
+
initToggly,
|
|
138
|
+
getToggly,
|
|
139
|
+
useToggly,
|
|
140
|
+
closeToggly,
|
|
141
|
+
createLoggingHook,
|
|
142
|
+
createMemoryCache,
|
|
143
|
+
createFileCache
|
|
144
|
+
} from "@ops-ai/toggly-node-core";
|
|
145
|
+
export {
|
|
146
|
+
closeFastifyToggly,
|
|
147
|
+
closeToggly,
|
|
148
|
+
createFileCache,
|
|
149
|
+
createLoggingHook,
|
|
150
|
+
createMemoryCache,
|
|
151
|
+
createTogglyClient2 as createTogglyClient,
|
|
152
|
+
featureGate,
|
|
153
|
+
featureRoutes,
|
|
154
|
+
featuresHandler,
|
|
155
|
+
getFastifyToggly,
|
|
156
|
+
getToggly,
|
|
157
|
+
initToggly,
|
|
158
|
+
togglyPlugin,
|
|
159
|
+
useToggly,
|
|
160
|
+
withFeature
|
|
161
|
+
};
|
|
162
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/plugin.ts","../src/index.ts"],"sourcesContent":["import type {\n FastifyInstance,\n FastifyRequest,\n FastifyReply,\n FastifyPluginAsync,\n} from 'fastify'\n\n// Custom type for preHandler hooks (simpler than Fastify's complex generic type)\ntype PreHandlerHook = (\n request: FastifyRequest,\n reply: FastifyReply\n) => Promise<unknown>\nimport fp from 'fastify-plugin'\nimport {\n createTogglyClient,\n normalizeFeatureKeys,\n type TogglyClient,\n type EvaluationContext,\n} from '@ops-ai/toggly-node-core'\nimport type {\n TogglyFastifyConfig,\n FeatureGateOptions,\n FeatureRouteOptions,\n} from './types.js'\n\n// Module-level client singleton\nlet fastifyClient: TogglyClient | null = null\n\n/**\n * Get the Fastify Toggly client\n */\nexport function getFastifyToggly(): TogglyClient | null {\n return fastifyClient\n}\n\n/**\n * Get identity from request\n */\nasync function extractIdentity(\n request: FastifyRequest,\n config: TogglyFastifyConfig\n): Promise<string | undefined> {\n // Use custom extractor if provided\n if (config.getIdentity) {\n return config.getIdentity(request)\n }\n\n // Default: check header\n const headerIdentity = request.headers['x-toggly-identity']\n if (typeof headerIdentity === 'string') {\n return headerIdentity\n }\n\n return undefined\n}\n\n/**\n * Get evaluation context from request\n */\nasync function extractContext(\n request: FastifyRequest,\n config: TogglyFastifyConfig\n): Promise<EvaluationContext> {\n // Use custom extractor if provided\n if (config.getContext) {\n return config.getContext(request)\n }\n\n // Default: extract basic context\n const identity = await extractIdentity(request, config)\n\n return {\n identity,\n traits: {\n ip: request.ip,\n userAgent: request.headers['user-agent'],\n path: request.url,\n method: request.method,\n },\n }\n}\n\n/**\n * Toggly Fastify plugin\n *\n * This plugin:\n * 1. Initializes the Toggly client\n * 2. Attaches feature flag helpers to the request object\n * 3. Extracts identity and context from the request\n */\nconst togglyPluginAsync: FastifyPluginAsync<TogglyFastifyConfig> = async (\n fastify: FastifyInstance,\n config: TogglyFastifyConfig\n) => {\n // Initialize client\n if (!fastifyClient) {\n fastifyClient = createTogglyClient(config)\n await fastifyClient.init()\n }\n\n // Add hook to attach toggly to request\n fastify.addHook('preHandler', async (request: FastifyRequest) => {\n if (!fastifyClient) {\n throw new Error('Toggly client not initialized')\n }\n\n // Extract context from request\n const context = await extractContext(request, config)\n const identity = context.identity\n\n // Attach Toggly helpers to request\n request.toggly = {\n client: fastifyClient,\n features: fastifyClient.state.features,\n identity,\n context,\n isFeatureOn: (featureKey: string) => fastifyClient!.isFeatureOn(featureKey, context),\n isFeatureOff: (featureKey: string) => fastifyClient!.isFeatureOff(featureKey, context),\n evaluateFeatureGate: (featureKeys, requirement, negate) =>\n fastifyClient!.evaluateFeatureGate(featureKeys, requirement, negate, context),\n }\n })\n\n // Close client when fastify closes\n fastify.addHook('onClose', async () => {\n closeFastifyToggly()\n })\n}\n\n/**\n * Toggly Fastify plugin with proper encapsulation\n */\nexport const togglyPlugin = fp(togglyPluginAsync, {\n name: '@ops-ai/toggly-fastify',\n fastify: '>=4.0.0',\n})\n\n/**\n * Create a feature gate preHandler hook\n *\n * Protects routes by checking if specified features are enabled\n */\nexport function featureGate(options: FeatureGateOptions): PreHandlerHook {\n const {\n featureKey,\n requirement = 'all',\n negate = false,\n onDisabled,\n redirectTo,\n redirectStatus = 302,\n } = options\n\n const featureKeys = normalizeFeatureKeys(featureKey)\n\n return async (request: FastifyRequest, reply: FastifyReply) => {\n if (!request.toggly) {\n throw new Error('Toggly plugin must be registered before using featureGate')\n }\n\n const isEnabled = await request.toggly.evaluateFeatureGate(featureKeys, requirement, negate)\n\n if (isEnabled) {\n return // Continue to handler\n }\n\n // Feature is disabled\n if (redirectTo) {\n return reply.redirect(redirectTo, redirectStatus)\n }\n\n if (onDisabled) {\n await onDisabled(request)\n return\n }\n\n // Default: 404\n return reply.status(404).send({\n error: 'Not Found',\n message: 'The requested resource is not available',\n })\n }\n}\n\n/**\n * Create preHandler that applies feature gates based on route patterns\n */\nexport function featureRoutes(routes: FeatureRouteOptions[]): PreHandlerHook {\n return async (request: FastifyRequest, reply: FastifyReply) => {\n for (const route of routes) {\n // Check if route matches\n const pathMatches =\n typeof route.path === 'string'\n ? request.url === route.path || request.url.startsWith(route.path)\n : route.path.test(request.url)\n\n if (!pathMatches) {\n continue\n }\n\n // Check if method matches\n if (route.methods && !route.methods.includes(request.method.toUpperCase())) {\n continue\n }\n\n // Apply feature gate\n const gate = featureGate(route)\n return gate(request, reply)\n }\n\n // No matching route, continue\n return\n }\n}\n\n/**\n * Create a decorated route handler that checks features\n */\nexport function withFeature(\n featureKey: string | string[],\n options: Omit<FeatureGateOptions, 'featureKey'> = {}\n): PreHandlerHook {\n return featureGate({ featureKey, ...options })\n}\n\n/**\n * Get features route handler\n */\nexport async function featuresHandler(\n request: FastifyRequest,\n reply: FastifyReply\n): Promise<void> {\n if (!request.toggly) {\n return reply.status(500).send({\n error: 'Internal Server Error',\n message: 'Toggly plugin not configured',\n })\n }\n\n return reply.send({\n features: request.toggly.features,\n identity: request.toggly.identity,\n })\n}\n\n/**\n * Close the Fastify Toggly client\n */\nexport function closeFastifyToggly(): void {\n if (fastifyClient) {\n fastifyClient.close()\n fastifyClient = null\n }\n}\n","// Plugin\nexport {\n togglyPlugin,\n featureGate,\n featureRoutes,\n withFeature,\n featuresHandler,\n getFastifyToggly,\n closeFastifyToggly,\n} from './plugin.js'\n\n// Types\nexport type {\n TogglyFastifyConfig,\n TogglyRequestData,\n FeatureGateOptions,\n FeatureRouteOptions,\n} from './types.js'\n\n// Re-export core types for convenience\nexport type {\n TogglyClient,\n TogglyConfig,\n TogglyServerConfig,\n TogglyState,\n FeatureDefinitions,\n FeatureRequirement,\n EvaluationContext,\n Hook,\n HookMetadata,\n} from '@ops-ai/toggly-node-core'\n\n// Re-export core utilities\nexport {\n createTogglyClient,\n initToggly,\n getToggly,\n useToggly,\n closeToggly,\n createLoggingHook,\n createMemoryCache,\n createFileCache,\n} from '@ops-ai/toggly-node-core'\n"],"mappings":";AAYA,OAAO,QAAQ;AACf;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAQP,IAAI,gBAAqC;AAKlC,SAAS,mBAAwC;AACtD,SAAO;AACT;AAKA,eAAe,gBACb,SACA,QAC6B;AAE7B,MAAI,OAAO,aAAa;AACtB,WAAO,OAAO,YAAY,OAAO;AAAA,EACnC;AAGA,QAAM,iBAAiB,QAAQ,QAAQ,mBAAmB;AAC1D,MAAI,OAAO,mBAAmB,UAAU;AACtC,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAKA,eAAe,eACb,SACA,QAC4B;AAE5B,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IACzB,UAAU,QAAQ,OAAO;AAAA,EAC3B,CAAC;AACH;AAKO,SAAS,qBAA2B;AACzC,MAAI,eAAe;AACjB,kBAAc,MAAM;AACpB,oBAAgB;AAAA,EAClB;AACF;;;AC3NA;AAAA,EACE,sBAAAA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;","names":["createTogglyClient"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ops-ai/toggly-fastify",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Toggly feature flags Fastify plugin",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md",
|
|
24
|
+
"LICENSE"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup",
|
|
28
|
+
"dev": "tsup --watch",
|
|
29
|
+
"test": "vitest run",
|
|
30
|
+
"test:watch": "vitest",
|
|
31
|
+
"test:coverage": "vitest run --coverage",
|
|
32
|
+
"lint": "eslint src tests",
|
|
33
|
+
"typecheck": "tsc --noEmit",
|
|
34
|
+
"clean": "rm -rf dist coverage"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"toggly",
|
|
38
|
+
"feature-flags",
|
|
39
|
+
"feature-toggles",
|
|
40
|
+
"fastify",
|
|
41
|
+
"fastify-plugin",
|
|
42
|
+
"ab-testing",
|
|
43
|
+
"experimentation"
|
|
44
|
+
],
|
|
45
|
+
"author": "ops.ai",
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "https://github.com/ops-ai/Toggly.FeatureManagement.git",
|
|
50
|
+
"directory": "Toggly.FeatureManagement.Node/toggly-fastify"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://toggly.io",
|
|
53
|
+
"bugs": {
|
|
54
|
+
"url": "https://github.com/ops-ai/Toggly.FeatureManagement/issues"
|
|
55
|
+
},
|
|
56
|
+
"engines": {
|
|
57
|
+
"node": ">=18.0.0"
|
|
58
|
+
},
|
|
59
|
+
"peerDependencies": {
|
|
60
|
+
"fastify": "^4.0.0 || ^5.0.0"
|
|
61
|
+
},
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"@ops-ai/toggly-node-core": "workspace:*",
|
|
64
|
+
"fastify-plugin": "^5.0.1"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@types/node": "^22.15.30",
|
|
68
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
69
|
+
"eslint": "^9.28.0",
|
|
70
|
+
"fastify": "^5.4.0",
|
|
71
|
+
"tsup": "^8.5.0",
|
|
72
|
+
"typescript": "^5.8.3",
|
|
73
|
+
"vitest": "^3.2.4"
|
|
74
|
+
}
|
|
75
|
+
}
|