@ops-ai/toggly-hono 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 +229 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +116 -0
- package/dist/index.d.ts +116 -0
- package/dist/index.js +200 -0
- package/dist/index.js.map +1 -0
- package/package.json +76 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
closeHonoToggly: () => closeHonoToggly,
|
|
24
|
+
closeToggly: () => import_toggly_node_core2.closeToggly,
|
|
25
|
+
createFileCache: () => import_toggly_node_core2.createFileCache,
|
|
26
|
+
createLoggingHook: () => import_toggly_node_core2.createLoggingHook,
|
|
27
|
+
createMemoryCache: () => import_toggly_node_core2.createMemoryCache,
|
|
28
|
+
createTogglyClient: () => import_toggly_node_core2.createTogglyClient,
|
|
29
|
+
featureGate: () => featureGate,
|
|
30
|
+
featureRoutes: () => featureRoutes,
|
|
31
|
+
featuresHandler: () => featuresHandler,
|
|
32
|
+
getHonoToggly: () => getHonoToggly,
|
|
33
|
+
getToggly: () => import_toggly_node_core2.getToggly,
|
|
34
|
+
initToggly: () => import_toggly_node_core2.initToggly,
|
|
35
|
+
togglyMiddleware: () => togglyMiddleware,
|
|
36
|
+
useToggly: () => import_toggly_node_core2.useToggly,
|
|
37
|
+
withFeature: () => withFeature
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(index_exports);
|
|
40
|
+
|
|
41
|
+
// src/middleware.ts
|
|
42
|
+
var import_toggly_node_core = require("@ops-ai/toggly-node-core");
|
|
43
|
+
var honoClient = null;
|
|
44
|
+
function getHonoToggly() {
|
|
45
|
+
return honoClient;
|
|
46
|
+
}
|
|
47
|
+
async function extractIdentity(c, config) {
|
|
48
|
+
if (config.getIdentity) {
|
|
49
|
+
return config.getIdentity(c);
|
|
50
|
+
}
|
|
51
|
+
const headerIdentity = c.req.header("x-toggly-identity");
|
|
52
|
+
if (headerIdentity) {
|
|
53
|
+
return headerIdentity;
|
|
54
|
+
}
|
|
55
|
+
return void 0;
|
|
56
|
+
}
|
|
57
|
+
async function extractContext(c, config) {
|
|
58
|
+
if (config.getContext) {
|
|
59
|
+
return config.getContext(c);
|
|
60
|
+
}
|
|
61
|
+
const identity = await extractIdentity(c, config);
|
|
62
|
+
return {
|
|
63
|
+
identity,
|
|
64
|
+
traits: {
|
|
65
|
+
ip: c.req.header("cf-connecting-ip") || c.req.header("x-forwarded-for"),
|
|
66
|
+
userAgent: c.req.header("user-agent"),
|
|
67
|
+
path: c.req.path,
|
|
68
|
+
method: c.req.method
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function togglyMiddleware(config) {
|
|
73
|
+
let initPromise = null;
|
|
74
|
+
const initialize = async () => {
|
|
75
|
+
if (!honoClient) {
|
|
76
|
+
honoClient = (0, import_toggly_node_core.createTogglyClient)(config);
|
|
77
|
+
await honoClient.init();
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
return async (c, next) => {
|
|
81
|
+
if (!initPromise) {
|
|
82
|
+
initPromise = initialize();
|
|
83
|
+
}
|
|
84
|
+
await initPromise;
|
|
85
|
+
if (!honoClient) {
|
|
86
|
+
throw new Error("Toggly client failed to initialize");
|
|
87
|
+
}
|
|
88
|
+
const evalContext = await extractContext(c, config);
|
|
89
|
+
const identity = evalContext.identity;
|
|
90
|
+
c.set("toggly", {
|
|
91
|
+
client: honoClient,
|
|
92
|
+
features: honoClient.state.features,
|
|
93
|
+
identity,
|
|
94
|
+
context: evalContext,
|
|
95
|
+
isFeatureOn: (featureKey) => honoClient.isFeatureOn(featureKey, evalContext),
|
|
96
|
+
isFeatureOff: (featureKey) => honoClient.isFeatureOff(featureKey, evalContext),
|
|
97
|
+
evaluateFeatureGate: (featureKeys, requirement, negate) => honoClient.evaluateFeatureGate(featureKeys, requirement, negate, evalContext)
|
|
98
|
+
});
|
|
99
|
+
await next();
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function featureGate(options) {
|
|
103
|
+
const {
|
|
104
|
+
featureKey,
|
|
105
|
+
requirement = "all",
|
|
106
|
+
negate = false,
|
|
107
|
+
onDisabled,
|
|
108
|
+
redirectTo,
|
|
109
|
+
redirectStatus = 302
|
|
110
|
+
} = options;
|
|
111
|
+
const featureKeys = (0, import_toggly_node_core.normalizeFeatureKeys)(featureKey);
|
|
112
|
+
return async (c, next) => {
|
|
113
|
+
const toggly = c.get("toggly");
|
|
114
|
+
if (!toggly) {
|
|
115
|
+
throw new Error("Toggly middleware must be applied before featureGate");
|
|
116
|
+
}
|
|
117
|
+
const isEnabled = await toggly.evaluateFeatureGate(featureKeys, requirement, negate);
|
|
118
|
+
if (isEnabled) {
|
|
119
|
+
await next();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (redirectTo) {
|
|
123
|
+
return c.redirect(redirectTo, redirectStatus);
|
|
124
|
+
}
|
|
125
|
+
if (onDisabled) {
|
|
126
|
+
return onDisabled(c);
|
|
127
|
+
}
|
|
128
|
+
return c.json(
|
|
129
|
+
{
|
|
130
|
+
error: "Not Found",
|
|
131
|
+
message: "The requested resource is not available"
|
|
132
|
+
},
|
|
133
|
+
404
|
|
134
|
+
);
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function featureRoutes(routes) {
|
|
138
|
+
return async (c, next) => {
|
|
139
|
+
for (const route of routes) {
|
|
140
|
+
const pathMatches = typeof route.path === "string" ? c.req.path === route.path || c.req.path.startsWith(route.path) : route.path.test(c.req.path);
|
|
141
|
+
if (!pathMatches) {
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (route.methods && !route.methods.includes(c.req.method.toUpperCase())) {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
const gate = featureGate(route);
|
|
148
|
+
return gate(c, next);
|
|
149
|
+
}
|
|
150
|
+
await next();
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function withFeature(featureKey, handler, options = {}) {
|
|
154
|
+
const {
|
|
155
|
+
requirement = "all",
|
|
156
|
+
negate = false,
|
|
157
|
+
onDisabled,
|
|
158
|
+
redirectTo,
|
|
159
|
+
redirectStatus = 302
|
|
160
|
+
} = options;
|
|
161
|
+
const featureKeys = (0, import_toggly_node_core.normalizeFeatureKeys)(featureKey);
|
|
162
|
+
return async (c, next) => {
|
|
163
|
+
const toggly = c.get("toggly");
|
|
164
|
+
if (!toggly) {
|
|
165
|
+
throw new Error("Toggly middleware must be applied before withFeature");
|
|
166
|
+
}
|
|
167
|
+
const isEnabled = await toggly.evaluateFeatureGate(featureKeys, requirement, negate);
|
|
168
|
+
if (isEnabled) {
|
|
169
|
+
return handler(c, next);
|
|
170
|
+
}
|
|
171
|
+
if (redirectTo) {
|
|
172
|
+
return c.redirect(redirectTo, redirectStatus);
|
|
173
|
+
}
|
|
174
|
+
if (onDisabled) {
|
|
175
|
+
return onDisabled(c);
|
|
176
|
+
}
|
|
177
|
+
return c.json(
|
|
178
|
+
{
|
|
179
|
+
error: "Not Found",
|
|
180
|
+
message: "The requested resource is not available"
|
|
181
|
+
},
|
|
182
|
+
404
|
|
183
|
+
);
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
var featuresHandler = async (c) => {
|
|
187
|
+
const toggly = c.get("toggly");
|
|
188
|
+
if (!toggly) {
|
|
189
|
+
return c.json(
|
|
190
|
+
{
|
|
191
|
+
error: "Internal Server Error",
|
|
192
|
+
message: "Toggly middleware not configured"
|
|
193
|
+
},
|
|
194
|
+
500
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
return c.json({
|
|
198
|
+
features: toggly.features,
|
|
199
|
+
identity: toggly.identity
|
|
200
|
+
});
|
|
201
|
+
};
|
|
202
|
+
function closeHonoToggly() {
|
|
203
|
+
if (honoClient) {
|
|
204
|
+
honoClient.close();
|
|
205
|
+
honoClient = null;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// src/index.ts
|
|
210
|
+
var import_toggly_node_core2 = require("@ops-ai/toggly-node-core");
|
|
211
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
212
|
+
0 && (module.exports = {
|
|
213
|
+
closeHonoToggly,
|
|
214
|
+
closeToggly,
|
|
215
|
+
createFileCache,
|
|
216
|
+
createLoggingHook,
|
|
217
|
+
createMemoryCache,
|
|
218
|
+
createTogglyClient,
|
|
219
|
+
featureGate,
|
|
220
|
+
featureRoutes,
|
|
221
|
+
featuresHandler,
|
|
222
|
+
getHonoToggly,
|
|
223
|
+
getToggly,
|
|
224
|
+
initToggly,
|
|
225
|
+
togglyMiddleware,
|
|
226
|
+
useToggly,
|
|
227
|
+
withFeature
|
|
228
|
+
});
|
|
229
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/middleware.ts"],"sourcesContent":["// Middleware\nexport {\n togglyMiddleware,\n featureGate,\n featureRoutes,\n withFeature,\n featuresHandler,\n getHonoToggly,\n closeHonoToggly,\n} from './middleware.js'\n\n// Types\nexport type {\n TogglyHonoConfig,\n TogglyContextData,\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 { Context, MiddlewareHandler, Handler } from 'hono'\nimport {\n createTogglyClient,\n normalizeFeatureKeys,\n type TogglyClient,\n type EvaluationContext,\n} from '@ops-ai/toggly-node-core'\nimport type {\n TogglyHonoConfig,\n FeatureGateOptions,\n FeatureRouteOptions,\n} from './types.js'\n\n// Module-level client singleton\nlet honoClient: TogglyClient | null = null\n\n/**\n * Get the Hono Toggly client\n */\nexport function getHonoToggly(): TogglyClient | null {\n return honoClient\n}\n\n/**\n * Get identity from Hono context\n */\nasync function extractIdentity(\n c: Context,\n config: TogglyHonoConfig\n): Promise<string | undefined> {\n // Use custom extractor if provided\n if (config.getIdentity) {\n return config.getIdentity(c)\n }\n\n // Default: check header\n const headerIdentity = c.req.header('x-toggly-identity')\n if (headerIdentity) {\n return headerIdentity\n }\n\n return undefined\n}\n\n/**\n * Get evaluation context from Hono context\n */\nasync function extractContext(\n c: Context,\n config: TogglyHonoConfig\n): Promise<EvaluationContext> {\n // Use custom extractor if provided\n if (config.getContext) {\n return config.getContext(c)\n }\n\n // Default: extract basic context\n const identity = await extractIdentity(c, config)\n\n return {\n identity,\n traits: {\n ip: c.req.header('cf-connecting-ip') || c.req.header('x-forwarded-for'),\n userAgent: c.req.header('user-agent'),\n path: c.req.path,\n method: c.req.method,\n },\n }\n}\n\n/**\n * Create Toggly middleware for Hono\n *\n * This middleware:\n * 1. Initializes the Toggly client (if not already initialized)\n * 2. Attaches feature flag helpers to the context\n * 3. Extracts identity and context from the request\n */\nexport function togglyMiddleware(config: TogglyHonoConfig): MiddlewareHandler {\n // Initialize client lazily\n let initPromise: Promise<void> | null = null\n\n const initialize = async () => {\n if (!honoClient) {\n honoClient = createTogglyClient(config)\n await honoClient.init()\n }\n }\n\n return async (c: Context, next) => {\n // Ensure client is initialized\n if (!initPromise) {\n initPromise = initialize()\n }\n await initPromise\n\n if (!honoClient) {\n throw new Error('Toggly client failed to initialize')\n }\n\n // Extract context from request\n const evalContext = await extractContext(c, config)\n const identity = evalContext.identity\n\n // Attach Toggly helpers to Hono context\n c.set('toggly', {\n client: honoClient,\n features: honoClient.state.features,\n identity,\n context: evalContext,\n isFeatureOn: (featureKey: string) => honoClient!.isFeatureOn(featureKey, evalContext),\n isFeatureOff: (featureKey: string) => honoClient!.isFeatureOff(featureKey, evalContext),\n evaluateFeatureGate: (featureKeys, requirement, negate) =>\n honoClient!.evaluateFeatureGate(featureKeys, requirement, negate, evalContext),\n })\n\n await next()\n }\n}\n\n/**\n * Create a feature gate middleware\n *\n * Protects routes by checking if specified features are enabled\n */\nexport function featureGate(options: FeatureGateOptions): MiddlewareHandler {\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 (c: Context, next) => {\n const toggly = c.get('toggly')\n\n if (!toggly) {\n throw new Error('Toggly middleware must be applied before featureGate')\n }\n\n const isEnabled = await toggly.evaluateFeatureGate(featureKeys, requirement, negate)\n\n if (isEnabled) {\n await next()\n return\n }\n\n // Feature is disabled\n if (redirectTo) {\n return c.redirect(redirectTo, redirectStatus)\n }\n\n if (onDisabled) {\n return onDisabled(c)\n }\n\n // Default: 404\n return c.json(\n {\n error: 'Not Found',\n message: 'The requested resource is not available',\n },\n 404\n )\n }\n}\n\n/**\n * Create middleware that applies feature gates based on route patterns\n */\nexport function featureRoutes(routes: FeatureRouteOptions[]): MiddlewareHandler {\n return async (c: Context, next) => {\n for (const route of routes) {\n // Check if route matches\n const pathMatches =\n typeof route.path === 'string'\n ? c.req.path === route.path || c.req.path.startsWith(route.path)\n : route.path.test(c.req.path)\n\n if (!pathMatches) {\n continue\n }\n\n // Check if method matches\n if (route.methods && !route.methods.includes(c.req.method.toUpperCase())) {\n continue\n }\n\n // Apply feature gate\n const gate = featureGate(route)\n return gate(c, next)\n }\n\n // No matching route, continue\n await next()\n }\n}\n\n/**\n * Create a route handler that only executes if feature is enabled\n */\nexport function withFeature(\n featureKey: string | string[],\n handler: Handler,\n options: Omit<FeatureGateOptions, 'featureKey'> = {}\n): MiddlewareHandler {\n const {\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 (c: Context, next) => {\n const toggly = c.get('toggly')\n\n if (!toggly) {\n throw new Error('Toggly middleware must be applied before withFeature')\n }\n\n const isEnabled = await toggly.evaluateFeatureGate(featureKeys, requirement, negate)\n\n if (isEnabled) {\n // Execute the handler\n return handler(c, next)\n }\n\n // Feature is disabled\n if (redirectTo) {\n return c.redirect(redirectTo, redirectStatus)\n }\n\n if (onDisabled) {\n return onDisabled(c)\n }\n\n // Default: 404\n return c.json(\n {\n error: 'Not Found',\n message: 'The requested resource is not available',\n },\n 404\n )\n }\n}\n\n/**\n * Get features handler for Hono\n */\nexport const featuresHandler: Handler = async (c: Context) => {\n const toggly = c.get('toggly')\n\n if (!toggly) {\n return c.json(\n {\n error: 'Internal Server Error',\n message: 'Toggly middleware not configured',\n },\n 500\n )\n }\n\n return c.json({\n features: toggly.features,\n identity: toggly.identity,\n })\n}\n\n/**\n * Close the Hono Toggly client\n */\nexport function closeHonoToggly(): void {\n if (honoClient) {\n honoClient.close()\n honoClient = null\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,8BAKO;AAQP,IAAI,aAAkC;AAK/B,SAAS,gBAAqC;AACnD,SAAO;AACT;AAKA,eAAe,gBACb,GACA,QAC6B;AAE7B,MAAI,OAAO,aAAa;AACtB,WAAO,OAAO,YAAY,CAAC;AAAA,EAC7B;AAGA,QAAM,iBAAiB,EAAE,IAAI,OAAO,mBAAmB;AACvD,MAAI,gBAAgB;AAClB,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAKA,eAAe,eACb,GACA,QAC4B;AAE5B,MAAI,OAAO,YAAY;AACrB,WAAO,OAAO,WAAW,CAAC;AAAA,EAC5B;AAGA,QAAM,WAAW,MAAM,gBAAgB,GAAG,MAAM;AAEhD,SAAO;AAAA,IACL;AAAA,IACA,QAAQ;AAAA,MACN,IAAI,EAAE,IAAI,OAAO,kBAAkB,KAAK,EAAE,IAAI,OAAO,iBAAiB;AAAA,MACtE,WAAW,EAAE,IAAI,OAAO,YAAY;AAAA,MACpC,MAAM,EAAE,IAAI;AAAA,MACZ,QAAQ,EAAE,IAAI;AAAA,IAChB;AAAA,EACF;AACF;AAUO,SAAS,iBAAiB,QAA6C;AAE5E,MAAI,cAAoC;AAExC,QAAM,aAAa,YAAY;AAC7B,QAAI,CAAC,YAAY;AACf,uBAAa,4CAAmB,MAAM;AACtC,YAAM,WAAW,KAAK;AAAA,IACxB;AAAA,EACF;AAEA,SAAO,OAAO,GAAY,SAAS;AAEjC,QAAI,CAAC,aAAa;AAChB,oBAAc,WAAW;AAAA,IAC3B;AACA,UAAM;AAEN,QAAI,CAAC,YAAY;AACf,YAAM,IAAI,MAAM,oCAAoC;AAAA,IACt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IACZ,UAAU,OAAO;AAAA,IACjB,UAAU,OAAO;AAAA,EACnB,CAAC;AACH;AAKO,SAAS,kBAAwB;AACtC,MAAI,YAAY;AACd,eAAW,MAAM;AACjB,iBAAa;AAAA,EACf;AACF;;;AD3PA,IAAAA,2BASO;","names":["import_toggly_node_core"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { Context, MiddlewareHandler, Handler } from 'hono';
|
|
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
|
+
* Hono-specific configuration
|
|
7
|
+
*/
|
|
8
|
+
interface TogglyHonoConfig extends TogglyServerConfig {
|
|
9
|
+
/**
|
|
10
|
+
* Function to extract identity from Hono context
|
|
11
|
+
* Default: Uses x-toggly-identity header
|
|
12
|
+
*/
|
|
13
|
+
getIdentity?: (c: Context) => string | undefined | Promise<string | undefined>;
|
|
14
|
+
/**
|
|
15
|
+
* Function to extract evaluation context from Hono context
|
|
16
|
+
*/
|
|
17
|
+
getContext?: (c: Context) => EvaluationContext | Promise<EvaluationContext>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Toggly data stored in Hono context variables
|
|
21
|
+
*/
|
|
22
|
+
interface TogglyContextData {
|
|
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?: (c: Context) => Response | Promise<Response>;
|
|
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 'hono' {
|
|
75
|
+
interface ContextVariableMap {
|
|
76
|
+
toggly: TogglyContextData;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get the Hono Toggly client
|
|
82
|
+
*/
|
|
83
|
+
declare function getHonoToggly(): TogglyClient | null;
|
|
84
|
+
/**
|
|
85
|
+
* Create Toggly middleware for Hono
|
|
86
|
+
*
|
|
87
|
+
* This middleware:
|
|
88
|
+
* 1. Initializes the Toggly client (if not already initialized)
|
|
89
|
+
* 2. Attaches feature flag helpers to the context
|
|
90
|
+
* 3. Extracts identity and context from the request
|
|
91
|
+
*/
|
|
92
|
+
declare function togglyMiddleware(config: TogglyHonoConfig): MiddlewareHandler;
|
|
93
|
+
/**
|
|
94
|
+
* Create a feature gate middleware
|
|
95
|
+
*
|
|
96
|
+
* Protects routes by checking if specified features are enabled
|
|
97
|
+
*/
|
|
98
|
+
declare function featureGate(options: FeatureGateOptions): MiddlewareHandler;
|
|
99
|
+
/**
|
|
100
|
+
* Create middleware that applies feature gates based on route patterns
|
|
101
|
+
*/
|
|
102
|
+
declare function featureRoutes(routes: FeatureRouteOptions[]): MiddlewareHandler;
|
|
103
|
+
/**
|
|
104
|
+
* Create a route handler that only executes if feature is enabled
|
|
105
|
+
*/
|
|
106
|
+
declare function withFeature(featureKey: string | string[], handler: Handler, options?: Omit<FeatureGateOptions, 'featureKey'>): MiddlewareHandler;
|
|
107
|
+
/**
|
|
108
|
+
* Get features handler for Hono
|
|
109
|
+
*/
|
|
110
|
+
declare const featuresHandler: Handler;
|
|
111
|
+
/**
|
|
112
|
+
* Close the Hono Toggly client
|
|
113
|
+
*/
|
|
114
|
+
declare function closeHonoToggly(): void;
|
|
115
|
+
|
|
116
|
+
export { type FeatureGateOptions, type FeatureRouteOptions, type TogglyContextData, type TogglyHonoConfig, closeHonoToggly, featureGate, featureRoutes, featuresHandler, getHonoToggly, togglyMiddleware, withFeature };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { Context, MiddlewareHandler, Handler } from 'hono';
|
|
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
|
+
* Hono-specific configuration
|
|
7
|
+
*/
|
|
8
|
+
interface TogglyHonoConfig extends TogglyServerConfig {
|
|
9
|
+
/**
|
|
10
|
+
* Function to extract identity from Hono context
|
|
11
|
+
* Default: Uses x-toggly-identity header
|
|
12
|
+
*/
|
|
13
|
+
getIdentity?: (c: Context) => string | undefined | Promise<string | undefined>;
|
|
14
|
+
/**
|
|
15
|
+
* Function to extract evaluation context from Hono context
|
|
16
|
+
*/
|
|
17
|
+
getContext?: (c: Context) => EvaluationContext | Promise<EvaluationContext>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Toggly data stored in Hono context variables
|
|
21
|
+
*/
|
|
22
|
+
interface TogglyContextData {
|
|
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?: (c: Context) => Response | Promise<Response>;
|
|
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 'hono' {
|
|
75
|
+
interface ContextVariableMap {
|
|
76
|
+
toggly: TogglyContextData;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get the Hono Toggly client
|
|
82
|
+
*/
|
|
83
|
+
declare function getHonoToggly(): TogglyClient | null;
|
|
84
|
+
/**
|
|
85
|
+
* Create Toggly middleware for Hono
|
|
86
|
+
*
|
|
87
|
+
* This middleware:
|
|
88
|
+
* 1. Initializes the Toggly client (if not already initialized)
|
|
89
|
+
* 2. Attaches feature flag helpers to the context
|
|
90
|
+
* 3. Extracts identity and context from the request
|
|
91
|
+
*/
|
|
92
|
+
declare function togglyMiddleware(config: TogglyHonoConfig): MiddlewareHandler;
|
|
93
|
+
/**
|
|
94
|
+
* Create a feature gate middleware
|
|
95
|
+
*
|
|
96
|
+
* Protects routes by checking if specified features are enabled
|
|
97
|
+
*/
|
|
98
|
+
declare function featureGate(options: FeatureGateOptions): MiddlewareHandler;
|
|
99
|
+
/**
|
|
100
|
+
* Create middleware that applies feature gates based on route patterns
|
|
101
|
+
*/
|
|
102
|
+
declare function featureRoutes(routes: FeatureRouteOptions[]): MiddlewareHandler;
|
|
103
|
+
/**
|
|
104
|
+
* Create a route handler that only executes if feature is enabled
|
|
105
|
+
*/
|
|
106
|
+
declare function withFeature(featureKey: string | string[], handler: Handler, options?: Omit<FeatureGateOptions, 'featureKey'>): MiddlewareHandler;
|
|
107
|
+
/**
|
|
108
|
+
* Get features handler for Hono
|
|
109
|
+
*/
|
|
110
|
+
declare const featuresHandler: Handler;
|
|
111
|
+
/**
|
|
112
|
+
* Close the Hono Toggly client
|
|
113
|
+
*/
|
|
114
|
+
declare function closeHonoToggly(): void;
|
|
115
|
+
|
|
116
|
+
export { type FeatureGateOptions, type FeatureRouteOptions, type TogglyContextData, type TogglyHonoConfig, closeHonoToggly, featureGate, featureRoutes, featuresHandler, getHonoToggly, togglyMiddleware, withFeature };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// src/middleware.ts
|
|
2
|
+
import {
|
|
3
|
+
createTogglyClient,
|
|
4
|
+
normalizeFeatureKeys
|
|
5
|
+
} from "@ops-ai/toggly-node-core";
|
|
6
|
+
var honoClient = null;
|
|
7
|
+
function getHonoToggly() {
|
|
8
|
+
return honoClient;
|
|
9
|
+
}
|
|
10
|
+
async function extractIdentity(c, config) {
|
|
11
|
+
if (config.getIdentity) {
|
|
12
|
+
return config.getIdentity(c);
|
|
13
|
+
}
|
|
14
|
+
const headerIdentity = c.req.header("x-toggly-identity");
|
|
15
|
+
if (headerIdentity) {
|
|
16
|
+
return headerIdentity;
|
|
17
|
+
}
|
|
18
|
+
return void 0;
|
|
19
|
+
}
|
|
20
|
+
async function extractContext(c, config) {
|
|
21
|
+
if (config.getContext) {
|
|
22
|
+
return config.getContext(c);
|
|
23
|
+
}
|
|
24
|
+
const identity = await extractIdentity(c, config);
|
|
25
|
+
return {
|
|
26
|
+
identity,
|
|
27
|
+
traits: {
|
|
28
|
+
ip: c.req.header("cf-connecting-ip") || c.req.header("x-forwarded-for"),
|
|
29
|
+
userAgent: c.req.header("user-agent"),
|
|
30
|
+
path: c.req.path,
|
|
31
|
+
method: c.req.method
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function togglyMiddleware(config) {
|
|
36
|
+
let initPromise = null;
|
|
37
|
+
const initialize = async () => {
|
|
38
|
+
if (!honoClient) {
|
|
39
|
+
honoClient = createTogglyClient(config);
|
|
40
|
+
await honoClient.init();
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
return async (c, next) => {
|
|
44
|
+
if (!initPromise) {
|
|
45
|
+
initPromise = initialize();
|
|
46
|
+
}
|
|
47
|
+
await initPromise;
|
|
48
|
+
if (!honoClient) {
|
|
49
|
+
throw new Error("Toggly client failed to initialize");
|
|
50
|
+
}
|
|
51
|
+
const evalContext = await extractContext(c, config);
|
|
52
|
+
const identity = evalContext.identity;
|
|
53
|
+
c.set("toggly", {
|
|
54
|
+
client: honoClient,
|
|
55
|
+
features: honoClient.state.features,
|
|
56
|
+
identity,
|
|
57
|
+
context: evalContext,
|
|
58
|
+
isFeatureOn: (featureKey) => honoClient.isFeatureOn(featureKey, evalContext),
|
|
59
|
+
isFeatureOff: (featureKey) => honoClient.isFeatureOff(featureKey, evalContext),
|
|
60
|
+
evaluateFeatureGate: (featureKeys, requirement, negate) => honoClient.evaluateFeatureGate(featureKeys, requirement, negate, evalContext)
|
|
61
|
+
});
|
|
62
|
+
await next();
|
|
63
|
+
};
|
|
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 (c, next) => {
|
|
76
|
+
const toggly = c.get("toggly");
|
|
77
|
+
if (!toggly) {
|
|
78
|
+
throw new Error("Toggly middleware must be applied before featureGate");
|
|
79
|
+
}
|
|
80
|
+
const isEnabled = await toggly.evaluateFeatureGate(featureKeys, requirement, negate);
|
|
81
|
+
if (isEnabled) {
|
|
82
|
+
await next();
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (redirectTo) {
|
|
86
|
+
return c.redirect(redirectTo, redirectStatus);
|
|
87
|
+
}
|
|
88
|
+
if (onDisabled) {
|
|
89
|
+
return onDisabled(c);
|
|
90
|
+
}
|
|
91
|
+
return c.json(
|
|
92
|
+
{
|
|
93
|
+
error: "Not Found",
|
|
94
|
+
message: "The requested resource is not available"
|
|
95
|
+
},
|
|
96
|
+
404
|
|
97
|
+
);
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function featureRoutes(routes) {
|
|
101
|
+
return async (c, next) => {
|
|
102
|
+
for (const route of routes) {
|
|
103
|
+
const pathMatches = typeof route.path === "string" ? c.req.path === route.path || c.req.path.startsWith(route.path) : route.path.test(c.req.path);
|
|
104
|
+
if (!pathMatches) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (route.methods && !route.methods.includes(c.req.method.toUpperCase())) {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const gate = featureGate(route);
|
|
111
|
+
return gate(c, next);
|
|
112
|
+
}
|
|
113
|
+
await next();
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function withFeature(featureKey, handler, options = {}) {
|
|
117
|
+
const {
|
|
118
|
+
requirement = "all",
|
|
119
|
+
negate = false,
|
|
120
|
+
onDisabled,
|
|
121
|
+
redirectTo,
|
|
122
|
+
redirectStatus = 302
|
|
123
|
+
} = options;
|
|
124
|
+
const featureKeys = normalizeFeatureKeys(featureKey);
|
|
125
|
+
return async (c, next) => {
|
|
126
|
+
const toggly = c.get("toggly");
|
|
127
|
+
if (!toggly) {
|
|
128
|
+
throw new Error("Toggly middleware must be applied before withFeature");
|
|
129
|
+
}
|
|
130
|
+
const isEnabled = await toggly.evaluateFeatureGate(featureKeys, requirement, negate);
|
|
131
|
+
if (isEnabled) {
|
|
132
|
+
return handler(c, next);
|
|
133
|
+
}
|
|
134
|
+
if (redirectTo) {
|
|
135
|
+
return c.redirect(redirectTo, redirectStatus);
|
|
136
|
+
}
|
|
137
|
+
if (onDisabled) {
|
|
138
|
+
return onDisabled(c);
|
|
139
|
+
}
|
|
140
|
+
return c.json(
|
|
141
|
+
{
|
|
142
|
+
error: "Not Found",
|
|
143
|
+
message: "The requested resource is not available"
|
|
144
|
+
},
|
|
145
|
+
404
|
|
146
|
+
);
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
var featuresHandler = async (c) => {
|
|
150
|
+
const toggly = c.get("toggly");
|
|
151
|
+
if (!toggly) {
|
|
152
|
+
return c.json(
|
|
153
|
+
{
|
|
154
|
+
error: "Internal Server Error",
|
|
155
|
+
message: "Toggly middleware not configured"
|
|
156
|
+
},
|
|
157
|
+
500
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
return c.json({
|
|
161
|
+
features: toggly.features,
|
|
162
|
+
identity: toggly.identity
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
function closeHonoToggly() {
|
|
166
|
+
if (honoClient) {
|
|
167
|
+
honoClient.close();
|
|
168
|
+
honoClient = null;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// src/index.ts
|
|
173
|
+
import {
|
|
174
|
+
createTogglyClient as createTogglyClient2,
|
|
175
|
+
initToggly,
|
|
176
|
+
getToggly,
|
|
177
|
+
useToggly,
|
|
178
|
+
closeToggly,
|
|
179
|
+
createLoggingHook,
|
|
180
|
+
createMemoryCache,
|
|
181
|
+
createFileCache
|
|
182
|
+
} from "@ops-ai/toggly-node-core";
|
|
183
|
+
export {
|
|
184
|
+
closeHonoToggly,
|
|
185
|
+
closeToggly,
|
|
186
|
+
createFileCache,
|
|
187
|
+
createLoggingHook,
|
|
188
|
+
createMemoryCache,
|
|
189
|
+
createTogglyClient2 as createTogglyClient,
|
|
190
|
+
featureGate,
|
|
191
|
+
featureRoutes,
|
|
192
|
+
featuresHandler,
|
|
193
|
+
getHonoToggly,
|
|
194
|
+
getToggly,
|
|
195
|
+
initToggly,
|
|
196
|
+
togglyMiddleware,
|
|
197
|
+
useToggly,
|
|
198
|
+
withFeature
|
|
199
|
+
};
|
|
200
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/middleware.ts","../src/index.ts"],"sourcesContent":["import type { Context, MiddlewareHandler, Handler } from 'hono'\nimport {\n createTogglyClient,\n normalizeFeatureKeys,\n type TogglyClient,\n type EvaluationContext,\n} from '@ops-ai/toggly-node-core'\nimport type {\n TogglyHonoConfig,\n FeatureGateOptions,\n FeatureRouteOptions,\n} from './types.js'\n\n// Module-level client singleton\nlet honoClient: TogglyClient | null = null\n\n/**\n * Get the Hono Toggly client\n */\nexport function getHonoToggly(): TogglyClient | null {\n return honoClient\n}\n\n/**\n * Get identity from Hono context\n */\nasync function extractIdentity(\n c: Context,\n config: TogglyHonoConfig\n): Promise<string | undefined> {\n // Use custom extractor if provided\n if (config.getIdentity) {\n return config.getIdentity(c)\n }\n\n // Default: check header\n const headerIdentity = c.req.header('x-toggly-identity')\n if (headerIdentity) {\n return headerIdentity\n }\n\n return undefined\n}\n\n/**\n * Get evaluation context from Hono context\n */\nasync function extractContext(\n c: Context,\n config: TogglyHonoConfig\n): Promise<EvaluationContext> {\n // Use custom extractor if provided\n if (config.getContext) {\n return config.getContext(c)\n }\n\n // Default: extract basic context\n const identity = await extractIdentity(c, config)\n\n return {\n identity,\n traits: {\n ip: c.req.header('cf-connecting-ip') || c.req.header('x-forwarded-for'),\n userAgent: c.req.header('user-agent'),\n path: c.req.path,\n method: c.req.method,\n },\n }\n}\n\n/**\n * Create Toggly middleware for Hono\n *\n * This middleware:\n * 1. Initializes the Toggly client (if not already initialized)\n * 2. Attaches feature flag helpers to the context\n * 3. Extracts identity and context from the request\n */\nexport function togglyMiddleware(config: TogglyHonoConfig): MiddlewareHandler {\n // Initialize client lazily\n let initPromise: Promise<void> | null = null\n\n const initialize = async () => {\n if (!honoClient) {\n honoClient = createTogglyClient(config)\n await honoClient.init()\n }\n }\n\n return async (c: Context, next) => {\n // Ensure client is initialized\n if (!initPromise) {\n initPromise = initialize()\n }\n await initPromise\n\n if (!honoClient) {\n throw new Error('Toggly client failed to initialize')\n }\n\n // Extract context from request\n const evalContext = await extractContext(c, config)\n const identity = evalContext.identity\n\n // Attach Toggly helpers to Hono context\n c.set('toggly', {\n client: honoClient,\n features: honoClient.state.features,\n identity,\n context: evalContext,\n isFeatureOn: (featureKey: string) => honoClient!.isFeatureOn(featureKey, evalContext),\n isFeatureOff: (featureKey: string) => honoClient!.isFeatureOff(featureKey, evalContext),\n evaluateFeatureGate: (featureKeys, requirement, negate) =>\n honoClient!.evaluateFeatureGate(featureKeys, requirement, negate, evalContext),\n })\n\n await next()\n }\n}\n\n/**\n * Create a feature gate middleware\n *\n * Protects routes by checking if specified features are enabled\n */\nexport function featureGate(options: FeatureGateOptions): MiddlewareHandler {\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 (c: Context, next) => {\n const toggly = c.get('toggly')\n\n if (!toggly) {\n throw new Error('Toggly middleware must be applied before featureGate')\n }\n\n const isEnabled = await toggly.evaluateFeatureGate(featureKeys, requirement, negate)\n\n if (isEnabled) {\n await next()\n return\n }\n\n // Feature is disabled\n if (redirectTo) {\n return c.redirect(redirectTo, redirectStatus)\n }\n\n if (onDisabled) {\n return onDisabled(c)\n }\n\n // Default: 404\n return c.json(\n {\n error: 'Not Found',\n message: 'The requested resource is not available',\n },\n 404\n )\n }\n}\n\n/**\n * Create middleware that applies feature gates based on route patterns\n */\nexport function featureRoutes(routes: FeatureRouteOptions[]): MiddlewareHandler {\n return async (c: Context, next) => {\n for (const route of routes) {\n // Check if route matches\n const pathMatches =\n typeof route.path === 'string'\n ? c.req.path === route.path || c.req.path.startsWith(route.path)\n : route.path.test(c.req.path)\n\n if (!pathMatches) {\n continue\n }\n\n // Check if method matches\n if (route.methods && !route.methods.includes(c.req.method.toUpperCase())) {\n continue\n }\n\n // Apply feature gate\n const gate = featureGate(route)\n return gate(c, next)\n }\n\n // No matching route, continue\n await next()\n }\n}\n\n/**\n * Create a route handler that only executes if feature is enabled\n */\nexport function withFeature(\n featureKey: string | string[],\n handler: Handler,\n options: Omit<FeatureGateOptions, 'featureKey'> = {}\n): MiddlewareHandler {\n const {\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 (c: Context, next) => {\n const toggly = c.get('toggly')\n\n if (!toggly) {\n throw new Error('Toggly middleware must be applied before withFeature')\n }\n\n const isEnabled = await toggly.evaluateFeatureGate(featureKeys, requirement, negate)\n\n if (isEnabled) {\n // Execute the handler\n return handler(c, next)\n }\n\n // Feature is disabled\n if (redirectTo) {\n return c.redirect(redirectTo, redirectStatus)\n }\n\n if (onDisabled) {\n return onDisabled(c)\n }\n\n // Default: 404\n return c.json(\n {\n error: 'Not Found',\n message: 'The requested resource is not available',\n },\n 404\n )\n }\n}\n\n/**\n * Get features handler for Hono\n */\nexport const featuresHandler: Handler = async (c: Context) => {\n const toggly = c.get('toggly')\n\n if (!toggly) {\n return c.json(\n {\n error: 'Internal Server Error',\n message: 'Toggly middleware not configured',\n },\n 500\n )\n }\n\n return c.json({\n features: toggly.features,\n identity: toggly.identity,\n })\n}\n\n/**\n * Close the Hono Toggly client\n */\nexport function closeHonoToggly(): void {\n if (honoClient) {\n honoClient.close()\n honoClient = null\n }\n}\n","// Middleware\nexport {\n togglyMiddleware,\n featureGate,\n featureRoutes,\n withFeature,\n featuresHandler,\n getHonoToggly,\n closeHonoToggly,\n} from './middleware.js'\n\n// Types\nexport type {\n TogglyHonoConfig,\n TogglyContextData,\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":";AACA;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAQP,IAAI,aAAkC;AAK/B,SAAS,gBAAqC;AACnD,SAAO;AACT;AAKA,eAAe,gBACb,GACA,QAC6B;AAE7B,MAAI,OAAO,aAAa;AACtB,WAAO,OAAO,YAAY,CAAC;AAAA,EAC7B;AAGA,QAAM,iBAAiB,EAAE,IAAI,OAAO,mBAAmB;AACvD,MAAI,gBAAgB;AAClB,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAKA,eAAe,eACb,GACA,QAC4B;AAE5B,MAAI,OAAO,YAAY;AACrB,WAAO,OAAO,WAAW,CAAC;AAAA,EAC5B;AAGA,QAAM,WAAW,MAAM,gBAAgB,GAAG,MAAM;AAEhD,SAAO;AAAA,IACL;AAAA,IACA,QAAQ;AAAA,MACN,IAAI,EAAE,IAAI,OAAO,kBAAkB,KAAK,EAAE,IAAI,OAAO,iBAAiB;AAAA,MACtE,WAAW,EAAE,IAAI,OAAO,YAAY;AAAA,MACpC,MAAM,EAAE,IAAI;AAAA,MACZ,QAAQ,EAAE,IAAI;AAAA,IAChB;AAAA,EACF;AACF;AAUO,SAAS,iBAAiB,QAA6C;AAE5E,MAAI,cAAoC;AAExC,QAAM,aAAa,YAAY;AAC7B,QAAI,CAAC,YAAY;A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;AAKO,IAAM,kBAA2B,OAAO,MAAe;AAC5D,QAAM,SAAS,EAAE,IAAI,QAAQ;AAE7B,MAAI,CAAC,QAAQ;AACX,WAAO,EAAE;AAAA,MACP;AAAA,QACE,OAAO;AAAA,QACP,SAAS;AAAA,MACX;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,SAAO,EAAE,KAAK;AAAA,IACZ,UAAU,OAAO;AAAA,IACjB,UAAU,OAAO;AAAA,EACnB,CAAC;AACH;AAKO,SAAS,kBAAwB;AACtC,MAAI,YAAY;AACd,eAAW,MAAM;AACjB,iBAAa;AAAA,EACf;AACF;;;AC3PA;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,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ops-ai/toggly-hono",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Toggly feature flags Hono middleware",
|
|
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
|
+
"hono",
|
|
41
|
+
"hono-middleware",
|
|
42
|
+
"edge",
|
|
43
|
+
"cloudflare-workers",
|
|
44
|
+
"ab-testing",
|
|
45
|
+
"experimentation"
|
|
46
|
+
],
|
|
47
|
+
"author": "ops.ai",
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "https://github.com/ops-ai/Toggly.FeatureManagement.git",
|
|
52
|
+
"directory": "Toggly.FeatureManagement.Node/toggly-hono"
|
|
53
|
+
},
|
|
54
|
+
"homepage": "https://toggly.io",
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/ops-ai/Toggly.FeatureManagement/issues"
|
|
57
|
+
},
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": ">=18.0.0"
|
|
60
|
+
},
|
|
61
|
+
"peerDependencies": {
|
|
62
|
+
"hono": "^4.0.0"
|
|
63
|
+
},
|
|
64
|
+
"dependencies": {
|
|
65
|
+
"@ops-ai/toggly-node-core": "workspace:*"
|
|
66
|
+
},
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"@types/node": "^22.15.30",
|
|
69
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
70
|
+
"eslint": "^9.28.0",
|
|
71
|
+
"hono": "^4.7.11",
|
|
72
|
+
"tsup": "^8.5.0",
|
|
73
|
+
"typescript": "^5.8.3",
|
|
74
|
+
"vitest": "^3.2.4"
|
|
75
|
+
}
|
|
76
|
+
}
|