rocket-launch-live-client 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/src/Client.ts ADDED
@@ -0,0 +1,262 @@
1
+ import {
2
+ RLLClientOptions,
3
+ RLLEndPoint,
4
+ RLLEntity,
5
+ RLLQueryConfig,
6
+ RLLResponse,
7
+ } from "./types/application";
8
+ import { fetcher } from "./fetcher";
9
+ import {
10
+ apiKeyValidator,
11
+ optionsValidator,
12
+ queryOptionsValidator,
13
+ } from "./utils";
14
+
15
+ class RLLClient {
16
+ private apiKey: string;
17
+ private config = {
18
+ keyInQueryParams: false,
19
+ };
20
+
21
+ constructor(apiKey: string, options?: RLLClientOptions) {
22
+ // Validate API Key is a valid UUID format
23
+ // Constructor throws if not
24
+ apiKeyValidator(apiKey);
25
+ this.apiKey = apiKey;
26
+
27
+ if (!options) {
28
+ return;
29
+ }
30
+
31
+ // Validate Options with warnings or throws
32
+ optionsValidator(options);
33
+
34
+ if (options.keyInQueryParams) {
35
+ this.config.keyInQueryParams = options.keyInQueryParams;
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Used internally to make API query
41
+ *
42
+ * @private
43
+ *
44
+ * @template T
45
+ *
46
+ * @param {string} endpoint - API endpoint (ie. "/launches")
47
+ * @param {URLSearchParams} params - API Search Params
48
+ *
49
+ * @returns {Promise<T>}
50
+ */
51
+ private query<T>(endpoint: string, params: URLSearchParams): Promise<T> {
52
+ return fetcher<T>(
53
+ this.apiKey,
54
+ endpoint,
55
+ params,
56
+ this.config.keyInQueryParams
57
+ );
58
+ }
59
+
60
+ /**
61
+ * Fetch launch companies
62
+ *
63
+ * @public
64
+ * @function
65
+ *
66
+ * @param {Object} [options] - Launch Company Search Options
67
+ * @param {number | string} options.id - Company id
68
+ * @param {number | string} options.page - Page number of results
69
+ * @param {number | string} options.name - Company name
70
+ * @param {ISO3166Alpha2.CountryCode} options.country_code - ISO 3166 Alpha 2 Country Code
71
+ * @param {boolean} options.inactive - Company inactive status
72
+ *
73
+ * @returns {Promise<RLLResponse<RLLEntity.Company[]>>} Array of companies wrapped in a standard RLL Response
74
+ *
75
+ * @example
76
+ *
77
+ * const response = client.companies({ country_code: "US" })
78
+ */
79
+ public companies(
80
+ options?: RLLQueryConfig.Companies
81
+ ): Promise<RLLResponse<RLLEntity.Company[]>> {
82
+ return queryOptionsValidator(RLLEndPoint.COMPANIES, options).then(
83
+ (params) =>
84
+ this.query<RLLResponse<RLLEntity.Company[]>>(
85
+ RLLEndPoint.COMPANIES,
86
+ params
87
+ )
88
+ );
89
+ }
90
+
91
+ /**
92
+ * Fetch launches
93
+ *
94
+ * @public
95
+ * @function
96
+ *
97
+ * @param {Object} [options] - Launch Search Options
98
+ * @param {number | string} options.id - Launch id
99
+ * @param {number | string} options.page - Page number of results
100
+ * @param {string} options.cospar_id - Launch COSPAR ID (ie. 2022-123)
101
+ * @param {Date | string} options.before_date - Only return launches before this date
102
+ * @param {Date | string} options.after_date - Only return launches after this date
103
+ * @param {Date | string} options.modified_since - Only return launches with API changes after this date
104
+ * @param {number | string} options.location_id - Launches from this Location
105
+ * @param {number | string} options.pad_id - Launches from this Pad
106
+ * @param {number | string} options.provider_id - Launches from this Company
107
+ * @param {number | string} options.tag_id - Launches with this Tag
108
+ * @param {number | string} options.vehicle_id - Launches on this Vehicle
109
+ * @param {ISO3166Alpha2.StateCodeUS} options.state_abbr - ISO 3166 Alpha 2 US State Code
110
+ * @param {ISO3166Alpha2.CountryCode} options.country_code - ISO 3166 Alpha 2 Country Code
111
+ * @param {number | string} options.search - Launches matching this search string
112
+ * @param {number | string} options.slug - Launches matching this unique slug
113
+ *
114
+ * @returns {Promise<RLLResponse<RLLEntity.Launch[]>>} Array of companies wrapped in a standard RLL Response
115
+ *
116
+ * @example
117
+ *
118
+ * const response = client.launches({ after_date: new Date("2022-10-10") })
119
+ */
120
+ public launches(
121
+ options?: RLLQueryConfig.Launches
122
+ ): Promise<RLLResponse<RLLEntity.Launch[]>> {
123
+ return queryOptionsValidator(RLLEndPoint.LAUNCHES, options).then((params) =>
124
+ this.query<RLLResponse<RLLEntity.Launch[]>>(RLLEndPoint.LAUNCHES, params)
125
+ );
126
+ }
127
+
128
+ /**
129
+ * Fetch launch locations
130
+ *
131
+ * @public
132
+ * @function
133
+ *
134
+ * @param {Object} [options] - Launch Location Search Options
135
+ * @param {number | string} options.id - Location id
136
+ * @param {number | string} options.page - Page number of results
137
+ * @param {number | string} options.name - Location name
138
+ * @param {ISO3166Alpha2.StateCodeUS} options.state_abbr - ISO 3166 Alpha 2 US State Code
139
+ * @param {ISO3166Alpha2.CountryCode} options.country_code - ISO 3166 Alpha 2 Country Code
140
+ *
141
+ * @returns {Promise<RLLResponse<RLLEntity.Location[]>>} Array of companies wrapped in a standard RLL Response
142
+ *
143
+ * @example
144
+ *
145
+ * const response = client.locations({ country_code: "US" })
146
+ */
147
+ public locations(
148
+ options?: RLLQueryConfig.Locations
149
+ ): Promise<RLLResponse<RLLEntity.Location[]>> {
150
+ return queryOptionsValidator(RLLEndPoint.LOCATIONS, options).then(
151
+ (params) =>
152
+ this.query<RLLResponse<RLLEntity.Location[]>>(
153
+ RLLEndPoint.LOCATIONS,
154
+ params
155
+ )
156
+ );
157
+ }
158
+
159
+ /**
160
+ * Fetch launch Missions
161
+ *
162
+ * @public
163
+ * @function
164
+ *
165
+ * @param {Object} [options] - Launch Mission Search Options
166
+ * @param {number | string} options.id - Mission id
167
+ * @param {number | string} options.page - Page number of results
168
+ * @param {number | string} options.name - Mission name
169
+ *
170
+ * @returns {Promise<RLLResponse<RLLEntity.Mission[]>>} Array of companies wrapped in a standard RLL Response
171
+ *
172
+ * @example
173
+ *
174
+ * const response = client.missions({ name: "Mars 2020" })
175
+ */
176
+ public missions(
177
+ options?: RLLQueryConfig.Missions
178
+ ): Promise<RLLResponse<RLLEntity.Mission[]>> {
179
+ return queryOptionsValidator(RLLEndPoint.MISSIONS, options).then((params) =>
180
+ this.query<RLLResponse<RLLEntity.Mission[]>>(RLLEndPoint.MISSIONS, params)
181
+ );
182
+ }
183
+
184
+ /**
185
+ * Fetch launch pads
186
+ *
187
+ * @public
188
+ * @function
189
+ *
190
+ * @param {Object} [options] - Launch Pad Search Options
191
+ * @param {number | string} options.id - Pad id
192
+ * @param {number | string} options.page - Page number of results
193
+ * @param {number | string} options.name - Pad name
194
+ * @param {ISO3166Alpha2.StateCodeUS} options.state_abbr - ISO 3166 Alpha 2 US State Code
195
+ * @param {ISO3166Alpha2.CountryCode} options.country_code - ISO 3166 Alpha 2 Country Code
196
+ *
197
+ * @returns {Promise<RLLResponse<RLLEntity.Pad[]>>} Array of companies wrapped in a standard RLL Response
198
+ *
199
+ * @example
200
+ *
201
+ * const response = client.pads({ country_code: "US" })
202
+ */
203
+ public pads(
204
+ options?: RLLQueryConfig.Pads
205
+ ): Promise<RLLResponse<RLLEntity.Pad[]>> {
206
+ return queryOptionsValidator(RLLEndPoint.PADS, options).then((params) =>
207
+ this.query<RLLResponse<RLLEntity.Pad[]>>(RLLEndPoint.PADS, params)
208
+ );
209
+ }
210
+
211
+ /**
212
+ * Fetch launch tags
213
+ *
214
+ * @public
215
+ * @function
216
+ *
217
+ * @param {Object} [options] - Launch Tag Search Options
218
+ * @param {number | string} options.id - Tag id
219
+ * @param {number | string} options.page - Page number of results
220
+ * @param {number | string} options.text - Tag text
221
+ *
222
+ * @returns {Promise<RLLResponse<RLLEntity.Tag[]>>} Array of companies wrapped in a standard RLL Response
223
+ *
224
+ * @example
225
+ *
226
+ * const response = client.tags({ text: "Crewed" })
227
+ */
228
+ public tags(
229
+ options?: RLLQueryConfig.Tags
230
+ ): Promise<RLLResponse<RLLEntity.Tag[]>> {
231
+ return queryOptionsValidator(RLLEndPoint.TAGS, options).then((params) =>
232
+ this.query<RLLResponse<RLLEntity.Tag[]>>(RLLEndPoint.TAGS, params)
233
+ );
234
+ }
235
+
236
+ /**
237
+ * Fetch launch Vehicles
238
+ *
239
+ * @public
240
+ * @function
241
+ *
242
+ * @param {Object} [options] - Launch Vehicles Search Options
243
+ * @param {number | string} options.id - Vehicle id
244
+ * @param {number | string} options.page - Page number of results
245
+ * @param {number | string} options.name - Vehicle name
246
+ *
247
+ * @returns {Promise<RLLResponse<RLLEntity.Vehicle[]>>} Array of companies wrapped in a standard RLL Response
248
+ *
249
+ * @example
250
+ *
251
+ * const response = client.vehicles({ name: "Falcon 9" })
252
+ */
253
+ public vehicles(
254
+ options?: RLLQueryConfig.Vehicles
255
+ ): Promise<RLLResponse<RLLEntity.Vehicle[]>> {
256
+ return queryOptionsValidator(RLLEndPoint.VEHICLES, options).then((params) =>
257
+ this.query<RLLResponse<RLLEntity.Vehicle[]>>(RLLEndPoint.VEHICLES, params)
258
+ );
259
+ }
260
+ }
261
+
262
+ export default RLLClient;
package/src/fetcher.ts ADDED
@@ -0,0 +1,50 @@
1
+ const https = require("node:https");
2
+
3
+ const BASE_URL = "https://fdo.rocketlaunch.live";
4
+
5
+ export const fetcher = <T>(
6
+ apiKey: string,
7
+ endpoint: string,
8
+ params: URLSearchParams,
9
+ keyInQueryParams: boolean
10
+ ): Promise<T> => {
11
+ const url = new URL("json/" + endpoint, BASE_URL);
12
+ let headers: HeadersInit | undefined;
13
+
14
+ if (keyInQueryParams) {
15
+ params.set("key", apiKey);
16
+ } else {
17
+ headers = { authorization: `Bearer ${apiKey}` };
18
+ }
19
+
20
+ params.forEach((v, k) => url.searchParams.set(k, v));
21
+
22
+ return query<T>(url, headers);
23
+ };
24
+
25
+ const query = <T>(url: URL, headers?: HeadersInit): Promise<T> => {
26
+ return new Promise((resolve, reject) => {
27
+ const req = https.get(url, { headers }, (res) => {
28
+ let data = [];
29
+
30
+ res.on("data", (chunk) => data.push(chunk));
31
+
32
+ res.on("end", () => {
33
+ if (res.statusCode === 200) {
34
+ const response = Buffer.concat(data).toString();
35
+ resolve(JSON.parse(response));
36
+ } else if (res.statusCode === 404) {
37
+ reject({
38
+ error: "Resource not found",
39
+ statusCode: 404,
40
+ message:
41
+ "The server returned a 404 Not Found response. Check that your API key is valid, and that you are not requesting a page number beyond the available results.",
42
+ });
43
+ } else {
44
+ reject(res);
45
+ }
46
+ });
47
+ });
48
+ req.on("error", reject);
49
+ });
50
+ };
package/src/index.ts ADDED
@@ -0,0 +1,29 @@
1
+ import RLLClient from "./Client";
2
+ import { RLLClientOptions } from "./types/application";
3
+
4
+ /**
5
+ * Generate a RocketLaunch.Live client
6
+ *
7
+ * @public
8
+ * @function
9
+ *
10
+ * @param {string} apiKey - Your RocketLaunch.Live API Key
11
+ * @param {Object} [options] - Optional Client Configuration options
12
+ * @param {boolean} options.keyInQueryParams - Set to true to send your API Key via Query parameters instead of Authorization Header (not recommended)
13
+ *
14
+ * @returns {RLLCLient}
15
+ *
16
+ * @example
17
+ *
18
+ * const MY_KEY = process.env.ROCKETLAUNCH_LIVE_API_KEY
19
+ *
20
+ * const client = clientGenerator(MY_KEY, { keyInQueryParams: true })
21
+ */
22
+ const clientGenerator = (
23
+ apiKey: string,
24
+ options?: RLLClientOptions
25
+ ): RLLClient => {
26
+ return new RLLClient(apiKey, options);
27
+ };
28
+
29
+ export default clientGenerator;
@@ -0,0 +1,193 @@
1
+ import { ISO3166Alpha2 } from "./standards";
2
+
3
+ export enum RLLEndPoint {
4
+ COMPANIES = "companies",
5
+ LAUNCHES = "launches",
6
+ LOCATIONS = "locations",
7
+ MISSIONS = "missions",
8
+ PADS = "pads",
9
+ TAGS = "tags",
10
+ VEHICLES = "vehicles",
11
+ }
12
+
13
+ interface RLLRecord {
14
+ id: number;
15
+ }
16
+
17
+ export namespace RLLEntity {
18
+ interface Country {
19
+ name: string;
20
+ code: ISO3166Alpha2.CountryCode;
21
+ }
22
+
23
+ interface State {
24
+ name: string;
25
+ abbr: ISO3166Alpha2.StateCodeUS;
26
+ }
27
+
28
+ export interface Company extends RLLRecord {
29
+ country: Country;
30
+ inactive: boolean | null;
31
+ }
32
+
33
+ enum LaunchResult {
34
+ NOT_SET = -1,
35
+ FAILURE = 0,
36
+ SUCCESS = 1,
37
+ PARTIAL_FAILURE = 2,
38
+ IN_FLIGHT_ABORT_CREWED = 3,
39
+ }
40
+
41
+ interface Media extends RLLRecord {
42
+ media_url: string | null;
43
+ youtube_vidid: string | null;
44
+ featured: boolean;
45
+ ldfeatured: boolean;
46
+ approved: boolean;
47
+ }
48
+
49
+ export interface Launch extends RLLRecord {
50
+ name: string;
51
+ cospar_id: string;
52
+ sort_date: string;
53
+ provider: Omit<Company, "inactive" | "country">;
54
+ vehicle: Omit<Vehicle, "company_id" | "company">;
55
+ pad: Omit<Pad, "full_name">;
56
+ missions: Omit<Mission, "launch_id">[];
57
+ mission_description: string | null;
58
+ launch_description: string;
59
+ win_open: string | null;
60
+ t0: string | null;
61
+ win_close: string | null;
62
+ est_date: {
63
+ month: number | null;
64
+ day: number | null;
65
+ year: number | null;
66
+ quarter: number | null;
67
+ };
68
+ date_str: string;
69
+ tags: Omit<Tag, "slug">[];
70
+ slug: string;
71
+ weather_summary: string | null;
72
+ weather_temp: number | null;
73
+ weather_icon: string | null;
74
+ weather_updated: string | null;
75
+ quicktext: string;
76
+ media?: Media[];
77
+ result: LaunchResult;
78
+ suborbital: boolean;
79
+ modified: string;
80
+ }
81
+
82
+ export interface Location extends RLLRecord {
83
+ name: string;
84
+ latitute: string; // original API had a typo which was preserved for backwards compatibility
85
+ latitude: string;
86
+ longitude: string;
87
+ state: State | null;
88
+ statename?: string | null;
89
+ country: Country;
90
+ pads: Omit<Pad, "location" | "country" | "state">[];
91
+ utc_offset: number;
92
+ }
93
+
94
+ export interface Mission extends RLLRecord {
95
+ name: string;
96
+ description: string | null;
97
+ launch_id: number;
98
+ company: Omit<Company, "slug" | "inactive" | "country">;
99
+ }
100
+
101
+ export interface Pad extends RLLRecord {
102
+ name: string;
103
+ full_name: string;
104
+ location: Omit<Location, "pads" | "utc_offset" | "latitute">;
105
+ }
106
+
107
+ export interface Tag extends RLLRecord {
108
+ text: string;
109
+ slug: string;
110
+ }
111
+
112
+ export interface Vehicle extends RLLRecord {
113
+ name: string;
114
+ company_id?: number;
115
+ company: Omit<Company, "slug" | "inactive" | "country">;
116
+ }
117
+ }
118
+
119
+ interface RLLBaseQueryConfig {
120
+ id?: number | string;
121
+ page?: number | string;
122
+ }
123
+
124
+ export namespace RLLQueryConfig {
125
+ export interface Companies extends RLLBaseQueryConfig {
126
+ name?: string | number;
127
+ country_code?: ISO3166Alpha2.CountryCode;
128
+ inactive?: boolean;
129
+ }
130
+
131
+ export interface Launches extends RLLBaseQueryConfig {
132
+ cospar_id?: string;
133
+ after_date?: Date | string;
134
+ before_date?: Date | string;
135
+ modified_since?: Date | string;
136
+ location_id?: number | string;
137
+ pad_id?: number | string;
138
+ provider_id?: number | string;
139
+ tag_id?: number | string;
140
+ vehicle_id?: number | string;
141
+ state_abbr?: ISO3166Alpha2.StateCodeUS;
142
+ country_code?: ISO3166Alpha2.CountryCode;
143
+ search?: string | number;
144
+ slug?: string | number;
145
+ }
146
+
147
+ export interface Locations extends RLLBaseQueryConfig {
148
+ name?: string | number;
149
+ state_abbr?: ISO3166Alpha2.StateCodeUS;
150
+ country_code?: ISO3166Alpha2.CountryCode;
151
+ }
152
+
153
+ export interface Missions extends RLLBaseQueryConfig {
154
+ name?: string | number;
155
+ }
156
+
157
+ export interface Pads extends RLLBaseQueryConfig {
158
+ name?: string | number;
159
+ state_abbr?: ISO3166Alpha2.StateCodeUS;
160
+ country_code?: ISO3166Alpha2.CountryCode;
161
+ }
162
+
163
+ export interface Tags extends RLLBaseQueryConfig {
164
+ text?: string | number;
165
+ }
166
+
167
+ export interface Vehicles extends RLLBaseQueryConfig {
168
+ name?: string | number;
169
+ }
170
+
171
+ export interface All
172
+ extends Companies,
173
+ Launches,
174
+ Locations,
175
+ Missions,
176
+ Pads,
177
+ Tags,
178
+ Vehicles {}
179
+ }
180
+
181
+ export type RLLClientOptions = {
182
+ keyInQueryParams?: boolean;
183
+ };
184
+
185
+ export type RLLResponse<T> = {
186
+ errors?: string[];
187
+ valid_auth: boolean;
188
+ count: number;
189
+ limit: number;
190
+ total: number;
191
+ last_page: number;
192
+ result: T;
193
+ };