Skip to content

Commit

Permalink
feat: add audiobooks manager
Browse files Browse the repository at this point in the history
  • Loading branch information
FC5570 committed Oct 1, 2022
1 parent c285434 commit 2f8022e
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules/
dist/
docs/
*.gz
.env*
.env*
tests/index.ts
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"prettier": "^2.7.1",
"pretty-quick": "^3.1.3",
"standard-version": "^9.5.0",
"ts-node": "^10.9.1",
"typedoc": "^0.23.15",
"typescript": "^4.8.4"
},
Expand Down
8 changes: 8 additions & 0 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { RestManager } from './rest';
import {
AlbumsManager,
ArtistsManager,
AudiobooksManager,
CategoriesManager,
EpisodesManager,
GeneresManager,
Expand Down Expand Up @@ -56,6 +57,12 @@ export class Client {
*/
public artists!: ArtistsManager;

/**
* The manager for handling audiobooks.
* @type {AudiobooksManager}
*/
public audiobooks!: AudiobooksManager;

/**
* The manager for handling browsing categories.
* @type {CategoriesManager}
Expand Down Expand Up @@ -145,6 +152,7 @@ export class Client {
this.rest = new RestManager(this);
this.albums = new AlbumsManager(this);
this.artists = new ArtistsManager(this);
this.audiobooks = new AudiobooksManager(this);
this.categories = new CategoriesManager(this);
this.episodes = new EpisodesManager(this);
this.genres = new GeneresManager(this);
Expand Down
100 changes: 100 additions & 0 deletions src/lib/classes/Audiobook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { Base } from '.';
import type { Client } from '../';
import type { APIAudiobook } from '../types';

export class Audiobook extends Base {
/**
* The Spotify ID for the audiobook.
*/
public id!: string;

/**
* The name of the audiobook.
*/
public name!: string;

/**
* A description of the audiobook. HTML tags are stripped away from this field, use html_description field in case HTML tags are needed.
*/
public description!: string;

/**
* A description of the audiobook. This field may contain HTML tags.
*/
public html_description!: string;

/**
* A link to the Web API endpoint providing full details of the audiobook.
*/
public href!: string;

/**
* Whether or not the audiobook has explicit content (true = yes it does; false = no it does not OR unknown).
*/
public explicit!: boolean;

/**
* The author(s) for the audiobook.
*/
public authors!: APIAudiobook['authors'];

/**
*A list of the languages used in the audiobook, identified by their ISO 639 code.
*/
public languages!: string[];

/**
* The media type of the audiobook.
*/
public media_type!: string;

/**
* The cover art for the audiobook in various sizes, widest first.
*/
public images!: APIAudiobook['images'];

/**
* External URLs for this audiobook.
*/
public external_urls!: APIAudiobook['external_urls'];

/**
* The narrator(s) for the audiobook.
*/
public narrators!: APIAudiobook['narrators'];

/**
* The copyright statements of the audiobook.
*/
public copyrights!: APIAudiobook['copyrights'];

/**
* The publisher of the audiobook.
*/
public publisher!: string;

/**
* The object type.
*/
public type!: 'audiobook';

/**
* The object type.
*/
public uri!: string;

/**
* The number of chapters in this audiobook.
*/
public total_chapters!: number;

/**
* The chapters of the audiobook.
*/
public chapters!: APIAudiobook['chapters'];

public constructor(client: Client, data: APIAudiobook) {
super(client);
Object.assign(this, data);
}
}
1 change: 1 addition & 0 deletions src/lib/classes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './Base';

export * from './Album';
export * from './Artist';
export * from './Audiobook';
export * from './Category';
export * from './Episode';
export * from './Playlist';
Expand Down
1 change: 1 addition & 0 deletions src/lib/rest/APIRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class APIRequest {
if (!this.client.options.accessToken) throw new SpotifyTSError('CLIENT_MISSING_ACCESS_TOKEN');

const options = this.makeOptions();
console.log(options);
return phin({ ...options }) as Promise<IJSONResponse<unknown>>;
}

Expand Down
39 changes: 39 additions & 0 deletions src/lib/types/classes/Audiobook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { APIImage } from '.';

export interface APIAudiobook {
id: string;
name: string;
description: string;
html_description: string;
href: string;
explicit: boolean;
authors: {
name: string;
}[];
languages: string[];
media_type: string;
images: APIImage[];
external_urls: {
spotify: string;
};
narrators: {
name: string;
};
copyrights: {
text: string;
type: 'C' | 'P';
}[];
publisher: string;
type: 'audiobook';
uri: string;
total_chapters: number;
chapters: {
href: string;
items: Record<string, unknown>;
limit: number;
next: string;
offset: number;
previous: string;
total: number;
};
}
2 changes: 2 additions & 0 deletions src/lib/types/classes/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './Album';
export * from './Artist';
export * from './Audiobook';
export * from './Audiobook';
export * from './Category';
export * from './Episode';
export * from './Image';
Expand Down
45 changes: 45 additions & 0 deletions src/managers/AudiobooksManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { BaseManager } from '.';
import { SpotifyTSError, Audiobook, RequestData, type Client, type APIAudiobook } from '../lib';

export class AudiobooksManager extends BaseManager {
public constructor(client: Client) {
super(client, 'audiobooks');
}

/**
* Get Spotify catalog information for a single audiobook.
* **Note: Audiobooks are only available for the US market.**
* @param {string} id: The Spotify ID for the audiobook.
* @param {string} [country]: The ISO 3166-1 alpha-2 country code. TIf specified, only the content available in this country will be returned, if not, the country of the current user will be used.
* @returns {Promise<Audiobook>} The audiobook.
**/
public async fetch(id: string, country?: string): Promise<Audiobook> {
if (!id) throw new SpotifyTSError('MANAGER_MISSING_ARGUMENT', 'AudiobooksManager', 'fetch', 'id');
if (typeof id !== 'string') throw new SpotifyTSError('MANAGER_ARGUMENT_INVALID_TYPE', 'AudiobooksManager', 'fetch', 'id', 'string');

const data = await super.get<APIAudiobook>(undefined, id, country ? new RequestData({ query: { market: country } }) : undefined);
return new Audiobook(this.client, data);
}

/**
* Fetch several audiobooks by their Spotify IDs.
* @param {Array<string>} ids: The Spotify IDs of the audiobooks.
* @param {string} [country]: An ISO 3166-1 alpha-2 country code, if specified, returns only the content available in this country. If this argument is not specified, the country code of the user account will be used.
* @returns {Promise<Audiobook[]>} Array of Episode objects.
*/
public async fetchSeveral(ids: string[], country?: string): Promise<Audiobook[]> {
if (!ids || !ids?.length) throw new SpotifyTSError('MANAGER_MISSING_ARGUMENT', 'AudiobooksManager', 'fetch', 'ids');
if (!Array.isArray(ids)) throw new SpotifyTSError('MANAGER_ARGUMENT_INVALID_TYPE', 'AudiobooksManager', 'fetch', 'ids', 'Array<string>');
if (country && typeof country !== 'string')
throw new SpotifyTSError('MANAGER_MISSING_ARGUMENT', 'AudiobooksManager', 'fetch', 'country', 'string');

const query = country
? new RequestData({ query: { ids: ids.join(','), market: country } })
: new RequestData({ query: { ids: ids.join(',') } });
const { audiobooks } = await super.get<{ audiobooks: APIAudiobook[] }>(undefined, undefined, query);
const parsed = [];
for (const audiobook of audiobooks) parsed.push(new Audiobook(this.client, audiobook));

return parsed;
}
}
1 change: 1 addition & 0 deletions src/managers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './BaseManager';

export * from './AlbumsManager';
export * from './ArtistsManager';
export * from './AudiobooksManager';
export * from './CategoriesManager';
export * from './EpisodesManager';
export * from './GenresManager';
Expand Down
3 changes: 1 addition & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4453,7 +4453,6 @@ __metadata:
prettier: ^2.7.1
pretty-quick: ^3.1.3
standard-version: ^9.5.0
ts-node: ^10.9.1
tslib: ^2.4.0
typedoc: ^0.23.15
typescript: ^4.8.4
Expand Down Expand Up @@ -4728,7 +4727,7 @@ __metadata:
languageName: node
linkType: hard

"ts-node@npm:^10.8.1, ts-node@npm:^10.9.1":
"ts-node@npm:^10.8.1":
version: 10.9.1
resolution: "ts-node@npm:10.9.1"
dependencies:
Expand Down

0 comments on commit 2f8022e

Please sign in to comment.