Skip to content

Commit

Permalink
feat: enable endpointRegistrationLogging option for programmatic use
Browse files Browse the repository at this point in the history
  • Loading branch information
anoack93 committed Sep 18, 2023
1 parent 11bed4b commit 175a30c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const mainApp = express();

const options = {
responseDelay_ms: 100,
// servemocks prints every endpoint it registers to the console
// this might bloat the console log and thus you can change this setting here
endpointRegistrationLogging: 'compact', // default is 'verbose', use 'disabled' to not show any of those logs
// enable javascript code to be executed from a mock file with
// .mjs file extension
// eval can be used as alternative strategy if dynamicImport does not work
Expand Down
4 changes: 2 additions & 2 deletions examples/mock-api/v3/person.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export default function ({ query }) {
// create a return object
return {
meta: {
title: 'Person object',
description: 'Some random persons',
title: 'Person List',
description: 'Some random persons, use the "amount" query parameter to change number of persons in response',
},
items: persons,
}
Expand Down
9 changes: 9 additions & 0 deletions src/serve-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ScriptEvaluationService } from './service/script-evaluation.service.js'
* @typedef {Object} ServemocksOptions
* @property {number} responseDelay_ms - default delay which will be added before sending a response
* @property {'eval' | 'dynamicImport' | 'disabled'} dynamicMockResponsesMode
* @property {'verbose' | 'compact' | 'disabled'} endpointRegistrationLogging
*/

/**
Expand All @@ -19,6 +20,7 @@ import { ScriptEvaluationService } from './service/script-evaluation.service.js'
export const defaultServeMocksOptions = {
responseDelay_ms: 100,
dynamicMockResponsesMode: 'eval',
endpointRegistrationLogging: 'verbose'
}

/**
Expand All @@ -27,6 +29,9 @@ export const defaultServeMocksOptions = {
* @return {Express}
*/
export function createServeMocksExpressApp (mockDirectory, options = {}) {
/**
* @type {ServemocksOptions}
**/
const effectiveOptions = {
...defaultServeMocksOptions,
...options
Expand Down Expand Up @@ -71,6 +76,10 @@ export function createServeMocksExpressApp (mockDirectory, options = {}) {
const files = globSync(mockFilePattern)
files.forEach(fileName => endpointRegistrationService.registerEndpoint(fileName, fileType))
}
if (effectiveOptions.endpointRegistrationLogging === 'compact') {
logger.info('...')
logger.info('Total number of API endpoints registered: ' + endpointRegistrationService.numberOfRegisteredEndpoints)
}
return app
}

Expand Down
33 changes: 26 additions & 7 deletions src/service/endpoint-registration.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ajv = new Ajv()
// this is being used for directories which have the same name as a file like /test.jpg/medium
// you would name that file /test.jpg---medium.jpg
const SLASH_ALIAS = '---'
const maxNumberOfLogEntriesInCompactMode = 2

// eslint-disable-next-line require-jsdoc
export class EndpointRegistrationService {
Expand All @@ -25,6 +26,20 @@ export class EndpointRegistrationService {
this.scriptEvaluationService = scriptEvaluationService
this.mockFileRoot = mockFileRoot
this.options = options
this.numberOfRegisteredEndpoints = 0
this._isLoggingVerbose = options.endpointRegistrationLogging === 'verbose'
this._isLoggingCompact = options.endpointRegistrationLogging === 'compact'
}

/**
* @return {boolean}
* @private
*/
get _isEndpointLoggingEnabled () {
return (
this._isLoggingVerbose ||
(this._isLoggingCompact && this.numberOfRegisteredEndpoints < maxNumberOfLogEntriesInCompactMode)
)
}

/**
Expand Down Expand Up @@ -104,6 +119,7 @@ export class EndpointRegistrationService {
const statusCode = responseOptions.statusCode ? responseOptions.statusCode : 200
let response = endpointParams.response ? endpointParams.response : { success: true }
this.logger.logRequest(HttpMethod.POST, apiPath, req.body)
this.numberOfRegisteredEndpoints++

await sleep(responseDelay)

Expand Down Expand Up @@ -133,12 +149,15 @@ export class EndpointRegistrationService {
throw new Error('Unknown Http Method')
}

this.logger.info(
'%s %s \n ⇒ %s (%s)',
httpMethod.toUpperCase(),
apiPath,
fileName.replace(this.mockFileRoot, '$MOCK_DIR'),
fileType.contentType,
)
if (this._isEndpointLoggingEnabled) {
this.logger.info(
'%s %s \n ⇒ %s (%s)',
httpMethod.toUpperCase(),
apiPath,
fileName.replace(this.mockFileRoot, '$MOCK_DIR'),
fileType.contentType,
)
}
this.numberOfRegisteredEndpoints++
}
}

0 comments on commit 175a30c

Please sign in to comment.