Skip to content

Commit 67d2209

Browse files
committed
Add ts-auto-guard to WeatherProvider interface.
1 parent c896ade commit 67d2209

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

packages/server-api/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"scripts": {
8-
"build": "tsc --declaration",
8+
"build": "npm run generate && tsc --declaration",
9+
"generate": "ts-auto-guard src/weatherapi.ts 2>/dev/null",
910
"watch": "tsc --declaration --watch",
1011
"prepublishOnly": "npm run build",
1112
"typedoc": "typedoc --out docs src",
@@ -33,6 +34,7 @@
3334
"express": "^4.10.4",
3435
"mocha": "^10.2.0",
3536
"prettier": "^2.8.4",
37+
"ts-auto-guard": "^4.2.0",
3638
"ts-node": "^10.9.1",
3739
"typedoc": "^0.23.23",
3840
"typescript": "^4.1.5"

packages/server-api/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ export * from './deltas'
2525
export * from './coursetypes'
2626
export * from './resourcetypes'
2727
export * from './resourcesapi'
28-
export { ResourceProviderRegistry } from './resourcesapi'
2928
import { ResourceProviderRegistry } from './resourcesapi'
3029
import { PointDestination, RouteDestination, CourseInfo } from './coursetypes'
3130

3231
export * from './autopilotapi'
32+
3333
export * from './weatherapi'
34-
export { WeatherProviderRegistry } from './weatherapi'
3534
import { WeatherProviderRegistry, WeatherWarning } from './weatherapi'
35+
export * from './weatherapi.guard'
3636

3737
export type SignalKApiId =
3838
| 'resources'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Generated type guards for "weatherapi.ts".
3+
* WARNING: Do not manually change this file.
4+
*/
5+
import { WeatherProvider } from './weatherapi'
6+
7+
export function isWeatherProvider(obj: unknown): obj is WeatherProvider {
8+
const typedObj = obj as WeatherProvider
9+
return (
10+
((typedObj !== null && typeof typedObj === 'object') ||
11+
typeof typedObj === 'function') &&
12+
typeof typedObj['name'] === 'string' &&
13+
((typedObj['methods'] !== null &&
14+
typeof typedObj['methods'] === 'object') ||
15+
typeof typedObj['methods'] === 'function') &&
16+
(typeof typedObj['methods']['pluginId'] === 'undefined' ||
17+
typeof typedObj['methods']['pluginId'] === 'string') &&
18+
typeof typedObj['methods']['getData'] === 'function' &&
19+
typeof typedObj['methods']['getObservations'] === 'function' &&
20+
typeof typedObj['methods']['getForecasts'] === 'function' &&
21+
typeof typedObj['methods']['getWarnings'] === 'function'
22+
)
23+
}

packages/server-api/src/weatherapi.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface WeatherProviders {
2121
}
2222
}
2323

24+
/** @see {isWeatherProvider} ts-auto-guard:type-guard */
2425
export interface WeatherProvider {
2526
name: string // e.g. OpenWeather, Open-Meteo, NOAA
2627
methods: WeatherProviderMethods

src/api/weather/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
WeatherProviderMethods,
1515
WeatherWarning,
1616
WeatherData,
17-
//isWeatherProvider,
17+
isWeatherProvider,
1818
SKVersion,
1919
Path,
2020
Delta,
@@ -51,18 +51,18 @@ export class WeatherApi {
5151
if (!pluginId || !provider) {
5252
throw new Error(`Error registering provider ${pluginId}!`)
5353
}
54-
/*if (!isWeatherProvider(provider)) {
54+
if (!isWeatherProvider(provider)) {
5555
throw new Error(
5656
`${pluginId} is missing WeatherProvider properties/methods!`
5757
)
58-
} else {*/
59-
if (!this.weatherProviders.has(pluginId)) {
60-
this.weatherProviders.set(pluginId, provider)
61-
}
62-
if (this.weatherProviders.size === 1) {
63-
this.defaultProviderId = pluginId
58+
} else {
59+
if (!this.weatherProviders.has(pluginId)) {
60+
this.weatherProviders.set(pluginId, provider)
61+
}
62+
if (this.weatherProviders.size === 1) {
63+
this.defaultProviderId = pluginId
64+
}
6465
}
65-
//}
6666
debug(`No. of WeatherProviders registered =`, this.weatherProviders.size)
6767
}
6868

0 commit comments

Comments
 (0)