This simple TruckersMP TypeScript library provides type definitions for the TruckersMP API (and other connected services). It should be used along an HTTP client library for actually invoking requests.
This library supports only documented properties.
Install with npm / yarn:
npm install @truckersmp_official/api-types
yarn add @truckersmp_official/api-types
Performing a GET
request to retrieve player's data using the axios library:
import axios, { AxiosInstance } from 'axios';
import { APIWebRoutes, APIWebRouteBases } from '@truckersmp_official/api-types/v2';
import type { APIPlayer, APIResponse } from '@truckersmp_official/api-types/v2';
class APIManager {
private readonly web: AxiosInstance;
public constructor() {
this.web = axios.create({
baseURL: APIWebRouteBases.api,
});
}
public player(id: bigint): Promise<APIResponse<APIPlayer>> {
return this.web.get<APIResponse<APIPlayer>>(APIWebRoutes.player(id))
.then(response => response.data);
}
}
async function requestPlayer(id: bigint): Promise<APIPlayer | null> {
const api = new APIManager();
const response = await api.player(id);
return response.error === false ? response.response : null;
}
requestPlayer(BigInt(2)).then(data => console.log('Player data:', data));
Notice how we import a specific version of the API. This is not required. If the version is not specified, the latest one will be used. However, keep in mind that new API versions may bring high impact changes.
If you have any questions about the library, you can create a topic on our forum.
This package is open-source and is licensed under the MIT license.