Skip to content

Commit

Permalink
WIP: Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
scriptex committed Oct 27, 2023
1 parent 5deb751 commit ab94f8d
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 74 deletions.
208 changes: 146 additions & 62 deletions src/album.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,144 @@
import { LastFM } from './base';
import { LastFMParam, LastFMApiRequest, LastFMRequestParams, LastFMUnknownFunction } from './api-request';
import {
LastFMParam,
LastFMApiRequest,
LastFMBooleanNumber,
LastFMRequestParams,
LastFMUnknownFunction,
LastFMBooleanNumberOrVoid
} from './api-request';

export interface LastFMAlbumParams {
readonly album: string;
readonly artist: string;
}
export type LastFMAlbumParams = Readonly<{
album: string;
artist: string;
}>;

export interface LastFMAlbumOptionalParams {
readonly mbid?: string;
readonly autocorrect?: 0 | 1;
}
export type LastFMAlbumOptionalParams = Readonly<{
mbid?: string;
autocorrect?: LastFMBooleanNumber;
}>;

export interface LastFMAlbumAddTagsParams extends LastFMRequestParams<LastFMParam>, LastFMAlbumParams {
readonly tags: string | string[];
}
export type LastFMAlbumAddTagsParams = LastFMRequestParams<LastFMParam> &
LastFMAlbumParams &
Readonly<{
tags: string | string[];
}>;

export interface LastFMAlbumGetInfoParams
extends LastFMRequestParams<0 | 1 | void>,
LastFMAlbumParams,
LastFMAlbumOptionalParams {
readonly lang?: string;
readonly username?: string;
}
export type LastFMAlbumGetInfoParams = LastFMRequestParams<LastFMBooleanNumberOrVoid> &
LastFMAlbumParams &
LastFMAlbumOptionalParams &
Readonly<{
lang?: string;
username?: string;
}>;

export interface LastFMAlbumGetTagsParams
extends LastFMRequestParams<0 | 1 | void>,
LastFMAlbumParams,
LastFMAlbumOptionalParams {
readonly user?: string;
}
export type LastFMAlbumGetTagsParams = LastFMRequestParams<LastFMBooleanNumberOrVoid> &
LastFMAlbumParams &
LastFMAlbumOptionalParams &
Readonly<{
user?: string;
}>;

export interface LastFMAlbumGetTopTagsParams
extends LastFMRequestParams<0 | 1 | void>,
LastFMAlbumParams,
LastFMAlbumOptionalParams {}
export type LastFMAlbumGetTopTagsParams = LastFMRequestParams<LastFMBooleanNumberOrVoid> &
LastFMAlbumParams &
LastFMAlbumOptionalParams;

export interface LastFMAlbumRemoveLastFMTagParams extends LastFMRequestParams<LastFMParam>, LastFMAlbumParams {
readonly tag: string;
}
export type LastFMAlbumRemoveLastFMTagParams = LastFMRequestParams<LastFMParam> &
LastFMAlbumParams &
Readonly<{
tag: string;
}>;

export interface LastFMAlbumSearchParams extends LastFMRequestParams<number | void> {
readonly page?: number;
readonly album: string;
readonly limit?: number;
}
export type LastFMAlbumSearchParams = LastFMRequestParams<number | void> &
Readonly<{
page?: number;
album: string;
limit?: number;
}>;

export class LastFMAlbum extends LastFM {
constructor(apiKey: string, secret?: string, sessionKey?: string) {
super(apiKey, secret, sessionKey);
}
export type LastFMAlbumGetInfoResponse = Readonly<{
album: {
artist: string;
mbid: string;
tags: {
tag: Array<{
url: string;
name: string;
}>;
};
playcount: string;
image: Array<{
size: string;
'#text': string;
}>;
tracks: {
track: Array<{
streamable: { fulltrack: string; '#text': string };
duration: number;
url: string;
name: string;
'@attr': {
rank: number;
};
artist: {
url: string;
name: string;
mbid: string;
};
}>;
};
url: string;
name: string;
listeners: string;
wiki: {
published: string;
summary: string;
};
};
}>;

public addTags(
params: LastFMAlbumAddTagsParams,
callback: LastFMUnknownFunction
): Promise<LastFMApiRequest> | void {
return new LastFMApiRequest()
export type LastFMAlbumGetTagsResponse = Readonly<{
tags: {
tag: Array<{
name: string;
url: string;
}>;
};
}>;

export type LastFMAlbumSearchResponse = Readonly<{
results: {
'opensearch:Query': {
'#text': string;
role: string;
searchTerms: string;
startPage: string;
};
'opensearch:totalResults': string;
'opensearch:startIndex': string;
'opensearch:itemsPerPage': string;
albummatches: {
album: Array<{
name: string;
artist: string;
url: string;
image: Array<{
'#text': string;
size: string;
}>;
streamable: LastFMBooleanNumber;
mbid: string;
}>;
};
'@attr': {
for: string;
};
};
}>;

export class LastFMAlbum extends LastFM {
public addTags(params: LastFMAlbumAddTagsParams, callback?: LastFMUnknownFunction): Promise<void> {
return new LastFMApiRequest<void>()
.set(params)
.set({
api_key: this.apiKey,
Expand All @@ -67,9 +151,9 @@ export class LastFMAlbum extends LastFM {

public getInfo(
params: LastFMAlbumGetInfoParams,
callback: LastFMUnknownFunction
): Promise<LastFMApiRequest> | void {
return new LastFMApiRequest()
callback?: LastFMUnknownFunction
): Promise<LastFMAlbumGetInfoResponse> {
return new LastFMApiRequest<LastFMAlbumGetInfoResponse>()
.set(params)
.set({
api_key: this.apiKey,
Expand All @@ -80,9 +164,9 @@ export class LastFMAlbum extends LastFM {

public getTags(
params: LastFMAlbumGetTagsParams,
callback: LastFMUnknownFunction
): Promise<LastFMApiRequest> | void {
return new LastFMApiRequest()
callback?: LastFMUnknownFunction
): Promise<LastFMAlbumGetTagsResponse> {
return new LastFMApiRequest<LastFMAlbumGetTagsResponse>()
.set(params)
.set({
api_key: this.apiKey,
Expand All @@ -93,9 +177,9 @@ export class LastFMAlbum extends LastFM {

public getTopTags(
params: LastFMAlbumGetTopTagsParams,
callback: LastFMUnknownFunction
): Promise<LastFMApiRequest> | void {
return new LastFMApiRequest()
callback?: LastFMUnknownFunction
): Promise<LastFMAlbumGetTagsResponse> {
return new LastFMApiRequest<LastFMAlbumGetTagsResponse>()
.set(params)
.set({
api_key: this.apiKey,
Expand All @@ -104,11 +188,8 @@ export class LastFMAlbum extends LastFM {
.send(callback);
}

public removeTag(
params: LastFMAlbumRemoveLastFMTagParams,
callback: LastFMUnknownFunction
): Promise<LastFMApiRequest> | void {
return new LastFMApiRequest()
public removeTag(params: LastFMAlbumRemoveLastFMTagParams, callback?: LastFMUnknownFunction): Promise<void> {
return new LastFMApiRequest<void>()
.set(params)
.set({
api_key: this.apiKey,
Expand All @@ -119,8 +200,11 @@ export class LastFMAlbum extends LastFM {
.send('POST', callback);
}

public search(params: LastFMAlbumSearchParams, callback: LastFMUnknownFunction): Promise<LastFMApiRequest> | void {
return new LastFMApiRequest()
public search(
params: LastFMAlbumSearchParams,
callback?: LastFMUnknownFunction
): Promise<LastFMAlbumSearchResponse> {
return new LastFMApiRequest<LastFMAlbumSearchResponse>()
.set(params)
.set({
api_key: this.apiKey,
Expand Down
23 changes: 11 additions & 12 deletions src/api-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ import { createHash } from 'crypto';
import { parse, stringify } from 'querystring';
import { request, RequestOptions } from 'https';

export type LastFMUnknownFunction = (...args: unknown[]) => unknown;
export type LastFMParam = string | string[];
export type LastFMParams<T> = Record<string, LastFMParam | T>;
export type LastFMVoidOrNumber = void | number;
export type LastFMBooleanNumber = 0 | 1;
export type LastFMUnknownFunction = (...args: unknown[]) => unknown;
export type LastFMRequestParams<T> = Record<string, LastFMParam | T>;
export type LastFMBooleanNumberOrVoid = LastFMBooleanNumber | void;

export class LastFMApiRequest {
export class LastFMApiRequest<T> {
private params: Map<string, any> = new Map();

constructor() {
this.params.set('format', 'json');
}

public set<T>(params: LastFMParams<void | T>): LastFMApiRequest {
public set<P>(params: LastFMParams<void | P>): this {
Object.entries(params).forEach(([key, value]) => {
if (typeof value !== 'undefined') {
this.params.set(key, value);
Expand All @@ -24,7 +27,7 @@ export class LastFMApiRequest {
return this;
}

public sign(secret?: string): LastFMApiRequest {
public sign(secret?: string): this {
const paramsObj: LastFMParams<string> = this.setParams();
const paramsObjParsed = parse(stringify(paramsObj));

Expand Down Expand Up @@ -59,10 +62,7 @@ export class LastFMApiRequest {
return this;
}

public send(
method?: string | LastFMUnknownFunction,
callback?: LastFMUnknownFunction
): void | Promise<LastFMApiRequest> {
public send(method?: string | LastFMUnknownFunction, callback?: LastFMUnknownFunction): Promise<T> {
callback =
callback === undefined
? typeof method === 'function'
Expand All @@ -71,6 +71,7 @@ export class LastFMApiRequest {
: typeof callback === 'function'
? callback
: undefined;

method = typeof method === 'string' ? method : undefined;

if (this.params.has('callback')) {
Expand Down Expand Up @@ -129,12 +130,10 @@ export class LastFMApiRequest {
});

if (callback && typeof callback === 'function') {
LastFMapiRequest.then(
return LastFMapiRequest.then(
data => callback!(null, data),
err => callback!(err, null)
);

return undefined;
) as typeof LastFMapiRequest;
}

return LastFMapiRequest;
Expand Down

0 comments on commit ab94f8d

Please sign in to comment.