Skip to content

Commit

Permalink
destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierZal committed Oct 13, 2024
1 parent 917e8d8 commit f8aab12
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 27 deletions.
11 changes: 11 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,17 @@ const config = [
caughtErrorsIgnorePattern: '^_',
},
],
'@typescript-eslint/prefer-destructuring': [
'error',
{
array: true,
object: true,
},
{
enforceForDeclarationWithTypeAnnotation: true,
enforceForRenamedProperties: true,
},
],
'@typescript-eslint/prefer-readonly-parameter-types': 'off',
'@typescript-eslint/return-await': ['error', 'in-try-catch'],
'@typescript-eslint/typedef': 'off',
Expand Down
12 changes: 6 additions & 6 deletions src/facades/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ export abstract class BaseFacade<

public readonly id: number

public readonly manager: FacadeManager

protected isFrostProtectionDefined: boolean | null = null

protected isHolidayModeDefined: boolean | null = null

readonly #manager: FacadeManager

protected abstract readonly frostProtectionLocation: keyof FrostProtectionLocation

protected abstract readonly holidayModeLocation: keyof HolidayModeLocation
Expand All @@ -79,9 +79,9 @@ export abstract class BaseFacade<
protected abstract readonly tableName: SettingsParams['tableName']

public constructor(manager: FacadeManager, instance: T) {
this.manager = manager
this.api = manager.api
this.id = instance.id
this.#manager = manager
;({ api: this.api } = manager)
;({ id: this.id } = instance)
}

public get devices(): DeviceModelAny[] {
Expand Down Expand Up @@ -120,7 +120,7 @@ export abstract class BaseFacade<
}

public async getErrors(query: ErrorLogQuery): Promise<ErrorLog> {
return this.manager.getErrors(query, this.#deviceIds)
return this.#manager.getErrors(query, this.#deviceIds)
}

public async getFrostProtection(): Promise<FrostProtectionData> {
Expand Down
3 changes: 1 addition & 2 deletions src/facades/base_device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ export abstract class BaseDeviceFacade<T extends keyof typeof DeviceType>

public constructor(manager: FacadeManager, instance: DeviceModel<T>) {
super(manager, instance as DeviceModelAny)
this.type = instance.type

;({ type: this.type } = instance)
this.#initMetadata()
this.#values = this.constructor[Symbol.metadata]?.[valueSymbol] as Set<
keyof this
Expand Down
8 changes: 6 additions & 2 deletions src/facades/building.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ export class BuildingFacade

public constructor(manager: FacadeManager, instance: BuildingModel) {
super(manager, instance)
this.isFrostProtectionDefined = this.data.FPDefined
this.isHolidayModeDefined = this.data.HMDefined
;({
data: {
FPDefined: this.isFrostProtectionDefined,
HMDefined: this.isHolidayModeDefined,
},
} = this)
}

public get data(): ZoneSettings {
Expand Down
8 changes: 5 additions & 3 deletions src/facades/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ export class FacadeManager {
instance?: AreaModelAny | BuildingModel | DeviceModelAny | FloorModel,
): AreaFacade | BuildingFacade | DeviceFacadeAny | FloorFacade | undefined {
if (instance) {
const modelName = instance.constructor.name
const {
constructor: { name },
} = instance
const modelId = String(instance.id)
const id = `${modelName}:${modelId}`
const id = `${name}:${modelId}`
if (!this.#facades.has(id)) {
switch (true) {
case instance instanceof AreaModel:
Expand All @@ -134,7 +136,7 @@ export class FacadeManager {
}
const facade = this.#facades.get(id)
if (!facade) {
throw new Error(`Facade not found for ${modelName} with id ${modelId}`)
throw new Error(`Facade not found for ${name} with id ${modelId}`)
}
return facade
}
Expand Down
2 changes: 1 addition & 1 deletion src/logging/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export abstract class APICallContextData {
]
.map((key) => {
if (key in this) {
const value = this[key as keyof this]
const { [key as keyof this]: value } = this
if (value !== undefined) {
return `${key}: ${
typeof value === 'object' ?
Expand Down
25 changes: 12 additions & 13 deletions src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,22 +350,21 @@ export class API implements IAPI {
password: string
username: string
}): Promise<boolean> {
const { LoginData: loginData } = (
await this.#login({
postData: {
AppVersion: '1.34.10.0',
Email: username,
Language: this.#getLanguageCode(),
Password: password,
Persist: true,
},
})
).data
const {
data: { LoginData: loginData },
} = await this.#login({
postData: {
AppVersion: '1.34.10.0',
Email: username,
Language: this.#getLanguageCode(),
Password: password,
Persist: true,
},
})
if (loginData) {
this.username = username
this.password = password
this.contextKey = loginData.ContextKey
this.expiry = loginData.Expiry
;({ ContextKey: this.contextKey, Expiry: this.expiry } = loginData)
await this.fetch()
}
return loginData !== null
Expand Down

0 comments on commit f8aab12

Please sign in to comment.