1.0.1 • Published 1 year ago

@truckersmp_official/api-types v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Introduction

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.

Disclaimer

This library supports only documented properties.

Installation

Install with npm / yarn:

npm install @truckersmp_official/api-types
yarn add @truckersmp_official/api-types

Example

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.

Support

If you have any questions about the library, you can create a topic on our forum.

License

This package is open-source and is licensed under the MIT license.

1.0.1

1 year ago

1.0.0

1 year ago