I want to make a post api call once a post or page is published/updated and show a javascript alert box saying ‘Post has been published/updated’

If you only want an alert then why do you write it in a separate file and enqueue it?

You can directly write it inside the invoke_save_post function using

echo "<script>alert('Api called');</script>";

or if you want to write it in a separate file then you have to write your alert inside the function and call that js function inside the PHP function so it will not execute Until the function is called

for example:

function alert_wrapper(){
     alert('Api has been called');
}

Here your alert is called in the front end because there may be a chance this file is loaded in the front end so every time file is loaded your code executes because it will not inside any function.