The network API gives the ability to influence network requests. It enables preprocessing requests and preprocessing responses.

interface NetworkConfig {
    nativeId?: string;
    preprocessHttpRequest?: ((type, request) => Promise<HttpRequest>);
    preprocessHttpResponse?: ((type, response) => Promise<HttpResponse>);
}

Hierarchy

  • NativeInstanceConfig
    • NetworkConfig

Properties

nativeId?: string

Optionally user-defined string id for the native instance. Used to access a certain native instance from any point in the source code then call methods/properties on it.

When left empty, a random UUIDv4 is generated for it.

Example

Accessing or creating the Player with nativeId equal to my-player:

const player = new Player({ nativeId: 'my-player' })
player.play(); // call methods and properties...
preprocessHttpRequest?: ((type, request) => Promise<HttpRequest>)

Called before an HTTP request is made. Can be used to change request parameters.

Type declaration

Returns

A Promise that resolves to an HttpRequest object. - If the promise resolves, the player will use the processed request. - If the promise rejects, the player will fall back to using the original request.

Examples

 const requestCallback = (type: HttpRequestType, request: HttpRequest) => {
// Access current properties

console.log(JSON.stringify(type));
console.log(JSON.stringify(request));

// Modify the request

request.headers['New-Header'] = 'val';
request.method = 'GET';

// Return the processed request via a Promise

const processed: HttpRequest = {
body: request.body,
headers: request.headers,
method: request.method,
url: request.url,
};
return Promise.resolve(processed);
};

const player = usePlayer({
networkConfig: {
preprocessHttpRequest: requestCallback,
},
});
preprocessHttpResponse?: ((type, response) => Promise<HttpResponse>)

Called before an HTTP response is accessed by the player. Can be used to access or change the response.

Type declaration

Returns

A Promise that resolves to an HttpResponse object. - If the promise resolves, the player will use the processed response. - If the promise rejects, the player will fall back to using the original response.

Example

 const responseCallback = (type: HttpRequestType, response: HttpResponse) => {
// Access response properties

console.log(JSON.stringify(type));
console.log(JSON.stringify(response));

// Modify the response

response.headers['New-Header'] = 'val';
response.url = response.request.url; // remove eventual redirect changes

// Return the processed response via a Promise

const processed: HttpResponse = {
body: response.body,
headers: response.headers,
request: response.request,
status: response.status,
url: response.url,
};
return Promise.resolve(processed);
};

// Properly attach the callback to the config
const player = usePlayer({
networkConfig: {
preprocessHttpResponse: responseCallback,
},
});