Skip to content

Commit 2ad854e

Browse files
committed
cleanup methods, make private
1 parent 0274ddd commit 2ad854e

File tree

3 files changed

+18
-242
lines changed

3 files changed

+18
-242
lines changed

packages/nylas-connect/README.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ Hook return values (essentials):
479479
| connect | (opts) => Promise<ConnectResult\|string> | Start auth flow |
480480
| logout | () => Promise<void> | Sign out |
481481

482-
Optional: `enableDebug`, `disableDebug`, `setLogLevel`, `logger`, `connectClient`.
482+
Optional: `setLogLevel`, `connectClient`.
483483

484484
---
485485

@@ -492,8 +492,6 @@ Optional: `enableDebug`, `disableDebug`, `setLogLevel`, `logger`, `connectClient
492492
| getAuthUrl(options?) | Promise<{ url: string; state: string; scopes: string[] }> | Build auth URL for backend-only exchange |
493493
| getSession(grantId?) | Promise<SessionData\|null> | Get current session |
494494
| getAccessToken(grantId?) | Promise<string\|null> | Get stored access token |
495-
| getGrantInfo(grantId?) | Promise<GrantInfo\|null> | Get stored grant info |
496-
| isConnected(grantId?) | Promise<boolean> | Is grant currently connected? |
497495
| getConnectionStatus(grantId?) | Promise<ConnectionStatus> | Get connection status |
498496
| logout(grantId?) | Promise<void> | Clear stored tokens and logout |
499497
| onConnectStateChange(cb) | () => void | Subscribe to connect state changes |
@@ -536,14 +534,12 @@ Method groups and when to use:
536534
### Session
537535

538536
- `getSession(grantId?)`: Read the current session for a grant.
539-
- `isConnected(grantId?)`: Check whether a grant is connected.
540537
- `getConnectionStatus(grantId?)`: Inspect connection state.
541538
- `logout(grantId?)`: Clear stored tokens and logout.
542539

543540
### Tokens & Grants
544541

545542
- `getAccessToken(grantId?)`: Read the stored access token, if available.
546-
- `getGrantInfo(grantId?)`: Read grant metadata (e.g., name, email).
547543

548544
### Events
549545

@@ -567,8 +563,6 @@ Common errors:
567563
<summary>Logging</summary>
568564

569565
```ts
570-
nylasConnect.enableDebug();
571-
nylasConnect.disableDebug();
572566
nylasConnect.setLogLevel("warn");
573567
```
574568

@@ -614,7 +608,7 @@ Symptom → Cause → Fix
614608
- Session not persisting → Memory-only mode or blocked storage → Set `persistTokens: true`; allow localStorage.
615609
- Redirect mismatch → `redirectUri` differs from Dashboard config → Ensure exact match including protocol/port/path.
616610
- Region errors/CORS → Wrong `apiUrl` vs account region → Use correct region base URL (US/EU) and allow domain in Dashboard.
617-
- Token expired → Session stale → Re-auth or refresh; check `isConnected()` and handle expiration.
611+
- Token expired → Session stale → Re-auth or refresh; check `getConnectionStatus()` and handle expiration.
618612
- Inline flow not navigating → Ensure you return early after setting `window.location.href`.
619613
- Connect called outside a user gesture → Browsers may block popups; call directly in a click/tap handler (avoid extra awaits first).
620614

@@ -663,7 +657,7 @@ License: MIT
663657
When handling multiple accounts, pass a `grantId` to target a specific connection:
664658

665659
```ts
666-
const isPrimaryConnected = await nylasConnect.isConnected(primaryGrantId);
660+
const status = await nylasConnect.getConnectionStatus(primaryGrantId);
667661
const token = await nylasConnect.getAccessToken(secondaryGrantId);
668662
await nylasConnect.logout(primaryGrantId);
669663
```

packages/nylas-connect/src/connect-client.test.ts

Lines changed: 14 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe("NylasConnect (fundamentals)", () => {
137137
expect(localStorage.getItem("@nylas/connect:token_g")).toBeNull();
138138
});
139139

140-
it("enableDebug() and disableDebug() control logger levels correctly", () => {
140+
it("setLogLevel controls logger levels correctly", () => {
141141
const auth = new NylasConnect({
142142
clientId,
143143
redirectUri,
@@ -153,43 +153,7 @@ describe("NylasConnect (fundamentals)", () => {
153153
};
154154

155155
try {
156-
// Test enableDebug - should enable debug level logging
157-
auth.enableDebug();
158-
159-
// Reset spies
160-
Object.values(consoleSpy).forEach((spy) => spy.mockClear());
161-
162-
// Test that debug messages now show
163-
logger.debug("test debug message");
164-
logger.info("test info message");
165-
166-
expect(consoleSpy.log).toHaveBeenCalledWith(
167-
expect.stringMatching(/\[.*\] \[NYLAS-AUTH\] \[DEBUG\]/),
168-
"test debug message",
169-
);
170-
expect(consoleSpy.info).toHaveBeenCalledWith(
171-
expect.stringMatching(/\[.*\] \[NYLAS-AUTH\] \[INFO\]/),
172-
"test info message",
173-
);
174-
175-
// Test disableDebug - should disable all logging
176-
auth.disableDebug();
177-
178-
// Reset spies
179-
Object.values(consoleSpy).forEach((spy) => spy.mockClear());
180-
181-
// Test that no messages show now
182-
logger.debug("debug should not show");
183-
logger.info("info should not show");
184-
logger.warn("warn should not show");
185-
logger.error("error should not show");
186-
187-
expect(consoleSpy.log).not.toHaveBeenCalled();
188-
expect(consoleSpy.info).not.toHaveBeenCalled();
189-
expect(consoleSpy.warn).not.toHaveBeenCalled();
190-
expect(consoleSpy.error).not.toHaveBeenCalled();
191-
192-
// Test setLogLevel method
156+
// Set WARN level - only warn and error messages should show
193157
auth.setLogLevel(LogLevel.WARN);
194158

195159
// Reset spies
@@ -211,6 +175,18 @@ describe("NylasConnect (fundamentals)", () => {
211175
expect.stringMatching(/\[.*\] \[NYLAS-AUTH\] \[ERROR\]/),
212176
"error should show",
213177
);
178+
179+
// Disable all logs
180+
auth.setLogLevel("off");
181+
Object.values(consoleSpy).forEach((spy) => spy.mockClear());
182+
logger.debug("debug off");
183+
logger.info("info off");
184+
logger.warn("warn off");
185+
logger.error("error off");
186+
expect(consoleSpy.log).not.toHaveBeenCalled();
187+
expect(consoleSpy.info).not.toHaveBeenCalled();
188+
expect(consoleSpy.warn).not.toHaveBeenCalled();
189+
expect(consoleSpy.error).not.toHaveBeenCalled();
214190
} finally {
215191
// Clean up spies
216192
Object.values(consoleSpy).forEach((spy) => spy.mockRestore());
@@ -672,84 +648,6 @@ describe("NylasConnect (sessions, validation, and events)", () => {
672648
expect(localStorage.getItem("@nylas/connect:token_default")).toBeNull();
673649
});
674650

675-
it("validateToken returns true when response ok and has data", async () => {
676-
const auth = new NylasConnect({
677-
clientId,
678-
redirectUri,
679-
apiUrl: "https://api.nylas.com",
680-
});
681-
682-
const validSession = {
683-
accessToken: "t_ok",
684-
idToken: "h.e.y",
685-
grantId: "g_ok",
686-
expiresAt: Date.now() + 60_000,
687-
scope: "email",
688-
};
689-
localStorage.setItem(
690-
"@nylas/connect:token_default",
691-
JSON.stringify(validSession),
692-
);
693-
694-
const spy = vi.fn();
695-
auth.onConnectStateChange(spy);
696-
697-
vi.stubGlobal(
698-
"fetch",
699-
vi.fn().mockResolvedValue({
700-
ok: true,
701-
json: async () => ({ data: { grant_id: "g_ok" } }),
702-
}),
703-
);
704-
705-
const result = await auth.validateToken();
706-
expect(result).toBe(true);
707-
expect(spy).not.toHaveBeenCalledWith(
708-
"TOKEN_VALIDATION_ERROR",
709-
expect.anything(),
710-
expect.anything(),
711-
);
712-
});
713-
714-
it("validateToken returns false and emits TOKEN_VALIDATION_ERROR when invalid", async () => {
715-
const auth = new NylasConnect({
716-
clientId,
717-
redirectUri,
718-
apiUrl: "https://api.nylas.com",
719-
});
720-
721-
const session = {
722-
accessToken: "t_bad",
723-
idToken: "h.e.y",
724-
grantId: "g_bad",
725-
expiresAt: Date.now() + 60_000,
726-
scope: "email",
727-
};
728-
localStorage.setItem(
729-
"@nylas/connect:token_default",
730-
JSON.stringify(session),
731-
);
732-
733-
const spy = vi.fn();
734-
auth.onConnectStateChange(spy);
735-
736-
vi.stubGlobal(
737-
"fetch",
738-
vi.fn().mockResolvedValue({
739-
ok: true,
740-
json: async () => ({}),
741-
}),
742-
);
743-
744-
const result = await auth.validateToken();
745-
expect(result).toBe(false);
746-
expect(spy).toHaveBeenCalledWith(
747-
"TOKEN_VALIDATION_ERROR",
748-
null,
749-
expect.objectContaining({ grantId: "g_bad" }),
750-
);
751-
});
752-
753651
it("onAuthStateChange unsubscribe stops subsequent emissions", async () => {
754652
const auth = new NylasConnect({
755653
clientId,
@@ -769,48 +667,6 @@ describe("NylasConnect (sessions, validation, and events)", () => {
769667
expect(spy).not.toHaveBeenCalled();
770668
});
771669

772-
it("getGrantInfo emits GRANT_PROFILE_LOADED with source 'token'", async () => {
773-
const auth = new NylasConnect({
774-
clientId,
775-
redirectUri,
776-
apiUrl: "https://api.nylas.com",
777-
});
778-
779-
const grantInfo = {
780-
id: "user_1",
781-
email: "alice@example.com",
782-
name: "Alice",
783-
picture: "https://example.com/p.png",
784-
provider: "google",
785-
emailVerified: true,
786-
};
787-
788-
const session = {
789-
accessToken: "t",
790-
idToken: "h.e.y",
791-
grantId: "g1",
792-
expiresAt: Date.now() + 60_000,
793-
scope: "email profile",
794-
grantInfo,
795-
};
796-
localStorage.setItem(
797-
"@nylas/connect:token_default",
798-
JSON.stringify(session),
799-
);
800-
801-
const spy = vi.fn();
802-
auth.onConnectStateChange(spy);
803-
804-
const result = await auth.getGrantInfo();
805-
expect(result).toEqual(grantInfo);
806-
807-
expect(spy).toHaveBeenCalledWith(
808-
"GRANT_PROFILE_LOADED",
809-
expect.objectContaining({ grantId: "g1" }),
810-
{ grantInfo, source: "token" },
811-
);
812-
});
813-
814670
describe("getConnectionStatus", () => {
815671
it("returns not_connected when no session", async () => {
816672
const auth = new NylasConnect({

packages/nylas-connect/src/connect-client.ts

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ export class NylasConnect {
674674
/**
675675
* Validate an access token
676676
*/
677-
async validateToken(token?: string): Promise<boolean> {
677+
private async validateToken(token?: string): Promise<boolean> {
678678
let accessToken = token;
679679
let grantId = "unknown";
680680

@@ -728,32 +728,6 @@ export class NylasConnect {
728728
}
729729
}
730730

731-
/**
732-
* Get grant information from stored token
733-
*/
734-
async getGrantInfo(grantId?: string): Promise<GrantInfo | null> {
735-
const session = await this.getSession(grantId);
736-
const grantInfo = session?.grantInfo ?? null;
737-
738-
if (grantInfo) {
739-
// Emit GRANT_PROFILE_LOADED event
740-
this.triggerConnectStateChange("GRANT_PROFILE_LOADED", session, {
741-
grantInfo,
742-
source: "token",
743-
});
744-
}
745-
746-
return grantInfo;
747-
}
748-
749-
/**
750-
* Check if a grant is currently connected
751-
*/
752-
async isConnected(grantId?: string): Promise<boolean> {
753-
const session = await this.getSession(grantId);
754-
return session !== null;
755-
}
756-
757731
/**
758732
* Get current connection status
759733
*/
@@ -845,20 +819,6 @@ export class NylasConnect {
845819
logger.info("Grant logged out", { grantId });
846820
}
847821

848-
/**
849-
* Enable debug logging
850-
*/
851-
enableDebug(): void {
852-
logger.enable();
853-
}
854-
855-
/**
856-
* Disable debug logging
857-
*/
858-
disableDebug(): void {
859-
logger.disable();
860-
}
861-
862822
/**
863823
* Set logging level
864824
* @param level - Log level: LogLevel enum values or "off"
@@ -867,40 +827,6 @@ export class NylasConnect {
867827
logger.setLevel(level);
868828
}
869829

870-
/**
871-
* Get access to the logger instance for advanced usage
872-
*/
873-
get logger(): Pick<
874-
typeof logger,
875-
| "debug"
876-
| "info"
877-
| "warn"
878-
| "error"
879-
| "log"
880-
| "setLevel"
881-
| "enable"
882-
| "disable"
883-
> {
884-
return {
885-
debug: logger.debug.bind(logger),
886-
info: logger.info.bind(logger),
887-
warn: logger.warn.bind(logger),
888-
error: logger.error.bind(logger),
889-
log: logger.log.bind(logger),
890-
setLevel: logger.setLevel.bind(logger),
891-
enable: logger.enable.bind(logger),
892-
disable: logger.disable.bind(logger),
893-
};
894-
}
895-
896-
/**
897-
* Check if automatic callback handling is enabled
898-
* @returns true if auto callback handling is enabled
899-
*/
900-
isAutoCallbackEnabled(): boolean {
901-
return this.config.autoHandleCallback;
902-
}
903-
904830
/**
905831
* Check if defaultScopes is configured as provider-specific
906832
*/

0 commit comments

Comments
 (0)