@@ -759,3 +759,129 @@ describe("NylasConnect (sessions, validation, and events)", () => {
759759 } ) ;
760760 } ) ;
761761} ) ;
762+
763+ describe ( "NylasConnect (API URL normalization)" , ( ) => {
764+ const clientId = "client_123" ;
765+ const redirectUri = "https://app.example/callback" ;
766+
767+ beforeEach ( ( ) => {
768+ localStorage . clear ( ) ;
769+ vi . restoreAllMocks ( ) ;
770+ } ) ;
771+
772+ it ( "should append /v3 to default API URL when no version is present" , async ( ) => {
773+ const auth = new NylasConnect ( {
774+ clientId,
775+ redirectUri,
776+ apiUrl : "https://api.us.nylas.com" ,
777+ } ) ;
778+
779+ const { url } = await auth . getAuthUrl ( ) ;
780+ expect ( url ) . toContain ( "https://api.us.nylas.com/v3/connect/auth" ) ;
781+ } ) ;
782+
783+ it ( "should append /v3 to custom API URL when no version is present" , async ( ) => {
784+ const auth = new NylasConnect ( {
785+ clientId,
786+ redirectUri,
787+ apiUrl : "https://custom.api.com" ,
788+ } ) ;
789+
790+ const { url } = await auth . getAuthUrl ( ) ;
791+ expect ( url ) . toContain ( "https://custom.api.com/v3/connect/auth" ) ;
792+ } ) ;
793+
794+ it ( "should preserve existing version suffix (v2)" , async ( ) => {
795+ const auth = new NylasConnect ( {
796+ clientId,
797+ redirectUri,
798+ apiUrl : "https://api.us.nylas.com/v2" ,
799+ } ) ;
800+
801+ const { url } = await auth . getAuthUrl ( ) ;
802+ expect ( url ) . toContain ( "https://api.us.nylas.com/v2/connect/auth" ) ;
803+ expect ( url ) . not . toContain ( "/v3" ) ;
804+ } ) ;
805+
806+ it ( "should preserve existing version suffix (v1)" , async ( ) => {
807+ const auth = new NylasConnect ( {
808+ clientId,
809+ redirectUri,
810+ apiUrl : "https://api.us.nylas.com/v1" ,
811+ } ) ;
812+
813+ const { url } = await auth . getAuthUrl ( ) ;
814+ expect ( url ) . toContain ( "https://api.us.nylas.com/v1/connect/auth" ) ;
815+ expect ( url ) . not . toContain ( "/v3" ) ;
816+ } ) ;
817+
818+ it ( "should handle trailing slashes correctly" , async ( ) => {
819+ const auth = new NylasConnect ( {
820+ clientId,
821+ redirectUri,
822+ apiUrl : "https://api.us.nylas.com/" ,
823+ } ) ;
824+
825+ const { url } = await auth . getAuthUrl ( ) ;
826+ expect ( url ) . toContain ( "https://api.us.nylas.com/v3/connect/auth" ) ;
827+ expect ( url ) . not . toContain ( "//v3" ) ; // Should not have double slashes
828+ } ) ;
829+
830+ it ( "should handle multiple trailing slashes correctly" , async ( ) => {
831+ const auth = new NylasConnect ( {
832+ clientId,
833+ redirectUri,
834+ apiUrl : "https://api.us.nylas.com///" ,
835+ } ) ;
836+
837+ const { url } = await auth . getAuthUrl ( ) ;
838+ expect ( url ) . toContain ( "https://api.us.nylas.com/v3/connect/auth" ) ;
839+ expect ( url ) . not . toContain ( "//v3" ) ; // Should not have double slashes
840+ } ) ;
841+
842+ it ( "should preserve version with trailing slashes" , async ( ) => {
843+ const auth = new NylasConnect ( {
844+ clientId,
845+ redirectUri,
846+ apiUrl : "https://api.us.nylas.com/v2/" ,
847+ } ) ;
848+
849+ const { url } = await auth . getAuthUrl ( ) ;
850+ expect ( url ) . toContain ( "https://api.us.nylas.com/v2/connect/auth" ) ;
851+ expect ( url ) . not . toContain ( "/v3" ) ;
852+ } ) ;
853+
854+ it ( "should append /v3 to default URL when no apiUrl is provided" , async ( ) => {
855+ const auth = new NylasConnect ( {
856+ clientId,
857+ redirectUri,
858+ // No apiUrl provided - should use default
859+ } ) ;
860+
861+ const { url } = await auth . getAuthUrl ( ) ;
862+ expect ( url ) . toContain ( "https://api.us.nylas.com/v3/connect/auth" ) ;
863+ } ) ;
864+
865+ it ( "should handle EU API URL correctly" , async ( ) => {
866+ const auth = new NylasConnect ( {
867+ clientId,
868+ redirectUri,
869+ apiUrl : "https://api.eu.nylas.com" ,
870+ } ) ;
871+
872+ const { url } = await auth . getAuthUrl ( ) ;
873+ expect ( url ) . toContain ( "https://api.eu.nylas.com/v3/connect/auth" ) ;
874+ } ) ;
875+
876+ it ( "should work with higher version numbers" , async ( ) => {
877+ const auth = new NylasConnect ( {
878+ clientId,
879+ redirectUri,
880+ apiUrl : "https://api.us.nylas.com/v10" ,
881+ } ) ;
882+
883+ const { url } = await auth . getAuthUrl ( ) ;
884+ expect ( url ) . toContain ( "https://api.us.nylas.com/v10/connect/auth" ) ;
885+ expect ( url ) . not . toContain ( "/v3" ) ;
886+ } ) ;
887+ } ) ;
0 commit comments