UNPKG

3.37 kBTypeScriptView Raw
1import { RandomUUIDOptions } from "crypto";
2import { AppError } from "../src/error.js";
3
4export type Either<T, E = AppError> =
5 | { value: T; error?: never }
6 | { value?: never; error: E };
7
8export type EitherN<T, E = AppError> =
9 | { value: T; errors?: never }
10 | { value?: never; errors: E[] };
11
12export interface NoopFn {
13 (): void;
14}
15
16export interface UuidFunc {
17 /**
18 * Returns true if value conforms a basic uuid structure.
19 * This check is case-insensitive.
20 */
21 isValid: (value: any) => boolean;
22
23 /**
24 * Return a new uuid v4
25 */
26 (options?: RandomUUIDOptions): string;
27}
28
29/**
30 * Basic timing and call information
31 */
32export type InsightEventCall =
33 | {
34 type: "stop" | "aborted";
35 name?: string;
36
37 /**
38 * Time in milliseconds since some kind of epoch, this may be unix epoch or process start
39 */
40 time: number;
41 }
42 | {
43 type: "start";
44 name?: string;
45
46 /**
47 * Duration in milliseconds between (end|aborted) and start time. This is filled when an
48 * event is aborted or stopped via `eventStop`.
49 */
50 duration?: number;
51
52 /**
53 * Time in milliseconds since some kind of epoch, this may be unix epoch or process start
54 */
55 time: number;
56 }
57 | InsightEventCall[];
58
59/**
60 * Encapsulate the base information needed to dispatch events
61 */
62export interface InsightEvent {
63 log: Logger;
64
65 signal?: AbortSignal;
66
67 /**
68 * If event is first event dispatched in chain
69 */
70 parent?: InsightEvent;
71
72 name?: string;
73
74 callStack: InsightEventCall[];
75}
76
77/**
78 * The logger only has two severities:
79 * - info
80 * - error
81 *
82 * Either a log line is innocent enough and only provides debug information if needed, or
83 * someone should be paged because something goes wrong. For example handled 400 errors
84 * don't need any ones attention, but unhandled 500 errors do.
85 *
86 * The log functions {@ee Logger#info} only accepts a single parameter. This prevents magic
87 * outputs like automatic concatenating strings in to a single message, or always having a top
88 * level array as a message.
89 */
90export interface Logger {
91 info(arg: any): void;
92
93 error(arg: any): void;
94}
95
96/**
97 * Context that should be logged in all log lines. e.g
98 * a common request id.
99 */
100interface LoggerContext {
101 type?: string;
102}
103
104export interface LoggerOptions<T extends LoggerContext> {
105 /**
106 * Replaces log.info with a 'noop'.Defaults to 'false'.
107 */
108 disableInfoLogger?: true | undefined;
109
110 /**
111 * Replaces log.error with a 'noop'.Defaults to 'false'.
112 */
113 disableErrorLogger?: true | undefined;
114
115 /**
116 * Set the printer to be used. Defaults to "pretty" when 'NODE_ENV===development',
117 * "github-actions" when 'GITHUB_ACTIONS===true' and "ndjson" by default.
118 */
119 printer?: "pretty" | "ndjson" | "github-actions" | undefined;
120
121 /**
122 * The stream to write the logs to
123 */
124 stream?: NodeJS.WriteStream;
125
126 /**
127 * Context that should be logged in all log lines. e.g
128 * a common request id.
129 */
130 ctx?: T;
131}
132
133/**
134 * Options for processDirectoryRecursive and processDirectoryRecursiveSync
135 */
136export interface ProcessDirectoryOptions {
137 /**
138 * Skip node_modules directory, true by default
139 */
140 skipNodeModules?: boolean;
141
142 /**
143 * Skip files and directories starting with a '.', true
144 * by default
145 */
146 skipDotFiles?: boolean;
147}