@@ -29,6 +29,19 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
2929 '$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --api-type graphql --namespace "GraphQL" ' ,
3030 ] ;
3131
32+ // Check if a region is a default Contentstack region
33+ private isDefaultRegion ( region : string ) : boolean {
34+ const defaultRegions = [
35+ "US" ,
36+ "EU" ,
37+ "AZURE_NA" ,
38+ "AZURE_EU" ,
39+ "GCP_NA" ,
40+ "GCP_EU" ,
41+ ] ;
42+ return defaultRegions . includes ( region . toUpperCase ( ) ) ;
43+ }
44+
3245 static flags : FlagInput = {
3346 "token-alias" : flags . string ( {
3447 char : "a" ,
@@ -127,7 +140,37 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
127140 if ( config . region === "us" ) {
128141 config . region = "US" ;
129142 }
130- const result = await graphqlTS ( { ...config , namespace : namespace } ) ;
143+
144+ // Check if token has delivery type (required for GraphQL)
145+ if ( token . type !== "delivery" ) {
146+ throw new Error (
147+ "GraphQL API requires a delivery token. Management tokens are not supported for GraphQL operations." ,
148+ ) ;
149+ }
150+
151+ // Prepare GraphQL config - only include host for custom regions
152+ const graphqlConfig : any = {
153+ apiKey : config . apiKey ,
154+ token : config . token ,
155+ environment : config . environment ,
156+ namespace : namespace ,
157+ } ;
158+
159+ // Add region or host based on whether it's a custom region
160+ if ( config . host && ! this . isDefaultRegion ( config . region ) ) {
161+ // Custom region - include both region and host
162+ graphqlConfig . region = config . region ;
163+ graphqlConfig . host = config . host ;
164+ } else {
165+ // Default region - only include region
166+ graphqlConfig . region = config . region ;
167+ }
168+
169+ const result = await graphqlTS ( graphqlConfig ) ;
170+
171+ if ( ! result ) {
172+ throw new Error ( "GraphQL API returned no result" ) ;
173+ }
131174
132175 fs . writeFileSync ( outputPath , result ) ;
133176 this . log (
0 commit comments