musicbrainz-api
Version:
MusicBrainz API client for reading and submitting metadata
69 lines • 2.67 kB
JavaScript
import { StatusCodes as HttpStatus } from 'http-status-codes';
export { XmlMetadata } from './xml/xml-metadata.js';
export { XmlIsrc } from './xml/xml-isrc.js';
export { XmlIsrcList } from './xml/xml-isrc-list.js';
export { XmlRecording } from './xml/xml-recording.js';
import { HttpClientNode } from "./http-client-node.js";
import { MusicBrainzApi as MusicBrainzApiDefault } from "./musicbrainz-api.js";
export * from './musicbrainz.types.js';
export * from './http-client.js';
/*
* https://musicbrainz.org/doc/Development/XML_Web_Service/Version_2#Subqueries
*/
export class MusicBrainzApi extends MusicBrainzApiDefault {
initHttpClient() {
return new HttpClientNode({
baseUrl: this.config.baseUrl,
timeout: 500,
userAgent: `${this.config.appName}/${this.config.appVersion} ( ${this.config.appContactInfo} )`
});
}
async login() {
if (!this.config.botAccount?.username)
throw new Error('bot username should be set');
if (!this.config.botAccount?.password)
throw new Error('bot password should be set');
if (this.session?.loggedIn) {
const cookies = await this.httpClient.getCookies();
return cookies.indexOf('musicbrainz_server_session') !== -1;
}
this.session = await this.getSession();
const redirectUri = '/success';
const formData = {
username: this.config.botAccount.username,
password: this.config.botAccount.password,
csrf_session_key: this.session.csrf.sessionKey,
csrf_token: this.session.csrf.token,
remember_me: '1'
};
const response = await this.httpClient.postForm('login', formData, {
query: {
returnto: redirectUri
},
followRedirects: false
});
const success = response.status === HttpStatus.MOVED_TEMPORARILY && response.headers.get('location') === redirectUri;
if (success) {
this.session.loggedIn = true;
}
return success;
}
/**
* Logout
*/
async logout() {
const redirectUri = '/success';
const response = await this.httpClient.post('logout', {
followRedirects: false,
query: {
returnto: redirectUri
}
});
const success = response.status === HttpStatus.MOVED_TEMPORARILY && response.headers.get('location') === redirectUri;
if (success && this.session) {
this.session.loggedIn = true;
}
return success;
}
}
//# sourceMappingURL=musicbrainz-api-node.js.map