Skip to content

Commit

Permalink
Merge pull request #128 from tago-io/SDKJS-45-region-adapt
Browse files Browse the repository at this point in the history
Feature: Support custom region endpoints for TagoDeploy
  • Loading branch information
FabianoEger authored Dec 17, 2024
2 parents 3c11a93 + 5237f22 commit 684bb83
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
9 changes: 6 additions & 3 deletions src/common/TagoIOModule.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { AxiosRequestConfig, Method } from "axios";
import qs from "qs";
import apiRequest from "../infrastructure/apiRequest";
import regions, { Regions } from "../regions";
import regions, { Regions, RegionsObj } from "../regions";
import { RefType, GenericID } from "./common.types";

interface GenericModuleParams {
token?: string;
region?: Regions;
region?: Regions | RegionsObj;
// options?: Object;
}

Expand Down Expand Up @@ -113,7 +113,10 @@ abstract class TagoIOModule<T extends GenericModuleParams> {
return result as Promise<TR>;
}

protected static async doRequestAnonymous<TR>(requestObj: doRequestParams, region?: Regions): Promise<TR> {
protected static async doRequestAnonymous<TR>(
requestObj: doRequestParams,
region?: Regions | RegionsObj
): Promise<TR> {
const apiURI = regions(region)?.api;
if (!apiURI) {
throw new Error("Invalid region");
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/envParams.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version": "11.3.2"}
{"version": "11.3.2"}
6 changes: 3 additions & 3 deletions src/modules/Dictionary/dictionary.types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { GenericModuleParams } from "../../common/TagoIOModule";
import { Regions } from "../../regions";
import { Regions, RegionsObj } from "../../regions";

interface IDictionaryModuleParams extends GenericModuleParams {
token: string;
region?: Regions;
region?: Regions | RegionsObj;
language?: string;
options?: object;
}

interface IDictionaryModuleParamsAnonymous extends GenericModuleParams {
runURL: string;
region?: Regions;
region?: Regions | RegionsObj;
language?: string;
options?: object;
}
Expand Down
28 changes: 18 additions & 10 deletions src/modules/RunUser/RunUser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GenericID, GenericToken } from "../../common/common.types";
import TagoIOModule, { doRequestParams, GenericModuleParams } from "../../common/TagoIOModule";
import { Regions } from "../../regions";
import { Regions, RegionsObj } from "../../regions";
import { NotificationInfo, NotificationQuery } from "../Resources/notifications.types";
import { OTPType } from "../Resources/account.types";
import dateParser from "../Utils/dateParser";
Expand Down Expand Up @@ -40,7 +40,7 @@ class RunUser extends TagoIOModule<GenericModuleParams> {
public static async create(
tagoIORunURL: string,
newUserObj: RunUserCreateInfo,
region?: Regions
region?: Regions | RegionsObj
): Promise<RunUserCreate> {
const params: doRequestParams = {
path: `/run/${tagoIORunURL}/signup`,
Expand Down Expand Up @@ -90,7 +90,7 @@ class RunUser extends TagoIOModule<GenericModuleParams> {
public static async login(
tagoIORunURL: string,
credentialsObj: RunUserLogin,
region?: Regions
region?: Regions | RegionsObj
): Promise<RunUserLoginResponse> {
const params: doRequestParams = {
path: `/run/${tagoIORunURL}/login`,
Expand Down Expand Up @@ -128,7 +128,11 @@ class RunUser extends TagoIOModule<GenericModuleParams> {
* @param email Run user email to recover the password
* @param region TagoIO Region Server [default usa-1]
*/
public static async passwordRecover(tagoIORunURL: string, email: string, region?: Regions): Promise<string> {
public static async passwordRecover(
tagoIORunURL: string,
email: string,
region?: Regions | RegionsObj
): Promise<string> {
const params: doRequestParams = {
path: `/run/${tagoIORunURL}/passwordreset/${email}`,
method: "GET",
Expand Down Expand Up @@ -264,13 +268,17 @@ class RunUser extends TagoIOModule<GenericModuleParams> {
public static async requestLoginPINCode(
tagoIORunURL: string,
credentials: RunUserCredentials,
typeOTP: OTPType
typeOTP: OTPType,
region?: Regions | RegionsObj
): Promise<string> {
const result = await this.doRequestAnonymous<string>({
path: `/run/${tagoIORunURL}/login/otp`,
method: "POST",
body: { ...credentials, otp_type: typeOTP },
});
const result = await this.doRequestAnonymous<string>(
{
path: `/run/${tagoIORunURL}/login/otp`,
method: "POST",
body: { ...credentials, otp_type: typeOTP },
},
region
);

return result;
}
Expand Down
7 changes: 4 additions & 3 deletions src/regions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ const regionsDefinition = {
* @internal
* @param region Region
*/
function getConnectionURI(region?: Regions): RegionsObj {
const value = regionsDefinition[region];
function getConnectionURI(region?: Regions | RegionsObj): RegionsObj {
const value =
typeof region === "string" ? regionsDefinition[region] : typeof region === "object" ? region : undefined;

if (value) {
return value;
Expand Down Expand Up @@ -61,4 +62,4 @@ function getConnectionURI(region?: Regions): RegionsObj {
type Regions = "usa-1" | "env";

export default getConnectionURI;
export { Regions };
export { Regions, RegionsObj };

0 comments on commit 684bb83

Please sign in to comment.