@solvice/routing 0.0.0-stage → 0.1.0-beta.1
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/README.md +107 -2
- package/dist/index.d.ts +56 -0
- package/dist/index.js +67 -0
- package/dist/v3.d.ts +4303 -0
- package/dist/v3.js +5 -0
- package/package.json +44 -4
package/README.md
CHANGED
|
@@ -1,3 +1,108 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @solvice/routing
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TypeScript client for the Solvice V3 Routing API. The request and response types
|
|
4
|
+
are generated from the API's OpenAPI spec, so they match the API field for field.
|
|
5
|
+
|
|
6
|
+
V3 is in beta until general availability. Until then this package is published
|
|
7
|
+
under the `beta` dist-tag as `0.x` prereleases, and any release may change types.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @solvice/routing@beta
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { createSolvice, SolviceError } from '@solvice/routing';
|
|
15
|
+
|
|
16
|
+
const solvice = createSolvice({ apiKey: process.env.SOLVICE_API_KEY! });
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
const solution = await solvice.solve({
|
|
20
|
+
problem: {
|
|
21
|
+
jobs: [
|
|
22
|
+
{
|
|
23
|
+
id: 'delivery-1',
|
|
24
|
+
location: [4.7005, 50.8798], // [longitude, latitude]
|
|
25
|
+
service_duration_s: 300,
|
|
26
|
+
time_windows: [{ from: '2026-04-01T09:00:00+02:00', to: '2026-04-01T12:00:00+02:00' }],
|
|
27
|
+
delivery: { weight: 10 },
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
vehicles: [
|
|
31
|
+
{
|
|
32
|
+
id: 'truck-1',
|
|
33
|
+
capacity: { weight: 100 },
|
|
34
|
+
shifts: [
|
|
35
|
+
{
|
|
36
|
+
id: 'mon',
|
|
37
|
+
from: '2026-04-01T08:00:00+02:00',
|
|
38
|
+
to: '2026-04-01T17:00:00+02:00',
|
|
39
|
+
start: { location: [4.3517, 50.8503] },
|
|
40
|
+
end: { location: [4.3517, 50.8503] },
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
options: { time_limit_s: 5 },
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
for (const route of solution.routes) {
|
|
50
|
+
console.log(`${route.vehicle} / ${route.shift}: ${route.distance_m} m`);
|
|
51
|
+
for (const stop of route.stops) {
|
|
52
|
+
console.log(` ${stop.type} ${stop.id ?? ''} arrives ${stop.arrival}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (const job of solution.unassigned) {
|
|
56
|
+
console.log(`unassigned ${job.job_id}: ${job.category}`);
|
|
57
|
+
}
|
|
58
|
+
} catch (err) {
|
|
59
|
+
if (!(err instanceof SolviceError)) throw err;
|
|
60
|
+
console.error(err.status, err.body?.code, err.message);
|
|
61
|
+
for (const item of err.body?.errors ?? []) {
|
|
62
|
+
console.error(` ${item.code} at ${item.pointer}: ${item.message}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
| Method | Endpoint |
|
|
68
|
+
| --- | --- |
|
|
69
|
+
| `solve(request)` | `POST /v3/routing/solve` |
|
|
70
|
+
| `evaluate(request)` | `POST /v3/routing/evaluate` |
|
|
71
|
+
| `suggest(request)` | `POST /v3/routing/suggest` |
|
|
72
|
+
| `validate.solve(request)`, `validate.evaluate(request)`, `validate.suggest(request)` | the same three with `?validate_only=true` |
|
|
73
|
+
|
|
74
|
+
Any non-2xx response throws a `SolviceError` with the HTTP `status`, the
|
|
75
|
+
`response`, and, when the response carried one, the API's error `body`. A `401`
|
|
76
|
+
or a `429` comes from the API gateway and does not use that body (see
|
|
77
|
+
[Errors](https://docs.solvice.io/guides/vrp/errors)): branch on `status`, and read
|
|
78
|
+
`raw` for whatever JSON came back. Every `503` carries `Retry-After`; wait
|
|
79
|
+
`err.retryAfterS` seconds before retrying.
|
|
80
|
+
|
|
81
|
+
Every method takes a second argument, `{ signal, headers }`. The client sets no
|
|
82
|
+
timeout of its own: pass `AbortSignal.timeout(ms)` as `signal`, with a margin on
|
|
83
|
+
top of `options.time_limit_s`. It does not retry; pass your own `fetch` to
|
|
84
|
+
`createSolvice` to add that. `client` is the underlying
|
|
85
|
+
[openapi-fetch](https://openapi-ts.dev/openapi-fetch/) client.
|
|
86
|
+
|
|
87
|
+
Runs on Node.js 20 or later. Keep the API key on your server.
|
|
88
|
+
|
|
89
|
+
Documentation: <https://docs.solvice.io>
|
|
90
|
+
|
|
91
|
+
## Development
|
|
92
|
+
|
|
93
|
+
`src/v3.ts` is generated; do not edit it. After an API change:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
npm run generate # from mintlify/openapi.json
|
|
97
|
+
npm test # builds, typechecks tests/types.ts and examples/, runs the unit tests
|
|
98
|
+
npm run smoke # live calls; needs SOLVICE_API_KEY
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
To release, bump `version` to the next `0.x.y-beta.n`, merge it to `main`, then run
|
|
102
|
+
the "Publish TypeScript SDK" workflow on `main` (Actions → Run workflow). It runs
|
|
103
|
+
`npm run publish:beta` through npm trusted publishing; never publish
|
|
104
|
+
from a branch. The explicit `--tag beta` matters: npm 11 ignores
|
|
105
|
+
`publishConfig.tag` and would move `latest` to the prerelease. Until a stable version
|
|
106
|
+
exists, `latest` still points at the first prerelease (npm gives a package's first
|
|
107
|
+
version `latest` whatever the tag), so `npm install @solvice/routing` without
|
|
108
|
+
`@beta` installs the beta too.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { components, paths } from "./v3.js";
|
|
2
|
+
export type { components, paths };
|
|
3
|
+
export type Schemas = components["schemas"];
|
|
4
|
+
export type SolveRequest = Schemas["SolveRequest"];
|
|
5
|
+
export type EvaluateRequest = Schemas["EvaluateRequest"];
|
|
6
|
+
export type SuggestRequest = Schemas["SuggestRequest"];
|
|
7
|
+
export type SolveResponse = Schemas["SolveResponse"];
|
|
8
|
+
export type SuggestResponse = Schemas["SuggestResponse"];
|
|
9
|
+
export type ValidateOnlyResponse = Schemas["ValidateOnlyResponse"];
|
|
10
|
+
export type ErrorBody = Schemas["ErrorBody"];
|
|
11
|
+
export interface SolviceOptions {
|
|
12
|
+
/** Sent as the `Authorization` header value, with no scheme prefix. */
|
|
13
|
+
apiKey: string;
|
|
14
|
+
baseUrl?: string;
|
|
15
|
+
fetch?: typeof globalThis.fetch;
|
|
16
|
+
}
|
|
17
|
+
/** Per-call options. The client sets no timeout: pass `AbortSignal.timeout(ms)` as `signal`. */
|
|
18
|
+
export interface RequestOptions {
|
|
19
|
+
signal?: AbortSignal;
|
|
20
|
+
headers?: Record<string, string>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Any non-2xx response. `body` is the API's error body when the response
|
|
24
|
+
* carried one. A `401` or a `429` comes from the API gateway and does not use
|
|
25
|
+
* that body (a JSON `{ message, error }` or nothing): branch on `status`, and
|
|
26
|
+
* read `raw` for whatever came back. `response` is the `Response` itself, for
|
|
27
|
+
* its headers: every `503` carries `Retry-After` (`retryAfterS`), and
|
|
28
|
+
* `X-Request-Id` survives a bodiless error.
|
|
29
|
+
*/
|
|
30
|
+
export declare class SolviceError extends Error {
|
|
31
|
+
readonly status: number;
|
|
32
|
+
readonly response: Response;
|
|
33
|
+
readonly body?: ErrorBody | undefined;
|
|
34
|
+
/** The parsed response body as received, whatever its shape; `undefined` when there was none. */
|
|
35
|
+
readonly raw?: unknown | undefined;
|
|
36
|
+
constructor(status: number, response: Response, body?: ErrorBody | undefined,
|
|
37
|
+
/** The parsed response body as received, whatever its shape; `undefined` when there was none. */
|
|
38
|
+
raw?: unknown | undefined);
|
|
39
|
+
/** Seconds to wait before retrying, from the `Retry-After` header; `undefined` without one (or with an HTTP date). */
|
|
40
|
+
get retryAfterS(): number | undefined;
|
|
41
|
+
}
|
|
42
|
+
/** The client `createSolvice` returns, for naming it in your own signatures. */
|
|
43
|
+
export type Solvice = ReturnType<typeof createSolvice>;
|
|
44
|
+
export declare function createSolvice({ apiKey, baseUrl, fetch }: SolviceOptions): {
|
|
45
|
+
solve: (body: SolveRequest, options?: RequestOptions) => Promise<SolveResponse>;
|
|
46
|
+
evaluate: (body: EvaluateRequest, options?: RequestOptions) => Promise<SolveResponse>;
|
|
47
|
+
suggest: (body: SuggestRequest, options?: RequestOptions) => Promise<SuggestResponse>;
|
|
48
|
+
/** The same requests with `?validate_only=true`: no matrix fetch, no search. */
|
|
49
|
+
validate: {
|
|
50
|
+
solve: (body: SolveRequest, options?: RequestOptions) => Promise<ValidateOnlyResponse>;
|
|
51
|
+
evaluate: (body: EvaluateRequest, options?: RequestOptions) => Promise<ValidateOnlyResponse>;
|
|
52
|
+
suggest: (body: SuggestRequest, options?: RequestOptions) => Promise<ValidateOnlyResponse>;
|
|
53
|
+
};
|
|
54
|
+
/** The underlying openapi-fetch client, for anything the methods above do not cover. */
|
|
55
|
+
client: import("openapi-fetch").Client<paths, `${string}/${string}`>;
|
|
56
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import createClient from "openapi-fetch";
|
|
2
|
+
/**
|
|
3
|
+
* Any non-2xx response. `body` is the API's error body when the response
|
|
4
|
+
* carried one. A `401` or a `429` comes from the API gateway and does not use
|
|
5
|
+
* that body (a JSON `{ message, error }` or nothing): branch on `status`, and
|
|
6
|
+
* read `raw` for whatever came back. `response` is the `Response` itself, for
|
|
7
|
+
* its headers: every `503` carries `Retry-After` (`retryAfterS`), and
|
|
8
|
+
* `X-Request-Id` survives a bodiless error.
|
|
9
|
+
*/
|
|
10
|
+
export class SolviceError extends Error {
|
|
11
|
+
status;
|
|
12
|
+
response;
|
|
13
|
+
body;
|
|
14
|
+
raw;
|
|
15
|
+
constructor(status, response, body,
|
|
16
|
+
/** The parsed response body as received, whatever its shape; `undefined` when there was none. */
|
|
17
|
+
raw) {
|
|
18
|
+
super(body?.message ?? gatewayMessage(raw) ?? `Solvice API responded ${status}`);
|
|
19
|
+
this.status = status;
|
|
20
|
+
this.response = response;
|
|
21
|
+
this.body = body;
|
|
22
|
+
this.raw = raw;
|
|
23
|
+
this.name = "SolviceError";
|
|
24
|
+
}
|
|
25
|
+
/** Seconds to wait before retrying, from the `Retry-After` header; `undefined` without one (or with an HTTP date). */
|
|
26
|
+
get retryAfterS() {
|
|
27
|
+
const value = this.response.headers.get("retry-after");
|
|
28
|
+
if (value === null)
|
|
29
|
+
return undefined;
|
|
30
|
+
const seconds = Number(value);
|
|
31
|
+
return Number.isFinite(seconds) ? seconds : undefined;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
35
|
+
/** The API's error body has `status`, `code` and `message`; the gateway's `401` has `message` and `error`. */
|
|
36
|
+
const isErrorBody = (value) => isRecord(value) && "status" in value && "code" in value && "message" in value;
|
|
37
|
+
const gatewayMessage = (raw) => isRecord(raw) && typeof raw.message === "string" ? raw.message : undefined;
|
|
38
|
+
function unwrap({ data, error, response }) {
|
|
39
|
+
if (!response.ok) {
|
|
40
|
+
// openapi-fetch hands back the raw text when the error is not JSON, and "" for no body.
|
|
41
|
+
const raw = error === "" || error == null ? undefined : error;
|
|
42
|
+
throw new SolviceError(response.status, response, isErrorBody(raw) ? raw : undefined, raw);
|
|
43
|
+
}
|
|
44
|
+
return data;
|
|
45
|
+
}
|
|
46
|
+
// The spec types every 200 as `Response | ValidateOnlyResponse` (#1626 H).
|
|
47
|
+
// Which one comes back is decided by `validate_only`, which each method below
|
|
48
|
+
// fixes, so these two pick the matching side of the union.
|
|
49
|
+
const result = (r) => unwrap(r);
|
|
50
|
+
const validation = (r) => unwrap(r);
|
|
51
|
+
const VALIDATE_ONLY = { query: { validate_only: true } };
|
|
52
|
+
export function createSolvice({ apiKey, baseUrl = "https://api.solvice.io", fetch }) {
|
|
53
|
+
const client = createClient({ baseUrl, headers: { Authorization: apiKey }, fetch });
|
|
54
|
+
return {
|
|
55
|
+
solve: async (body, options) => result(await client.POST("/v3/routing/solve", { body, ...options })),
|
|
56
|
+
evaluate: async (body, options) => result(await client.POST("/v3/routing/evaluate", { body, ...options })),
|
|
57
|
+
suggest: async (body, options) => result(await client.POST("/v3/routing/suggest", { body, ...options })),
|
|
58
|
+
/** The same requests with `?validate_only=true`: no matrix fetch, no search. */
|
|
59
|
+
validate: {
|
|
60
|
+
solve: async (body, options) => validation(await client.POST("/v3/routing/solve", { body, params: VALIDATE_ONLY, ...options })),
|
|
61
|
+
evaluate: async (body, options) => validation(await client.POST("/v3/routing/evaluate", { body, params: VALIDATE_ONLY, ...options })),
|
|
62
|
+
suggest: async (body, options) => validation(await client.POST("/v3/routing/suggest", { body, params: VALIDATE_ONLY, ...options })),
|
|
63
|
+
},
|
|
64
|
+
/** The underlying openapi-fetch client, for anything the methods above do not cover. */
|
|
65
|
+
client,
|
|
66
|
+
};
|
|
67
|
+
}
|