Skip to content

Commit

Permalink
Merge pull request #11 from BolajiOlajide/bo/allow-custom-istorer
Browse files Browse the repository at this point in the history
allow custom storer interface
  • Loading branch information
lilpolymath authored Jun 30, 2024
2 parents dc259a8 + 2076fa5 commit a863661
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
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

0 comments on commit a863661

Please sign in to comment.