-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
187 lines (157 loc) · 6.34 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const isRecord = (v: unknown): v is Record<string | symbol | number, unknown> => typeof v === 'object' && v !== null && !Array.isArray(v)
const isFunction = (v: unknown): v is ((...args: unknown[]) => unknown) => typeof v === 'function'
const isConstructor = (v: unknown): v is new (...args: unknown[]) => unknown => isFunction(v) && isRecord(v.prototype) && v.prototype.constructor === v
// const isString = (v: unknown): v is string => typeof v === 'string'
const isBoolean = (v: unknown): v is boolean => typeof v === 'boolean'
// const withProp = <T extends string | symbol | number,>(value: Record<string | symbol | number, unknown>, key: T): value is Record<T, unknown> => key in value
export class TransformTypeError extends SyntaxError { }
type TypesList = | 'string' |
typeof String |
'number' |
typeof Number |
'boolean' |
typeof Boolean |
'bigint' |
typeof BigInt
export type TransformTypeFunction<T,> = (v: string) => T;
export type TransformTypeConstructor<T,> = new (...args: any) => T
export type TransformType<T,> = string | TransformTypeConstructor<T> | TransformTypeFunction<T>;
const NumberTransformType = (v: string): number => parseFloat(v)
const BooleanTransformType = (v: string): boolean => /^on|1|true$/i.test(v)
const DateTransformType = (v: string): Date => (console.log(v), new Date(v))
const BigIntTransformType = (v: string): bigint => {
const exp = /^(\d+)n?$/.exec(v);
if (!exp) {
throw new TransformTypeError(`Cannot convert ${v} to a BigInt`)
}
const [, num] = exp;
return BigInt(num);
}
export const types = {
string: (v: string): string => v,
number: NumberTransformType,
boolean: BooleanTransformType,
bigint: BigIntTransformType,
date: DateTransformType,
}
type EnvconfigOptions = {
env?: Record<string | number | symbol, string | undefined>;
prefix?: string;
suffix?: string;
optionalPrefix?: string;
optionalSuffix?: string;
}
const globalThisProcessEnv = () => {
const g: any = globalThis;
// Load process.env from globalThis
if (isRecord(g) && isRecord(g.process) && isRecord(g.process.env)) return g.process.env as Record<string, string | undefined>;
// Load Deno.env from globalThis
if (isRecord(g) && isRecord(g.Deno) && isRecord(g.Deno.env) && isFunction(g.Deno.env.toObject)) {
const res = g.Deno.env.toObject();
if (isRecord(res)) return res as Record<string, string | undefined>;
}
return {}
}
type GetConfigArgs<T, R extends boolean> = [args1?: TransformType<T> | { type?: TransformType<T>, required?: R }, args2?: R | { required: R }]
type ResultGetConfig<T, R extends boolean> = R extends true ? T : T | undefined
const aliasType: [TypesList, TransformTypeFunction<unknown>][] = [
['string', types.string],
[String, types.string],
['number', types.number],
[Number, types.number],
['boolean', types.boolean],
[Boolean, types.boolean],
['bigint', types.bigint],
[BigInt, types.bigint],
];
const resolveTransformType = (tt: TransformType<unknown>): TransformTypeFunction<unknown> => {
const aliasTypeFound = aliasType.find(([k]) => k === tt);
if (aliasTypeFound) {
const [, t] = aliasTypeFound;
return t;
}
if (isConstructor(tt)) {
return (v: string) => new tt(v)
}
if (!isFunction(tt)) {
throw new TransformTypeError(`Invalid type ${tt.toString()}`)
}
return tt;
}
export class Envconfig {
readonly env: Record<string | number | symbol, string | undefined>;
constructor(private options?: EnvconfigOptions) {
this.env = this.options?.env ?? globalThisProcessEnv()
}
*findValue(envPath: string): Generator<[string, string | undefined]> {
const { optionalPrefix, optionalSuffix, prefix, suffix } = this.options ?? {}
const principalKey = `${prefix ?? ''}${envPath}${suffix ?? ''}`;
let keys: string[] = [principalKey];
if (!prefix && optionalPrefix) {
keys = [`${optionalPrefix}${principalKey}`, ...keys];
}
if (!suffix && optionalSuffix) {
keys = [`${principalKey}${optionalSuffix}`, ...keys];
}
if (!prefix && optionalPrefix && !suffix && optionalSuffix) {
keys = [`${optionalPrefix}${principalKey}${optionalSuffix}`, ...keys];
}
for (const key of keys) {
yield [key, this.env[key]];
}
}
getConfig<R extends boolean = false>(configPath: string, ...args: GetConfigArgs<'string', R>): ResultGetConfig<string, R>
getConfig<R extends boolean = false>(configPath: string, ...args: GetConfigArgs<'bigint', R>): ResultGetConfig<bigint, R>
getConfig<R extends boolean = false>(configPath: string, ...args: GetConfigArgs<'number', R>): ResultGetConfig<number, R>
getConfig<R extends boolean = false>(configPath: string, ...args: GetConfigArgs<'boolean', R>): ResultGetConfig<boolean, R>
getConfig<T, R extends boolean = false>(configPath: string, ...args: GetConfigArgs<T, R>): ResultGetConfig<T, R>
getConfig<R extends boolean = false>(configPath: string, ...args: GetConfigArgs<unknown, R>): ResultGetConfig<unknown, R> {
const [arg1, arg2] = args;
let required: boolean = false;
let transformType: TransformType<any> = function defaultTransformType(v: any) { return v };
try {
if (isFunction(arg1)) {
transformType = resolveTransformType(arg1);
if (isRecord(arg2)) {
required = arg2.required;
}
if (isBoolean(arg2)) {
required = arg2;
}
} else if (isRecord(arg1)) {
if (arg1.type !== undefined) {
transformType = resolveTransformType(arg1.type);
}
if (isBoolean(arg1.required)) {
required = arg1.required;
} else if (isBoolean(arg2)) {
required = arg2;
} else if (isRecord(arg2)) {
required = arg2.required;
}
}
for (const [_key, value] of this.findValue(configPath)) {
if (value !== undefined) {
return transformType(value);
}
}
if (required) {
throw new TransformTypeError(`Cannot found config ${configPath}`)
}
} catch (ex) {
if (ex instanceof TransformTypeError) {
Error.captureStackTrace(ex, Envconfig.prototype.getConfig);
throw ex;
}
throw ex;
}
return undefined as any;
}
}
export const envconfig = (options?: EnvconfigOptions) => {
const e = new Envconfig(options);
return e.getConfig.bind(e);
}
export const e = envconfig();
export const required = { required: true } as const;
export default envconfig;