@@ -12,7 +12,7 @@ import { getAccessToken } from './utils';
12
12
const redact = async (
13
13
documents : any [ ] ,
14
14
parameters : PluginParameters < { pii : AzureCredentials } > ,
15
- options ?: Record < string , any >
15
+ pluginOptions ?: Record < string , any >
16
16
) => {
17
17
const body = {
18
18
kind : 'PiiEntityRecognition' ,
@@ -35,8 +35,8 @@ const redact = async (
35
35
const { token, error : tokenError } = await getAccessToken (
36
36
credentials as any ,
37
37
'pii' ,
38
- options ,
39
- options ?. env
38
+ pluginOptions ,
39
+ pluginOptions ?. env
40
40
) ;
41
41
42
42
const headers : Record < string , string > = {
@@ -66,20 +66,19 @@ const redact = async (
66
66
}
67
67
68
68
const timeout = parameters . timeout || 5000 ;
69
- const response = await post (
70
- url ,
71
- body ,
72
- { headers, dispatcher : agent } ,
73
- timeout
74
- ) ;
69
+ const requestOptions : Record < string , any > = { headers } ;
70
+ if ( agent ) {
71
+ requestOptions . dispatcher = agent ;
72
+ }
73
+ const response = await post ( url , body , requestOptions , timeout ) ;
75
74
return response ;
76
75
} ;
77
76
78
77
export const handler : PluginHandler < { pii : AzureCredentials } > = async (
79
78
context : PluginContext ,
80
79
parameters : PluginParameters < { pii : AzureCredentials } > ,
81
80
eventType : HookEventType ,
82
- options ?: Record < string , any >
81
+ pluginOptions ?: Record < string , any >
83
82
) => {
84
83
let error = null ;
85
84
let verdict = true ;
@@ -134,9 +133,18 @@ export const handler: PluginHandler<{ pii: AzureCredentials }> = async (
134
133
} ) ) ;
135
134
136
135
try {
137
- const response = await redact ( documents , parameters , options ) ;
136
+ const response = await redact ( documents , parameters , pluginOptions ) ;
137
+ if ( ! response ?. results ?. documents ) {
138
+ throw new Error ( 'Invalid response from Azure PII API' ) ;
139
+ }
138
140
data = response . results . documents ;
139
- if ( parameters . redact ) {
141
+ const containsPII =
142
+ data . length > 0 && data . some ( ( doc : any ) => doc . entities . length > 0 ) ;
143
+ if ( containsPII ) {
144
+ verdict = false ;
145
+ }
146
+ if ( parameters . redact && containsPII ) {
147
+ verdict = true ;
140
148
const redactedData = ( response . results . documents ?? [ ] ) . map (
141
149
( doc : any ) => doc . redactedText
142
150
) ;
0 commit comments