Skip to content

[DX-3038], fix custom config for graphQL and updated regions #90

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

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/types-generator",
"version": "3.1.0",
"version": "3.2.0",
"description": "Contentstack type definition generation library",
"private": false,
"author": "Contentstack",
Expand Down
4 changes: 4 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ export const TOKEN_TYPE = {
export const REGIONS = {
US: "US",
EU: "EU",
AWS_NA: "AWS-NA",
AWS_EU: "AWS-EU",
AZURE_NA: "AZURE_NA",
AZURE_EU: "AZURE_EU",
GCP_NA: "GCP_NA",
GCP_EU: "GCP_EU",
CUSTOM: "CUSTOM",
};
28 changes: 19 additions & 9 deletions src/graphqlTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export async function graphqlTS({
environment,
branch,
namespace,
host,
}: GraphQLBase) {
try {
if (!token || !apiKey || !environment || !region) {
Expand All @@ -34,7 +35,9 @@ export async function graphqlTS({
let config = {
method: "post",
maxBodyLength: Infinity,
url: `${GRAPHQL_REGION_URL_MAPPING[region]}/${apiKey}`,
url: host
? `https://${host}/stacks/${apiKey}`
: `${GRAPHQL_REGION_URL_MAPPING[region]}/${apiKey}`,
headers: {
access_token: token,
branch: "",
Expand All @@ -50,10 +53,10 @@ export async function graphqlTS({
config.headers.branch = branch;
}

if (!GRAPHQL_REGION_URL_MAPPING[region]) {
if (!GRAPHQL_REGION_URL_MAPPING[region] && !host) {
throw {
type: "validation",
error_message: `GraphQL content delivery api unavailable for '${region}' region`,
error_message: `GraphQL content delivery api unavailable for '${region}' region and no custom host provided`,
};
}

Expand All @@ -80,14 +83,21 @@ export async function graphqlTS({
"Unauthorized: The apiKey, token or environment is not valid.",
};
} else {
let details = '';
if (error.response.data.errors[0]?.extensions?.errors?.[0]?.code === 'SCHEMA_BUILD_ERROR') {
error.response.data.errors[0].extensions.errors[0].details.forEach((element: {error: string}) => {
details += element.error + '\n'
});
let details = "";
if (
error.response.data.errors[0]?.extensions?.errors?.[0]?.code ===
"SCHEMA_BUILD_ERROR"
) {
error.response.data.errors[0].extensions.errors[0].details.forEach(
(element: { error: string }) => {
details += element.error + "\n";
}
);
Comment on lines +91 to +95
Copy link
Preview

Copilot AI Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The manual concatenation of error details could be simplified using map() and join('\n') for clearer intent and brevity.

Suggested change
error.response.data.errors[0].extensions.errors[0].details.forEach(
(element: { error: string }) => {
details += element.error + "\n";
}
);
details = error.response.data.errors[0].extensions.errors[0].details
.map((element: { error: string }) => element.error)
.join("\n");

Copilot uses AI. Check for mistakes.

}
throw {
error_message: details ? details : error.response.data.errors[0]?.extensions?.errors[0].message,
error_message: details
? details
: error.response.data.errors[0]?.extensions?.errors[0].message,
};
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/sdk/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ export const initializeContentstackSdk = ({
return Region.US;
case REGIONS.EU:
return Region.EU;
case REGIONS.AWS_NA:
return Region.US;
case REGIONS.AWS_EU:
return Region.EU;
case REGIONS.AZURE_NA:
return Region.AZURE_NA;
case REGIONS.AZURE_EU:
return Region.AZURE_EU;
case REGIONS.GCP_NA:
return Region.GCP_NA;
case REGIONS.GCP_EU:
return Region.GCP_EU;
default:
isCustomRegion = true;
break;
Expand Down
11 changes: 10 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
export interface StackConnectionConfig {
apiKey: string;
token: string;
region: "US" | "EU" | "AZURE_NA" | "AZURE_EU" | "GCP_NA";
region:
| "US"
| "EU"
| "AWS-NA"
| "AWS-EU"
| "AZURE_NA"
| "AZURE_EU"
| "GCP_NA"
| "GCP_EU"
| "CUSTOM";
environment: string;
branch?: string;
host?: string;
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/graphqlTS/graphqlTS.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe("graphqlTS function with errors", () => {
});
} catch (err: any) {
expect(err.error_message).toEqual(
"GraphQL content delivery api unavailable for 'wrong-region' region"
"GraphQL content delivery api unavailable for 'wrong-region' region and no custom host provided"
);
}
});
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/graphqlTS/graphqlTS.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe("graphqlTS function with errors", () => {
});
} catch (err: any) {
expect(err.error_message).toEqual(
"GraphQL content delivery api unavailable for 'wrong-region' region"
"GraphQL content delivery api unavailable for 'wrong-region' region and no custom host provided"
);
}
});
Expand Down