@@ -12,7 +12,7 @@ import { getAccessToken } from './utils';
1212const redact = async (
1313 documents : any [ ] ,
1414 parameters : PluginParameters < { pii : AzureCredentials } > ,
15- options ?: Record < string , any >
15+ pluginOptions ?: Record < string , any >
1616) => {
1717 const body = {
1818 kind : 'PiiEntityRecognition' ,
@@ -35,8 +35,8 @@ const redact = async (
3535 const { token, error : tokenError } = await getAccessToken (
3636 credentials as any ,
3737 'pii' ,
38- options ,
39- options ?. env
38+ pluginOptions ,
39+ pluginOptions ?. env
4040 ) ;
4141
4242 const headers : Record < string , string > = {
@@ -66,20 +66,19 @@ const redact = async (
6666 }
6767
6868 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 ) ;
7574 return response ;
7675} ;
7776
7877export const handler : PluginHandler < { pii : AzureCredentials } > = async (
7978 context : PluginContext ,
8079 parameters : PluginParameters < { pii : AzureCredentials } > ,
8180 eventType : HookEventType ,
82- options ?: Record < string , any >
81+ pluginOptions ?: Record < string , any >
8382) => {
8483 let error = null ;
8584 let verdict = true ;
@@ -134,9 +133,18 @@ export const handler: PluginHandler<{ pii: AzureCredentials }> = async (
134133 } ) ) ;
135134
136135 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+ }
138140 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 ;
140148 const redactedData = ( response . results . documents ?? [ ] ) . map (
141149 ( doc : any ) => doc . redactedText
142150 ) ;
0 commit comments