Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(arcjet): Infer types when no detect function is specified #1446

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions arcjet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,38 +336,39 @@ type DetectSensitiveInfoEntities<T> = (
tokens: string[],
) => Array<ArcjetSensitiveInfoType | T | undefined>;

type SensitiveInfoOptionsAllow<
Detect extends DetectSensitiveInfoEntities<CustomEntities>,
CustomEntities extends string,
> = {
allow: Array<
ArcjetSensitiveInfoType | Exclude<ReturnType<Detect>[number], undefined>
>;
type ValidEntities<Detect> = 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<infer CustomEntities>
? ArcjetSensitiveInfoType | CustomEntities
: never
>;

type SensitiveInfoOptionsAllow<Detect> = {
allow: ValidEntities<Detect>;
deny?: never;
contextWindowSize?: number;
mode?: ArcjetMode;
detect?: Detect;
};

type SensitiveInfoOptionsDeny<
Detect extends DetectSensitiveInfoEntities<CustomEntities>,
CustomEntities extends string,
> = {
type SensitiveInfoOptionsDeny<Detect> = {
allow?: never;
deny: Array<
ArcjetSensitiveInfoType | Exclude<ReturnType<Detect>[number], undefined>
>;
deny: ValidEntities<Detect>;
contextWindowSize?: number;
mode?: ArcjetMode;
detect?: Detect;
};

export type SensitiveInfoOptions<
Detect extends DetectSensitiveInfoEntities<CustomEntities>,
CustomEntities extends string,
> =
| SensitiveInfoOptionsAllow<Detect, CustomEntities>
| SensitiveInfoOptionsDeny<Detect, CustomEntities>;
export type SensitiveInfoOptions<Detect> =
| SensitiveInfoOptionsAllow<Detect>
| SensitiveInfoOptionsDeny<Detect>;

const Priority = {
SensitiveInfo: 1,
Expand Down Expand Up @@ -652,11 +653,11 @@ function convertAnalyzeDetectedSensitiveInfoEntity(
}

export function sensitiveInfo<
const Detect extends DetectSensitiveInfoEntities<CustomEntities>,
const Detect extends DetectSensitiveInfoEntities<CustomEntities> | undefined,
const CustomEntities extends string,
>(
options: SensitiveInfoOptions<Detect, CustomEntities>,
...additionalOptions: SensitiveInfoOptions<Detect, CustomEntities>[]
options: SensitiveInfoOptions<Detect>,
...additionalOptions: SensitiveInfoOptions<Detect>[]
): Primitive<{}> {
const rules: ArcjetSensitiveInfoRule<{}>[] = [];

Expand Down
4 changes: 2 additions & 2 deletions arcjet/test/index.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down