Optional
nativeOptionally 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.
Accessing or creating the Player
with nativeId
equal to my-player
:
const player = new Player({ nativeId: 'my-player' })
player.play(); // call methods and properties...
Optional
preprocessCalled before an HTTP request is made. Can be used to change request parameters.
Type of the request to be made.
The HTTP request to process.
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.
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,
},
});
Optional
preprocessCalled before an HTTP response is accessed by the player. Can be used to access or change the response.
Type of the corresponding request object of the response.
The HTTP response to process.
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.
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,
},
});
The network API gives the ability to influence network requests. It enables preprocessing requests and preprocessing responses.