11import { messageHandler , logger } from '@contentstack/cli-utilities' ;
22
3- /**
4- * Validate delivery token
5- * @param contentStackClient
6- * @param apiKey
7- * @param deliveryToken
8- * @param environment
9- * @param region
10- * @returns
11- */
12- export const validateDeliveryToken = async (
13- contentStackClient : any ,
14- apiKey : string ,
15- deliveryToken : string ,
16- environment : string ,
17- region : string ,
18- host : string ,
19- branch : string ,
20- ) : Promise < any > => {
21- let result : { valid : boolean ; message : string } ;
22- try {
23- const regionMap = {
24- EU : 'eu' ,
25- NA : 'us' ,
26- AZURE_NA : 'azure-na' ,
27- AZURE_EU : 'azure-eu' ,
28- } ;
29-
30- const stack = contentStackClient . Stack ( {
31- api_key : apiKey ,
32- delivery_token : deliveryToken ,
33- environment,
34- region : regionMap [ region ] ,
35- host,
36- branch,
37- } ) ;
38-
39- const parsedHost = host . replace ( / ^ h t t p s ? : \/ \/ / , '' ) ;
40- stack . setHost ( parsedHost ) ;
41- const deliveryTokenResult = await stack . getContentTypes ( { limit : 1 } ) ;
42-
43- logger . debug ( 'delivery token validation result' , deliveryTokenResult ) ;
44- if ( deliveryTokenResult ?. content_types ) {
45- result = { valid : true , message : deliveryTokenResult } ;
46- } else {
47- result = { valid : false , message : messageHandler . parse ( 'CLI_AUTH_TOKENS_VALIDATION_INVALID_DELIVERY_TOKEN' ) } ;
48- }
49- } catch ( error ) {
50- logger . debug ( 'validate delivery token error' , error ) ;
51- if ( error . error_code === 109 ) {
52- result = { valid : false , message : messageHandler . parse ( 'CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY' ) } ;
53- } else if ( error . error_code === 141 ) {
54- result = { valid : false , message : messageHandler . parse ( 'CLI_AUTH_TOKENS_VALIDATION_INVALID_ENVIRONMENT_NAME' ) } ;
55- } else if ( error ?. error_code ) {
56- throw new Error ( error ?. error_message ) ;
57- } else {
58- result = { valid : false , message : messageHandler . parse ( 'CLI_AUTH_TOKENS_VALIDATION_INVALID_DELIVERY_TOKEN' ) } ;
59- }
60- }
61- return result ;
62- } ;
63-
643/**
654 * Validate environment
665 * @param contentStackClient
@@ -75,7 +14,7 @@ export const validateEnvironment = async (
7514) : Promise < any > => {
7615 let result : { valid : boolean ; message : string } ;
7716 try {
78- const validationResult = await contentStackClient . stack ( { api_key : apiKey } ) . environment ( environment ) . fetch ( ) ;
17+ const validationResult = await contentStackClient . Stack ( { api_key : apiKey } ) . environment ( environment ) . fetch ( ) ;
7918 logger . debug ( 'environment validation result' , validationResult ) ;
8019 if ( validationResult . name === environment ) {
8120 result = { valid : true , message : validationResult } ;
@@ -89,46 +28,6 @@ export const validateEnvironment = async (
8928 return result ;
9029} ;
9130
92- /**
93- * Validate management token
94- * @param contentStackClient
95- * @param apiKey
96- * @param managementToken
97- * @returns { valid: boolean; message: any }
98- * Note: Fetching one content type using the management token to check whether it is valid or not
99- */
100- export const validateManagementToken = async (
101- contentStackClient : any ,
102- apiKey : string ,
103- managementToken : string ,
104- branch : string ,
105- ) : Promise < any > => {
106- let result : { valid : boolean ; message : string } ;
107- try {
108- const validationResuslt = await contentStackClient . axiosInstance . get ( '/content_types?limit=1&include_branch=true' , {
109- headers : { api_key : apiKey , authorization : managementToken , branch } ,
110- } ) ;
111-
112- logger . debug ( 'Management validation result' , validationResuslt ) ;
113- if ( validationResuslt . status === 200 ) {
114- result = { valid : true , message : validationResuslt } ;
115- } else {
116- result = { valid : false , message : messageHandler . parse ( 'CLI_AUTH_TOKENS_VALIDATION_INVALID_MANAGEMENT_TOKEN' ) } ;
117- }
118- } catch ( error ) {
119- logger . error ( 'Failed to validate management token' , error ) ;
120- if ( error . response && error . response . status === 401 ) {
121- result = { valid : false , message : messageHandler . parse ( 'CLI_AUTH_TOKENS_VALIDATION_INVALID_MANAGEMENT_TOKEN' ) } ;
122- }
123- if ( error ?. response ?. data ) {
124- throw new Error ( error . response . data ?. error_message ) ;
125- } else {
126- result = { valid : false , message : messageHandler . parse ( 'CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY' ) } ;
127- }
128- }
129- return result ;
130- } ;
131-
13231/**
13332 * Validate API key
13433 * @param contentStackClient
0 commit comments