Source: server/src/documentsManager/documentsManager.js

/**
 * @module server/documentsManager
 */

let documents = {}

/**
 * registers a document's creation function along its identifier
 * @param {string} documentName document identifier
 * @param {func} documentCreatorAsync creation function which gets authorization header and returns the data and pages of the document
 */
module.exports.registerDocument = function (documentName, documentCreatorAsync) {
  if (!documentName) {
    throw new Error('documentName argument is missing')
  }
  if (!documentCreatorAsync) {
    throw new Error('documentCreatorAsync argument is missing')
  }

  documents[documentName] = documentCreatorAsync
}

/**
 * gets a document initialized with its required data
 * @param {string} authorizationHeader HTTP header variable `authorization` from the current request
 * @param {string} documentName document identifier which used when registered with {@link registerDocument} function
 * @returns {object} document initialized with its required data
 */
module.exports.getDocumentAsync = function (authorizationHeader, documentName) {
  let documentCreatorAsync = documents[documentName]
  if (!documentCreatorAsync) {
    throw new Error(`required document ${documentName} is not registered`)
  }

  return documentCreatorAsync(authorizationHeader)
}