Skip to content

Commit

Permalink
chore(binding-http): enable eslint/strict-boolean-expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Sep 22, 2023
1 parent bd247a0 commit 8d6d16b
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 171 deletions.
5 changes: 4 additions & 1 deletion packages/binding-http/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "../../.eslintrc.js"
"extends": "../../.eslintrc.js",
"rules": {
"@typescript-eslint/strict-boolean-expressions": ["error"]
}
}
21 changes: 16 additions & 5 deletions packages/binding-http/src/codecs/tuya-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,28 @@ import { ContentCodec } from "@node-wot/core";
import * as TD from "@node-wot/td-tools";
import { DataSchemaValue } from "wot-typescript-definitions";

interface TuyaOutput {
success?: boolean;
msg?: string;
result?: {
code?: string;
}[];
}

export default class HttpTuyaCodec implements ContentCodec {
getMediaType(): string {
return "application/json+tuya";
}

bytesToValue(bytes: Buffer, schema: TD.DataSchema, parameters: { [key: string]: string }): DataSchemaValue {
const parsedBody = JSON.parse(bytes.toString());
if (!parsedBody.success) throw new Error(parsedBody.msg ? parsedBody.msg : JSON.stringify(parsedBody));
for (const key in parsedBody.result) {
if (parsedBody.result[key].code === schema["tuya:PropertyName"]) {
return parsedBody.result[key].value;
const parsedBody: TuyaOutput = JSON.parse(bytes.toString());
if (parsedBody.success !== true) {
throw new Error(parsedBody.msg != null ? parsedBody.msg : JSON.stringify(parsedBody));
}

for (const value of Object.values(parsedBody.result ?? {})) {
if (value.code === schema["tuya:PropertyName"]) {
return value;
}
}
throw new Error("Property not found");
Expand Down
56 changes: 41 additions & 15 deletions packages/binding-http/src/credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ export interface TuyaCustomBearerCredentialConfiguration {
secret: string;
}

interface TokenResponse {
success?: boolean;
result?: {
// eslint-disable-next-line camelcase
access_token?: string;
// eslint-disable-next-line camelcase
refresh_token?: string;
// eslint-disable-next-line camelcase
expire_time?: number;
};
}

export class TuyaCustomBearer extends Credential {
protected key: string;
protected secret: string;
Expand All @@ -184,14 +196,15 @@ export class TuyaCustomBearer extends Credential {
await this.requestAndRefreshToken(isTokenExpired);

const url: string = request.url;
const body = request.body ? request.body.read().toString() : "";
const headers = this.getHeaders(true, request.headers.raw(), body, url, request.method);
const body = request.body?.read().toString();
const method = request.method;
const headers = this.getHeaders(true, request.headers.raw(), body, url, method);
Object.assign(headers, request.headers.raw());
return new Request(url, { method: request.method, body: body !== "" ? body : undefined, headers: headers });
return new Request(url, { method, body, headers });
}

protected async requestAndRefreshToken(refresh: boolean): Promise<void> {
const headers = this.getHeaders(false, {}, "");
const headers = this.getHeaders(false, {});

Check warning on line 207 in packages/binding-http/src/credential.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-http/src/credential.ts#L207

Added line #L207 was not covered by tests
const request = {
headers: headers,
method: "GET",
Expand All @@ -200,36 +213,49 @@ export class TuyaCustomBearer extends Credential {
if (refresh) {
url = `${this.baseUri}/token/${this.refreshToken}`;
}
const data = await (await fetch(url, request)).json();
if (data.success) {
this.token = data.result.access_token;
this.refreshToken = data.result.refresh_token;
this.expireTime = new Date(Date.now() + data.result.expire_time * 1000);
const data: TokenResponse = await (await fetch(url, request)).json();
if (data.success === true) {
this.token = data.result?.access_token;
this.refreshToken = data.result?.refresh_token;

const expireTime = data.result?.expire_time;
if (expireTime != null) {
this.expireTime = new Date(Date.now() + expireTime * 1000);
}

Check warning on line 224 in packages/binding-http/src/credential.ts

View check run for this annotation

Codecov / codecov/patch

packages/binding-http/src/credential.ts#L216-L224

Added lines #L216 - L224 were not covered by tests
} else {
throw new Error("token fetch failed");
}
}

private getHeaders(NormalRequest: boolean, headers: unknown, body: string, url?: string, method?: string) {
private getHeaders(NormalRequest: boolean, headers: unknown, body?: string, url?: string, method?: string) {
const requestTime = Date.now().toString();
const replaceUri = this.baseUri.replace("/v1.0", "");
const _url = url ? url.replace(`${replaceUri}`, "") : undefined;
const _url = url?.replace(replaceUri, "");
const sign = this.requestSign(NormalRequest, requestTime, body, _url, method);
return {
t: requestTime,
client_id: this.key,
sign_method: "HMAC-SHA256",
sign,
access_token: this.token || "",
access_token: this.token ?? "",
};
}

private requestSign(NormalRequest: boolean, requestTime: string, body: string, path = "", method?: string): string {
const bodyHash = crypto.createHash("sha256").update(body).digest("hex");
private requestSign(
NormalRequest: boolean,
requestTime: string,
body?: string,
path = "",
method?: string
): string {
const bodyHash = crypto
.createHash("sha256")
.update(body ?? "")
.digest("hex");
let signUrl = "/v1.0/token?grant_type=1";
const headerString = "";
let useToken = "";
const _method = method || "GET";
const _method = method ?? "GET";
if (NormalRequest) {
useToken = this.token ?? "";
const pathQuery = queryString.parse(path.split("?")[1]);
Expand Down
10 changes: 5 additions & 5 deletions packages/binding-http/src/http-client-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export default class HttpClient implements ProtocolClient {
}

public async invokeResource(form: HttpForm, content?: Content): Promise<Content> {
const headers = content ? [["content-type", content.type]] : [];
const headers = content != null ? [["content-type", content.type]] : [];

const request = await this.generateFetchRequest(form, "POST", {
headers: headers,
Expand All @@ -191,8 +191,8 @@ export default class HttpClient implements ProtocolClient {

debug(
`HttpClient (invokeResource) sending ${request.method} ${
content ? "with '" + request.headers.get("Content-Type") + "' " : " "
}to ${request.url}`
content != null ? `with '"${request.headers.get("Content-Type")}"` : ""
} to ${request.url}`
);

const result = await this.fetch(request);
Expand Down Expand Up @@ -225,7 +225,7 @@ export default class HttpClient implements ProtocolClient {

public async stop(): Promise<void> {
// When running in browser mode, Agent.destroy() might not exist.
if (this.agent && this.agent.destroy) this.agent.destroy();
this.agent?.destroy?.();
}

public setSecurity(metadata: Array<TD.SecurityScheme>, credentials?: unknown): boolean {
Expand Down Expand Up @@ -287,7 +287,7 @@ export default class HttpClient implements ProtocolClient {
return false;
}

if (security.proxy) {
if (security.proxy != null) {
if (this.proxyRequest !== null) {
debug(`HttpClient overriding client-side proxy with security proxy '${security.proxy}`);
}
Expand Down
Loading

0 comments on commit 8d6d16b

Please sign in to comment.