@pryv/boiler 1.2.4 → 1.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/config.js +3 -5
- package/src/index.d.ts +224 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pryv/boiler",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.6",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Logging and config boilerplate library for Node.js apps and services at Pryv",
|
|
6
6
|
"keywords": [
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"license": "BSD-3-Clause",
|
|
20
20
|
"author": "Pryv S.A <info@pryv.com> (https://pryv.com)",
|
|
21
21
|
"main": "src/index",
|
|
22
|
+
"types": "src/index.d.ts",
|
|
22
23
|
"scripts": {
|
|
23
24
|
"license": "source-licenser -c .licenser.yml .",
|
|
24
25
|
"lint": "semistandard"
|
|
@@ -27,7 +28,6 @@
|
|
|
27
28
|
"debug": "^4.3.4",
|
|
28
29
|
"js-yaml": "^4.1.0",
|
|
29
30
|
"nconf": "^0.12.0",
|
|
30
|
-
"superagent": "^8.0.9",
|
|
31
31
|
"winston": "^3.9.0",
|
|
32
32
|
"winston-daily-rotate-file": "^4.7.1"
|
|
33
33
|
},
|
package/src/config.js
CHANGED
|
@@ -24,8 +24,6 @@ const path = require('path');
|
|
|
24
24
|
const nconf = require('nconf');
|
|
25
25
|
nconf.formats.yaml = require('./lib/nconf-yaml');
|
|
26
26
|
|
|
27
|
-
const superagent = require('superagent');
|
|
28
|
-
|
|
29
27
|
/**
|
|
30
28
|
* Default values for Logger
|
|
31
29
|
*/
|
|
@@ -287,7 +285,7 @@ class Config {
|
|
|
287
285
|
if (store.type === 'file') {
|
|
288
286
|
res.info = 'From file: ' + store.file;
|
|
289
287
|
} else {
|
|
290
|
-
info = 'Type: ' + store.type;
|
|
288
|
+
res.info = 'Type: ' + store.type;
|
|
291
289
|
}
|
|
292
290
|
return res;
|
|
293
291
|
}
|
|
@@ -333,8 +331,8 @@ const FILE_PROTOCOL = 'file://';
|
|
|
333
331
|
const FILE_PROTOCOL_LENGTH = FILE_PROTOCOL.length;
|
|
334
332
|
|
|
335
333
|
async function loadFromUrl (url) {
|
|
336
|
-
const res = await
|
|
337
|
-
return res.
|
|
334
|
+
const res = await fetch(url);
|
|
335
|
+
return res.json();
|
|
338
336
|
}
|
|
339
337
|
|
|
340
338
|
function loadFromFile (fileUrl, baseFilesDir) {
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* [BSD-3-Clause](https://github.com/pryv/pryv-boiler/blob/master/LICENSE)
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// ----- Exported Types (for ESM: import type { Config, Logger } from '@pryv/boiler') -----
|
|
7
|
+
|
|
8
|
+
export interface InitOptions {
|
|
9
|
+
/** The name of the application, used by Logger and debug */
|
|
10
|
+
appName: string;
|
|
11
|
+
/** Directory to use to look for configs (default, env) */
|
|
12
|
+
baseConfigDir?: string;
|
|
13
|
+
/** Directory to use for `file://` relative paths */
|
|
14
|
+
baseFilesDir?: string;
|
|
15
|
+
/** Array of extra config sources to load */
|
|
16
|
+
extraConfigs?: Array<ConfigFile | ConfigPlugin | ConfigPluginAsync | ConfigData | ConfigRemoteURL | ConfigRemoteURLFromKey | ConfigFileAsync>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ConfigFile {
|
|
20
|
+
/** Scope for nconf hierarchical load */
|
|
21
|
+
scope: string;
|
|
22
|
+
/** The config file path (.yml, .json, .js) */
|
|
23
|
+
file: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ConfigFileAsync {
|
|
27
|
+
/** Scope for nconf hierarchical load */
|
|
28
|
+
scope: string;
|
|
29
|
+
/** The async config file path (.js that exports an async function) */
|
|
30
|
+
fileAsync: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ConfigPlugin {
|
|
34
|
+
plugin: {
|
|
35
|
+
/** Function that takes the Config instance and returns the plugin name */
|
|
36
|
+
load: (config: Config) => string;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface ConfigPluginAsync {
|
|
41
|
+
pluginAsync: {
|
|
42
|
+
/** Async function that takes the Config instance and returns the plugin name */
|
|
43
|
+
load: (config: Config) => Promise<string>;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ConfigData {
|
|
48
|
+
/** Scope for nconf hierarchical load */
|
|
49
|
+
scope: string;
|
|
50
|
+
/** Key to load data under. If null, loaded at root of the config */
|
|
51
|
+
key?: string;
|
|
52
|
+
/** The data to load */
|
|
53
|
+
data: object;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface ConfigRemoteURL {
|
|
57
|
+
/** Scope for nconf hierarchical load */
|
|
58
|
+
scope: string;
|
|
59
|
+
/** Key to load result of URL under. If null, loaded at root of the config */
|
|
60
|
+
key?: string;
|
|
61
|
+
/** The URL to the config definition */
|
|
62
|
+
url: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface ConfigRemoteURLFromKey {
|
|
66
|
+
/** Scope for nconf hierarchical load */
|
|
67
|
+
scope: string;
|
|
68
|
+
/** Key to load result of URL under. If null, override */
|
|
69
|
+
key?: string;
|
|
70
|
+
/** Retrieve URL from config matching this key */
|
|
71
|
+
urlFromKey: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface ScopeAndValue {
|
|
75
|
+
value: unknown;
|
|
76
|
+
scope: string;
|
|
77
|
+
info: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Type alias for extra config entries */
|
|
81
|
+
export type ExtraConfig = ConfigFile | ConfigPlugin | ConfigPluginAsync | ConfigData | ConfigRemoteURL | ConfigRemoteURLFromKey | ConfigFileAsync;
|
|
82
|
+
|
|
83
|
+
export interface Config {
|
|
84
|
+
/** The nconf store instance */
|
|
85
|
+
store: unknown;
|
|
86
|
+
/** The logger instance for config */
|
|
87
|
+
logger: Logger;
|
|
88
|
+
/** Base directory for config files */
|
|
89
|
+
baseConfigDir: string;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Return true if key has a value
|
|
93
|
+
* @param key - The config key to check
|
|
94
|
+
*/
|
|
95
|
+
has(key: string): boolean;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Retrieve value
|
|
99
|
+
* @param key - The config key. If no key is provided, all the config is returned
|
|
100
|
+
*/
|
|
101
|
+
get(key?: string): unknown;
|
|
102
|
+
get<T>(key: string): T;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Retrieve value and store info that applies
|
|
106
|
+
* @param key - The config key
|
|
107
|
+
*/
|
|
108
|
+
getScopeAndValue(key: string): ScopeAndValue | null;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Set value
|
|
112
|
+
* @param key - The config key
|
|
113
|
+
* @param value - The value to set
|
|
114
|
+
*/
|
|
115
|
+
set(key: string, value: unknown): void;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Inject test config and override any other option
|
|
119
|
+
* @param configObject - The config object to inject for testing
|
|
120
|
+
*/
|
|
121
|
+
injectTestConfig(configObject: object): void;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Replace a scope config set
|
|
125
|
+
* @param scope - The scope to replace
|
|
126
|
+
* @param configObject - The new config object for the scope
|
|
127
|
+
*/
|
|
128
|
+
replaceScopeConfig(scope: string, configObject: object): void;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface Logger {
|
|
132
|
+
/** The name of this logger */
|
|
133
|
+
name: string;
|
|
134
|
+
/** The parent logger, if any */
|
|
135
|
+
parent: Logger | null;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Log a message at the specified level
|
|
139
|
+
* @param level - The log level
|
|
140
|
+
* @param message - The log message
|
|
141
|
+
* @param context - Additional context
|
|
142
|
+
*/
|
|
143
|
+
log(level: string, message: string, ...context: unknown[]): void;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Log an info message
|
|
147
|
+
* @param message - The log message
|
|
148
|
+
* @param context - Additional context
|
|
149
|
+
*/
|
|
150
|
+
info(message: string, ...context: unknown[]): void;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Log a warning message
|
|
154
|
+
* @param message - The log message
|
|
155
|
+
* @param context - Additional context
|
|
156
|
+
*/
|
|
157
|
+
warn(message: string, ...context: unknown[]): void;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Log an error message
|
|
161
|
+
* @param message - The log message
|
|
162
|
+
* @param context - Additional context
|
|
163
|
+
*/
|
|
164
|
+
error(message: string, ...context: unknown[]): void;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Log a debug message
|
|
168
|
+
* @param message - The log message
|
|
169
|
+
* @param context - Additional context
|
|
170
|
+
*/
|
|
171
|
+
debug(message: string, ...context: unknown[]): void;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Get a "sub" Logger
|
|
175
|
+
* @param name - The name of the child logger
|
|
176
|
+
*/
|
|
177
|
+
getLogger(name: string): Logger;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Dump objects with file and line for debugging
|
|
181
|
+
*/
|
|
182
|
+
inspect(...args: unknown[]): void;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** The Boiler instance type (returned by init()) */
|
|
186
|
+
export interface Boiler {
|
|
187
|
+
/**
|
|
188
|
+
* Get a Logger
|
|
189
|
+
* @param name - The name of the logger
|
|
190
|
+
*/
|
|
191
|
+
getLogger: (name?: string) => Logger;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Preferred way to get the configuration.
|
|
195
|
+
* Waits until the configuration is fully initialized.
|
|
196
|
+
*/
|
|
197
|
+
getConfig: () => Promise<Config>;
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Get the configuration.
|
|
201
|
+
* If the configuration is not fully initialized, throws an error (or warns if warnOnly is true).
|
|
202
|
+
* @param warnOnly - Only warns about potential misuse of config instead of throwing
|
|
203
|
+
*/
|
|
204
|
+
getConfigUnsafe: (warnOnly?: boolean) => Config;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Initialize Boiler. Should be called just once when starting an app.
|
|
208
|
+
* @param options - Initialization options
|
|
209
|
+
* @param fullyLoadedCallback - Called when the config is fully loaded
|
|
210
|
+
*/
|
|
211
|
+
init: (options: InitOptions, fullyLoadedCallback?: (config: Config) => void) => Boiler;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ----- Module-level functions (for: const { getConfig, getLogger } = boiler) -----
|
|
215
|
+
|
|
216
|
+
export function getLogger(name?: string): Logger;
|
|
217
|
+
export function getConfig(): Promise<Config>;
|
|
218
|
+
export function getConfigUnsafe(warnOnly?: boolean): Config;
|
|
219
|
+
export function init(options: InitOptions, fullyLoadedCallback?: (config: Config) => void): Boiler;
|
|
220
|
+
|
|
221
|
+
// ----- Default Export (for: import boiler from '@pryv/boiler') -----
|
|
222
|
+
|
|
223
|
+
declare const boiler: Boiler;
|
|
224
|
+
export default boiler;
|