wili 0.6.0 → 0.7.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 ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2018-present Jan T. Sott
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.de.md CHANGED
@@ -17,9 +17,9 @@ Node Wrapper für die Echtzeitdaten-API der Wiener Linien
17
17
  Beispielanwendung:
18
18
 
19
19
  ```js
20
- import { WienerLinien } from 'wili';
20
+ import { createWienerLinien } from 'wili';
21
21
 
22
- const wili = new WienerLinien();
22
+ const wili = createWienerLinien();
23
23
 
24
24
  const options = {
25
25
  relatedLine: ['U2', 'U4', 'U6']
@@ -48,10 +48,10 @@ try {
48
48
  **Beispiel**
49
49
 
50
50
  ```js
51
- import { WienerLinien } from 'wili';
51
+ import { createWienerLinien } from 'wili';
52
52
  import fetch from 'isomorphic-fetch';
53
53
 
54
- const wili = new WienerLinien(fetch);
54
+ const wili = createWienerLinien(fetch);
55
55
  ```
56
56
 
57
57
  ## API
package/README.md CHANGED
@@ -17,9 +17,9 @@ A Node wrapper for the Wiener Linien public transport API
17
17
  Example usage in script:
18
18
 
19
19
  ```js
20
- import { WienerLinien } from 'wili';
20
+ import { createWienerLinien } from 'wili';
21
21
 
22
- const wili = new WienerLinien();
22
+ const wili = createWienerLinien();
23
23
 
24
24
  const options = {
25
25
  relatedLine: ['U2', 'U4', 'U6']
@@ -48,10 +48,10 @@ try {
48
48
  **Example**
49
49
 
50
50
  ```js
51
- import { WienerLinien } from 'wili';
51
+ import { createWienerLinien } from 'wili';
52
52
  import fetch from 'isomorphic-fetch';
53
53
 
54
- const wili = new WienerLinien(fetch);
54
+ const wili = createWienerLinien(fetch);
55
55
  ```
56
56
 
57
57
  ## API
package/lib/wili.mjs CHANGED
@@ -1,42 +1,22 @@
1
1
  // src/index.ts
2
- var WienerLinien = class {
3
- #fetch;
4
- constructor(fetchParam = globalThis.fetch) {
5
- if (typeof fetch !== "function") {
6
- throw new TypeError("The supplied fetch parameter is not a function");
7
- }
8
- this.#fetch = fetchParam;
2
+ function createWienerLinien(fetchParam = globalThis.fetch) {
3
+ if (typeof fetchParam !== "function") {
4
+ throw new TypeError("The supplied fetch parameter is not a function");
9
5
  }
10
- /**
11
- * Returns real-time data for a station
12
- * @param {StringNumbers} rbl - RBL number
13
- * @param {MonitorOptions} options
14
- * @returns {object}
15
- */
16
- monitor(rbl, options = {}) {
6
+ async function monitor(rbl, options = {}) {
17
7
  const urlParams = {
18
8
  ...options,
19
9
  rbl
20
10
  };
21
- return this.#apiCall("/monitor", urlParams);
11
+ return await apiCall("/monitor", urlParams);
22
12
  }
23
- /**
24
- * Returns news, elevator maintenance and other information
25
- * @param {NewsListOptions} options
26
- * @returns {object}
27
- */
28
- newsList(options = {}) {
29
- return this.#apiCall("newsList", options);
13
+ async function newsList(options = {}) {
14
+ return await apiCall("newsList", options);
30
15
  }
31
- /**
32
- * Returns interruption of operations and elevator outage
33
- * @param {TrafficInfoOptions} options
34
- * @returns {object}
35
- */
36
- trafficInfoList(options = {}) {
37
- return this.#apiCall("trafficInfoList", options);
16
+ async function trafficInfoList(options = {}) {
17
+ return await apiCall("trafficInfoList", options);
38
18
  }
39
- #buildUrl(urlPath, urlParams) {
19
+ function buildUrl(urlPath, urlParams) {
40
20
  const searchParams = new URLSearchParams();
41
21
  for (const [key, value] of Object.entries(urlParams)) {
42
22
  if (Array.isArray(value)) {
@@ -49,18 +29,32 @@ var WienerLinien = class {
49
29
  }
50
30
  return new URL(`https://www.wienerlinien.at/ogd_realtime/${urlPath}?${searchParams.toString()}`);
51
31
  }
52
- #checkStatus(response) {
32
+ async function checkStatus(response) {
53
33
  if (response.ok) {
54
- return Promise.resolve(response);
34
+ return response;
55
35
  } else {
56
- return Promise.reject(new Error(response.statusText));
36
+ throw new Error(response.statusText);
57
37
  }
58
38
  }
59
- #apiCall(urlPath, urlParams) {
60
- const url = this.#buildUrl(urlPath, urlParams);
61
- return this.#fetch(url.href).then(this.#checkStatus).then((response) => response.json()).then((json) => json.data).catch(console.error);
39
+ async function apiCall(urlPath, urlParams) {
40
+ const url = buildUrl(urlPath, urlParams);
41
+ try {
42
+ const response = await fetchParam(url.href);
43
+ const checkedResponse = await checkStatus(response);
44
+ const json = await checkedResponse.json();
45
+ return json.data;
46
+ } catch (error) {
47
+ console.error(error);
48
+ }
62
49
  }
63
- };
50
+ return {
51
+ monitor,
52
+ newsList,
53
+ trafficInfoList
54
+ };
55
+ }
56
+ var WienerLinien = createWienerLinien();
64
57
  export {
65
- WienerLinien
58
+ WienerLinien,
59
+ createWienerLinien
66
60
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wili",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "A Node client for the Wiener Linien public transport API",
5
5
  "author": "Jan T. Sott",
6
6
  "license": "MIT",
@@ -11,7 +11,6 @@
11
11
  "node": ">=16.13 || >=18"
12
12
  },
13
13
  "files": [
14
- "data/",
15
14
  "lib/",
16
15
  "types/",
17
16
  "LICENSE",
@@ -29,7 +28,7 @@
29
28
  "prepack": "npm run build",
30
29
  "prepare": "husky install",
31
30
  "start": "npm run build -- --watch",
32
- "test": "uvu tests"
31
+ "test": "uvu -r tsm tests"
33
32
  },
34
33
  "keywords": [
35
34
  "wiener linien",
@@ -38,7 +37,6 @@
38
37
  "öpvn",
39
38
  "public transport"
40
39
  ],
41
- "dependencies": {},
42
40
  "devDependencies": {
43
41
  "@types/isomorphic-fetch": "^0.0.36",
44
42
  "@types/node": "^18.15.5",
@@ -49,6 +47,7 @@
49
47
  "husky": "^8.0.3",
50
48
  "lint-staged": "^13.2.0",
51
49
  "tslib": "^2.5.0",
50
+ "tsm": "^2.3.0",
52
51
  "typescript": "^5.0.2",
53
52
  "uvu": "^0.5.6"
54
53
  },
package/types/index.d.ts CHANGED
@@ -1,34 +1,34 @@
1
- type StringNumbers = number | string | number[] | string[];
2
1
 
3
- declare class WienerLinien {
4
- constructor(API_KEY?: string);
5
2
 
6
- monitor(rbl: StringNumbers, options: MonitorOptions): Promise<Record<string, unknown>>;
7
- newsList(options: NewsListOptions): Promise<Record<string, unknown>>;
8
- trafficInfoList(options: TrafficInfoOptions): Promise<Record<string, unknown>>;
3
+ declare namespace Wili {
4
+ function monitor(rbl: StringNumbers, options: MonitorOptions): Promise<Record<string, unknown>>;
5
+ function newsList(options: NewsListOptions): Promise<Record<string, unknown>>;
6
+ function trafficInfoList(options: TrafficInfoOptions): Promise<Record<string, unknown>>;
9
7
 
10
- baseUrl?: string;
11
- reqUrl?: string;
12
- }
8
+ type Fetch = (input: URL | RequestInfo, init?: RequestInit | undefined) => Promise<Response>;
9
+ type StringNumbers = number | string | number[] | string[];
10
+ type UrlParams = MonitorOptions | NewsListOptions | TrafficInfoOptions;
13
11
 
14
- type MonitorOptions = {
15
- activateTrafficInfo?: string | string[];
16
- }
12
+ type MonitorOptions = {
13
+ activateTrafficInfo?: string | string[];
14
+ }
17
15
 
18
- interface MonitorParams extends MonitorOptions {
19
- rbl?: StringNumbers;
20
- }
16
+ interface MonitorParams extends MonitorOptions {
17
+ rbl?: StringNumbers;
18
+ }
21
19
 
22
- type NewsListOptions = {
23
- name?: string | string[];
24
- relatedLine?: StringNumbers;
25
- relatedStop?: StringNumbers;
26
- }
20
+ type NewsListOptions = {
21
+ name?: string | string[];
22
+ relatedLine?: StringNumbers;
23
+ relatedStop?: StringNumbers;
24
+ }
27
25
 
28
- type TrafficInfoOptions = {
29
- name?: string | string[];
30
- relatedLine?: StringNumbers;
31
- relatedStop?: StringNumbers;
26
+ type TrafficInfoOptions = {
27
+ name?: string | string[];
28
+ relatedLine?: StringNumbers;
29
+ relatedStop?: StringNumbers;
30
+ }
32
31
  }
33
32
 
34
- type UrlParams = MonitorOptions | NewsListOptions | TrafficInfoOptions;
33
+ export = Wili;
34
+ export as namespace Wili;