-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement features API endpoint.
- Loading branch information
Showing
7 changed files
with
244 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { createDebug } from '../../debug' | ||
const debug = createDebug('signalk-server:api:features') | ||
|
||
import { IRouter, Request, Response } from 'express' | ||
|
||
import { SignalKMessageHub, WithConfig, WithFeatures } from '../../app' | ||
import { WithSecurityStrategy } from '../../security' | ||
|
||
const SIGNALK_API_PATH = `/signalk/v2/api` | ||
const FEATURES_API_PATH = `${SIGNALK_API_PATH}/features` | ||
|
||
interface FeaturesApplication | ||
extends IRouter, | ||
WithConfig, | ||
WithFeatures, | ||
WithSecurityStrategy, | ||
SignalKMessageHub {} | ||
|
||
interface FeatureInfo { | ||
apis: string[] | ||
plugins: string[] | ||
} | ||
|
||
export class FeaturesApi { | ||
private features: FeatureInfo = { | ||
apis: [], | ||
plugins: [] | ||
} | ||
|
||
constructor(private server: FeaturesApplication) {} | ||
|
||
async start() { | ||
// eslint-disable-next-line no-async-promise-executor | ||
return new Promise<void>(async (resolve) => { | ||
this.initApiRoutes() | ||
resolve() | ||
}) | ||
} | ||
|
||
// Exposed to plugins | ||
public getFeatures() { | ||
this.features.plugins = (this.server as any).plugins.map( | ||
(plugin: any) => { | ||
return { | ||
id: plugin.id, | ||
name: plugin.name, | ||
version: plugin.version | ||
} | ||
} | ||
) | ||
this.features.apis = this.server.apis.slice() | ||
return this.features | ||
} | ||
|
||
private initApiRoutes() { | ||
debug(`** Initialise ${FEATURES_API_PATH} path handlers **`) | ||
// return Feature information | ||
this.server.get( | ||
`${FEATURES_API_PATH}`, | ||
async (req: Request, res: Response) => { | ||
debug(`** GET ${FEATURES_API_PATH}`) | ||
res.json(this.getFeatures()) | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
{ | ||
"openapi": "3.0.0", | ||
"info": { | ||
"version": "2.0.0", | ||
"title": "Signal K Features API", | ||
"termsOfService": "http://signalk.org/terms/", | ||
"license": { | ||
"name": "Apache 2.0", | ||
"url": "http://www.apache.org/licenses/LICENSE-2.0.html" | ||
} | ||
}, | ||
"externalDocs": { | ||
"url": "http://signalk.org/specification/", | ||
"description": "Signal K specification." | ||
}, | ||
"tags": [ | ||
{ | ||
"name": "features", | ||
"description": "Signal K Server features." | ||
} | ||
], | ||
"components": { | ||
"schemas": { | ||
"PluginMetaData": { | ||
"type": "object", | ||
"required": ["id", "name", "version"], | ||
"description": "Plugin metadata.", | ||
"properties": { | ||
"id": { | ||
"type": "string", | ||
"description": "Plugin ID." | ||
}, | ||
"name": { | ||
"type": "string", | ||
"description": "Plugin name." | ||
}, | ||
"version": { | ||
"type": "string", | ||
"description": "Plugin verison." | ||
} | ||
} | ||
}, | ||
"FeaturesModel": { | ||
"type": "object", | ||
"required": ["apis", "plugins"], | ||
"description": "Features response", | ||
"properties": { | ||
"apis": { | ||
"type": "array", | ||
"description": "Implemented APIs.", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"plugins": { | ||
"type": "array", | ||
"description": "Installed Plugins.", | ||
"items": { | ||
"$ref": "#/components/schemas/PluginMetaData" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"responses": { | ||
"200Ok": { | ||
"description": "OK", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "object", | ||
"properties": { | ||
"state": { | ||
"type": "string", | ||
"enum": ["COMPLETED"] | ||
}, | ||
"statusCode": { | ||
"type": "number", | ||
"enum": [200] | ||
} | ||
}, | ||
"required": ["state", "statusCode"] | ||
} | ||
} | ||
} | ||
}, | ||
"ErrorResponse": { | ||
"description": "Failed operation", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "object", | ||
"description": "Request error response", | ||
"properties": { | ||
"state": { | ||
"type": "string", | ||
"enum": ["FAILED"] | ||
}, | ||
"statusCode": { | ||
"type": "number", | ||
"enum": [404] | ||
}, | ||
"message": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": ["state", "statusCode", "message"] | ||
} | ||
} | ||
} | ||
}, | ||
"FeaturesResponse": { | ||
"description": "Server features response.", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"description": "Features response.", | ||
"$ref": "#/components/schemas/FeaturesModel" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"paths": { | ||
"/": { | ||
"get": { | ||
"tags": ["features"], | ||
"summary": "Retrieve available server features.", | ||
"description": "Returns object detailing the available server features.", | ||
"responses": { | ||
"200": { | ||
"$ref": "#/components/responses/FeaturesResponse" | ||
}, | ||
"default": { | ||
"$ref": "#/components/responses/ErrorResponse" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { OpenApiDescription } from '../swagger' | ||
import featuresApiDoc from './openApi.json' | ||
|
||
export const featuresApiRecord = { | ||
name: 'features', | ||
path: '/signalk/v2/api/features', | ||
apiDoc: featuresApiDoc as unknown as OpenApiDescription | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters