Skip to content

Commit

Permalink
fix: use area code fallback for unknown area codes
Browse files Browse the repository at this point in the history
  • Loading branch information
milo526 committed Oct 6, 2023
1 parent cbc837f commit 9e0ed9c
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions src/api/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ export class Session {
}

public set areaCode(newAreaCode: string) {
if (!isAreaCode(newAreaCode)) {
throw new Error(
`Invalid area code ${newAreaCode}, must be one of ${Object.keys(
AreaCodeLookup,
).join(", ")}`,
);
}
const areaCodeLookup = {
AY: "https://px1.tuyacn.com",
EU: "https://px1.tuyaeu.com",
US: "https://px1.tuyaus.com",
};

this._areaCode = newAreaCode;
this._areaBaseUrl = AreaCodeLookup[newAreaCode] || AreaCodeLookup.US;
this._areaBaseUrl =
newAreaCode in areaCodeLookup
? areaCodeLookup[newAreaCode as keyof typeof areaCodeLookup]
: areaCodeLookup.US;
}

public resetToken(
Expand Down Expand Up @@ -75,15 +76,3 @@ export class Session {
return Math.round(new Date().getTime() / 1000);
}
}

const AreaCodeLookup = {
AY: "https://px1.tuyacn.com",
EU: "https://px1.tuyaeu.com",
US: "https://px1.tuyaus.com",
};

type AreaCode = keyof typeof AreaCodeLookup;

function isAreaCode(areaCode: string): areaCode is AreaCode {
return areaCode in AreaCodeLookup;
}

0 comments on commit 9e0ed9c

Please sign in to comment.