Skip to content

Commit 57ffbd2

Browse files
author
Matt Berther
committed
redefine type declaration and inject into winston.transports
1 parent 6e35ab6 commit 57ffbd2

File tree

1 file changed

+105
-82
lines changed

1 file changed

+105
-82
lines changed

Diff for: index.d.ts

+105-82
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,106 @@
1-
declare module "winston-daily-rotate-file" {
2-
import * as Transport from "winston-transport";
3-
4-
namespace DailyRotateFile {
5-
interface DailyRotateFileTransportOptions extends Transport.TransportStreamOptions {
6-
json?: boolean;
7-
eol?: string;
8-
9-
/**
10-
* A string representing the moment.js date format to be used for rotating. The meta characters used in this string will dictate the frequency of the file rotation. For example, if your datePattern is simply 'HH' you will end up with 24 log files that are picked up and appended to every day. (default 'YYYY-MM-DD')
11-
*/
12-
datePattern?: string;
13-
14-
/**
15-
* A boolean to define whether or not to gzip archived log files. (default 'false')
16-
*/
17-
zippedArchive?: boolean;
18-
19-
/**
20-
* Filename to be used to log to. This filename can include the %DATE% placeholder which will include the formatted datePattern at that point in the filename. (default: 'winston.log.%DATE%)
21-
*/
22-
filename?: string;
23-
24-
/**
25-
* The directory name to save log files to. (default: '.')
26-
*/
27-
dirname?: string;
28-
29-
/**
30-
* Write directly to a custom stream and bypass the rotation capabilities. (default: null)
31-
*/
32-
stream?: NodeJS.WritableStream;
33-
34-
/**
35-
* Maximum size of the file after which it will rotate. This can be a number of bytes, or units of kb, mb, and gb. If using the units, add 'k', 'm', or 'g' as the suffix. The units need to directly follow the number. (default: null)
36-
*/
37-
maxSize?: string | number;
38-
39-
/**
40-
* Maximum number of logs to keep. If not set, no logs will be removed. This can be a number of files or number of days. If using days, add 'd' as the suffix. (default: null)
41-
*/
42-
maxFiles?: string | number;
43-
44-
/**
45-
* An object resembling https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options indicating additional options that should be passed to the file stream. (default: `{ flags: 'a' }`)
46-
*/
47-
options?: string | object;
48-
49-
/**
50-
* A string representing the name of the name of the audit file. (default: './hash-audit.json')
51-
*/
52-
auditFile?: string;
53-
54-
/**
55-
* A string representing the frequency of rotation. (default: 'custom')
56-
*/
57-
frequency?: string;
58-
59-
/**
60-
* A boolean whether or not to generate file name from "datePattern" in UTC format. (default: false)
61-
*/
62-
utc?: boolean;
63-
64-
/**
65-
* A string representing an extension to be added to the filename, if not included in the filename property. (default: '')
66-
*/
67-
extension?: string;
68-
}
69-
}
70-
71-
interface DailyRotateFileTransportInstance extends Transport {
72-
filename: string;
73-
dirname: string;
74-
logStream: NodeJS.WritableStream;
75-
options: DailyRotateFile.DailyRotateFileTransportOptions;
76-
77-
new (options?: DailyRotateFile.DailyRotateFileTransportOptions): DailyRotateFileTransportInstance;
78-
}
79-
80-
const DailyRotateFile: DailyRotateFileTransportInstance;
81-
82-
export = DailyRotateFile;
1+
import TransportStream from "winston-transport";
2+
3+
// referenced from https://stackoverflow.com/questions/40510611/typescript-interface-require-one-of-two-properties-to-exist
4+
type RequireOnlyOne<T, Keys extends keyof T = keyof T> =
5+
Pick<T, Exclude<keyof T, Keys>>
6+
& {
7+
[K in Keys]-?:
8+
Required<Pick<T, K>>
9+
& Partial<Record<Exclude<Keys, K>, undefined>>
10+
}[Keys];
11+
12+
// merging into winston.transports
13+
declare module 'winston/lib/winston/transports' {
14+
interface Transports {
15+
DailyRotateFileTransport: typeof DailyRotateFileTransport;
16+
DailyRotateFileTransportOptions: DailyRotateFileTransportOptions;
17+
}
8318
}
19+
20+
export type DailyRotateFileTransportOptions = RequireOnlyOne<GeneralDailyRotateFileTransportOptions, 'filename' | 'stream'>;
21+
22+
interface GeneralDailyRotateFileTransportOptions extends TransportStream.TransportStreamOptions {
23+
json?: boolean;
24+
eol?: string;
25+
26+
/**
27+
* A string representing the moment.js date format to be used for rotating. The meta characters used in this string will dictate the frequency of the file rotation. For example, if your datePattern is simply 'HH' you will end up with 24 log files that are picked up and appended to every day. (default 'YYYY-MM-DD')
28+
*/
29+
datePattern?: string;
30+
31+
/**
32+
* A boolean to define whether or not to gzip archived log files. (default 'false')
33+
*/
34+
zippedArchive?: boolean;
35+
36+
/**
37+
* Filename to be used to log to. This filename can include the %DATE% placeholder which will include the formatted datePattern at that point in the filename. (default: 'winston.log.%DATE%)
38+
*/
39+
filename?: string;
40+
41+
/**
42+
* The directory name to save log files to. (default: '.')
43+
*/
44+
dirname?: string;
45+
46+
/**
47+
* Write directly to a custom stream and bypass the rotation capabilities. (default: null)
48+
*/
49+
stream?: NodeJS.WritableStream;
50+
51+
/**
52+
* Maximum size of the file after which it will rotate. This can be a number of bytes, or units of kb, mb, and gb. If using the units, add 'k', 'm', or 'g' as the suffix. The units need to directly follow the number. (default: null)
53+
*/
54+
maxSize?: string | number;
55+
56+
/**
57+
* Maximum number of logs to keep. If not set, no logs will be removed. This can be a number of files or number of days. If using days, add 'd' as the suffix. (default: null)
58+
*/
59+
maxFiles?: string | number;
60+
61+
/**
62+
* An object resembling https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options indicating additional options that should be passed to the file stream. (default: `{ flags: 'a' }`)
63+
*/
64+
options?: string | object;
65+
66+
/**
67+
* A string representing the name of the name of the audit file. (default: './hash-audit.json')
68+
*/
69+
auditFile?: string;
70+
71+
/**
72+
* A string representing the frequency of rotation. (default: 'custom')
73+
*/
74+
frequency?: string;
75+
76+
/**
77+
* A boolean whether or not to generate file name from "datePattern" in UTC format. (default: false)
78+
*/
79+
utc?: boolean;
80+
81+
/**
82+
* A string representing an extension to be added to the filename, if not included in the filename property. (default: '')
83+
*/
84+
extension?: string;
85+
86+
/**
87+
* Create a tailable symlink to the current active log file. (default: false)
88+
*/
89+
createSymlink?: boolean;
90+
91+
/**
92+
* The name of the tailable symlink. (default: 'current.log')
93+
*/
94+
symlinkName?: string;
95+
}
96+
97+
declare class DailyRotateFileTransport extends TransportStream {
98+
filename: string;
99+
dirname: string;
100+
logStream: NodeJS.WritableStream;
101+
options: DailyRotateFileTransportOptions;
102+
103+
constructor(options: DailyRotateFileTransportOptions);
104+
}
105+
106+
export default DailyRotateFileTransport;

0 commit comments

Comments
 (0)