Site icon Hip-Hop Website Design and Development

I’m receiving the following error using the below code: Uncaught Error: Cannot use object of type WP_REST_Response as array

<?php
/*
  Plugin Name: Flyp wp-json no cache
  Description: Disables the caching of /wp-json/ requests adding no chace headers to the response.
  Version: 0.1
  Author: Nolte Developer
*/

// The wp-json paths or any custom endpoints.
$regex_json_path_patterns = array(
    '#^/wp-json/wp/v2?#', 
    '#^/wp-json/?#'
);

// Filter response if patterns match.
foreach ( $regex_json_path_patterns as $regex_json_path_pattern ) {
    if ( preg_match( $regex_json_path_pattern, $_SERVER['REQUEST_URI'] ) ) {
        // Re-use the rest_post_dispatch filter in the Pantheon page cache plugin.
        add_filter( 'rest_post_dispatch', 'filter_rest_post_dispatch_send_cache_control', 12, 2 );
        // Re-define the send_header value with any custom Cache-Control header
        function filter_rest_post_dispatch_send_cache_control( WP_HTTP_Response $response, WP_REST_Server $server ) : WP_HTTP_Response {
            // Create a new instance from WP_REST_Response.
            $result = new WP_REST_Response( $response, 200 );
            // Add headers.
            $server->send_headers( wp_get_nocache_headers() );
            return $result;
        }
        break;
    }
}