@volant-autonomy/via-sdk 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Volant Autonomy
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 ADDED
@@ -0,0 +1,67 @@
1
+ # Volant Via SDK
2
+
3
+ `@volant-autonomy/via-sdk` is an auto-generated type-safe wrapper for the Via API with automatic authentication handling.
4
+ Usable in TypeScript or JavaScript.
5
+
6
+ **Note:** To use this SDK you need access to [the Via API](https://via.volantautonomy.com/api/v1.0/docs). Please contact us at via@volantautonomy.com to obtain access.
7
+
8
+ ## Installation
9
+
10
+ Run `npm install --save @volant-autonomy/via-sdk` to add the SDK as a dependency.
11
+
12
+ ## Basic usage
13
+
14
+ ```typescript
15
+ import createSDK from '@volant-autonomy/via-sdk'
16
+
17
+ const SDK = createSDK({
18
+ username: "username",
19
+ password: "password"
20
+ })
21
+
22
+ const {error, data} = await SDK.direct.getAllAircraft()
23
+ ```
24
+
25
+ ## Further information
26
+
27
+ ### SDK Layout
28
+
29
+ The SDK is split into three parts:
30
+
31
+ - Direct: Gives you direct access to every single API endpoint, returning the raw data
32
+ - Composite: Convenient wrappers around multi-endpoint operations
33
+ - Schemas: Every schema from the api as a TypeScript type
34
+
35
+ ### Auth
36
+
37
+ If you provide a username and password, the SDK will automatically check for expiry and refresh the authentication token whenever you make a request.
38
+
39
+ ### Request data format
40
+
41
+ In the API, data may be passed in through the body, the URL, the query, the header, etc. The SDK simplifies this, and just always takes data in as function arguments.
42
+
43
+ For example, `GET /flightplans/` takes in a filter from a query parameter. The SDK does this through function arguments, ie: `SDK.direct.getAllFlightplans(['Accepted'])` would filter for only accepted flightplans.
44
+
45
+ Another example, `GET /flightplans/{flightplan_id}` takes in a flightplan ID through the path. The SDK does this through function arguments, ie: `SDK.direct.getFlightplan('aaaaa-bbbbb-ccccc')`
46
+
47
+ ### Response data format
48
+
49
+ Direct functions return an object with a data field and an error field. Only exactly one of these is defined, the other is undefined.
50
+
51
+ The data returned is also not *identical* to the API, as any "links" are skipped over, to make the data easier to use.
52
+
53
+ For example, if the API would've returned:
54
+
55
+ ```js
56
+ {
57
+ link:"/url",
58
+ data: [
59
+ {data: 1, link: "/url"},
60
+ {data: 2, link: "/url"}
61
+ ]
62
+ }
63
+ ```
64
+
65
+ The SDK will instead just return `[1,2]`.
66
+
67
+ Keeping this in mind, [the API documentation](https://via.volantautonomy.com/api/v1.0/docs) is a great place to look for more information
@@ -0,0 +1,7 @@
1
+ import { Composite } from './composite';
2
+ import { Direct } from './direct';
3
+ import { FetcherArgs } from './fetch';
4
+ export declare function createSDK(opts: Partial<FetcherArgs>): {
5
+ direct: Direct;
6
+ composite: Composite;
7
+ };
package/dist/client.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createSDK = createSDK;
7
+ const composite_1 = require("./composite");
8
+ const direct_1 = require("./direct");
9
+ const fetch_1 = __importDefault(require("./fetch"));
10
+ const types_1 = require("./types");
11
+ function createSDK(opts) {
12
+ if (!opts.ignoreAuth && (opts.username === undefined || opts.password === undefined)) {
13
+ throw new types_1.UnauthenticatedError('ignoreAuth is not true and either username or password is missing');
14
+ }
15
+ const fetcher = (0, fetch_1.default)(opts);
16
+ const newDirect = new direct_1.Direct(fetcher);
17
+ return {
18
+ direct: newDirect,
19
+ composite: new composite_1.Composite(newDirect)
20
+ };
21
+ }
@@ -0,0 +1,150 @@
1
+ import { createPathingTask, Direct } from './direct';
2
+ import { schemas, Waypoint } from './types';
3
+ export declare class Composite {
4
+ private direct;
5
+ constructor(direct: Direct);
6
+ upsertFlightplan(args: schemas['Flightplan-Input'], id: string | null | undefined): Promise<{
7
+ data?: never;
8
+ error: {
9
+ errors: {
10
+ detail: string;
11
+ status: "401" | "400" | "422" | "403" | "404";
12
+ }[];
13
+ status: "401" | "400" | "422" | "403" | "404";
14
+ };
15
+ response: Response;
16
+ aborted: false;
17
+ } | {
18
+ data: {
19
+ id: string;
20
+ type?: "flightplan";
21
+ meta: import("./volant-schema").components["schemas"]["FlightplanMeta"];
22
+ attributes: import("./volant-schema").components["schemas"]["Flightplan-Output"];
23
+ };
24
+ error?: never;
25
+ response: Response;
26
+ aborted: false;
27
+ }>;
28
+ getAllDraftFlightplans(): Promise<{
29
+ data?: never;
30
+ error: {
31
+ errors: {
32
+ detail: string;
33
+ status: "401" | "400" | "422";
34
+ }[];
35
+ status: "401" | "400" | "422";
36
+ };
37
+ response: Response;
38
+ aborted: false;
39
+ } | {
40
+ data: {
41
+ id: string;
42
+ type?: "flightplan";
43
+ meta: import("./volant-schema").components["schemas"]["FlightplanMeta"];
44
+ attributes: import("./volant-schema").components["schemas"]["Flightplan-Output"];
45
+ }[];
46
+ error?: never;
47
+ response: Response;
48
+ aborted: false;
49
+ }>;
50
+ getAllClosedFlightplans(): Promise<{
51
+ data?: never;
52
+ error: {
53
+ errors: {
54
+ detail: string;
55
+ status: "401" | "400" | "422";
56
+ }[];
57
+ status: "401" | "400" | "422";
58
+ };
59
+ response: Response;
60
+ aborted: false;
61
+ } | {
62
+ data: {
63
+ id: string;
64
+ type?: "flightplan";
65
+ meta: import("./volant-schema").components["schemas"]["FlightplanMeta"];
66
+ attributes: import("./volant-schema").components["schemas"]["Flightplan-Output"];
67
+ }[];
68
+ error?: never;
69
+ response: Response;
70
+ aborted: false;
71
+ }>;
72
+ getAllPendingFlightplans(): Promise<{
73
+ data?: never;
74
+ error: {
75
+ errors: {
76
+ detail: string;
77
+ status: "401" | "400" | "422";
78
+ }[];
79
+ status: "401" | "400" | "422";
80
+ };
81
+ response: Response;
82
+ aborted: false;
83
+ } | {
84
+ data: {
85
+ id: string;
86
+ type?: "flightplan";
87
+ meta: import("./volant-schema").components["schemas"]["FlightplanMeta"];
88
+ attributes: import("./volant-schema").components["schemas"]["Flightplan-Output"];
89
+ }[];
90
+ error?: never;
91
+ response: Response;
92
+ aborted: false;
93
+ }>;
94
+ getAllAcceptedFlightplans(): Promise<{
95
+ data?: never;
96
+ error: {
97
+ errors: {
98
+ detail: string;
99
+ status: "401" | "400" | "422";
100
+ }[];
101
+ status: "401" | "400" | "422";
102
+ };
103
+ response: Response;
104
+ aborted: false;
105
+ } | {
106
+ data: {
107
+ id: string;
108
+ type?: "flightplan";
109
+ meta: import("./volant-schema").components["schemas"]["FlightplanMeta"];
110
+ attributes: import("./volant-schema").components["schemas"]["Flightplan-Output"];
111
+ }[];
112
+ error?: never;
113
+ response: Response;
114
+ aborted: false;
115
+ }>;
116
+ attemptAcceptFlightplan(id: string): Promise<{
117
+ data?: never;
118
+ error: {
119
+ errors: {
120
+ detail: string;
121
+ status: "401" | "400" | "422" | "404" | "409";
122
+ }[];
123
+ status: "401" | "400" | "422" | "404" | "409";
124
+ };
125
+ response: Response;
126
+ aborted: false;
127
+ } | {
128
+ data: boolean;
129
+ error?: never;
130
+ response: Response;
131
+ aborted: false;
132
+ }>;
133
+ /** Handles creating and polling a pathing task for you */
134
+ doPathingTask(args: createPathingTask['requestBody']['content']['application/json']): Promise<{
135
+ error: {
136
+ errors: {
137
+ detail: string;
138
+ status: "401" | "400" | "422" | "404";
139
+ }[];
140
+ status: "401" | "400" | "422" | "404";
141
+ };
142
+ data?: undefined;
143
+ } | {
144
+ data: Waypoint[];
145
+ error?: undefined;
146
+ } | {
147
+ error: number;
148
+ data?: undefined;
149
+ }>;
150
+ }
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Composite = void 0;
13
+ const types_1 = require("./types");
14
+ class Composite {
15
+ constructor(direct) {
16
+ this.direct = direct;
17
+ }
18
+ /// flightplans
19
+ upsertFlightplan(args, id) {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ if (id) {
22
+ return this.direct.modifyDraftFlightplan(id, args);
23
+ }
24
+ else {
25
+ return this.direct.createDraftFlightplan(args);
26
+ }
27
+ });
28
+ }
29
+ getAllDraftFlightplans() {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ return this.direct.getAllFlightplans(['Draft']);
32
+ });
33
+ }
34
+ getAllClosedFlightplans() {
35
+ return __awaiter(this, void 0, void 0, function* () {
36
+ return this.direct.getAllFlightplans(['Closed']);
37
+ });
38
+ }
39
+ getAllPendingFlightplans() {
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ return this.direct.getAllFlightplans(['Pending']);
42
+ });
43
+ }
44
+ getAllAcceptedFlightplans() {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ return this.direct.getAllFlightplans(['Accepted']);
47
+ });
48
+ }
49
+ // TODO: make error 409 more obvious, as it is different
50
+ attemptAcceptFlightplan(id) {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ return this.direct.changeFlightplanState(id, 'Accepted');
53
+ });
54
+ }
55
+ /// pathing tasks
56
+ /** Handles creating and polling a pathing task for you */
57
+ doPathingTask(args) {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ var _a, _b;
60
+ const { error, data } = yield this.direct.createPathingTask(args);
61
+ if (error) {
62
+ return { error };
63
+ }
64
+ const id = data.id;
65
+ while (true) {
66
+ const { data, error } = yield this.direct.getPathingTask(id);
67
+ if (error) {
68
+ return { error };
69
+ }
70
+ switch (data.meta.state) {
71
+ case 'queued':
72
+ yield (0, types_1.sleep)(500);
73
+ continue;
74
+ case 'in-progress':
75
+ yield (0, types_1.sleep)(50);
76
+ continue;
77
+ case 'successful':
78
+ if ((_a = data.attributes) === null || _a === void 0 ? void 0 : _a.waypoints) {
79
+ return { data: (_b = data.attributes) === null || _b === void 0 ? void 0 : _b.waypoints };
80
+ }
81
+ else {
82
+ throw new Error(`endpoint did not return waypoints ${data}`); // TODO:
83
+ }
84
+ case 'failed':
85
+ // TODO: this is bad, do this better.
86
+ return { error: data.meta.error_code };
87
+ }
88
+ }
89
+ });
90
+ }
91
+ }
92
+ exports.Composite = Composite;