-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
87 lines (72 loc) · 2.62 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
import type { EnvParserPropTypes, EnvParserReturnTypes } from '@/ts'
import { ClientEnvParser } from '@/lib'
export const EnvParser = async (props: EnvParserPropTypes): Promise<EnvParserReturnTypes> => {
try {
const { envContent, envPath } = props
if (envContent !== undefined && envPath !== undefined) return Promise.resolve({
success: false,
error: 'The envContent and envPath cannot be used together!',
variables: {}
})
if (envContent) {
if (typeof envContent !== 'string') return Promise.resolve({
success: false,
error: 'The envContent must be a string!',
variables: {}
})
else if (envContent === '') return Promise.resolve({
success: false,
error: 'The envContent must not be empty!',
variables: {}
})
else if (envContent) return Promise.resolve(ClientEnvParser(props))
else return Promise.resolve({
success: false,
error: 'The envContent must be provided!',
variables: {}
})
}
if (envPath) {
if (typeof envPath !== 'string') return Promise.resolve({
success: false,
error: 'The envPath must be a string!',
variables: {}
})
else if (envPath === '') return Promise.resolve({
success: false,
error: 'The envPath must not be empty!',
variables: {}
})
else if (envPath) {
const isServer = typeof window === 'undefined'
if (isServer) {
const { ServerEnvParser } = await import('@/lib')
return Promise.resolve(ServerEnvParser(props))
}
else return Promise.resolve({
success: false,
error: 'The envPath cannot be used in the browser!',
variables: {}
})
}
else return Promise.resolve({
success: false,
error: 'The envPath must be provided!',
variables: {}
})
}
else return Promise.resolve({
success: false,
error: 'The envContent or envPath must be provided!',
variables: {}
})
}
catch (error) {
return Promise.resolve({
success: false,
error: error as string,
variables: {}
})
}
}
export default EnvParser