|
| 1 | +import AES from "crypto-js/aes"; |
| 2 | +import encUtf8 from "crypto-js/enc-utf8"; |
| 3 | + |
| 4 | +export interface CloudConfigData { |
| 5 | + projectName: string; |
| 6 | + groupName: string; |
| 7 | + featureKey: string; |
| 8 | + value: unknown; |
| 9 | + valueType: string; |
| 10 | + prodEnabled?: boolean; |
| 11 | + devEnabled?: boolean; |
| 12 | + valueEncrypted?: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +export const CLOUD_CONFIG_DEFAULT_GROUP = "defaultGroup"; |
| 16 | +export const CLOUD_CONFIG_DEFAULT_PROJECT = "defaultProject"; |
| 17 | + |
| 18 | +const couldConfigSecretClient = |
| 19 | + process.env.NEXT_PUBLIC_CLOUD_CONFIG_CLIENT_ENCRYPT_SECRET; |
| 20 | + |
| 21 | +const couldConfigSecretServer = process.env.CLOUD_CONFIG_SERVER_ENCRYPT_SECRET; |
| 22 | + |
| 23 | +export const IS_PROD = process.env.NODE_ENV === "production"; |
| 24 | + |
| 25 | +export const decryptData = ( |
| 26 | + data: string, |
| 27 | + cryptSecret: string |
| 28 | +): string | null => { |
| 29 | + const decryptedText = AES.decrypt(data, cryptSecret); |
| 30 | + if (!decryptedText) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + return decryptedText.toString(encUtf8); |
| 34 | +}; |
| 35 | + |
| 36 | +export const parseSingleConfig = ( |
| 37 | + config: CloudConfigData, |
| 38 | + serverSideOnly = false |
| 39 | +): CloudConfigData => { |
| 40 | + if (!config.valueEncrypted) { |
| 41 | + return config; |
| 42 | + } |
| 43 | + const cryptSecret = serverSideOnly |
| 44 | + ? couldConfigSecretServer |
| 45 | + : couldConfigSecretClient; |
| 46 | + if (!cryptSecret) { |
| 47 | + // eslint-disable-next-line no-console |
| 48 | + console.log( |
| 49 | + `Can't decrypt featureKey ${config.featureKey}, Please set ${ |
| 50 | + serverSideOnly |
| 51 | + ? "CLOUD_CONFIG_SERVER_ENCRYPT_SECRET" |
| 52 | + : "NEXT_PUBLIC_CLOUD_CONFIG_CLIENT_ENCRYPT_SECRET" |
| 53 | + } in .env` |
| 54 | + ); |
| 55 | + return config; |
| 56 | + } |
| 57 | + const decryptedValue = decryptData(config.value as string, cryptSecret); |
| 58 | + if (!decryptedValue) { |
| 59 | + return config; |
| 60 | + } |
| 61 | + |
| 62 | + let newValue; |
| 63 | + if (config.valueType === "json") { |
| 64 | + try { |
| 65 | + newValue = JSON.parse(decryptedValue); |
| 66 | + } catch (error) { |
| 67 | + // eslint-disable-next-line no-console |
| 68 | + console.log("JSON.parse(decryptedValue) error", config.value, error); |
| 69 | + } |
| 70 | + } |
| 71 | + if (config.valueType === "array") { |
| 72 | + newValue = decryptedValue.split(",").map((tag) => tag.trim()); |
| 73 | + } |
| 74 | + if (config.valueType === "number") { |
| 75 | + newValue = parseFloat(decryptedValue); |
| 76 | + } |
| 77 | + if (config.valueType === "boolean") { |
| 78 | + newValue = Boolean(decryptedValue); |
| 79 | + } |
| 80 | + |
| 81 | + const newConfig = { |
| 82 | + ...config, |
| 83 | + value: newValue || decryptedValue, |
| 84 | + }; |
| 85 | + |
| 86 | + return newConfig; |
| 87 | +}; |
| 88 | + |
| 89 | +export const parseAllConfigs = ( |
| 90 | + configs: CloudConfigData[], |
| 91 | + serverSideOnly = false |
| 92 | +): CloudConfigData[] => { |
| 93 | + return configs.map((config) => parseSingleConfig(config, serverSideOnly)); |
| 94 | +}; |
| 95 | + |
| 96 | +interface GetCloudConfigParams { |
| 97 | + featureKey: string; |
| 98 | + groupName?: string; |
| 99 | + projectName?: string; |
| 100 | + configs: CloudConfigData[]; |
| 101 | +} |
| 102 | + |
| 103 | +export const getCloudConfig = <T>({ |
| 104 | + featureKey, |
| 105 | + groupName = CLOUD_CONFIG_DEFAULT_GROUP, |
| 106 | + projectName = CLOUD_CONFIG_DEFAULT_PROJECT, |
| 107 | + configs, |
| 108 | +}: GetCloudConfigParams) => { |
| 109 | + const config = configs.find((item) => { |
| 110 | + if (item.featureKey !== featureKey) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + if (item.groupName !== groupName) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + if (item.projectName !== projectName) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + return true; |
| 120 | + }); |
| 121 | + if (!config) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + if (IS_PROD && !config.prodEnabled) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + if (!IS_PROD && !config.devEnabled) { |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + return config.value as T; |
| 132 | +}; |
0 commit comments