Skip to content

Commit 2380307

Browse files
refactor(redact): reuse exposed types (#4856)
Some of the types from `@arcjet/redact-wasm` are not used anywhere. Other ones were redefined in `@arcjet/redact` itself. This commit removes the duplication and removes the (completely internal) unused API surface from `@arcjet/redact-wasm`. Some of the exposed API surface did not specify return types either. This adds return types so that we can verify that the API matches what we want instead of returning arbitrary objects (with `const`s).
1 parent 13bac85 commit 2380307

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

redact-wasm/index.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { instantiate } from "./wasm/arcjet_analyze_bindings_redact.component.js";
2-
import type {
3-
ImportObject,
4-
RedactedSensitiveInfoEntity,
5-
RedactSensitiveInfoConfig,
6-
SensitiveInfoEntity,
7-
} from "./wasm/arcjet_analyze_bindings_redact.component.js";
2+
import type { ImportObject } from "./wasm/arcjet_analyze_bindings_redact.component.js";
83
import type { ArcjetRedactCustomRedact } from "./wasm/interfaces/arcjet-redact-custom-redact.js";
94

105
import { wasm as componentCoreWasm } from "./wasm/arcjet_analyze_bindings_redact.component.core.wasm?js";
@@ -66,7 +61,7 @@ export async function initializeWasm(
6661
* @returns
6762
* Array of detected entities.
6863
*/
69-
export type CustomDetect = typeof ArcjetRedactCustomRedact.detectSensitiveInfo;
64+
type CustomDetect = typeof ArcjetRedactCustomRedact.detectSensitiveInfo;
7065

7166
/**
7267
* Redact sensitive info.
@@ -78,10 +73,10 @@ export type CustomDetect = typeof ArcjetRedactCustomRedact.detectSensitiveInfo;
7873
* @returns
7974
* Redacted string.
8075
*/
81-
export type CustomRedact = typeof ArcjetRedactCustomRedact.redactSensitiveInfo;
76+
type CustomRedact = typeof ArcjetRedactCustomRedact.redactSensitiveInfo;
8277

83-
export {
84-
type RedactedSensitiveInfoEntity,
85-
type RedactSensitiveInfoConfig,
86-
type SensitiveInfoEntity,
87-
};
78+
export type {
79+
RedactedSensitiveInfoEntity,
80+
RedactSensitiveInfoConfig,
81+
SensitiveInfoEntity,
82+
} from "./wasm/arcjet_analyze_bindings_redact.component.js";

redact/index.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { initializeWasm } from "@arcjet/redact-wasm";
2-
import type { SensitiveInfoEntity } from "@arcjet/redact-wasm";
2+
import type {
3+
RedactedSensitiveInfoEntity as RedactedSensitiveInfoEntityWasm,
4+
RedactSensitiveInfoConfig,
5+
SensitiveInfoEntity,
6+
} from "@arcjet/redact-wasm";
37

48
/**
5-
* Types of sensitive information.
9+
* Types of standard sensitive information that can be detected.
610
*/
7-
export type ArcjetSensitiveInfoType =
8-
| "email"
9-
| "phone-number"
10-
| "ip-address"
11-
| "credit-card-number";
11+
export type ArcjetSensitiveInfoType = Exclude<
12+
SensitiveInfoEntity["tag"],
13+
"custom"
14+
>;
1215

1316
type DetectSensitiveInfoEntities<T> = (
1417
tokens: string[],
@@ -65,34 +68,34 @@ export type RedactOptions<Detect> = {
6568
replace?: Replace<Detect>;
6669
};
6770

68-
function userEntitiesToWasm(entity: unknown) {
71+
function userEntitiesToWasm(entity: unknown): SensitiveInfoEntity {
6972
if (typeof entity !== "string") {
7073
throw new Error("redaction entities must be strings");
7174
}
7275

7376
if (entity === "email") {
74-
return { tag: "email" as const };
77+
return { tag: "email" };
7578
}
7679

7780
if (entity === "phone-number") {
78-
return { tag: "phone-number" as const };
81+
return { tag: "phone-number" };
7982
}
8083

8184
if (entity === "ip-address") {
82-
return { tag: "ip-address" as const };
85+
return { tag: "ip-address" };
8386
}
8487

8588
if (entity === "credit-card-number") {
86-
return { tag: "credit-card-number" as const };
89+
return { tag: "credit-card-number" };
8790
}
8891

8992
return {
90-
tag: "custom" as const,
93+
tag: "custom",
9194
val: entity,
9295
};
9396
}
9497

95-
function wasmEntitiesToString(entity: SensitiveInfoEntity) {
98+
function wasmEntitiesToString(entity: SensitiveInfoEntity): string {
9699
if (entity.tag === "email") {
97100
return "email";
98101
}
@@ -135,18 +138,15 @@ function noOpReplace(
135138
}
136139
/* c8 ignore stop */
137140

138-
interface RedactedSensitiveInfoEntity {
139-
original: string;
140-
redacted: string;
141-
start: number;
142-
end: number;
141+
interface RedactedSensitiveInfoEntity
142+
extends Omit<RedactedSensitiveInfoEntityWasm, "identifiedType"> {
143143
identifiedType: string;
144144
}
145145

146146
function getWasmOptions<
147147
const Detect extends DetectSensitiveInfoEntities<CustomEntities> | undefined,
148148
const CustomEntities extends string,
149-
>(options?: RedactOptions<Detect>) {
149+
>(options?: RedactOptions<Detect>): RedactSensitiveInfoConfig {
150150
if (typeof options === "object" && options !== null) {
151151
if (typeof options.entities !== "undefined") {
152152
if (Array.isArray(options.entities)) {

0 commit comments

Comments
 (0)