Skip to content

Commit a92c7a7

Browse files
Merge pull request #18 from cybercoder-naj/type-safety
Added Type safety to onSuccess Method
2 parents 170a409 + e3178be commit a92c7a7

File tree

4 files changed

+35
-42
lines changed

4 files changed

+35
-42
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG
22
## [1.0.1] - 30-03-2024
3+
### Added
4+
- Type-safety to onSuccess method, based on attributes passed on `use`. ([#6](https://github.com/cybercoder-naj/logestic/issues/6))
5+
36
### Fixed
47
- Status of type `number` and not `any`. ([#5](https://github.com/cybercoder-naj/logestic/issues/5))
58

src/index.ts

+12-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import Elysia from 'elysia';
77
import {
88
Attribute,
9-
FormatObj,
9+
Callback,
1010
LogLevelColour,
1111
LogesticOptions,
1212
Preset
@@ -23,10 +23,8 @@ export const chalk = c; // Re-export chalk for custom formatting
2323
/**
2424
* Logestic class provides methods to configure and perform logging.
2525
*/
26-
export class Logestic {
27-
private requestedAttrs: {
28-
[key in keyof Attribute]: boolean;
29-
};
26+
export class Logestic<K extends keyof Attribute = keyof Attribute> {
27+
private requestedAttrs: K[];
3028
private dest!: BunFile;
3129
private showLevel: boolean;
3230
private logLevelColour: LogLevelColour;
@@ -40,7 +38,7 @@ export class Logestic {
4038
* @see LogesticOptions
4139
*/
4240
constructor(options: LogesticOptions = {}) {
43-
this.requestedAttrs = {};
41+
this.requestedAttrs = [];
4442
this.showLevel = options.showLevel || false;
4543
this.logLevelColour = options.logLevelColour || {};
4644
this.httpLogging = options.httpLogging || true;
@@ -82,23 +80,23 @@ export class Logestic {
8280
* @param attrs - An attribute key or an array of attribute keys.
8381
* @returns The Logestic instance for chaining.
8482
*/
85-
use(attr: keyof Attribute): Logestic;
86-
use(attrs: (keyof Attribute)[]): Logestic;
87-
use(attrs: keyof Attribute | (keyof Attribute)[]): Logestic {
83+
use<NK extends K>(attr: NK): Logestic<NK>;
84+
use<NK extends K>(attrs: NK[]): Logestic<NK>;
85+
use<NK extends K>(attrs: NK | NK[]): Logestic<NK> {
8886
if (Array.isArray(attrs)) {
8987
for (const attr of attrs) {
9088
this._use(attr);
9189
}
92-
return this;
90+
return this as unknown as Logestic<NK>;
9391
}
9492

9593
// Single attribute
9694
this._use(attrs);
97-
return this;
95+
return this as unknown as Logestic<NK>;
9896
}
9997

100-
private _use(attr: keyof Attribute) {
101-
this.requestedAttrs[attr] = true;
98+
private _use(attr: K) {
99+
this.requestedAttrs.push(attr);
102100
}
103101

104102
/**
@@ -128,7 +126,7 @@ export class Logestic {
128126
* @param formatAttr - The format object containing functions to format successful and failed logs.
129127
* @returns Elysia instance with the logger plugged in.
130128
*/
131-
format(this: Logestic, formatAttr: FormatObj) {
129+
format(this: Logestic, formatAttr: Callback<K>) {
132130
return this.build()
133131
.state('logestic_timeStart', 0n)
134132
.onRequest(({ store }) => {

src/types.ts

+14-22
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
import { BunFile } from 'bun';
99

1010
export type Attribute = {
11-
ip?: string;
12-
method?: string;
13-
path?: string;
14-
body?: any;
15-
query?: Record<string, string | undefined>;
16-
time?: Date;
17-
contentLength?: number;
18-
status?: number;
19-
referer?: string;
20-
userAgent?: string;
21-
duration?: bigint;
11+
ip: string;
12+
method: string;
13+
path: string;
14+
body: any;
15+
query: Record<string, string | undefined>;
16+
time: Date;
17+
contentLength: number;
18+
status: number;
19+
referer: string;
20+
userAgent: string;
21+
duration: bigint;
2222
};
2323

2424
export type ErrorAttribute = {
@@ -28,23 +28,15 @@ export type ErrorAttribute = {
2828
datetime: Date;
2929
};
3030

31-
/**
32-
* `AttributeMap` is a type that maps each key of `Attribute` to a boolean.
33-
* It is used to indicate which attributes are enabled or disabled.
34-
*/
35-
export type AttributeMap = {
36-
[key in keyof Attribute]: boolean;
37-
};
38-
3931
/**
4032
* `Presets` is an object that contains preset configurations for the Logestic module.
4133
*/
4234
export type Preset = 'common' | 'fancy';
4335
/**
44-
* `FormatObj` is an object that contains functions to format successful and failed logs.
36+
* `Callback` is an object that contains functions to format successful and failed logs.
4537
*/
46-
export type FormatObj = {
47-
onSuccess: (attr: Attribute) => string;
38+
export type Callback<K extends keyof Attribute> = {
39+
onSuccess: (attr: Pick<Attribute, K>) => string;
4840
onFailure: (attr: ErrorAttribute) => string;
4941
};
5042

src/utils.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77

88
import { StatusMap } from 'elysia';
99
import { type Context } from 'elysia';
10-
import type { AttributeMap, Attribute, LogType, LogLevelColour } from './types';
10+
import type { Attribute, LogType, LogLevelColour } from './types';
1111
import chalk, { ChalkInstance } from 'chalk';
1212

1313
/**
1414
* @param ctx Elysia context
1515
* @param reqAttrs A map of attributes to be built
1616
* @returns The built attributes
1717
*/
18-
export const buildAttrs = (
18+
export const buildAttrs = <K extends keyof Attribute>(
1919
ctx: Context,
20-
reqAttrs: AttributeMap,
20+
reqAttrs: K[],
2121
timeStart: bigint
22-
): Attribute => {
22+
): Pick<Attribute, K> => {
2323
const { request, path, body, query, set } = ctx;
2424

25-
let attrs: Attribute = {};
25+
let attrs: Partial<Attribute> = {};
2626
for (const key in reqAttrs) {
2727
const k = key as keyof Attribute;
2828
switch (k) {
@@ -76,7 +76,7 @@ export const buildAttrs = (
7676
}
7777
}
7878

79-
return attrs;
79+
return attrs as Pick<Attribute, K>;
8080
};
8181

8282
/**

0 commit comments

Comments
 (0)