aboutsummaryrefslogtreecommitdiff
path: root/common/configHelper.ts
diff options
context:
space:
mode:
authorBobby <[email protected]>2022-11-30 23:16:07 -0500
committerBobby <[email protected]>2022-11-30 23:16:07 -0500
commitdaaa789068cebb5fdfcea6197ade6e663be46e0f (patch)
tree1cd315851b779ac28fe622da332c3c16fe1c433c /common/configHelper.ts
downloadtcssocialify-daaa789068cebb5fdfcea6197ade6e663be46e0f.tar.xz
tcssocialify-daaa789068cebb5fdfcea6197ade6e663be46e0f.zip
socialify update
Diffstat (limited to 'common/configHelper.ts')
-rw-r--r--common/configHelper.ts83
1 files changed, 83 insertions, 0 deletions
diff --git a/common/configHelper.ts b/common/configHelper.ts
new file mode 100644
index 0000000..8492584
--- /dev/null
+++ b/common/configHelper.ts
@@ -0,0 +1,83 @@
+import { RepoQueryResponse } from './github/repoQuery'
+import Configuration, {
+ Font,
+ OptionalConfigs,
+ OptionalConfigsKeys,
+ Pattern,
+ Theme
+} from './types/configType'
+import QueryType from './types/queryType'
+
+type Key = keyof typeof OptionalConfigsKeys
+
+const DEFAULT_CONFIG: Configuration = {
+ logo: '',
+ font: Font.inter,
+ theme: Theme.light,
+ pattern: Pattern.plus
+}
+
+const getOptionalConfig = (repository: RepoQueryResponse['repository']) => {
+ if (repository) {
+ const languages = repository.languages?.nodes || []
+ const language =
+ languages.length > 0 ? languages[0]?.name || 'unknown' : 'unknown'
+ const language2 =
+ languages.length > 1 ? languages[1]?.name || 'unknown' : 'unknown'
+ const newConfig: OptionalConfigs = {
+ owner: { state: false, value: repository.owner.login },
+ name: { state: true, value: repository.name },
+ description: {
+ state: false,
+ editable: true,
+ value: repository.description || ''
+ },
+ language: { state: false, value: language },
+ language2: { state: false, value: language2 },
+ stargazers: { state: false, value: repository.stargazerCount },
+ forks: { state: false, value: repository.forkCount },
+ pulls: { state: false, value: repository.pullRequests.totalCount },
+ issues: { state: false, value: repository.issues.totalCount }
+ }
+ return newConfig
+ }
+ return null
+}
+
+const mergeConfig = (
+ repository: RepoQueryResponse['repository'],
+ query: QueryType
+): Configuration | null => {
+ if (!repository) {
+ return null
+ }
+
+ const config: Configuration = {
+ logo: query.logo || DEFAULT_CONFIG.logo,
+ font: query.font || DEFAULT_CONFIG.font,
+ pattern: query.pattern || DEFAULT_CONFIG.pattern,
+ theme: query.theme || DEFAULT_CONFIG.theme
+ }
+ const optionalConfig = getOptionalConfig(repository)
+
+ if (optionalConfig) {
+ Object.assign(config, optionalConfig)
+ for (const key in query) {
+ if (key in OptionalConfigsKeys) {
+ Object.assign(config[key as Key] ?? {}, {
+ state: query[key as Key] === '1'
+ })
+ if (config[key as Key]?.editable) {
+ const editableValue = query[`${key}Editable` as keyof typeof query]
+ if (editableValue) {
+ Object.assign(config[key as Key] ?? {}, { value: editableValue })
+ }
+ }
+ }
+ }
+ }
+
+ return config
+}
+
+export { DEFAULT_CONFIG, getOptionalConfig, mergeConfig }