40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
export class DingtalkConfigError extends Error {
|
|
constructor(message = '当前环境未配置钉钉登录') {
|
|
super(message)
|
|
this.name = 'DingtalkConfigError'
|
|
}
|
|
}
|
|
|
|
export interface DingtalkAppConfig {
|
|
clientId: string
|
|
clientSecret: string
|
|
}
|
|
|
|
function readEnv(name: string): string {
|
|
return (process.env[name] || '').trim()
|
|
}
|
|
|
|
export function getDingtalkPublicConfig() {
|
|
const clientId = readEnv('DINGTALK_CLIENT_ID')
|
|
const clientSecret = readEnv('DINGTALK_CLIENT_SECRET')
|
|
const callbackUrl =
|
|
readEnv('DINGTALK_CALLBACK_URL') || readEnv('NEXT_PUBLIC_DINGTALK_CALLBACK_URL')
|
|
|
|
return {
|
|
enabled: Boolean(clientId && clientSecret),
|
|
clientId: clientId && clientSecret ? clientId : null,
|
|
callbackUrl: callbackUrl || null,
|
|
}
|
|
}
|
|
|
|
export function requireDingtalkAppConfig(): DingtalkAppConfig {
|
|
const clientId = readEnv('DINGTALK_CLIENT_ID')
|
|
const clientSecret = readEnv('DINGTALK_CLIENT_SECRET')
|
|
|
|
if (!clientId || !clientSecret) {
|
|
throw new DingtalkConfigError()
|
|
}
|
|
|
|
return { clientId, clientSecret }
|
|
}
|