rocket-launch-live-client 0.2.1 → 0.3.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/lib/esm/utils.js CHANGED
@@ -3,6 +3,9 @@ const getLeadingZero = (month, offset = 0) => {
3
3
  const str = "0".concat((month + offset).toString());
4
4
  return str.slice(-2);
5
5
  };
6
+ export const formatToRLLISODate = (date) => {
7
+ return date.toISOString().slice(0, 19).concat("Z");
8
+ };
6
9
  export const apiKeyValidator = (apiKey) => {
7
10
  if (apiKey === undefined) {
8
11
  error("RLL Client requires API Key", "type");
@@ -34,112 +37,94 @@ export const optionsValidator = (options) => {
34
37
  };
35
38
  const validators = {
36
39
  string: (option) => {
37
- return new Promise((resolve, reject) => {
38
- if (typeof option === "number") {
39
- resolve(option.toString());
40
- }
41
- if (typeof option !== "string") {
42
- return reject("Must be a string");
43
- }
44
- if (option.length <= 0) {
45
- return reject("String must have length greater than 0");
46
- }
47
- resolve(option);
48
- });
40
+ if (typeof option === "number") {
41
+ return option.toString();
42
+ }
43
+ if (typeof option !== "string") {
44
+ throw "Must be a string";
45
+ }
46
+ if (option.length <= 0) {
47
+ throw "String must have length greater than 0";
48
+ }
49
+ return option;
49
50
  },
50
51
  boolean: (option) => {
51
- return new Promise((resolve, reject) => {
52
- if (typeof option !== "boolean") {
53
- return reject("Must be a boolean");
54
- }
55
- resolve(option === true ? 1 : 0);
56
- });
52
+ if (typeof option !== "boolean") {
53
+ throw "Must be a boolean";
54
+ }
55
+ return option === true ? 1 : 0;
57
56
  },
58
57
  number: (option) => {
59
- return new Promise((resolve, reject) => {
60
- if (typeof option === "number") {
61
- return resolve(option);
62
- }
63
- if (typeof option !== "string" ||
64
- option === "" ||
65
- isNaN(Number(option))) {
66
- return reject("Must be a number");
67
- }
68
- else {
69
- resolve(Number(option));
70
- }
71
- });
58
+ if (typeof option === "number") {
59
+ return option;
60
+ }
61
+ if (typeof option !== "string" || option === "" || isNaN(Number(option))) {
62
+ throw "Must be a number";
63
+ }
64
+ else {
65
+ return Number(option);
66
+ }
72
67
  },
73
68
  countryCode: (option) => {
74
- return new Promise((resolve, reject) => {
75
- if (typeof option !== "string") {
76
- return reject("Must be a string");
77
- }
78
- if (!isValidCountryCode(option)) {
79
- return reject("Invalid country code. Country codes should follow ISO 3166-1 A2 convention, like 'US'.");
80
- }
81
- resolve(option);
82
- });
69
+ if (typeof option !== "string") {
70
+ throw "Must be a string";
71
+ }
72
+ if (!isValidCountryCode(option)) {
73
+ throw "Invalid country code. Country codes should follow ISO 3166-1 A2 convention, like 'US'.";
74
+ }
75
+ return option;
83
76
  },
84
77
  stateCode: (option) => {
85
- return new Promise((resolve, reject) => {
86
- if (typeof option !== "string") {
87
- return reject("Must be a string");
88
- }
89
- if (!isValidStateCode(option)) {
90
- return reject("Invalid United States State Code. State Codes should follow ISO 3166-2 convention, like 'FL'.");
91
- }
92
- resolve(option);
93
- });
78
+ if (typeof option !== "string") {
79
+ throw "Must be a string";
80
+ }
81
+ if (!isValidStateCode(option)) {
82
+ throw "Invalid United States State Code. State Codes should follow ISO 3166-2 convention, like 'FL'.";
83
+ }
84
+ return option;
94
85
  },
95
86
  cosparId: (option) => {
96
- return new Promise((resolve, reject) => {
97
- if (typeof option !== "string") {
98
- return reject("Must be a string");
99
- }
100
- if (!option.match(/^\d{4}\-\d{3}$/g)) {
101
- return reject("Cospar IDs must be in the format of YYYY-NNN (eg. 2023-123)");
102
- }
103
- resolve(option);
104
- });
87
+ if (typeof option !== "string") {
88
+ throw "Must be a string";
89
+ }
90
+ if (!option.match(/^\d{4}\-\d{3}$/g)) {
91
+ throw "Cospar IDs must be in the format of YYYY-NNN (eg. 2023-123)";
92
+ }
93
+ return option;
105
94
  },
106
95
  shortDate: (option) => {
107
- return new Promise((resolve, reject) => {
108
- if (typeof option === "string" || option instanceof Date) {
109
- let date;
110
- if (typeof option === "string") {
111
- const parsed = Date.parse(option);
112
- if (isNaN(parsed)) {
113
- return reject("Must be an ISO 8601 Date String");
114
- }
115
- date = new Date(parsed);
116
- }
117
- else {
118
- date = option;
119
- }
120
- resolve(`${date.getUTCFullYear()}-${getLeadingZero(date.getUTCMonth(), 1)}-${getLeadingZero(date.getUTCDate())}`);
96
+ if (typeof option !== "string" && !(option instanceof Date)) {
97
+ throw "Must be a JavaScript Date Object or ISO 8601 Date String";
98
+ }
99
+ let date;
100
+ if (typeof option === "string") {
101
+ const parsed = Date.parse(option);
102
+ if (isNaN(parsed)) {
103
+ throw "Must be an ISO 8601 Date String";
121
104
  }
122
- return reject("Must be a JavaScript Date Object or ISO 8601 Date String");
123
- });
105
+ date = new Date(parsed);
106
+ }
107
+ else {
108
+ date = option;
109
+ }
110
+ return `${date.getUTCFullYear()}-${getLeadingZero(date.getUTCMonth(), 1)}-${getLeadingZero(date.getUTCDate())}`;
124
111
  },
125
112
  isoDate: (option) => {
126
- return new Promise((resolve, reject) => {
127
- if (typeof option === "string" || option instanceof Date) {
128
- let date;
129
- if (typeof option === "string") {
130
- const parsed = Date.parse(option);
131
- if (isNaN(parsed)) {
132
- return reject("Must be an ISO 8601 Date String");
133
- }
134
- date = new Date(parsed);
135
- }
136
- else {
137
- date = option;
138
- }
139
- resolve(date.toISOString().slice(0, 19).concat("Z"));
113
+ if (typeof option !== "string" && !(option instanceof Date)) {
114
+ throw "Must be a JavaScript Date Object or ISO 8601 Date String";
115
+ }
116
+ let date;
117
+ if (typeof option === "string") {
118
+ const parsed = Date.parse(option);
119
+ if (isNaN(parsed)) {
120
+ throw "Must be an ISO 8601 Date String";
140
121
  }
141
- return reject("Must be a JavaScript Date Object or ISO 8601 Date String");
142
- });
122
+ date = new Date(parsed);
123
+ }
124
+ else {
125
+ date = option;
126
+ }
127
+ return formatToRLLISODate(date);
143
128
  },
144
129
  };
145
130
  const optionsMap = {
@@ -199,9 +184,18 @@ const optionsMap = {
199
184
  };
200
185
  export const queryOptionsValidator = (resource, options) => {
201
186
  const params = new URLSearchParams();
202
- const paramsPromises = [];
203
- if (!options) {
204
- return Promise.resolve(params);
187
+ if (options === undefined) {
188
+ return params;
189
+ }
190
+ if (typeof options === "string" ||
191
+ typeof options === "number" ||
192
+ typeof options === "boolean" ||
193
+ typeof options === "bigint" ||
194
+ typeof options === "symbol" ||
195
+ typeof options === "function" ||
196
+ options === null ||
197
+ Array.isArray(options)) {
198
+ error("Invalid type for query options. Must be an object.");
205
199
  }
206
200
  if ((options.id || "slug" in options || "cospar_id" in options) &&
207
201
  Object.keys(options).length > 1) {
@@ -216,14 +210,15 @@ export const queryOptionsValidator = (resource, options) => {
216
210
  warn(`Parameter "${option}" is undefined and will be ignored.`);
217
211
  continue;
218
212
  }
219
- const promise = optionsMap[resource][option](options[option])
220
- .then((o) => params.set(option, o))
221
- .catch((err) => {
222
- throw `Malformed query parameter for resource "${resource}" and parameter: "${option}": ${err}.`;
223
- });
224
- paramsPromises.push(promise);
213
+ try {
214
+ const o = optionsMap[resource][option](options[option]);
215
+ params.set(option, o);
216
+ }
217
+ catch (err) {
218
+ error(`Malformed query parameter for resource "${resource}" and parameter: "${option}": ${err}.`, "type");
219
+ }
225
220
  }
226
- return Promise.all(paramsPromises).then(() => params);
221
+ return params;
227
222
  };
228
223
  export const warn = (msg) => console.warn(`[RLL Client]: ${msg}`);
229
224
  export const error = (msg, type = "error") => {
package/package.json CHANGED
@@ -1,17 +1,14 @@
1
1
  {
2
2
  "name": "rocket-launch-live-client",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "A Node.JS client for interacting with the RocketLaunch.Live API",
5
5
  "types": "./lib/cjs/types/index.d.ts",
6
6
  "main": "./lib/cjs/index.js",
7
7
  "scripts": {
8
8
  "clear": "rm -rf ./lib",
9
- "dev": "nodemon ./lib/test.js --ignore 'lib/**' -e ts --exec \"npm run compile\"",
10
- "compile": "npm run clear && tsc && node ./lib/test.js",
11
9
  "build": "npm run clear && npm run build:esm && npm run build:cjs",
12
10
  "build:esm": "tsc -p ./configs/tsconfig.esm.json && mv lib/esm/index.js lib/esm/index.mjs",
13
11
  "build:cjs": "tsc -p ./configs/tsconfig.cjs.json",
14
- "start": "node ./lib/test.js",
15
12
  "prepublish": "npm run build",
16
13
  "test": "mocha",
17
14
  "test:watch": "mocha --watch"