type RULE_RESPONSE = 'block' | 'whitelist' | 'captcha' ; export async function addIpRule ( ip , rule_response = 'block' ) { try { const req = await fetch ( `https://customer-api.datadome.co/1.0/protection/custom-rules?apikey=${ process . env . DATADOME_MANAGEMENT_KEY }` , { method : 'POST' , headers : { Accept : '*/*' , 'Content-Type' : 'application/json' } , body : JSON . stringify ( { data : { rule_name : `ip_rule_${ ip }` , query : `ip:${ ip }` , rule_response : rule_response , rule_priority : 'high' , } , } ) , } ) ; const response = await req . json ( ) ; if ( response . status !== 200 ) { throw new Error ( JSON . stringify ( response ) ) ; } return response ; } catch ( err ) { console . log ( 'Error' , err ) ; throw new Error ( err ) ; } } export async function removeRuleById ( customRuleId ) { try { const req = await fetch ( `https://customer-api.datadome.co/1.0/protection/custom-rules/${ customRuleId }?apikey=${ process . env . DATADOME_MANAGEMENT_KEY }` , { method : 'DELETE' , headers : { Accept : '*/*' , 'Content-Type' : 'application/json' } , } ) ; return req . json ( ) ; } catch ( err ) { console . log ( 'Error' , err ) ; } } export async function getAllRules ( ) { try { const req = await fetch ( `https://customer-api.datadome.co/1.0/protection/custom-rules?apikey=${ process . env . DATADOME_MANAGEMENT_KEY }` , { method : 'GET' , headers : { Accept : '*/*' , 'Content-Type' : 'application/json' } , } ) ; const { data : { custom_rules } , } = await req . json ( ) ; return custom_rules ; } catch ( err ) { console . log ( 'Error' , err ) ; } }