@tracksim/sdk 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +150 -2
- package/dist/client.d.ts +64 -0
- package/dist/client.js +85 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2 -0
- package/dist/types.d.ts +161 -0
- package/dist/types.js +2 -0
- package/dist/webhooks.d.ts +9 -0
- package/dist/webhooks.js +31 -0
- package/package.json +50 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Omnibyte
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,151 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://tracksim-prod.s3.eu-west-2.amazonaws.com/public/logos/TrackSim-logo-full-color.png" alt="TrackSim" width="360" />
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<h1 align="center">TrackSim JavaScript / TypeScript SDK</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
An open-source, zero-dependency client for the <a href="https://docs.tracksim.app">TrackSim API</a> and webhooks.<br/>
|
|
9
|
+
Works in Node.js 20+, Bun, Deno, and edge runtimes (anything with <code>fetch</code> and Web Crypto).
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @tracksim/sdk
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick start
|
|
21
|
+
|
|
22
|
+
Generate an API key on the **API** tab of [tracksim.app/integrations](https://tracksim.app/integrations). You can only see the key once, so copy it when you create it.
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { TrackSim } from '@tracksim/sdk';
|
|
26
|
+
|
|
27
|
+
const tracksim = new TrackSim({ apiKey: process.env.TRACKSIM_API_KEY! });
|
|
28
|
+
|
|
29
|
+
const company = await tracksim.me();
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## API
|
|
33
|
+
|
|
34
|
+
All methods return Promises. Response types are exported (`Driver`, `RoutePoint`, `MapRoute`, …).
|
|
35
|
+
|
|
36
|
+
### Drivers
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
await tracksim.drivers.add('76561198000000000'); // POST /v1/drivers/add
|
|
40
|
+
await tracksim.drivers.remove('76561198000000000'); // DELETE /v1/drivers/remove
|
|
41
|
+
await tracksim.drivers.get('76561198000000000'); // GET /v3/drivers/{steam_id}/details
|
|
42
|
+
|
|
43
|
+
await tracksim.drivers.manage('76561198000000000', { // PATCH /v3/drivers/{steam_id}/manage
|
|
44
|
+
eut2_job_logging: true,
|
|
45
|
+
eut2_live_tracking: false,
|
|
46
|
+
ats_car_job_logging: true,
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Jobs
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
// GET /v2/jobs/{job_id}/route?job_type=truck|car|bus
|
|
54
|
+
const points = await tracksim.jobs.route(12345, 'truck'); // [{ x, z, time }]
|
|
55
|
+
const bus = await tracksim.jobs.route(12345, 'bus'); // adds speed_kmh, satisfaction, peak_*_g
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
A job's route doesn't change once it's recorded, so cache it to stay under the rate limits.
|
|
59
|
+
|
|
60
|
+
### Route Planner (maps)
|
|
61
|
+
|
|
62
|
+
This needs a paid plan and the Route Planner add-on. It's limited to 60 requests a minute.
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
const { full_url, flag } = await tracksim.maps.create(); // POST /v3/maps/create
|
|
66
|
+
await tracksim.maps.edit(routeId); // PATCH /v3/maps/{route_id}/edit
|
|
67
|
+
await tracksim.maps.list(); // GET /v3/maps/routes
|
|
68
|
+
await tracksim.maps.get(routeId); // GET /v3/maps/{route_id}/route
|
|
69
|
+
await tracksim.maps.delete(routeId); // DELETE /v3/maps/{route_id}/route
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`flag` is a JWT that expires after 24 hours. Treat it, and `full_url`, as secrets.
|
|
73
|
+
|
|
74
|
+
### Anything else
|
|
75
|
+
|
|
76
|
+
If an endpoint isn't wrapped yet, use the low-level helper. It adds auth and error handling:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const data = await tracksim.request('GET', '/v1/me');
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Errors
|
|
83
|
+
|
|
84
|
+
Non-2xx responses throw a `TrackSimError`:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import { TrackSimError } from '@tracksim/sdk';
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
await tracksim.drivers.add(steamId);
|
|
91
|
+
} catch (err) {
|
|
92
|
+
if (err instanceof TrackSimError) {
|
|
93
|
+
console.log(err.status); // 403
|
|
94
|
+
console.log(err.code); // e.g. "steam_profile_private", "max_driver_limit_reached"
|
|
95
|
+
console.log(err.body); // parsed response body
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Webhooks
|
|
101
|
+
|
|
102
|
+
Set up webhooks in [tracksim.app/settings](https://tracksim.app/settings). TrackSim signs each delivery with HMAC-SHA256 using your webhook secret and puts the hex digest in the `tracksim-signature` header.
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
import express from 'express';
|
|
106
|
+
import { constructWebhookEvent, SIGNATURE_HEADER } from '@tracksim/sdk';
|
|
107
|
+
|
|
108
|
+
const app = express();
|
|
109
|
+
|
|
110
|
+
// Use the raw body: re-serialising parsed JSON can change the bytes and break the signature.
|
|
111
|
+
app.post('/tracksim', express.text({ type: '*/*' }), async (req, res) => {
|
|
112
|
+
try {
|
|
113
|
+
const event = await constructWebhookEvent(req.body, req.get(SIGNATURE_HEADER), process.env.TRACKSIM_WEBHOOK_SECRET!);
|
|
114
|
+
|
|
115
|
+
if (event.type === 'job.delivered') {
|
|
116
|
+
console.log('Delivered job', event.data.id);
|
|
117
|
+
}
|
|
118
|
+
res.sendStatus(200);
|
|
119
|
+
} catch {
|
|
120
|
+
res.sendStatus(401);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Any 2xx response counts as delivered. TrackSim retries a failed delivery up to 3 times (after 5, 10 and 20 minutes).
|
|
126
|
+
|
|
127
|
+
If you only need the check, `verifyWebhookSignature(rawBody, signature, secret)` returns `Promise<boolean>`.
|
|
128
|
+
|
|
129
|
+
Event types (typed as `WebhookEventType`):
|
|
130
|
+
|
|
131
|
+
| Job type | Events |
|
|
132
|
+
| --- | --- |
|
|
133
|
+
| Truck | `job.started`, `job.delivered`, `job.cancelled` |
|
|
134
|
+
| Car | `car_job.started`, `car_job.delivered`, `car_job.cancelled` |
|
|
135
|
+
| Coach | `bus_job.started`, `bus_job.completed`, `bus_job.cancelled`, `satisfaction_decrease`, `satisfaction_increase` |
|
|
136
|
+
| Shared | `fine`, `tollgate`, `ferry`, `train`, `collision`, `repair`, `refuel`, `teleport`, `speeding` |
|
|
137
|
+
|
|
138
|
+
See the [payload reference](https://docs.tracksim.app/docs/truck-jobs/integrations/webhook-payloads) for full payloads. A `TruckJob` type is exported.
|
|
139
|
+
|
|
140
|
+
## Development
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
npm install
|
|
144
|
+
npm test # builds with tsc, then runs node:test
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Contributions are welcome. Open an issue or pull request.
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
[MIT](./LICENSE)
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { BusRoutePoint, Driver, DriverSettingsUpdate, MapRoute, MapRouteSummary, MapSession, RoutePoint } from './types.js';
|
|
2
|
+
export interface TrackSimOptions {
|
|
3
|
+
/** API key from https://tracksim.app/integrations (API tab). */
|
|
4
|
+
apiKey: string;
|
|
5
|
+
/** Defaults to https://api.tracksim.app */
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
/** Custom fetch implementation (defaults to global fetch). */
|
|
8
|
+
fetch?: typeof fetch;
|
|
9
|
+
}
|
|
10
|
+
export declare class TrackSimError extends Error {
|
|
11
|
+
/** HTTP status code. */
|
|
12
|
+
readonly status: number;
|
|
13
|
+
/** Error code/message from the API, e.g. `job_does_not_exist_in_company`. */
|
|
14
|
+
readonly code: string;
|
|
15
|
+
/** Parsed response body (or raw text if not JSON). */
|
|
16
|
+
readonly body: unknown;
|
|
17
|
+
constructor(
|
|
18
|
+
/** HTTP status code. */
|
|
19
|
+
status: number,
|
|
20
|
+
/** Error code/message from the API, e.g. `job_does_not_exist_in_company`. */
|
|
21
|
+
code: string,
|
|
22
|
+
/** Parsed response body (or raw text if not JSON). */
|
|
23
|
+
body: unknown);
|
|
24
|
+
}
|
|
25
|
+
export declare class TrackSim {
|
|
26
|
+
#private;
|
|
27
|
+
constructor(options: TrackSimOptions);
|
|
28
|
+
/** Low-level request helper. `path` includes the version, e.g. `/v1/me`. */
|
|
29
|
+
request<T = unknown>(method: string, path: string, body?: unknown): Promise<T>;
|
|
30
|
+
/** Information about your company. */
|
|
31
|
+
me<T = Record<string, any>>(): Promise<T>;
|
|
32
|
+
readonly drivers: {
|
|
33
|
+
/** Add a driver to your company by Steam64 ID. */
|
|
34
|
+
add: (steamId: string) => Promise<Driver>;
|
|
35
|
+
/** Remove a driver from your company. */
|
|
36
|
+
remove: (steamId: string) => Promise<unknown>;
|
|
37
|
+
/** Get a driver's details. */
|
|
38
|
+
get: (steamId: string) => Promise<Driver>;
|
|
39
|
+
/** Update a driver's job logging / live tracking settings. */
|
|
40
|
+
manage: (steamId: string, settings: DriverSettingsUpdate) => Promise<Driver>;
|
|
41
|
+
};
|
|
42
|
+
readonly jobs: {
|
|
43
|
+
/** Recorded route of a job. Results don't change once recorded, so cache them. */
|
|
44
|
+
route: {
|
|
45
|
+
(jobId: number | string, jobType: "bus"): Promise<BusRoutePoint[]>;
|
|
46
|
+
(jobId: number | string, jobType: "truck" | "car"): Promise<RoutePoint[]>;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
/** Route Planner (paid plan + Route Planner add-on, 60 requests/minute). */
|
|
50
|
+
readonly maps: {
|
|
51
|
+
/** Create a new route; returns a planner URL and a 24h flag. */
|
|
52
|
+
create: () => Promise<MapSession>;
|
|
53
|
+
/** Open an existing route for editing; returns a planner URL and a 24h flag. */
|
|
54
|
+
edit: (routeId: string) => Promise<MapSession>;
|
|
55
|
+
/** List all routes (without waypoints). */
|
|
56
|
+
list: () => Promise<MapRouteSummary[]>;
|
|
57
|
+
/** Get one route including waypoints. */
|
|
58
|
+
get: (routeId: string) => Promise<MapRoute>;
|
|
59
|
+
/** Delete a route. */
|
|
60
|
+
delete: (routeId: string) => Promise<{
|
|
61
|
+
deleted: boolean;
|
|
62
|
+
}>;
|
|
63
|
+
};
|
|
64
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export class TrackSimError extends Error {
|
|
2
|
+
status;
|
|
3
|
+
code;
|
|
4
|
+
body;
|
|
5
|
+
constructor(
|
|
6
|
+
/** HTTP status code. */
|
|
7
|
+
status,
|
|
8
|
+
/** Error code/message from the API, e.g. `job_does_not_exist_in_company`. */
|
|
9
|
+
code,
|
|
10
|
+
/** Parsed response body (or raw text if not JSON). */
|
|
11
|
+
body) {
|
|
12
|
+
super(`TrackSim API error ${status}: ${code}`);
|
|
13
|
+
this.status = status;
|
|
14
|
+
this.code = code;
|
|
15
|
+
this.body = body;
|
|
16
|
+
this.name = 'TrackSimError';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export class TrackSim {
|
|
20
|
+
#apiKey;
|
|
21
|
+
#baseUrl;
|
|
22
|
+
#fetch;
|
|
23
|
+
constructor(options) {
|
|
24
|
+
if (!options?.apiKey)
|
|
25
|
+
throw new TypeError('TrackSim: apiKey is required');
|
|
26
|
+
this.#apiKey = options.apiKey;
|
|
27
|
+
this.#baseUrl = (options.baseUrl ?? 'https://api.tracksim.app').replace(/\/+$/, '');
|
|
28
|
+
this.#fetch = options.fetch ?? globalThis.fetch;
|
|
29
|
+
}
|
|
30
|
+
/** Low-level request helper. `path` includes the version, e.g. `/v1/me`. */
|
|
31
|
+
async request(method, path, body) {
|
|
32
|
+
const res = await this.#fetch(this.#baseUrl + path, {
|
|
33
|
+
method,
|
|
34
|
+
headers: {
|
|
35
|
+
Authorization: `Api-Key ${this.#apiKey}`,
|
|
36
|
+
Accept: 'application/json',
|
|
37
|
+
...(body === undefined ? {} : { 'Content-Type': 'application/json' }),
|
|
38
|
+
},
|
|
39
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
40
|
+
});
|
|
41
|
+
const text = await res.text();
|
|
42
|
+
let data = text;
|
|
43
|
+
try {
|
|
44
|
+
data = text ? JSON.parse(text) : null;
|
|
45
|
+
}
|
|
46
|
+
catch { /* keep raw text */ }
|
|
47
|
+
if (!res.ok) {
|
|
48
|
+
const code = (data && typeof data === 'object' && (data.error ?? data.reason ?? data.message)) || res.statusText || 'unknown_error';
|
|
49
|
+
throw new TrackSimError(res.status, String(code), data);
|
|
50
|
+
}
|
|
51
|
+
return data;
|
|
52
|
+
}
|
|
53
|
+
/** Information about your company. */
|
|
54
|
+
me() {
|
|
55
|
+
return this.request('GET', '/v1/me');
|
|
56
|
+
}
|
|
57
|
+
drivers = {
|
|
58
|
+
/** Add a driver to your company by Steam64 ID. */
|
|
59
|
+
add: (steamId) => this.request('POST', '/v1/drivers/add', { steam_id: steamId }),
|
|
60
|
+
/** Remove a driver from your company. */
|
|
61
|
+
remove: (steamId) => this.request('DELETE', '/v1/drivers/remove', { steam_id: steamId }),
|
|
62
|
+
/** Get a driver's details. */
|
|
63
|
+
get: (steamId) => this.request('GET', `/v3/drivers/${enc(steamId)}/details`),
|
|
64
|
+
/** Update a driver's job logging / live tracking settings. */
|
|
65
|
+
manage: (steamId, settings) => this.request('PATCH', `/v3/drivers/${enc(steamId)}/manage`, settings),
|
|
66
|
+
};
|
|
67
|
+
jobs = {
|
|
68
|
+
/** Recorded route of a job. Results don't change once recorded, so cache them. */
|
|
69
|
+
route: ((jobId, jobType) => this.request('GET', `/v2/jobs/${enc(jobId)}/route?job_type=${enc(jobType)}`)),
|
|
70
|
+
};
|
|
71
|
+
/** Route Planner (paid plan + Route Planner add-on, 60 requests/minute). */
|
|
72
|
+
maps = {
|
|
73
|
+
/** Create a new route; returns a planner URL and a 24h flag. */
|
|
74
|
+
create: () => this.request('POST', '/v3/maps/create'),
|
|
75
|
+
/** Open an existing route for editing; returns a planner URL and a 24h flag. */
|
|
76
|
+
edit: (routeId) => this.request('PATCH', `/v3/maps/${enc(routeId)}/edit`),
|
|
77
|
+
/** List all routes (without waypoints). */
|
|
78
|
+
list: () => this.request('GET', '/v3/maps/routes'),
|
|
79
|
+
/** Get one route including waypoints. */
|
|
80
|
+
get: (routeId) => this.request('GET', `/v3/maps/${enc(routeId)}/route`),
|
|
81
|
+
/** Delete a route. */
|
|
82
|
+
delete: (routeId) => this.request('DELETE', `/v3/maps/${enc(routeId)}/route`),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const enc = (v) => encodeURIComponent(String(v));
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
export type Game = 'eut2' | 'ats';
|
|
2
|
+
export type JobType = 'truck' | 'car' | 'bus';
|
|
3
|
+
export interface ToggleSettings {
|
|
4
|
+
job_logging: boolean;
|
|
5
|
+
live_tracking: boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface Driver {
|
|
8
|
+
id: number;
|
|
9
|
+
steam_id: string;
|
|
10
|
+
username: string;
|
|
11
|
+
profile_photo_url: string;
|
|
12
|
+
client: {
|
|
13
|
+
is_installed: boolean;
|
|
14
|
+
version: {
|
|
15
|
+
version: string;
|
|
16
|
+
branch: string;
|
|
17
|
+
platform: string;
|
|
18
|
+
} | null;
|
|
19
|
+
};
|
|
20
|
+
settings: {
|
|
21
|
+
eut2: {
|
|
22
|
+
truck: ToggleSettings;
|
|
23
|
+
coach: ToggleSettings;
|
|
24
|
+
};
|
|
25
|
+
ats: {
|
|
26
|
+
truck: ToggleSettings;
|
|
27
|
+
car: ToggleSettings;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
is_banned: boolean;
|
|
31
|
+
last_active: string;
|
|
32
|
+
}
|
|
33
|
+
export interface DriverSettingsUpdate {
|
|
34
|
+
eut2_job_logging?: boolean;
|
|
35
|
+
eut2_live_tracking?: boolean;
|
|
36
|
+
eut2_coach_job_logging?: boolean;
|
|
37
|
+
eut2_coach_live_tracking?: boolean;
|
|
38
|
+
ats_job_logging?: boolean;
|
|
39
|
+
ats_live_tracking?: boolean;
|
|
40
|
+
ats_car_job_logging?: boolean;
|
|
41
|
+
ats_car_live_tracking?: boolean;
|
|
42
|
+
}
|
|
43
|
+
export interface RoutePoint {
|
|
44
|
+
x: number;
|
|
45
|
+
z: number;
|
|
46
|
+
/** Unix seconds */
|
|
47
|
+
time: number;
|
|
48
|
+
}
|
|
49
|
+
export interface BusRoutePoint extends RoutePoint {
|
|
50
|
+
speed_kmh: number;
|
|
51
|
+
satisfaction: number;
|
|
52
|
+
peak_lateral_g: number;
|
|
53
|
+
peak_braking_g: number;
|
|
54
|
+
peak_acceleration_g: number;
|
|
55
|
+
peak_vertical_g: number;
|
|
56
|
+
}
|
|
57
|
+
export interface MapSession {
|
|
58
|
+
full_url: string;
|
|
59
|
+
/** JWT, expires after 24 hours. Treat as a secret. */
|
|
60
|
+
flag: string;
|
|
61
|
+
}
|
|
62
|
+
export interface MapRouteSummary {
|
|
63
|
+
id: string;
|
|
64
|
+
game: Game;
|
|
65
|
+
map: string;
|
|
66
|
+
name: string | null;
|
|
67
|
+
convoy: boolean;
|
|
68
|
+
created_at: string;
|
|
69
|
+
updated_at: string;
|
|
70
|
+
}
|
|
71
|
+
export interface MapRoute extends MapRouteSummary {
|
|
72
|
+
waypoints: unknown;
|
|
73
|
+
tenant: {
|
|
74
|
+
id: number;
|
|
75
|
+
slug: string;
|
|
76
|
+
name: string;
|
|
77
|
+
custom_domain: {
|
|
78
|
+
domain: string | null;
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
export type TruckEventType = 'job.started' | 'job.delivered' | 'job.cancelled';
|
|
83
|
+
export type CarEventType = 'car_job.started' | 'car_job.delivered' | 'car_job.cancelled';
|
|
84
|
+
export type BusEventType = 'bus_job.started' | 'bus_job.completed' | 'bus_job.cancelled' | 'satisfaction_decrease' | 'satisfaction_increase';
|
|
85
|
+
export type SharedEventType = 'fine' | 'tollgate' | 'ferry' | 'train' | 'collision' | 'repair' | 'refuel' | 'teleport' | 'speeding';
|
|
86
|
+
export type WebhookEventType = TruckEventType | CarEventType | BusEventType | SharedEventType;
|
|
87
|
+
export interface WebhookEvent<T = Record<string, any>> {
|
|
88
|
+
object: 'event';
|
|
89
|
+
type: WebhookEventType | (string & {});
|
|
90
|
+
data: T;
|
|
91
|
+
}
|
|
92
|
+
export interface JobEvent {
|
|
93
|
+
type: string;
|
|
94
|
+
/** ISO 8601 UTC */
|
|
95
|
+
real_time: string;
|
|
96
|
+
/** Unix seconds */
|
|
97
|
+
time: number;
|
|
98
|
+
location: {
|
|
99
|
+
x: number;
|
|
100
|
+
y: number;
|
|
101
|
+
z: number;
|
|
102
|
+
};
|
|
103
|
+
meta: Record<string, any>;
|
|
104
|
+
}
|
|
105
|
+
interface Named {
|
|
106
|
+
unique_id: string;
|
|
107
|
+
name: string | null;
|
|
108
|
+
}
|
|
109
|
+
export interface TruckJob {
|
|
110
|
+
id: number;
|
|
111
|
+
object: string;
|
|
112
|
+
driver: {
|
|
113
|
+
id: number;
|
|
114
|
+
steam_id: string;
|
|
115
|
+
username: string;
|
|
116
|
+
profile_photo_url: string;
|
|
117
|
+
client: {
|
|
118
|
+
version: {
|
|
119
|
+
name: string;
|
|
120
|
+
branch_name: string;
|
|
121
|
+
platform: string;
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
last_active: string;
|
|
125
|
+
};
|
|
126
|
+
start_time: string;
|
|
127
|
+
stop_time: string | null;
|
|
128
|
+
time_spent: number;
|
|
129
|
+
planned_distance: number;
|
|
130
|
+
driven_distance: number;
|
|
131
|
+
adblue_used: number;
|
|
132
|
+
fuel_used: number;
|
|
133
|
+
is_special: boolean;
|
|
134
|
+
is_late: boolean;
|
|
135
|
+
market: string;
|
|
136
|
+
cargo: Named & {
|
|
137
|
+
mass: number;
|
|
138
|
+
damage: number;
|
|
139
|
+
};
|
|
140
|
+
game: {
|
|
141
|
+
short_name: Game;
|
|
142
|
+
language: string | null;
|
|
143
|
+
had_police_enabled: boolean;
|
|
144
|
+
};
|
|
145
|
+
multiplayer: {
|
|
146
|
+
mode: string;
|
|
147
|
+
meta: {
|
|
148
|
+
player_id: number | string;
|
|
149
|
+
server: string;
|
|
150
|
+
};
|
|
151
|
+
} | null;
|
|
152
|
+
source_city: Named;
|
|
153
|
+
source_company: Named;
|
|
154
|
+
destination_city: Named;
|
|
155
|
+
destination_company: Named;
|
|
156
|
+
truck: Record<string, any>;
|
|
157
|
+
trailers: Record<string, any>[];
|
|
158
|
+
events: JobEvent[];
|
|
159
|
+
[key: string]: unknown;
|
|
160
|
+
}
|
|
161
|
+
export {};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { WebhookEvent } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Verify the `tracksim-signature` header (hex HMAC-SHA256 of the body with your webhook secret).
|
|
4
|
+
* Pass the raw request body string, exactly as received.
|
|
5
|
+
*/
|
|
6
|
+
export declare function verifyWebhookSignature(rawBody: string, signature: string | null | undefined, secret: string): Promise<boolean>;
|
|
7
|
+
/** Verify the signature and parse the event. Throws if the signature is invalid. */
|
|
8
|
+
export declare function constructWebhookEvent<T = Record<string, any>>(rawBody: string, signature: string | null | undefined, secret: string): Promise<WebhookEvent<T>>;
|
|
9
|
+
export declare const SIGNATURE_HEADER = "tracksim-signature";
|
package/dist/webhooks.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const encoder = new TextEncoder();
|
|
2
|
+
async function hmacHex(secret, payload) {
|
|
3
|
+
const key = await crypto.subtle.importKey('raw', encoder.encode(secret), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
|
|
4
|
+
const sig = new Uint8Array(await crypto.subtle.sign('HMAC', key, encoder.encode(payload)));
|
|
5
|
+
return Array.from(sig, (b) => b.toString(16).padStart(2, '0')).join('');
|
|
6
|
+
}
|
|
7
|
+
function timingSafeEqual(a, b) {
|
|
8
|
+
if (a.length !== b.length)
|
|
9
|
+
return false;
|
|
10
|
+
let diff = 0;
|
|
11
|
+
for (let i = 0; i < a.length; i++)
|
|
12
|
+
diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
13
|
+
return diff === 0;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Verify the `tracksim-signature` header (hex HMAC-SHA256 of the body with your webhook secret).
|
|
17
|
+
* Pass the raw request body string, exactly as received.
|
|
18
|
+
*/
|
|
19
|
+
export async function verifyWebhookSignature(rawBody, signature, secret) {
|
|
20
|
+
if (!signature || !secret)
|
|
21
|
+
return false;
|
|
22
|
+
return timingSafeEqual(await hmacHex(secret, rawBody), signature.trim().toLowerCase());
|
|
23
|
+
}
|
|
24
|
+
/** Verify the signature and parse the event. Throws if the signature is invalid. */
|
|
25
|
+
export async function constructWebhookEvent(rawBody, signature, secret) {
|
|
26
|
+
if (!(await verifyWebhookSignature(rawBody, signature, secret))) {
|
|
27
|
+
throw new Error('TrackSim: invalid webhook signature');
|
|
28
|
+
}
|
|
29
|
+
return JSON.parse(rawBody);
|
|
30
|
+
}
|
|
31
|
+
export const SIGNATURE_HEADER = 'tracksim-signature';
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tracksim/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Open-source JavaScript & TypeScript client for the TrackSim API and webhooks",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/tracksim-app/tracksim-js.git"
|
|
9
|
+
},
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/tracksim-app/tracksim-js/issues"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=20"
|
|
27
|
+
},
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"keywords": [
|
|
30
|
+
"tracksim",
|
|
31
|
+
"ets2",
|
|
32
|
+
"ats",
|
|
33
|
+
"euro-truck-simulator",
|
|
34
|
+
"american-truck-simulator",
|
|
35
|
+
"vtc",
|
|
36
|
+
"api",
|
|
37
|
+
"sdk",
|
|
38
|
+
"webhooks"
|
|
39
|
+
],
|
|
40
|
+
"homepage": "https://docs.tracksim.app",
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "rm -rf dist && tsc",
|
|
43
|
+
"test": "npm run build && node --test test/*.test.js",
|
|
44
|
+
"prepublishOnly": "npm test"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"typescript": "^5.6.0"
|
|
51
|
+
}
|
|
52
|
+
}
|