Skip to content

Commit db57c1b

Browse files
Prune undefined env values and fix afdian check
Introduce a recursive pruneUndefined helper to remove undefined values from env/config objects instead of using JSON.stringify/parse. Apply it in loadEnvCredentials and loadEnv to produce clean credential/config shapes. Also tighten the afdian presence check in providers (use optional chaining) to avoid errors when config.afdian is absent.
1 parent 4123ef1 commit db57c1b

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

src/configs/credentials.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,24 @@ interface ConfigWithCredentials extends ContribkitConfig {
3333
credentials?: EnvCredentials
3434
}
3535

36+
export function pruneUndefined<T>(value: T): T {
37+
if (Array.isArray(value))
38+
return value.map(item => pruneUndefined(item)) as T
39+
40+
if (!value || typeof value !== 'object')
41+
return value
42+
43+
return Object.fromEntries(
44+
Object.entries(value)
45+
.filter(([, item]) => item !== undefined)
46+
.map(([key, item]) => [key, pruneUndefined(item)]),
47+
) as T
48+
}
49+
3650
export function loadEnvCredentials(): EnvCredentials {
3751
dotenv.config({ quiet: true })
3852

39-
return JSON.parse(JSON.stringify({
53+
return pruneUndefined({
4054
github: {
4155
token: process.env.CONTRIBKIT_GITHUB_TOKEN || process.env.GITHUB_TOKEN,
4256
},
@@ -61,7 +75,7 @@ export function loadEnvCredentials(): EnvCredentials {
6175
githubContributions: {
6276
token: process.env.CONTRIBKIT_GITHUB_CONTRIBUTIONS_TOKEN,
6377
},
64-
}))
78+
})
6579
}
6680

6781
export function getCredentials(config: ContribkitConfig): EnvCredentials {

src/configs/env.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ContribkitConfig, GitHubAccountType } from '../types'
22
import process from 'node:process'
33
import dotenv from 'dotenv'
4-
import { loadEnvCredentials } from './credentials'
4+
import { loadEnvCredentials, pruneUndefined } from './credentials'
55

66
export interface EnvConfig {
77
config: Partial<ContribkitConfig>
@@ -57,7 +57,7 @@ export function loadEnv(): EnvConfig {
5757

5858
// remove undefined keys
5959
return {
60-
config: JSON.parse(JSON.stringify(config)),
60+
config: pruneUndefined(config),
6161
credentials: loadEnvCredentials(),
6262
}
6363
}

src/providers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function guessProviders(config: ContribkitConfig) {
3838
if (config.opencollective && (config.opencollective.id || config.opencollective.slug || config.opencollective.githubHandle))
3939
items.push('opencollective')
4040

41-
if (config.afdian && config.afdian.userId && credentials.afdian?.token)
41+
if (config.afdian?.userId && credentials.afdian?.token)
4242
items.push('afdian')
4343

4444
if (credentials.polar?.token)

0 commit comments

Comments
 (0)