Skip to content

Commit

Permalink
Add ts-auto-guard to WeatherProvider interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
panaaj committed May 14, 2024
1 parent c896ade commit 67d2209
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
4 changes: 3 additions & 1 deletion packages/server-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc --declaration",
"build": "npm run generate && tsc --declaration",
"generate": "ts-auto-guard src/weatherapi.ts 2>/dev/null",
"watch": "tsc --declaration --watch",
"prepublishOnly": "npm run build",
"typedoc": "typedoc --out docs src",
Expand Down Expand Up @@ -33,6 +34,7 @@
"express": "^4.10.4",
"mocha": "^10.2.0",
"prettier": "^2.8.4",
"ts-auto-guard": "^4.2.0",
"ts-node": "^10.9.1",
"typedoc": "^0.23.23",
"typescript": "^4.1.5"
Expand Down
4 changes: 2 additions & 2 deletions packages/server-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ export * from './deltas'
export * from './coursetypes'
export * from './resourcetypes'
export * from './resourcesapi'
export { ResourceProviderRegistry } from './resourcesapi'
import { ResourceProviderRegistry } from './resourcesapi'
import { PointDestination, RouteDestination, CourseInfo } from './coursetypes'

export * from './autopilotapi'

export * from './weatherapi'
export { WeatherProviderRegistry } from './weatherapi'
import { WeatherProviderRegistry, WeatherWarning } from './weatherapi'
export * from './weatherapi.guard'

export type SignalKApiId =
| 'resources'
Expand Down
23 changes: 23 additions & 0 deletions packages/server-api/src/weatherapi.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Generated type guards for "weatherapi.ts".
* WARNING: Do not manually change this file.
*/
import { WeatherProvider } from './weatherapi'

export function isWeatherProvider(obj: unknown): obj is WeatherProvider {
const typedObj = obj as WeatherProvider
return (
((typedObj !== null && typeof typedObj === 'object') ||
typeof typedObj === 'function') &&
typeof typedObj['name'] === 'string' &&
((typedObj['methods'] !== null &&
typeof typedObj['methods'] === 'object') ||
typeof typedObj['methods'] === 'function') &&
(typeof typedObj['methods']['pluginId'] === 'undefined' ||
typeof typedObj['methods']['pluginId'] === 'string') &&
typeof typedObj['methods']['getData'] === 'function' &&
typeof typedObj['methods']['getObservations'] === 'function' &&
typeof typedObj['methods']['getForecasts'] === 'function' &&
typeof typedObj['methods']['getWarnings'] === 'function'
)
}
1 change: 1 addition & 0 deletions packages/server-api/src/weatherapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface WeatherProviders {
}
}

/** @see {isWeatherProvider} ts-auto-guard:type-guard */
export interface WeatherProvider {
name: string // e.g. OpenWeather, Open-Meteo, NOAA
methods: WeatherProviderMethods
Expand Down
18 changes: 9 additions & 9 deletions src/api/weather/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
WeatherProviderMethods,
WeatherWarning,
WeatherData,
//isWeatherProvider,
isWeatherProvider,
SKVersion,
Path,
Delta,
Expand Down Expand Up @@ -51,18 +51,18 @@ export class WeatherApi {
if (!pluginId || !provider) {
throw new Error(`Error registering provider ${pluginId}!`)
}
/*if (!isWeatherProvider(provider)) {
if (!isWeatherProvider(provider)) {
throw new Error(
`${pluginId} is missing WeatherProvider properties/methods!`
)
} else {*/
if (!this.weatherProviders.has(pluginId)) {
this.weatherProviders.set(pluginId, provider)
}
if (this.weatherProviders.size === 1) {
this.defaultProviderId = pluginId
} else {
if (!this.weatherProviders.has(pluginId)) {
this.weatherProviders.set(pluginId, provider)
}
if (this.weatherProviders.size === 1) {
this.defaultProviderId = pluginId
}
}
//}
debug(`No. of WeatherProviders registered =`, this.weatherProviders.size)
}

Expand Down

1 comment on commit 67d2209

@tkurki
Copy link
Member

@tkurki tkurki commented on 67d2209 May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the build generates the file anyway I would not add it to git. No biggie, this is also ok if you prefer this way.

Please sign in to comment.