diff --git a/src/main/ts/index.ts b/src/main/ts/index.ts index 0443808..32e7bc6 100644 --- a/src/main/ts/index.ts +++ b/src/main/ts/index.ts @@ -35,7 +35,7 @@ const createFrontLogProxyTransmitter = ({ url, }: { appName: string - url: string + url?: string }) => { const appContextId = nanoid() const clientId = getClientId(appName) @@ -57,18 +57,32 @@ const createFrontLogProxyTransmitter = ({ LogLevel.DEBUG, LogLevel.WARN, ].reduce((acc, level) => { - acc[level] = (data: IClientEventDtoFlp) => { - const { details, meta } = data + acc[level] = (data: IClientEventDtoFlp | Error | string | number) => { + if ( + typeof data === 'string' || + typeof data === 'number' || + typeof data === 'boolean' || + data === null || + data === undefined || + data instanceof Error + ) { + return transmitter.push(({ + message: data, + details: { appContextId, clientId }, + meta: { appName }, + level, + })) + } return transmitter.push({ ...data, level, - details: { ...details, appContextId, clientId }, - meta: { ...meta, appName }, + details: { ...data.details, appContextId, clientId }, + meta: { ...data.meta, appName }, }) } return acc - }, {} as Record Promise>) + }, {} as Record Promise>) } export { diff --git a/src/test/ts/createFrontLogProxyTransmitter.ts b/src/test/ts/createFrontLogProxyTransmitter.ts index ba32b2d..e8a823c 100644 --- a/src/test/ts/createFrontLogProxyTransmitter.ts +++ b/src/test/ts/createFrontLogProxyTransmitter.ts @@ -117,4 +117,82 @@ test('createFrontLogProxyTransmitter does not throw an error on several calls', await callAndCheck('baz') }) +test('createFrontLogProxyTransmitter accepts error as input', async () => { + const transmitter = createFrontLogProxyTransmitter({ + appName: 'testApp', + url: 'https://reqres.in/api/users/2', + }) + + const error = new Error('foo') + const [err, res] = await transmitter.error(error) + assert.equal(err, null) + assert.equal(res.message, error.message) + assert.equal(res.stacktrace, error.stack) + assert.ok(res.details.clientId) + assert.ok(res.details.appContextId) + assert.equal(res.meta, { appName: 'testApp' }) + assert.equal(res.level, 'error') +}) + +test('createFrontLogProxyTransmitter does not throw on invalid arg type', async () => { + const transmitter = createFrontLogProxyTransmitter({ + appName: 'testApp', + url: 'https://reqres.in/api/users/2', + }) + + await assert.not.throws(async () => { + // @ts-ignore + await transmitter.info(undefined) // eslint-disable-line unicorn/no-useless-undefined + }) + await assert.not.throws(async () => { + // @ts-ignore + await transmitter.info(null) + }) +}) + +test('createFrontLogProxyTransmitter accepts string', async () => { + const transmitter = createFrontLogProxyTransmitter({ + appName: 'testApp', + url: 'https://reqres.in/api/users/2', + }) + + const [err, res] = await transmitter.info('string') + assert.equal(err, null) + assert.equal(res.message, 'string') + assert.ok(res.details.clientId) + assert.ok(res.details.appContextId) + assert.equal(res.meta, { appName: 'testApp' }) + assert.equal(res.level, 'info') +}) + +test('createFrontLogProxyTransmitter accepts number', async () => { + const transmitter = createFrontLogProxyTransmitter({ + appName: 'testApp', + url: 'https://reqres.in/api/users/2', + }) + + const [err, res] = await transmitter.info(42) + assert.equal(err, null) + assert.equal(res.message, 42) + assert.ok(res.details.clientId) + assert.ok(res.details.appContextId) + assert.equal(res.meta, { appName: 'testApp' }) + assert.equal(res.level, 'info') +}) + +test('createFrontLogProxyTransmitter accepts boolean', async () => { + const transmitter = createFrontLogProxyTransmitter({ + appName: 'testApp', + url: 'https://reqres.in/api/users/2', + }) + + const [err, res] = await transmitter.info(true) + assert.equal(err, null) + assert.equal(res.message, 'true') + assert.ok(res.details.clientId) + assert.ok(res.details.appContextId) + assert.equal(res.meta, { appName: 'testApp' }) + assert.equal(res.level, 'info') +}) + test.run()