Skip to content

Commit da13291

Browse files
authored
Merge pull request #21 from tclxshunquan-wang/feat/fix_typing
refactor: 💡 update logger interface and tests for improved setup function handling
2 parents 0640e3f + 207449e commit da13291

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

.changeset/big-masks-cry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hyperse/logger": patch
3+
---
4+
5+
refactor: update logger interface and tests for improved setup function handling

packages/logger/src/types/type-logger.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export interface LoggerBuilderWithRequired<
140140
* @returns The Logger type.
141141
*/
142142
build(
143-
setup: SetupContext<InitialContext, PluginContext>
143+
setup: StrictSetupFunction<InitialContext, PluginContext>
144144
): Logger<MergedLoggerContext<InitialContext, PluginContext>>;
145145

146146
/**
@@ -153,12 +153,12 @@ export interface LoggerBuilderWithRequired<
153153
): Logger<MergedLoggerContext<InitialContext, PluginContext>>;
154154

155155
/**
156-
* Build the logger with a function parameters.
157-
* @param setup The setup function.
156+
* Build the logger with a context object.
157+
* @param setup The setup context object.
158158
* @returns The Logger type.
159159
*/
160160
build(
161-
setup: StrictSetupFunction<InitialContext, PluginContext>
161+
setup: SetupContext<InitialContext, PluginContext>
162162
): Logger<MergedLoggerContext<InitialContext, PluginContext>>;
163163
}
164164

@@ -171,7 +171,7 @@ export interface LoggerBuilderWithRequired<
171171
export interface LoggerBuilderNoRequired<
172172
InitialContext extends object = object,
173173
PluginContext extends object = object,
174-
> extends LoggerBuilderWithRequired<InitialContext, PluginContext> {
174+
> {
175175
/**
176176
* Build the logger without parameters.
177177
* @returns The Logger type.
@@ -190,7 +190,8 @@ export type LoggerBuilder<
190190
PluginContext extends object = object,
191191
> =
192192
HasRequiredProperties<PluginContext> extends false
193-
? LoggerBuilderNoRequired<InitialContext, PluginContext>
193+
? LoggerBuilderNoRequired<InitialContext, PluginContext> &
194+
LoggerBuilderWithRequired<InitialContext, PluginContext>
194195
: LoggerBuilderWithRequired<InitialContext, PluginContext>;
195196

196197
/**

packages/logger/tests/ctx.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ describe('Logger Context Setup', () => {
99

1010
type NewLoggerContext = {
1111
env?: string;
12+
platform?: 'android' | 'ios';
13+
agent?: string;
1214
};
1315

1416
const consolePlugin = definePlugin<NewLoggerContext>({
@@ -21,12 +23,10 @@ describe('Logger Context Setup', () => {
2123
name: 'sampleLogger',
2224
})
2325
.use(consolePlugin)
24-
.build(() => {
25-
return {
26-
platform: 'android',
27-
agent: 'chrome 123',
28-
env: 'node',
29-
};
26+
.build({
27+
env: 'node',
28+
platform: 'android',
29+
agent: 'chrome 123',
3030
});
3131

3232
logger.info('info message');

packages/logger/tests/logger.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ describe('Logger Basic Functionality', () => {
195195

196196
type AppContext = {
197197
userId: string;
198-
environment: string;
198+
environment?: string;
199199
};
200200

201201
const consolePlugin = definePlugin<AppContext>({
@@ -300,7 +300,10 @@ describe('Logger Basic Functionality', () => {
300300
version: '1.0.0',
301301
})
302302
.use(consolePlugin)
303-
.build();
303+
.build({
304+
env: 'production',
305+
version: '1.0.0',
306+
});
304307

305308
// Function message - string
306309
logger.info((ctx) => `App v${ctx.version} running in ${ctx.env}`);

0 commit comments

Comments
 (0)