Skip to content

Commit

Permalink
chore: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
panaaj committed Jul 11, 2022
1 parent 367d6dd commit db94686
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 143 deletions.
42 changes: 23 additions & 19 deletions signalk-client/http-api.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Alarm, AlarmType, Path } from './utils.ts';
import { SKServer } from './signalk-client.ts'
import { debug } from './mod.ts'
import { Alarm, AlarmType, Path } from "./utils.ts";
import { SKServer } from "./signalk-client.ts";
import { debug } from "./mod.ts";

export class SignalKHttp {
private _token = "";

public server: SKServer = {version: '', id: ''};
public server: SKServer = { version: "", id: "" };
public endpoint = "";
public version = 1;

Expand All @@ -18,27 +18,27 @@ export class SignalKHttp {

constructor() {}

// ** get the contents of the Signal K tree pointed to by self. returns: Observable
getSelf(): Promise<{[key:string]:unknown}> {
// ** get the contents of the Signal K tree pointed to by self. returns: Promise
getSelf(): Promise<{ [key: string]: unknown }> {
return this.get(`vessels/self`);
}

//** get ID of vessel self via http. returns: Observable
getSelfId(): Promise<{[key:string]:unknown}> {
//** get ID of vessel self via http. returns: Promise
getSelfId(): Promise<{ [key: string]: unknown }> {
return this.get(`self`);
}

// ** return observable response for meta object at the specified context and path
getMeta(context: string, path: string): Promise<{[key:string]:unknown}> {
getMeta(context: string, path: string): Promise<{ [key: string]: unknown }> {
return this.get(
`${Path.contextToPath(context)}/${Path.dotToSlash(path)}/meta`,
);
}

//** get API path value via http. returns: Observable
//** get API path value via http. returns: Promise

async get(path: string): Promise<{[key:string]:unknown}>;
async get(version: number, path: string): Promise<{[key:string]:unknown}>;
async get(path: string): Promise<{ [key: string]: unknown }>;
async get(version: number, path: string): Promise<{ [key: string]: unknown }>;
async get(p1: any, p2?: any) {
if (!this.endpoint) {
return;
Expand Down Expand Up @@ -75,7 +75,7 @@ export class SignalKHttp {
}
}

//** send value to API path via http PUT. returns: Observable
//** send value to API path via http PUT. returns: Promise
async put(path: string, value: any): Promise<Response>;
async put(version: number, path: string, value: any): Promise<Response>;
async put(p1: any, p2: string, p3?: any) {
Expand Down Expand Up @@ -116,8 +116,12 @@ export class SignalKHttp {
return await fetch(url, options);
}

//** send value to API path via http PUT. returns: Observable
async putWithContext(context: string, path: string, value: any): Promise<Response>;
//** send value to API path via http PUT. returns: Promise
async putWithContext(
context: string,
path: string,
value: any,
): Promise<Response>;
async putWithContext(
version: number,
context: string,
Expand Down Expand Up @@ -165,7 +169,7 @@ export class SignalKHttp {
return await fetch(url, options);
}

//** send value to API path via http POST. returns: Observable
//** send value to API path via http POST. returns: Promise
async post(path: string, value: any): Promise<Response>;
async post(version: number, path: string, value: any): Promise<Response>;
async post(p1: any, p2: string, p3?: any) {
Expand Down Expand Up @@ -202,7 +206,7 @@ export class SignalKHttp {
return await fetch(url, options);
}

//** delete value from API path via http DELETE. returns: Observable
//** delete value from API path via http DELETE. returns: Promise
async delete(path: string): Promise<Response>;
async delete(version: number, path: string): Promise<Response>;
async delete(p1: any, p2?: any) {
Expand Down Expand Up @@ -235,7 +239,7 @@ export class SignalKHttp {
return await fetch(url, options);
}

// ** raise alarm for path (name), returns: Observable
// ** raise alarm for path (name), returns: Promise
raiseAlarm(context: string, name: string, alarm: Alarm): Promise<Response>;
raiseAlarm(context: string, type: AlarmType, alarm: Alarm): Promise<Response>;
raiseAlarm(context = "*", alarmId: string | AlarmType, alarm: Alarm) {
Expand All @@ -250,7 +254,7 @@ export class SignalKHttp {
return this.putWithContext(context, path, alarm.value);
}

// ** raise alarm for path (name), returns: Observable
// ** raise alarm for path (name), returns: Promise
clearAlarm(context = "*", name: string): Promise<Response> {
const path = name.indexOf("notifications.") === -1
? `notifications.${name}`
Expand Down
69 changes: 39 additions & 30 deletions signalk-client/signalk-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export interface SKServer {
}

interface HelloResponse {
endpoints: {[key:string]: EndPoint};
server: SKServer
endpoints: { [key: string]: EndPoint };
server: SKServer;
}

interface PlaybackOptions {
Expand Down Expand Up @@ -116,8 +116,8 @@ export class SignalKClient {

apps: SignalKApps;
api: SignalKHttp;
stream: SignalKStream
stream: SignalKStream;

constructor() {
this.apps = new SignalKApps();
this.api = new SignalKHttp();
Expand Down Expand Up @@ -156,7 +156,7 @@ export class SignalKClient {
hostname: string = this.hostname,
port = 3000,
useSSL = false,
): Promise<{[key: string]: unknown}> {
): Promise<{ [key: string]: unknown }> {
this.init(hostname, port, useSSL);
return this.get("/signalk");
}
Expand Down Expand Up @@ -237,7 +237,7 @@ export class SignalKClient {
this.connect(hostname, port, useSSL)
.then(() => {
// ** connect to playback api at preferred version else fall back to default version
this.openPlayback('', options, this._token);
this.openPlayback("", options, this._token);
resolve(true);
})
.catch((e) => {
Expand All @@ -247,7 +247,11 @@ export class SignalKClient {
}

// ** connect to delta stream with (NO endpoint discovery)
openStream(url: string = this.hostname, subscribe?: string, token?: string): boolean {
openStream(
url: string = this.hostname,
subscribe?: string,
token?: string,
): boolean {
debug("openStream.........");
if (!url) {
// connect to stream api at discovered endpoint
Expand All @@ -261,18 +265,22 @@ export class SignalKClient {
}

// ** connect to playback stream (NO endpoint discovery)
openPlayback(url: string = this.hostname, options?: PlaybackOptions, token?: string) {
openPlayback(
url: string = this.hostname,
options?: PlaybackOptions,
token?: string,
): boolean {
debug("openPlayback.........");
if (!url) {
// connect to stream api at discovered endpoint
url = this.resolveStreamEndpoint();
if (!url) {
return new Error("Server has no advertised Stream endpoints!");
throw new Error("Server has no advertised Stream endpoints!");
}
url = url.replace("stream", "playback");
}
let pb = "";
let subscribe: string | undefined = '';
let subscribe: string | undefined = "";
if (options && typeof options === "object") {
pb += options.startTime
? "?startTime=" +
Expand Down Expand Up @@ -360,7 +368,7 @@ export class SignalKClient {
}

//** HTTP GET from http path
async get(path: string): Promise<{[key:string]: unknown}> {
async get(path: string): Promise<{ [key: string]: unknown }> {
if (path && path.length > 0 && path[0] == "/") {
path = path.slice(1);
}
Expand Down Expand Up @@ -424,8 +432,9 @@ export class SignalKClient {
// ** get auth token for supplied user details **
async login(username: string, password: string): Promise<Response> {
const headers = new Headers({ "Content-Type": "application/json" });
const url = `${this.protocol}://${this.hostname}:${this.port}/signalk/${this._version}/auth/login`;

const url =
`${this.protocol}://${this.hostname}:${this.port}/signalk/${this._version}/auth/login`;

debug(`post ${url}`);

if (this._token) {
Expand All @@ -434,7 +443,7 @@ export class SignalKClient {
const options = {
method: "POST",
headers: headers,
body: JSON.stringify({ username: username, password: password })
body: JSON.stringify({ username: username, password: password }),
};

const response = await fetch(
Expand All @@ -443,11 +452,11 @@ export class SignalKClient {
);

if (response.ok) {
const cookies = response.headers.get('set-cookie')?.split(';');
cookies?.forEach( c => {
if (c.indexOf('JAUTHENTICATION') !== -1) {
this.authToken = c.split('=')[1];
debug('authToken: ', this._token);
const cookies = response.headers.get("set-cookie")?.split(";");
cookies?.forEach((c) => {
if (c.indexOf("JAUTHENTICATION") !== -1) {
this.authToken = c.split("=")[1];
debug("authToken: ", this._token);
}
});
}
Expand Down Expand Up @@ -486,7 +495,7 @@ export class SignalKClient {
}

// ** fetch login status from server **
getLoginStatus(): Promise<{[key: string]: unknown}> {
getLoginStatus(): Promise<{ [key: string]: unknown }> {
let url = `/skServer/loginStatus`;
if (this.server && this.server.info.id === "signalk-server-node") {
const ver = this.server.info["version"].split(".");
Expand All @@ -498,14 +507,14 @@ export class SignalKClient {
}

//** get data via the snapshot http api path for supplied time
snapshot(context: string, time: string): Promise<{[key: string]: unknown}> {
snapshot(context: string, time: string): Promise<{ [key: string]: unknown }> {
if (!time) {
throw new Error('Error: No time value supplied!');
throw new Error("Error: No time value supplied!");
}
time = time.slice(0, time.indexOf(".")) + "Z";
let url = this.resolveHttpEndpoint();
if (!url) {
throw new Error('Error: Unable to resolve URL!');
throw new Error("Error: Unable to resolve URL!");
}
url = `${url.replace("api", "snapshot")}${
Path.contextToPath(
Expand Down Expand Up @@ -547,7 +556,7 @@ export class SignalKClient {
appDataVersions(
context: APPDATA_CONTEXT = APPDATA_CONTEXT.USER,
appId: string = this._appId,
): Promise<{[key: string]: unknown}> {
): Promise<{ [key: string]: unknown }> {
const url = this.resolveAppDataEndpoint(context, appId);
return this.get(url);
}
Expand All @@ -558,7 +567,7 @@ export class SignalKClient {
context: APPDATA_CONTEXT = APPDATA_CONTEXT.USER,
appId: string = this._appId,
version: string = this._appVersion,
): Promise<{[key: string]: unknown}> {
): Promise<{ [key: string]: unknown }> {
path = path.length != 0 && path[0] === "/" ? path.slice(1) : path;
let url = this.resolveAppDataEndpoint(context, appId);
url += `${version}/${path}?keys=true`;
Expand All @@ -571,7 +580,7 @@ export class SignalKClient {
context: APPDATA_CONTEXT = APPDATA_CONTEXT.USER,
appId: string = this._appId,
version: string = this._appVersion,
): Promise<{[key: string]: unknown}> {
): Promise<{ [key: string]: unknown }> {
path = path.length != 0 && path[0] === "/" ? path.slice(1) : path;
let url = this.resolveAppDataEndpoint(context, appId);
url += `${version}/${path}`;
Expand All @@ -581,20 +590,20 @@ export class SignalKClient {
// ** set stored value at provided path **
appDataSet(
path: string,
value: {[key: string]: unknown},
value: { [key: string]: unknown },
context: APPDATA_CONTEXT = APPDATA_CONTEXT.USER,
appId: string = this._appId,
version: string = this._appVersion,
): Promise<Response> {
if (!path) {
throw new Error('Error: Invalid path!');
throw new Error("Error: Invalid path!");
}
if (path[0] === "/") {
path = path.slice(1);
}
const ep = this.resolveAppDataEndpoint(context, appId);
if (!ep) {
throw new Error('Error: Invalid path!');
throw new Error("Error: Invalid path!");
}
return this.post(
`${ep}${version}/${Path.dotToSlash(path)}`,
Expand All @@ -611,7 +620,7 @@ export class SignalKClient {
): Promise<Response> {
const ep = this.resolveAppDataEndpoint(context, appId);
if (!ep || !version) {
throw new Error('Error: Invalid path or version!');
throw new Error("Error: Invalid path or version!");
}
return this.post(`${ep}${version}`, value);
}
Expand Down
Loading

0 comments on commit db94686

Please sign in to comment.