Skip to content

Commit

Permalink
fix!: standardize update interface
Browse files Browse the repository at this point in the history
  • Loading branch information
JadsonLucena committed Jun 20, 2024
1 parent 5af4fdb commit 9b9310f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 51 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
35 changes: 12 additions & 23 deletions src/MediaTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -47,32 +43,27 @@ declare module '@jadsonlucena/mediatypes' {
* @method
* @param {boolean} [force=false] - Force update even if no version changes
*
* @fires MediaTypes#update
*
* @return {Promise<null | Object.<string, MIMEType[]>>} List of all extensions with their media types
* @return {Promise<Object.<string, MIMEType[]>>} List of all extensions with their media types
*/
update(force?: boolean): Promise<null | Record<string, MIMEType[]>>
update(force?: boolean): Promise<Record<string, MIMEType[]>>

/**
* @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[]

/**
* @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}
*/
Expand All @@ -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}
*/
Expand Down
30 changes: 11 additions & 19 deletions src/MediaTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -131,9 +131,7 @@ class MediaTypes {
* @method
* @param {boolean} [force=false] - Force update even if no version changes
*
* @fires MediaTypes#update
*
* @return {Promise<null | Object.<string, MIMEType[]>>} List of all extensions with their media types
* @return {Promise<Object.<string, MIMEType[]>>} List of all extensions with their media types
*/
update (force = false) {
return Promise.allSettled([
Expand Down Expand Up @@ -222,7 +220,7 @@ class MediaTypes {
}

if (!Object.keys(list).length) {
return null
return {}
}

fs.writeFileSync(join(__dirname, 'DB.json'), JSON.stringify({
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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}
*/
Expand Down
16 changes: 8 additions & 8 deletions test/MediaTypes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -455,7 +455,7 @@ describe('Fetch', () => {

const mediaType = new MediaTypes(-1)

return expect(mediaType.update()).resolves.toBeNull()
return expect(mediaType.update()).resolves.toStrictEqual({})
})
})

Expand Down

0 comments on commit 9b9310f

Please sign in to comment.