Skip to content

Commit

Permalink
re-use prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Zalmanski committed Apr 9, 2024
1 parent 4fb298a commit c85f136
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 136 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/.homeybuild/
/.vscode/*
!/.vscode/settings.json
/coverage/
/node_modules/
/env.json
25 changes: 12 additions & 13 deletions ariston/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ export default class AristonAPI {
return (
await this.login({ email: username, password, rememberMe: true })
).data.ok
}
catch (error) {
} catch (error) {
if (typeof data !== 'undefined') {
throw new Error(
error instanceof Error ? error.message : String(error),
Expand Down Expand Up @@ -117,10 +116,6 @@ export default class AristonAPI {
return this.#api.post<ReportData>(`/R2/PlantMetering/GetData/${id}`)
}

public async plants(): Promise<{ data: Plant[] }> {
return this.#api.get<Plant[]>('/api/v2/velis/plants')
}

public async plantSettings(
id: string,
settings: PostSettings,
Expand All @@ -131,13 +126,17 @@ export default class AristonAPI {
)
}

public async plants(): Promise<{ data: Plant[] }> {
return this.#api.get<Plant[]>('/api/v2/velis/plants')
}

async #handleError(error: AxiosError): Promise<AxiosError> {
const apiCallData = createAPICallErrorData(error)
this.#errorLogger(String(apiCallData))
if (
error.response?.status === axios.HttpStatusCode.MethodNotAllowed
&& this.#retry
&& error.config?.url !== LOGIN_URL
error.response?.status === axios.HttpStatusCode.MethodNotAllowed &&
this.#retry &&
error.config?.url !== LOGIN_URL
) {
this.#handleRetry()
if ((await this.applyLogin()) && error.config) {
Expand All @@ -164,9 +163,9 @@ export default class AristonAPI {
this.#logger(String(new APICallResponseData(response)))
if (
// @ts-expect-error: `axios` is partially typed
response.headers.hasContentType('application/json') !== true
&& this.#retry
&& response.config.url !== LOGIN_URL
response.headers.hasContentType('application/json') !== true &&
this.#retry &&
response.config.url !== LOGIN_URL
) {
this.#handleRetry()
if (await this.applyLogin()) {
Expand Down Expand Up @@ -197,7 +196,7 @@ export default class AristonAPI {
return
}
const aspNetCookie = cookies.find(
cookie => cookie.key === '.AspNet.ApplicationCookie',
(cookie) => cookie.key === '.AspNet.ApplicationCookie',
)
if (aspNetCookie) {
const expiresDate = new Date(String(aspNetCookie.expires))
Expand Down
2 changes: 1 addition & 1 deletion ariston/lib/APICallContextData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default abstract class APICallContextData {
}
return null
})
.filter(line => line !== null)
.filter((line) => line !== null)
.join('\n')
}
}
8 changes: 4 additions & 4 deletions ariston/lib/APICallErrorData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ interface APICallContextDataWithErrorMessage extends APICallContextData {
const withErrorMessage = <T extends new (...args: any[]) => APICallContextData>(
base: T,
error: AxiosError,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): new (...args: any[]) => APICallContextDataWithErrorMessage =>
class extends base {
public readonly errorMessage = error.message
}
class extends base {
public readonly errorMessage = error.message
}

const createAPICallErrorData = (
error: AxiosError,
Expand Down
2 changes: 1 addition & 1 deletion ariston/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export interface GetSettings {
}

export interface HistogramData {
readonly items: readonly { readonly x: string, readonly y: number }[]
readonly items: readonly { readonly x: string; readonly y: number }[]
readonly period: string
readonly series: 'DhwHp' | 'DhwResistor'
readonly tab: string
Expand Down
72 changes: 36 additions & 36 deletions decorators/addToLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,47 @@ import type { SimpleClass } from 'homey'
const FIRST_CHAR = 0
const PARENTHESES = '()'

const addToLogs
= <T extends new (...args: any[]) => SimpleClass>(...logs: string[]) =>
(target: T, context: ClassDecoratorContext<T>): T => {
class LogsDecorator extends target {
public error(...args: any[]): void {
this.#commonLog('error', ...args)
}
const addToLogs =
<T extends new (...args: any[]) => SimpleClass>(...logs: string[]) =>
(target: T, context: ClassDecoratorContext<T>): T => {
class LogsDecorator extends target {
public error(...args: any[]): void {
this.#commonLog('error', ...args)
}

public log(...args: any[]): void {
this.#commonLog('log', ...args)
}
public log(...args: any[]): void {
this.#commonLog('log', ...args)
}

#commonLog(logType: 'error' | 'log', ...args: any[]): void {
super[logType](
...logs.flatMap((log): [any, '-'] => {
if (log in this) {
return [this[log as keyof this], '-']
#commonLog(logType: 'error' | 'log', ...args: any[]): void {
super[logType](
...logs.flatMap((log): [any, '-'] => {
if (log in this) {
return [this[log as keyof this], '-']
}
if (log.endsWith(PARENTHESES)) {
const funcName = log.slice(FIRST_CHAR, -PARENTHESES.length)
if (
!(funcName in this) ||
typeof this[funcName as keyof this] !== 'function'
) {
return [log, '-']
}
if (log.endsWith(PARENTHESES)) {
const funcName = log.slice(FIRST_CHAR, -PARENTHESES.length)
if (
!(funcName in this)
|| typeof this[funcName as keyof this] !== 'function'
) {
return [log, '-']
}
const func = this[funcName as keyof this] as (
...funcArgs: any[]
) => any
if (!func.length) {
return [func.call(this), '-']
}
const func = this[funcName as keyof this] as (
...funcArgs: any[]
) => any
if (!func.length) {
return [func.call(this), '-']
}
return [log, '-']
}),
...args,
)
}
}
return [log, '-']
}),
...args,
)
}
Object.defineProperty(LogsDecorator, 'name', { value: context.name })
return LogsDecorator
}
Object.defineProperty(LogsDecorator, 'name', { value: context.name })
return LogsDecorator
}

export default addToLogs
Loading

0 comments on commit c85f136

Please sign in to comment.