Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DX-1894], added support for custom region #38

Merged
merged 7 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
307 changes: 187 additions & 120 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/types-generator",
"version": "2.0.2",
"version": "2.0.3",
"description": "Contentstack type definition generation library",
"private": false,
"author": "Contentstack",
Expand Down Expand Up @@ -42,9 +42,9 @@
"typescript": "^5.4.5"
},
"dependencies": {
"@contentstack/delivery-sdk": "^4.2.0",
"@contentstack/delivery-sdk": "^4.4.3",
"@gql2ts/from-schema": "^2.0.0-4",
"axios": "^1.7.5",
"axios": "^1.7.8",
"lodash": "^4.17.21",
"prettier": "^3.3.3"
},
Expand Down
8 changes: 8 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export const TOKEN_TYPE = {
DELIVERY: "delivery",
};

export const REGIONS = {
US: "US",
EU: "EU",
AZURE_NA: "AZURE_NA",
AZURE_EU: "AZURE_EU",
GCP_NA: "GCP_NA",
};
11 changes: 2 additions & 9 deletions src/generateTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { defaultInterfaces } from "./stack/builtins";
import { format } from "../format/index";
import { ContentType } from "../types/schema";

const validRegions = ["US", "EU", "AZURE_NA", "AZURE_EU", "GCP_NA"];

export const generateTS = async ({
token,
tokenType,
Expand All @@ -21,6 +19,7 @@ export const generateTS = async ({
prefix,
includeDocumentation,
systemFields,
host,
}: GenerateTS) => {
try {
if (!token || !tokenType || !apiKey || !environment || !region) {
Expand All @@ -31,20 +30,14 @@ export const generateTS = async ({
};
}

if (!validRegions.includes(region)) {
throw {
type: "validation",
error_message:
"Please provide a valid region, supported regions are : (US, EU, AZURE_NA, AZURE_EU, GCP_NA)",
};
}
if (tokenType === TOKEN_TYPE.DELIVERY) {
const Stack = initializeContentstackSdk({
apiKey,
token,
environment,
region,
branch,
host,
});

const contentTypeQuery = Stack.contentType();
Expand Down
41 changes: 28 additions & 13 deletions src/sdk/utils.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
import Contentstack, { Region } from "@contentstack/delivery-sdk";
import { StackConnectionConfig } from "../types";
import { REGIONS } from "../constants";

export const initializeContentstackSdk = ({
apiKey,
token,
environment,
region,
branch,
host,
}: StackConnectionConfig) => {
try {
let isCustomRegion = false;
const regionVal: Region | undefined = (function (regionValue: string) {
switch (regionValue) {
case "US":
case REGIONS.US:
return Region.US;
case "EU":
case REGIONS.EU:
return Region.EU;
case "AZURE_NA":
case REGIONS.AZURE_NA:
return Region.AZURE_NA;
case "AZURE_EU":
case REGIONS.AZURE_EU:
return Region.AZURE_EU;
case "GCP_NA":
case REGIONS.GCP_NA:
return Region.GCP_NA;
default:
return undefined;
isCustomRegion = true;
break;
}
})(region as string);

const Stack = Contentstack.stack({
apiKey: apiKey,
deliveryToken: token,
environment,
region: regionVal,
branch: branch,
});
let Stack;
if (isCustomRegion && host) {
Stack = Contentstack.stack({
apiKey: apiKey,
deliveryToken: token,
environment,
host: host,
branch: branch,
});
} else {
Stack = Contentstack.stack({
apiKey: apiKey,
deliveryToken: token,
environment,
region: regionVal,
branch: branch,
});
}

return Stack;
} catch (err: any) {
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface StackConnectionConfig {
region: "US" | "EU" | "AZURE_NA" | "AZURE_EU" | "GCP_NA";
environment: string;
branch?: string;
host?: string;
}

export interface GenerateTSBase extends StackConnectionConfig {
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/generateTS/generateTS.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ describe("generateTS function with errors", () => {
branch,
});
} catch (err: any) {
expect(err.error_message).toEqual(
"Please provide a valid region, supported regions are : (US, EU, AZURE_NA, AZURE_EU, GCP_NA)"
);
expect(err.error_message).toEqual("Something went wrong");
}
});

Expand Down
4 changes: 1 addition & 3 deletions tests/unit/generateTS/generateTS.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@ describe("generateTS function with errors", () => {
branch,
});
} catch (err: any) {
expect(err.error_message).toEqual(
"Please provide a valid region, supported regions are : (US, EU, AZURE_NA, AZURE_EU, GCP_NA)"
);
expect(err.error_message).toEqual("Something went wrong");
}
});

Expand Down
Loading