Skip to content

Commit

Permalink
feat: add support for adding and overriding logs
Browse files Browse the repository at this point in the history
  • Loading branch information
skoshx committed Jul 26, 2024
1 parent 37b1341 commit 00cf269
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 27 deletions.
15 changes: 8 additions & 7 deletions src/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ export const textFormatter: Formatter = (logParts) => {

const logMessageParts = [
capitalize(events.map(({ message }) => message).join(' and ')),
' because ',
events.length > 0 && ' because ',
explanations.map(({ message }) => message).join(' and '),
'.',
'\n',
'\n',
'Possible solutions:\n',
solutions.map(({ message }, solutionIndex) => `${solutionIndex + 1}) ${message}`)
explanations.length > 0 && '.',
solutions.length > 0 && explanations.length > 0 && '\n',
solutions.length > 0 && explanations.length > 0 && '\n',
solutions.length > 0 && 'Possible solutions:\n',
solutions.length > 0 &&
solutions.map(({ message }, solutionIndex) => `${solutionIndex + 1}) ${message}`).join('\n')
]
return logMessageParts.join('')
return logMessageParts.filter(Boolean).join('')
}
42 changes: 31 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,44 @@ export function createHumanLogs<LogParts extends LogObject[]>(
// ...[params]: CombineParams<LogParts, LogNames> extends never ? [] : [CombineParams<LogParts, LogNames>]
params: CombineParams<LogParts, LogNames>
) {
const matchingParts = logs.filter(({ name }) => logParts.includes(name))
let localLogParts = [...logParts]
let localParams: CombineParams<LogParts, LogNames> = {
...params
}

// Then template all of the parts
const templatedParts = matchingParts.map((part) => {
return {
...part,
params: params as LogObject['params'],
message: replaceTemplateParams(part.message, params as LogObject['params'])
}
})
const getTemplatedParts = () =>
logs
.filter(({ name }) => localLogParts.includes(name))
.map((part) => ({
...part,
// params: params as LogObject['params'],
params: localParams,
message: replaceTemplateParams(part.message, localParams)
}))

return {
get parts() {
return templatedParts
return getTemplatedParts()
},
add<AddLogNames extends Array<LogParts[number]['name']>>(
logParts: AddLogNames,
params: CombineParams<LogParts, AddLogNames>
) {
localLogParts.push(...logParts)
localParams = { ...localParams, ...params }
return this
},
override<AddLogNames extends Array<LogParts[number]['name']>>(
logParts: AddLogNames,
params: CombineParams<LogParts, AddLogNames>
) {
localLogParts = logParts
localParams = { ...localParams, ...params }
return this
},
toString() {
const formatter = options.formatter ?? textFormatter
return formatter(templatedParts)
return formatter(getTemplatedParts())
}
}
}
Expand Down
72 changes: 66 additions & 6 deletions test/logs.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { mockErrors } from './setup'
import { mockErrors } from './test-mocks'

describe('formatters', () => {
it('default formatter', () => {
Expand All @@ -22,10 +22,70 @@ describe('formatters', () => {
})
})

describe.skip('chaining', () => {
const error = mockErrors(['fetch_posts_failed', 'missing_params', 'provide_fallback'], {
paramName: 'image',
paramType: 'Image',
postId: 'abcd-123'
describe('chaining', () => {
it('add > no params', () => {
const error = mockErrors(['fetch_posts_failed', 'missing_params', 'provide_fallback'], {
paramName: 'image',
paramType: 'Image',
postId: 'abcd-123'
}).add(['check_statuspage'], {})

expect(error.toString()).toBe(
[
'Fetching posts failed because the Image `image` is missing for post with ID `abcd-123`, and no fallback was provided.',
'',
'Possible solutions:',
'1) add a fallback to your parameter definition like this: ',
'',
'url(`image`, { fallback: `https://useflytrap.com` })',
'2) you can check the status of our services on our status page'
].join('\n')
)
})

it('add > with params', () => {
const error = mockErrors(['fetch_posts_failed', 'missing_params', 'provide_fallback'], {
paramName: 'image',
paramType: 'Image',
postId: 'abcd-123'
}).add(['unsupported_blocktype'], {
blockType: 'Video'
})

expect(error.toString()).toBe(
[
'Fetching posts failed because the Image `image` is missing for post with ID `abcd-123`, and no fallback was provided and unsupported block type `Video` is included on this page.',
'',
'Possible solutions:',
'1) add a fallback to your parameter definition like this: ',
'',
'url(`image`, { fallback: `https://useflytrap.com` })'
].join('\n')
)
})

it('override > no params', () => {
const error = mockErrors(['fetch_posts_failed', 'missing_params', 'provide_fallback'], {
paramName: 'image',
paramType: 'Image',
postId: 'abcd-123'
}).override(['check_statuspage'], {})

expect(error.toString()).toBe(
[
'Possible solutions:',
'1) you can check the status of our services on our status page'
].join('\n')
)
})

it('override > with params', () => {
const error = mockErrors(['check_statuspage'], {}).override(['unsupported_blocktype'], {
blockType: 'Image'
})

expect(error.toString()).toBe(
['unsupported block type `Image` is included on this page.'].join('\n')
)
})
})
2 changes: 1 addition & 1 deletion test/setup.ts → test/test-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const mockErrors = createHumanLogs([
),
explanation(
'unsupported_blocktype',
'unsupported block type `{blockType} is included on this page.`',
'unsupported block type `{blockType}` is included on this page',
{
params: { blockType: '' }
}
Expand Down
4 changes: 2 additions & 2 deletions test/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expectTypeOf, it } from 'vitest'
import { mockErrors } from './setup'
import { describe, it } from 'vitest'
import { mockErrors } from './test-mocks'

describe('basic type tests', () => {
it('only valid keys', () => {
Expand Down

0 comments on commit 00cf269

Please sign in to comment.