Skip to content

Commit 109e6d6

Browse files
authored
docs(env): add @example blocks to env module exports (#172)
1 parent e1747ac commit 109e6d6

22 files changed

Lines changed: 846 additions & 0 deletions

src/env.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,20 @@ export function createEnvProxy(
175175

176176
/**
177177
* Convert an environment variable value to a boolean.
178+
*
179+
* @param value - The value to convert
180+
* @param defaultValue - Default when value is null/undefined (default: `false`)
181+
* @returns `true` if value is '1' or 'true' (case-insensitive), `false` otherwise
182+
*
183+
* @example
184+
* ```typescript
185+
* import { envAsBoolean } from '@socketsecurity/lib/env'
186+
*
187+
* envAsBoolean('true') // true
188+
* envAsBoolean('1') // true
189+
* envAsBoolean('false') // false
190+
* envAsBoolean(undefined) // false
191+
* ```
178192
*/
179193
/*@__NO_SIDE_EFFECTS__*/
180194
export function envAsBoolean(value: unknown, defaultValue = false): boolean {
@@ -190,6 +204,19 @@ export function envAsBoolean(value: unknown, defaultValue = false): boolean {
190204

191205
/**
192206
* Convert an environment variable value to a number.
207+
*
208+
* @param value - The value to convert
209+
* @param defaultValue - Default when value is not a finite number (default: `0`)
210+
* @returns The parsed integer, or the default value if parsing fails
211+
*
212+
* @example
213+
* ```typescript
214+
* import { envAsNumber } from '@socketsecurity/lib/env'
215+
*
216+
* envAsNumber('3000') // 3000
217+
* envAsNumber('abc') // 0
218+
* envAsNumber(undefined) // 0
219+
* ```
193220
*/
194221
/*@__NO_SIDE_EFFECTS__*/
195222
export function envAsNumber(value: unknown, defaultValue = 0): number {
@@ -203,6 +230,19 @@ export function envAsNumber(value: unknown, defaultValue = 0): number {
203230

204231
/**
205232
* Convert an environment variable value to a trimmed string.
233+
*
234+
* @param value - The value to convert
235+
* @param defaultValue - Default when value is null/undefined (default: `''`)
236+
* @returns The trimmed string value, or the default value
237+
*
238+
* @example
239+
* ```typescript
240+
* import { envAsString } from '@socketsecurity/lib/env'
241+
*
242+
* envAsString(' hello ') // 'hello'
243+
* envAsString(undefined) // ''
244+
* envAsString(null, 'n/a') // 'n/a'
245+
* ```
206246
*/
207247
/*@__NO_SIDE_EFFECTS__*/
208248
export function envAsString(value: unknown, defaultValue = ''): string {

src/env/ci.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@
55

66
import { isInEnv } from './rewire'
77

8+
/**
9+
* Returns whether the CI environment variable is set.
10+
*
11+
* @returns `true` if running in a CI environment, `false` otherwise
12+
*
13+
* @example
14+
* ```typescript
15+
* import { getCI } from '@socketsecurity/lib/env/ci'
16+
*
17+
* if (getCI()) {
18+
* console.log('Running in CI')
19+
* }
20+
* ```
21+
*/
822
/*@__NO_SIDE_EFFECTS__*/
923
export function getCI(): boolean {
1024
return isInEnv('CI')

src/env/debug.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55

66
import { getEnvValue } from './rewire'
77

8+
/**
9+
* Returns the value of the DEBUG environment variable.
10+
*
11+
* @returns The debug filter string, or `undefined` if not set
12+
*
13+
* @example
14+
* ```typescript
15+
* import { getDebug } from '@socketsecurity/lib/env/debug'
16+
*
17+
* const debug = getDebug()
18+
* // e.g. 'socket:*' or undefined
19+
* ```
20+
*/
821
/*@__NO_SIDE_EFFECTS__*/
922
export function getDebug(): string | undefined {
1023
return getEnvValue('DEBUG')

src/env/github.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import { getEnvValue } from './rewire'
88
/**
99
* GITHUB_API_URL environment variable.
1010
* GitHub API URL (e.g., https://api.github.com).
11+
*
12+
* @returns The GitHub API URL, or `undefined` if not set
13+
*
14+
* @example
15+
* ```typescript
16+
* import { getGithubApiUrl } from '@socketsecurity/lib/env/github'
17+
*
18+
* const apiUrl = getGithubApiUrl()
19+
* // e.g. 'https://api.github.com' or undefined
20+
* ```
1121
*/
1222
/*@__NO_SIDE_EFFECTS__*/
1323
export function getGithubApiUrl(): string | undefined {
@@ -17,6 +27,16 @@ export function getGithubApiUrl(): string | undefined {
1727
/**
1828
* GITHUB_BASE_REF environment variable.
1929
* GitHub pull request base branch.
30+
*
31+
* @returns The pull request base branch name, or `undefined` if not set
32+
*
33+
* @example
34+
* ```typescript
35+
* import { getGithubBaseRef } from '@socketsecurity/lib/env/github'
36+
*
37+
* const baseRef = getGithubBaseRef()
38+
* // e.g. 'main' or undefined
39+
* ```
2040
*/
2141
/*@__NO_SIDE_EFFECTS__*/
2242
export function getGithubBaseRef(): string | undefined {
@@ -26,6 +46,16 @@ export function getGithubBaseRef(): string | undefined {
2646
/**
2747
* GITHUB_REF_NAME environment variable.
2848
* GitHub branch or tag name.
49+
*
50+
* @returns The branch or tag name, or `undefined` if not set
51+
*
52+
* @example
53+
* ```typescript
54+
* import { getGithubRefName } from '@socketsecurity/lib/env/github'
55+
*
56+
* const refName = getGithubRefName()
57+
* // e.g. 'feature/my-branch' or 'v1.0.0'
58+
* ```
2959
*/
3060
/*@__NO_SIDE_EFFECTS__*/
3161
export function getGithubRefName(): string | undefined {
@@ -35,6 +65,16 @@ export function getGithubRefName(): string | undefined {
3565
/**
3666
* GITHUB_REF_TYPE environment variable.
3767
* GitHub ref type (branch or tag).
68+
*
69+
* @returns The ref type ('branch' or 'tag'), or `undefined` if not set
70+
*
71+
* @example
72+
* ```typescript
73+
* import { getGithubRefType } from '@socketsecurity/lib/env/github'
74+
*
75+
* const refType = getGithubRefType()
76+
* // e.g. 'branch' or 'tag'
77+
* ```
3878
*/
3979
/*@__NO_SIDE_EFFECTS__*/
4080
export function getGithubRefType(): string | undefined {
@@ -44,6 +84,16 @@ export function getGithubRefType(): string | undefined {
4484
/**
4585
* GITHUB_REPOSITORY environment variable.
4686
* GitHub repository name in owner/repo format.
87+
*
88+
* @returns The repository name, or `undefined` if not set
89+
*
90+
* @example
91+
* ```typescript
92+
* import { getGithubRepository } from '@socketsecurity/lib/env/github'
93+
*
94+
* const repo = getGithubRepository()
95+
* // e.g. 'SocketDev/socket-cli' or undefined
96+
* ```
4797
*/
4898
/*@__NO_SIDE_EFFECTS__*/
4999
export function getGithubRepository(): string | undefined {
@@ -53,6 +103,16 @@ export function getGithubRepository(): string | undefined {
53103
/**
54104
* GITHUB_SERVER_URL environment variable.
55105
* GitHub server URL (e.g., https://github.com).
106+
*
107+
* @returns The GitHub server URL, or `undefined` if not set
108+
*
109+
* @example
110+
* ```typescript
111+
* import { getGithubServerUrl } from '@socketsecurity/lib/env/github'
112+
*
113+
* const serverUrl = getGithubServerUrl()
114+
* // e.g. 'https://github.com' or undefined
115+
* ```
56116
*/
57117
/*@__NO_SIDE_EFFECTS__*/
58118
export function getGithubServerUrl(): string | undefined {
@@ -62,6 +122,16 @@ export function getGithubServerUrl(): string | undefined {
62122
/**
63123
* GITHUB_TOKEN environment variable.
64124
* GitHub authentication token for API access.
125+
*
126+
* @returns The GitHub token, or `undefined` if not set
127+
*
128+
* @example
129+
* ```typescript
130+
* import { getGithubToken } from '@socketsecurity/lib/env/github'
131+
*
132+
* const token = getGithubToken()
133+
* // e.g. 'ghp_abc123...' or undefined
134+
* ```
65135
*/
66136
/*@__NO_SIDE_EFFECTS__*/
67137
export function getGithubToken(): string | undefined {
@@ -71,6 +141,16 @@ export function getGithubToken(): string | undefined {
71141
/**
72142
* GH_TOKEN environment variable.
73143
* Alternative GitHub authentication token for API access (used by GitHub CLI).
144+
*
145+
* @returns The GH CLI token, or `undefined` if not set
146+
*
147+
* @example
148+
* ```typescript
149+
* import { getGhToken } from '@socketsecurity/lib/env/github'
150+
*
151+
* const token = getGhToken()
152+
* // e.g. 'gho_abc123...' or undefined
153+
* ```
74154
*/
75155
/*@__NO_SIDE_EFFECTS__*/
76156
export function getGhToken(): string | undefined {

src/env/helpers.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22
* @fileoverview Environment variable type conversion helpers.
33
*/
44

5+
/**
6+
* Convert an environment variable string to a boolean.
7+
*
8+
* @param value - The environment variable value to convert
9+
* @returns `true` if value is 'true', '1', or 'yes' (case-insensitive), `false` otherwise
10+
*
11+
* @example
12+
* ```typescript
13+
* import { envAsBoolean } from '@socketsecurity/lib/env/helpers'
14+
*
15+
* envAsBoolean('true') // true
16+
* envAsBoolean('1') // true
17+
* envAsBoolean('yes') // true
18+
* envAsBoolean(undefined) // false
19+
* ```
20+
*/
521
/*@__NO_SIDE_EFFECTS__*/
622
export function envAsBoolean(value: string | undefined): boolean {
723
if (!value) {
@@ -11,6 +27,21 @@ export function envAsBoolean(value: string | undefined): boolean {
1127
return lower === 'true' || lower === '1' || lower === 'yes'
1228
}
1329

30+
/**
31+
* Convert an environment variable string to a number.
32+
*
33+
* @param value - The environment variable value to convert
34+
* @returns The parsed number, or `0` if the value is undefined or not a valid number
35+
*
36+
* @example
37+
* ```typescript
38+
* import { envAsNumber } from '@socketsecurity/lib/env/helpers'
39+
*
40+
* envAsNumber('3000') // 3000
41+
* envAsNumber(undefined) // 0
42+
* envAsNumber('abc') // 0
43+
* ```
44+
*/
1445
/*@__NO_SIDE_EFFECTS__*/
1546
export function envAsNumber(value: string | undefined): number {
1647
if (!value) {
@@ -20,6 +51,20 @@ export function envAsNumber(value: string | undefined): number {
2051
return Number.isNaN(num) ? 0 : num
2152
}
2253

54+
/**
55+
* Convert an environment variable value to a string.
56+
*
57+
* @param value - The environment variable value to convert
58+
* @returns The string value, or an empty string if undefined
59+
*
60+
* @example
61+
* ```typescript
62+
* import { envAsString } from '@socketsecurity/lib/env/helpers'
63+
*
64+
* envAsString('hello') // 'hello'
65+
* envAsString(undefined) // ''
66+
* ```
67+
*/
2368
/*@__NO_SIDE_EFFECTS__*/
2469
export function envAsString(value: string | undefined): string {
2570
return value || ''

src/env/home.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55

66
import { getEnvValue } from './rewire'
77

8+
/**
9+
* Returns the value of the HOME environment variable.
10+
*
11+
* @returns The user's home directory path, or `undefined` if not set
12+
*
13+
* @example
14+
* ```typescript
15+
* import { getHome } from '@socketsecurity/lib/env/home'
16+
*
17+
* const home = getHome()
18+
* // e.g. '/tmp/user' or undefined
19+
* ```
20+
*/
821
/*@__NO_SIDE_EFFECTS__*/
922
export function getHome(): string | undefined {
1023
return getEnvValue('HOME')

src/env/locale.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import { getEnvValue } from './rewire'
88
/**
99
* LANG environment variable.
1010
* System locale and language settings.
11+
*
12+
* @returns The system locale string, or `undefined` if not set
13+
*
14+
* @example
15+
* ```typescript
16+
* import { getLang } from '@socketsecurity/lib/env/locale'
17+
*
18+
* const lang = getLang()
19+
* // e.g. 'en_US.UTF-8' or undefined
20+
* ```
1121
*/
1222
/*@__NO_SIDE_EFFECTS__*/
1323
export function getLang(): string | undefined {
@@ -17,6 +27,16 @@ export function getLang(): string | undefined {
1727
/**
1828
* LC_ALL environment variable.
1929
* Override for all locale settings.
30+
*
31+
* @returns The locale override string, or `undefined` if not set
32+
*
33+
* @example
34+
* ```typescript
35+
* import { getLcAll } from '@socketsecurity/lib/env/locale'
36+
*
37+
* const lcAll = getLcAll()
38+
* // e.g. 'C' or 'en_US.UTF-8'
39+
* ```
2040
*/
2141
/*@__NO_SIDE_EFFECTS__*/
2242
export function getLcAll(): string | undefined {
@@ -26,6 +46,16 @@ export function getLcAll(): string | undefined {
2646
/**
2747
* LC_MESSAGES environment variable.
2848
* Locale setting for message translations.
49+
*
50+
* @returns The messages locale string, or `undefined` if not set
51+
*
52+
* @example
53+
* ```typescript
54+
* import { getLcMessages } from '@socketsecurity/lib/env/locale'
55+
*
56+
* const lcMessages = getLcMessages()
57+
* // e.g. 'en_US.UTF-8' or undefined
58+
* ```
2959
*/
3060
/*@__NO_SIDE_EFFECTS__*/
3161
export function getLcMessages(): string | undefined {

src/env/node-auth-token.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55

66
import { getEnvValue } from './rewire'
77

8+
/**
9+
* Returns the value of the NODE_AUTH_TOKEN environment variable.
10+
*
11+
* @returns The Node.js registry auth token, or `undefined` if not set
12+
*
13+
* @example
14+
* ```typescript
15+
* import { getNodeAuthToken } from '@socketsecurity/lib/env/node-auth-token'
16+
*
17+
* const token = getNodeAuthToken()
18+
* // e.g. 'npm_abc123...' or undefined
19+
* ```
20+
*/
821
/*@__NO_SIDE_EFFECTS__*/
922
export function getNodeAuthToken(): string | undefined {
1023
return getEnvValue('NODE_AUTH_TOKEN')

0 commit comments

Comments
 (0)