-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enh: cache nango access tokens (#2229)
- Loading branch information
1 parent
5911537
commit e7aa66b
Showing
7 changed files
with
176 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { redisClient } from "@connectors/lib/redis"; | ||
import { NangoConnectionId } from "@connectors/types/nango_connection_id"; | ||
|
||
import { nango_client } from "./nango_client"; | ||
|
||
const NANGO_ACCESS_TOKEN_TTL_SECONDS = 60 * 5; // 5 minutes | ||
|
||
export async function getAccessTokenFromNango({ | ||
connectionId, | ||
integrationId, | ||
useCache = false, | ||
}: { | ||
connectionId: NangoConnectionId; | ||
integrationId: string; | ||
useCache?: boolean; | ||
}) { | ||
const cacheKey = `nango_access_token:${integrationId}/${connectionId}`; | ||
const redis = await redisClient(); | ||
|
||
const _setCache = (token: string) => | ||
redis.set(cacheKey, token, { | ||
EX: NANGO_ACCESS_TOKEN_TTL_SECONDS, | ||
}); | ||
|
||
if (!useCache) { | ||
const accessToken = await _getAccessTokenFromNango({ | ||
connectionId, | ||
integrationId, | ||
}); | ||
await _setCache(accessToken); | ||
return accessToken; | ||
} | ||
|
||
const maybeAccessToken = await redis.get(cacheKey); | ||
if (maybeAccessToken) { | ||
return maybeAccessToken; | ||
} | ||
const accessToken = await nango_client().getToken( | ||
integrationId, | ||
connectionId | ||
); | ||
await _setCache(accessToken); | ||
return accessToken; | ||
} | ||
|
||
async function _getAccessTokenFromNango({ | ||
connectionId, | ||
integrationId, | ||
}: { | ||
connectionId: NangoConnectionId; | ||
integrationId: string; | ||
}) { | ||
const accessToken = await nango_client().getToken( | ||
integrationId, | ||
connectionId | ||
); | ||
return accessToken; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { createClient } from "redis"; | ||
|
||
export async function redisClient() { | ||
const { REDIS_URI } = process.env; | ||
if (!REDIS_URI) { | ||
throw new Error("REDIS_URI is not defined"); | ||
} | ||
const client = createClient({ | ||
url: REDIS_URI, | ||
}); | ||
client.on("error", (err) => console.log("Redis Client Error", err)); | ||
|
||
await client.connect(); | ||
|
||
return client; | ||
} |