diff --git a/README.md b/README.md index 0cec52c..0e058b7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # MediaTypes [![CodeQL](https://github.com/JadsonLucena/MediaTypes.js/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/JadsonLucena/MediaTypes.js/actions/workflows/github-code-scanning/codeql) [![Test](https://github.com/JadsonLucena/MediaTypes.js/actions/workflows/test.yml/badge.svg)](https://github.com/JadsonLucena/MediaTypes.js/actions/workflows/test.yml) -[![Coverage Status](https://coveralls.io/repos/github/JadsonLucena/MediaTypes.js/badge.svg)](https://coveralls.io/github/JadsonLucena/MediaTypes.js) +[![Coverage](https://coveralls.io/repos/github/JadsonLucena/MediaTypes.js/badge.svg)](https://coveralls.io/github/JadsonLucena/MediaTypes.js) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white)](https://conventionalcommits.org) diff --git a/src/MediaTypes.d.ts b/src/MediaTypes.d.ts index 5757c9e..0318c7f 100644 --- a/src/MediaTypes.d.ts +++ b/src/MediaTypes.d.ts @@ -9,29 +9,25 @@ type Versions = { declare module '@jadsonlucena/mediatypes' { /** - * @classdesc This is a comprehensive compilation of media types that is periodically updated through the following projects: Apache, NGINX and Debian + * @classdesc This is a comprehensive compilation of media types that may be periodically updated + * + * @emits MediaTypes#update + * @emits MediaTypes#error */ export default class MediaTypes { /** * Create a MediaType class * @param {number} [updateInterval=86400000] - Periodic database update in milliseconds. if less than zero, will be disabled - * - * @fires MediaTypes#update - * @fires MediaTypes#error + * @see https://developer.mozilla.org/en-US/docs/Web/API/setInterval#delay * * @throws {TypeError} Invalid updateInterval - * - * @see https://developer.mozilla.org/en-US/docs/Web/API/setInterval#delay */ constructor (updateInterval?: number) /** * @default '86400000' * - * @fires MediaTypes#update - * @fires MediaTypes#error - * * @throws {TypeError} Invalid updateInterval * * @see https://developer.mozilla.org/en-US/docs/Web/API/setInterval#delay @@ -47,21 +43,18 @@ declare module '@jadsonlucena/mediatypes' { * @method * @param {boolean} [force=false] - Force update even if no version changes * - * @fires MediaTypes#update - * - * @return {Promise>} List of all extensions with their media types + * @return {Promise>} List of all extensions with their media types */ - update(force?: boolean): Promise> + update(force?: boolean): Promise> /** * @param {string} path - File path + * @see https://nodejs.org/api/path.html#pathparsepath * * @throws {TypeError} Invalid path * @throws {SyntaxError} Invalid extension * * @return {MIMEType[]} - * - * @see https://nodejs.org/api/path.html#pathparsepath */ get(path: string): MIMEType[] @@ -69,10 +62,8 @@ declare module '@jadsonlucena/mediatypes' { * @param {string} extension - File extension * @param {string} mediaType - {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#structure_of_a_mime_type IANA media types} * - * @throws {TypeError} Invalid extension - * @throws {SyntaxError} Invalid extension - * @throws {TypeError} Invalid mediaType - * @throws {SyntaxError} Invalid mediaType + * @throws {TypeError|SyntaxError} Invalid extension + * @throws {TypeError|SyntaxError} Invalid mediaType * * @return {boolean} */ @@ -82,10 +73,8 @@ declare module '@jadsonlucena/mediatypes' { * @param {string} extension - File extension * @param {string} mediaType - {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#structure_of_a_mime_type IANA media types} * - * @throws {TypeError} Invalid extension - * @throws {SyntaxError} Invalid extension - * @throws {TypeError} Invalid mediaType - * @throws {SyntaxError} Invalid mediaType + * @throws {TypeError|SyntaxError} Invalid extension + * @throws {TypeError|SyntaxError} Invalid mediaType * * @return {boolean} */ diff --git a/src/MediaTypes.js b/src/MediaTypes.js index 84a827c..1d297b7 100644 --- a/src/MediaTypes.js +++ b/src/MediaTypes.js @@ -13,6 +13,9 @@ function removeDuplicates (array) { * @class * @classdesc This is a comprehensive compilation of media types that may be periodically updated * + * @emits MediaTypes#update + * @emits MediaTypes#error + * * @typedef {Object} Versions * @property {string} Versions.apache * @property {string} Versions.debian @@ -34,9 +37,6 @@ class MediaTypes { * @param {number} [updateInterval=86400000] - Periodic database update in milliseconds. if less than zero, will be disabled * @see https://developer.mozilla.org/en-US/docs/Web/API/setInterval#delay * - * @fires MediaTypes#update - * @fires MediaTypes#error - * * @throws {TypeError} Invalid updateInterval */ constructor (updateInterval = 86400000) { @@ -131,9 +131,7 @@ class MediaTypes { * @method * @param {boolean} [force=false] - Force update even if no version changes * - * @fires MediaTypes#update - * - * @return {Promise>} List of all extensions with their media types + * @return {Promise>} List of all extensions with their media types */ update (force = false) { return Promise.allSettled([ @@ -222,7 +220,7 @@ class MediaTypes { } if (!Object.keys(list).length) { - return null + return {} } fs.writeFileSync(join(__dirname, 'DB.json'), JSON.stringify({ @@ -252,12 +250,10 @@ class MediaTypes { /** * @type {number} [updateInterval=86400000] - * @see https://developer.mozilla.org/en-US/docs/Web/API/setInterval#delay - * - * @fires MediaTypes#update - * @fires MediaTypes#error * * @throws {TypeError} Invalid updateInterval + * + * @see https://developer.mozilla.org/en-US/docs/Web/API/setInterval#delay */ set updateInterval (updateInterval = 86400000) { if ( @@ -340,10 +336,8 @@ class MediaTypes { * @param {string} extension - File extension * @param {string} mediaType - {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#structure_of_a_mime_type IANA media types} * - * @throws {TypeError} Invalid extension - * @throws {SyntaxError} Invalid extension - * @throws {TypeError} Invalid mediaType - * @throws {SyntaxError} Invalid mediaType + * @throws {TypeError|SyntaxError} Invalid extension + * @throws {TypeError|SyntaxError} Invalid mediaType * * @return {boolean} */ @@ -382,10 +376,8 @@ class MediaTypes { * @param {string} extension - File extension * @param {string} mediaType - {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#structure_of_a_mime_type IANA media types} * - * @throws {TypeError} Invalid extension - * @throws {SyntaxError} Invalid extension - * @throws {TypeError} Invalid mediaType - * @throws {SyntaxError} Invalid mediaType + * @throws {TypeError|SyntaxError} Invalid extension + * @throws {TypeError|SyntaxError} Invalid mediaType * * @return {boolean} */ diff --git a/test/MediaTypes.spec.js b/test/MediaTypes.spec.js index a3ec26e..4536caa 100644 --- a/test/MediaTypes.spec.js +++ b/test/MediaTypes.spec.js @@ -335,7 +335,7 @@ describe('Methods', () => { expect(mediaType.delete(extension, contentType)).toBeTruthy() - await expect(mediaType.update()).resolves.toBeNull() + await expect(mediaType.update()).resolves.toStrictEqual({}) await expect(mediaType.update(true).then(res => { return Object.keys(res) @@ -361,7 +361,7 @@ describe('Fetch', () => { const mediaType = new MediaTypes(-1) - return expect(mediaType.update()).resolves.toBeNull() + return expect(mediaType.update()).resolves.toStrictEqual({}) }) test('Given that one wants to try to update the list of media types at some point and there was an error processing the media types or the file extension', () => { @@ -381,7 +381,7 @@ describe('Fetch', () => { const mediaType = new MediaTypes(-1) - return expect(mediaType.update()).resolves.toBeNull() + return expect(mediaType.update()).resolves.toStrictEqual({}) }) test('Given that the update method was called and the request returns an invalid data type', () => { @@ -398,7 +398,7 @@ describe('Fetch', () => { const mediaType = new MediaTypes(-1) - return expect(mediaType.update()).resolves.toBeNull() + return expect(mediaType.update()).resolves.toStrictEqual({}) }) test('Given that the update method was called and the request does not have an etag header', () => { @@ -414,7 +414,7 @@ describe('Fetch', () => { const mediaType = new MediaTypes(-1) - return expect(mediaType.update()).resolves.toBeNull() + return expect(mediaType.update()).resolves.toStrictEqual({}) }) test('Given that the update method was called and the request returns an error', () => { @@ -431,7 +431,7 @@ describe('Fetch', () => { const mediaType = new MediaTypes(-1) - return expect(mediaType.update()).resolves.toBeNull() + return expect(mediaType.update()).resolves.toStrictEqual({}) }) test('Given that the update method was called and the fetch function throws an exception', () => { @@ -443,7 +443,7 @@ describe('Fetch', () => { const mediaType = new MediaTypes(-1) - return expect(mediaType.update()).resolves.toBeNull() + return expect(mediaType.update()).resolves.toStrictEqual({}) }) test('Given that the update method was called and the fetch function is rejected', () => { @@ -455,7 +455,7 @@ describe('Fetch', () => { const mediaType = new MediaTypes(-1) - return expect(mediaType.update()).resolves.toBeNull() + return expect(mediaType.update()).resolves.toStrictEqual({}) }) })