onion-ai 1.0.2 → 1.0.3

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.
@@ -10,8 +10,14 @@ type MiddlewareNext = () => Promise<void> | void;
10
10
  * @example
11
11
  * app.use(onionRing(new OnionAI(), { promptField: 'body.query' }));
12
12
  */
13
+ interface MinimalRequest extends Record<string, any> {
14
+ body?: any;
15
+ query?: any;
16
+ params?: any;
17
+ onionThreats?: string[];
18
+ }
13
19
  export declare function onionRing(onion: OnionAI, options?: {
14
20
  promptField?: string;
15
21
  outputField?: string;
16
- }): (req: any, res: any, next: MiddlewareNext) => Promise<void>;
22
+ }): <T extends MinimalRequest>(req: T, res: unknown, next: MiddlewareNext) => Promise<void>;
17
23
  export {};
@@ -1,18 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.onionRing = onionRing;
4
- /**
5
- * Creates an Express/Connect style middleware for OnionAI.
6
- *
7
- * @param onion - The OnionAI instance
8
- * @param options - Configuration for mapping request body fields
9
- * @returns Middleware function
10
- *
11
- * @example
12
- * app.use(onionRing(new OnionAI(), { promptField: 'body.query' }));
13
- */
14
4
  function onionRing(onion, options = {}) {
15
5
  const promptPath = options.promptField || 'body.prompt';
6
+ // Using generic T for Request to allow users to pass their own types if needed, defaulting to MinimalRequest
16
7
  return async (req, res, next) => {
17
8
  try {
18
9
  // 1. Resolve prompt from request
@@ -24,7 +15,7 @@ function onionRing(onion, options = {}) {
24
15
  });
25
16
  // 2. Replace the prompt in the request body with the sanitized version
26
17
  setNestedValue(req, promptPath, safePrompt);
27
- if (!safePrompt && req.onionThreats?.length > 0) {
18
+ if (!safePrompt && req.onionThreats && req.onionThreats.length > 0) {
28
19
  // Option: Block request entirely if heavily compromised?
29
20
  // For now, we pass the empty/sanitized string.
30
21
  // Users can check req.onionThreats to decide to 400.
@@ -40,12 +31,12 @@ function onionRing(onion, options = {}) {
40
31
  }
41
32
  // Helpers
42
33
  function getNestedValue(obj, path) {
43
- return path.split('.').reduce((acc, part) => acc && acc[part], obj);
34
+ return path.split('.').reduce((acc, part) => (acc && typeof acc === 'object' ? acc[part] : undefined), obj);
44
35
  }
45
36
  function setNestedValue(obj, path, value) {
46
37
  const parts = path.split('.');
47
38
  const last = parts.pop();
48
- const target = parts.reduce((acc, part) => acc && acc[part], obj);
39
+ const target = parts.reduce((acc, part) => (acc && typeof acc === 'object' ? acc[part] : undefined), obj);
49
40
  if (target && last) {
50
41
  target[last] = value;
51
42
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onion-ai",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Layered security for AI prompting - input sanitization, injection protection, and output validation.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -33,22 +33,15 @@
33
33
  "author": "Himanshu Mamgain",
34
34
  "license": "MIT",
35
35
  "dependencies": {
36
- "dompurify": "^3.0.6",
37
- "jsdom": "^22.1.0",
38
36
  "validator": "^13.11.0",
39
37
  "xss": "^1.0.14",
40
- "xss-filters": "^1.2.7",
41
38
  "zod": "^3.22.4"
42
39
  },
43
40
  "devDependencies": {
44
- "@ai-sdk/provider": "^3.0.1",
45
- "@types/dompurify": "^3.0.5",
46
41
  "@types/jest": "^30.0.0",
47
- "@types/jsdom": "^21.1.6",
48
42
  "@types/node": "^20.10.5",
49
43
  "@types/validator": "^13.11.7",
50
44
  "jest": "^30.2.0",
51
- "openai": "^6.15.0",
52
45
  "ts-jest": "^29.4.6",
53
46
  "ts-node": "^10.9.2",
54
47
  "typescript": "^5.3.3"