Skip to content

Commit

Permalink
Convert CommonJS module to ECMAScript Module
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Nov 11, 2023
1 parent 5db4829 commit d15be44
Show file tree
Hide file tree
Showing 13 changed files with 361 additions and 887 deletions.
6 changes: 6 additions & 0 deletions .c8rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"reporter": [
"lcov",
"text"
]
}
2 changes: 1 addition & 1 deletion .github/workflows/nodejs-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 16.x, 18.x, 20.x]
node-version: [14.x, 16.x, 18.x, 20.x]

steps:

Expand Down
7 changes: 7 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extension": ["ts", "tsx"],
"watch-files": ["lib/**/*.ts", "test/**/*.ts"],
"spec": ["test/test-*.ts"],
"loader": ["ts-node/esm"],
"extensions": ["ts", "tsx"]
}
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,20 @@ If you plan to use this module for submitting metadata, please ensure you comply

## Example

Import the module
JavaScript example, how to import 'musicbrainz-api:
```js
const MusicBrainzApi = require('musicbrainz-api').MusicBrainzApi;

const mbApi = new MusicBrainzApi({
appName: 'my-app',
appVersion: '0.1.0',
appContactInfo: 'user@mail.org'
});
```

In TypeScript it would look like this:
Example, how to import 'musicbrainz-api:
```js
import {MusicBrainzApi} from 'musicbrainz-api';

const mbApi = new MusicBrainzApi({
appName: 'my-app',
appVersion: '0.1.0',
appContactInfo: 'user@mail.org' // Or URL to application home page
appContactInfo: 'user@mail.org'
});
```

The following configuration settings can be passed
```js
import {MusicBrainzApi} from '../src/musicbrainz-api';
import {MusicBrainzApi} from 'musicbrainz-api';

const config = {
// MusicBrainz bot account username & password (optional)
Expand Down
38 changes: 20 additions & 18 deletions lib/musicbrainz-api.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import * as assert from 'assert';

import { StatusCodes as HttpStatus, getReasonPhrase } from 'http-status-codes';
import * as Url from 'url';
import * as Debug from 'debug';
import Debug from 'debug';

export { XmlMetadata } from './xml/xml-metadata';
export { XmlIsrc } from './xml/xml-isrc';
export { XmlIsrcList } from './xml/xml-isrc-list';
export { XmlRecording } from './xml/xml-recording';
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 { XmlMetadata } from './xml/xml-metadata';
import { DigestAuth } from './digest-auth';
import { XmlMetadata } from './xml/xml-metadata.js';
import { DigestAuth } from './digest-auth.js';

import { RateLimiter } from './rate-limiter';
import * as mb from './musicbrainz.types';
import { RateLimiter } from './rate-limiter.js';
import * as mb from './musicbrainz.types.js';

/* eslint-disable-next-line */
import got, {type Options, type ToughCookieJar} from 'got';

import {type Cookie, CookieJar} from 'tough-cookie';

export * from './musicbrainz.types';
export * from './musicbrainz.types.js';

import { promisify } from 'util';

Expand Down Expand Up @@ -239,9 +238,12 @@ export class MusicBrainzApi {
const cookieJar: CookieJar = new CookieJar();
this.getCookies = promisify(cookieJar.getCookies.bind(cookieJar));

// @ts-ignore
this.options = {
prefixUrl: this.config.baseUrl,
timeout: 20 * 1000,
prefixUrl: this.config.baseUrl as string,
timeout: {
read: 20 * 1000
},
headers: {
'User-Agent': `${this.config.appName}/${this.config.appVersion} ( ${this.config.appContactInfo} )`
},
Expand Down Expand Up @@ -566,7 +568,7 @@ export class MusicBrainzApi {
if (response.statusCode === HttpStatus.UNAUTHORIZED) {
// Respond to digest challenge
const auth = new DigestAuth(this.config.botAccount as {username: string, password: string});
const relPath = Url.parse(response.requestUrl).path; // Ensure path is relative
const relPath = response.requestUrl.pathname; // Ensure path is relative
digest = auth.digest(response.request.method, relPath as string, response.headers['www-authenticate']);
++n;
} else {
Expand Down Expand Up @@ -599,7 +601,7 @@ export class MusicBrainzApi {
remember_me: 1
};

const response: any = await got.post('login', {
const response = await got.post('login', {
...this.options,
followRedirect: false,
searchParams: {
Expand All @@ -620,7 +622,7 @@ export class MusicBrainzApi {
public async logout(): Promise<boolean> {
const redirectUri = '/success';

const response: any = await got.get('logout', {
const response = await got.get('logout', {
...this.options,
followRedirect: false,
searchParams: {
Expand Down Expand Up @@ -652,7 +654,7 @@ export class MusicBrainzApi {
formData.password = this.config.botAccount.password;
formData.remember_me = 1;

const response: any = await got.post(`${entity}/${mbid}/edit`, {
const response = await got.post(`${entity}/${mbid}/edit`, {
...this.options,
form: formData,
followRedirect: false
Expand Down Expand Up @@ -780,7 +782,7 @@ export class MusicBrainzApi {

private async getSession(): Promise<ISessionInformation> {

const response: any = await got.get('login', {
const response = await got.get('login', {
...this.options,
followRedirect: false, // Disable redirects
responseType: 'text'
Expand Down
2 changes: 1 addition & 1 deletion lib/musicbrainz.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import DateTimeFormat = Intl.DateTimeFormat;
import { IFormData } from './musicbrainz-api';
import type { IFormData } from './musicbrainz-api.js';

export interface IPeriod {
'begin': string;
Expand Down
2 changes: 1 addition & 1 deletion lib/xml/xml-isrc-list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { XmlIsrc } from './xml-isrc';
import { XmlIsrc } from './xml-isrc.js';

export class XmlIsrcList {

Expand Down
4 changes: 2 additions & 2 deletions lib/xml/xml-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// https://musicbrainz.org/doc/Development/XML_Web_Service/Version_2#ISRC_submission

import * as jsontoxml from 'jsontoxml';
import { XmlRecording } from './xml-recording';
import jsontoxml from 'jsontoxml';
import { XmlRecording } from './xml-recording.js';

const ns_metadata = 'http://musicbrainz.org/ns/mmd-2.0#';

Expand Down
2 changes: 1 addition & 1 deletion lib/xml/xml-recording.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { XmlIsrcList } from './xml-isrc-list';
import { XmlIsrcList } from './xml-isrc-list.js';

export class XmlRecording {

Expand Down
27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
"name": "musicbrainz-api",
"version": "0.11.0",
"description": "MusicBrainz API client for reading and submitting metadata",
"main": "lib/musicbrainz-api",
"types": "lib/musicbrainz-api",
"exports": "./lib/musicbrainz-api.js",
"types": "lib/musicbrainz-api.d.ts",
"files": [
"lib/**/*.js",
"lib/**/*.d.ts"
],
"type": "module",
"author": {
"name": "Borewit",
"url": "https://github.com/Borewit"
Expand All @@ -25,7 +30,7 @@
"license": "MIT",
"private": false,
"engines": {
"node": "*"
"node": "^14.13.1 || >=16.0.0"
},
"repository": {
"type": "git",
Expand All @@ -39,8 +44,8 @@
"@types/request-promise-native": "^1.0.17",
"@types/uuid": "^9.0.0",
"caseless": "^0.12.0",
"debug": "^4.1.1",
"got": "^11.8.5",
"debug": "^4.3.4",
"got": "^13.0.0",
"http-status-codes": "^2.1.4",
"json-stringify-safe": "^5.0.1",
"jsontoxml": "^1.0.1",
Expand All @@ -55,6 +60,7 @@
"@types/node": "^20.8.10",
"@typescript-eslint/eslint-plugin": "^5.13.0",
"@typescript-eslint/parser": "^5.13.0",
"c8": "^8.0.1",
"chai": "^4.2.0",
"del-cli": "^5.0.0",
"eslint": "^8.10.0",
Expand All @@ -64,18 +70,13 @@
"eslint-plugin-jsdoc": "^46.8.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-unicorn": "^46.0.0",
"mocha": "^9.0.1",
"nyc": "^15.0.0",
"mocha": "^10.1.0",
"remark-cli": "^11.0.0",
"remark-preset-lint-recommended": "^6.1.2",
"ts-node": "^10.0.0",
"tslint": "^6.1.1",
"typescript": "^5.0.2"
},
"files": [
"lib/**/*.js",
"lib/**/*.d.ts"
],
"scripts": {
"clean": "del-cli lib/**/*.js lib/**/*.js.map lib/**/*.d.ts test/**/*.js test/**/*.js.map",
"compile-lib": "tsc -p lib",
Expand All @@ -84,10 +85,10 @@
"eslint": "eslint lib/**/*.ts --ignore-pattern lib/**/*.d.ts test/**/*.ts",
"lint-md": "remark -u preset-lint-recommended .",
"lint": "npm run lint-md && npm run eslint",
"test": "mocha --require ts-node/register --require source-map-support/register --full-trace test/test-*.ts",
"test": "mocha",
"build": "npm run clean && npm run compile",
"start": "npm-run-all compile lint cover-test",
"test-coverage": "nyc npm run test",
"test-coverage": "c8 npm run test",
"send-codacy": "nyc report --reporter=text-lcov | codacy-coverage"
},
"nyc": {
Expand Down

0 comments on commit d15be44

Please sign in to comment.