Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow custom storer interface #11

Merged
merged 7 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,37 @@ yarn add @BolajiOlajide/now-playing
## Usage

```ts
import { NowPlaying, Providers } from '@BolajiOlajide/now-playing'
import { NowPlaying, Providers, type IStorer } from '@BolajiOlajide/now-playing'

let myCustomStorer: IStorer

const np = new NowPlaying(Providers.SPOTIFY, {
useCache: false, // default is true
cacheDuration: 30000, // in milliseconds
useCache: false, // (optional) default is true
cacheDuration: 30000, // (optional) in milliseconds
streamerArgs: {
clientId: 'foo',
clientSecret: 'bar',
refreshToken: 'baz',
},
storer: myCustomStorer, // (optional) custom storage implementation
})
```

### Storage

Data is stored in memory. This is to reduce overhead. We plan to expose the `IStorer` interface later to allow you to use your own storage mechanism.
We default to inmemory storage for saving information gotten via the API, (e.g Access tokens, Song data e.t.c).
However you can pass in your own custom storage object, provided it satisfies the `IStorer` interface.

```ts
interface IStorer {
set<T>(key: string, value: T, duration: number): void
get<T>(key: string): T | undefined
delete(key: string): boolean
has(key: string): boolean
clear(): void
pruneExpiredEntries(): void
}
```

### Providers

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { NowPlaying } from './main'
export { Providers } from './schema'
export { type IStorer } from './storage'
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class NowPlaying {

// We only support in memory storage for now, if there's a need we can
// support more storage mechanism.
this.storer = this.getStorer()
this.storer = args.storer || this.getStorer()
this.streamer = this.getStreamer()
} catch (err: unknown) {
if (err instanceof ZodError) {
Expand Down
20 changes: 20 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from 'zod'
import type { IStorer } from './storage'

export enum Providers {
SPOTIFY = 'SPOTIFY',
Expand All @@ -10,6 +11,25 @@ export const providerSchema = z.nativeEnum(Providers)
export const BaseNowPlayingArgsSchema = z.object({
useCache: z.boolean().optional(),
cacheDuration: z.number().optional(),
storer: z
.custom<IStorer>(
(data: unknown): data is IStorer => {
return (
typeof data === 'object' &&
data !== null &&
typeof (data as IStorer).set === 'function' &&
typeof (data as IStorer).get === 'function' &&
typeof (data as IStorer).delete === 'function' &&
typeof (data as IStorer).has === 'function' &&
typeof (data as IStorer).clear === 'function' &&
typeof (data as IStorer).pruneExpiredEntries === 'function'
)
},
{
message: 'Object does not implement IStorer interface',
}
)
.optional(),
})
export type BaseNowPlayingArgs = z.infer<typeof BaseNowPlayingArgsSchema>

Expand Down