Skip to content

Commit

Permalink
fix: accept Error, string, number in createFrontLogProxyTransmitter (#65
Browse files Browse the repository at this point in the history
)
  • Loading branch information
oljekechoro authored Mar 23, 2023
1 parent 67b6acf commit a84638a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const createFrontLogProxyTransmitter = ({
url,
}: {
appName: string
url: string
url?: string
}) => {
const appContextId = nanoid()
const clientId = getClientId(appName)
Expand All @@ -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<LogLevel, (data: IClientEventDtoFlp) => Promise<IPipeOutput>>)
}, {} as Record<LogLevel, (data: IClientEventDtoFlp | Error | string | number) => Promise<IPipeOutput>>)
}

export {
Expand Down
78 changes: 78 additions & 0 deletions src/test/ts/createFrontLogProxyTransmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit a84638a

Please sign in to comment.