-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b487d43
commit e239b0b
Showing
9 changed files
with
213 additions
and
46 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
dist | ||
testPlayground.mjs | ||
testPlayground.ts |
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,94 @@ | ||
import { Cast, DataOrError, User, UserWithOptionalViewerContext } from "./types"; | ||
|
||
export type StringOrNumberArray = (string | number)[]; | ||
|
||
type MapData<T> = { | ||
data:T | ||
timestamp: number; | ||
} | ||
|
||
export class Cache { | ||
private cache = new Map<string, MapData<User | UserWithOptionalViewerContext | Cast>>(); | ||
private ttl: number = 60 * 60 * 1000; | ||
constructor(config: CacheConfig = {}) { | ||
if (config.ttl) { | ||
this.ttl = config.ttl; | ||
} | ||
} | ||
|
||
private getData(key: string): User | UserWithOptionalViewerContext | Cast | null { | ||
const cachedData = this.cache.get(key); | ||
if (cachedData) { | ||
return cachedData.data; | ||
} | ||
return null; | ||
} | ||
|
||
private getUserCachedData(params: StringOrNumberArray): User |UserWithOptionalViewerContext | null { | ||
const cacheKey = `${["user", ...params].join(":")}`; | ||
return this.getData(cacheKey) as User |UserWithOptionalViewerContext | null; | ||
} | ||
|
||
private getCastCachedData(params: StringOrNumberArray): Cast | ||
| null { | ||
const cacheKey = `${["cast", ...params].join(":")}`; | ||
|
||
return this.getData(cacheKey) as Cast | null; | ||
} | ||
|
||
private setUserCachedData(data: User, params: StringOrNumberArray): User { | ||
const fidKey = `${["user", data.fid, ...params].join(":")}`; | ||
const usernameKey = `${["user", data.username, ...params].join(":")}`; | ||
const setData = { data, timestamp: Date.now() }; | ||
this.cache.set(fidKey, setData); | ||
this.cache.set(usernameKey, setData); | ||
return data | ||
} | ||
private setCastCachedData(data: Cast, params: StringOrNumberArray): Cast { | ||
const hashKey = `${["cast", data.hash, ...params].join(":")}`; | ||
const urlKey = `${["cast", data.url, ...params].join(":")}`; | ||
const setData = { data, timestamp: Date.now() }; | ||
this.cache.set(hashKey, setData); | ||
this.cache.set(urlKey, setData); | ||
return data | ||
} | ||
|
||
public get<T extends CacheKeys>(type: T, params: StringOrNumberArray) { | ||
|
||
if (type === "user") { | ||
return this.getUserCachedData(params); | ||
} | ||
if (type === "cast") { | ||
return this.getCastCachedData(params); | ||
} | ||
//Add more cache types if needed | ||
throw new Error("Invalid cache type"); | ||
} | ||
|
||
public set<T extends CacheKeys>( | ||
type: T, | ||
data: CacheTypes[T], | ||
params: StringOrNumberArray | ||
): User | Cast { | ||
if (type === "user") { | ||
return this.setUserCachedData(data as User, params); | ||
} | ||
if (type === "cast") { | ||
return this.setCastCachedData(data as Cast, params); | ||
} | ||
//Add more cache types if needed | ||
throw new Error("Invalid cache type"); | ||
} | ||
} | ||
|
||
type CacheConfig = { | ||
ttl?: number; | ||
}; | ||
|
||
export interface CacheTypes { | ||
user: User | UserWithOptionalViewerContext; | ||
cast: Cast; | ||
} | ||
export type CacheKeys = keyof CacheTypes; | ||
|
||
type P = CacheTypes[CacheKeys] |
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
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 |
---|---|---|
@@ -1,15 +1,27 @@ | ||
import uniFarcasterSdk from "@"; | ||
import uniFarcasterSdk, { Cache } from "../dist/index.mjs"; | ||
|
||
const sdk = new uniFarcasterSdk({ | ||
neynarApiKey: "NEYNAR_API_DOCS", | ||
airstackApiKey: "1534c9578b8e645b796727f8d3236993b", | ||
airstackApiKey: "AIRSTACK_API_DOCS", | ||
activeService: "neynar", | ||
debug: true, | ||
logLevel: "warning" | ||
debug: true, | ||
}); | ||
|
||
sdk.getActiveService(); | ||
// sdk.getActiveService(); | ||
// const {data: user, error} = await sdk.getUserByUsername('noseals'); | ||
// const { data: user2, error2 } = await sdk.getUserByUsername('noseals'); | ||
// sdk.setActiveService('neynar'); | ||
// const { data: user3, error3 } = await sdk.getUserByUsername('noseals'); | ||
// const { data: user4, error4 } = await sdk.getUserByUsername(user3.fid); | ||
|
||
const { data: cast, error: errorCast } = await sdk.getCastByHash( | ||
"0xbed8c16b624d656b3471746f0d73ab9d8ec1fb95" | ||
); | ||
sdk.setActiveService("airstack"); | ||
const user = await sdk.getUserByFid(213144); | ||
// sdk.setActiveService("neynar"); | ||
const user2 = await sdk.getUserByUsername("v") | ||
const { data: cast1, error: errorCast1 } = await sdk.getCastByUrl( | ||
cast.url | ||
); | ||
const { data: cast2, error: errorCast2 } = await sdk.getCastByHash( | ||
cast.hash | ||
); | ||
|
Oops, something went wrong.