diff --git a/arcjet/index.ts b/arcjet/index.ts index 7badf4d0c..063d4c9d5 100644 --- a/arcjet/index.ts +++ b/arcjet/index.ts @@ -336,38 +336,39 @@ type DetectSensitiveInfoEntities = ( tokens: string[], ) => Array; -type SensitiveInfoOptionsAllow< - Detect extends DetectSensitiveInfoEntities, - CustomEntities extends string, -> = { - allow: Array< - ArcjetSensitiveInfoType | Exclude[number], undefined> - >; +type ValidEntities = Array< + // Via https://www.reddit.com/r/typescript/comments/17up72w/comment/k958cb0/ + // Conditional types distribute over unions. If you have ((string | undefined) + // extends undefined ? 1 : 0) it is evaluated separately for each member of + // the union, then union-ed together again. The result is (string extends + // undefined ? 1 : 0) | (undefined extends undefined ? 1 : 0) which simplifies + // to 0 | 1 + undefined extends Detect + ? ArcjetSensitiveInfoType + : Detect extends DetectSensitiveInfoEntities + ? ArcjetSensitiveInfoType | CustomEntities + : never +>; + +type SensitiveInfoOptionsAllow = { + allow: ValidEntities; deny?: never; contextWindowSize?: number; mode?: ArcjetMode; detect?: Detect; }; -type SensitiveInfoOptionsDeny< - Detect extends DetectSensitiveInfoEntities, - CustomEntities extends string, -> = { +type SensitiveInfoOptionsDeny = { allow?: never; - deny: Array< - ArcjetSensitiveInfoType | Exclude[number], undefined> - >; + deny: ValidEntities; contextWindowSize?: number; mode?: ArcjetMode; detect?: Detect; }; -export type SensitiveInfoOptions< - Detect extends DetectSensitiveInfoEntities, - CustomEntities extends string, -> = - | SensitiveInfoOptionsAllow - | SensitiveInfoOptionsDeny; +export type SensitiveInfoOptions = + | SensitiveInfoOptionsAllow + | SensitiveInfoOptionsDeny; const Priority = { SensitiveInfo: 1, @@ -652,11 +653,11 @@ function convertAnalyzeDetectedSensitiveInfoEntity( } export function sensitiveInfo< - const Detect extends DetectSensitiveInfoEntities, + const Detect extends DetectSensitiveInfoEntities | undefined, const CustomEntities extends string, >( - options: SensitiveInfoOptions, - ...additionalOptions: SensitiveInfoOptions[] + options: SensitiveInfoOptions, + ...additionalOptions: SensitiveInfoOptions[] ): Primitive<{}> { const rules: ArcjetSensitiveInfoRule<{}>[] = []; diff --git a/arcjet/test/index.node.test.ts b/arcjet/test/index.node.test.ts index e867af54e..5279be9b5 100644 --- a/arcjet/test/index.node.test.ts +++ b/arcjet/test/index.node.test.ts @@ -4202,12 +4202,12 @@ describe("SDK", () => { const customDetect = (tokens: string[]) => { expect(tokens).toHaveLength(3); - return new Array(tokens.length).fill(undefined); + return tokens.map(() => undefined); }; const [rule] = sensitiveInfo({ mode: "LIVE", - allow: ["custom"], + allow: [], detect: customDetect, contextWindowSize: 3, });