-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat] Create settings route and class (#48)
* feat: add settings router to server file * feat: add OtherType file type to File class * feat: add GET settings route * feat: Create a separate Settings class * feat: Create a new POST method for settings route * fix: defer content decoding to frontend * refactor: remove unnecessary file type in File class * refactor: general cleanup * fix: retintroduce yaml package import * refactor: rename variables for clarity * feat: send and receive only fields that the frontend requires This commit limits the response being sent to the frontend to the relevant fields (title, favicon, resources_name, colors) from the config.yml file. It also modifies the code to receive these same fields and make updates to the config file as opposed to rewriting it completely. This commit also switches out the base64 library for the js-base64 library for the Settings file as we had some problems with encoding/decoding with this library in the past * feat: remove transmission of config sha to frontend
- Loading branch information
Showing
7 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const { Base64 } = require('js-base64') | ||
const yaml = require('js-yaml') | ||
|
||
// import classes | ||
const { Config } = require('../classes/Config.js') | ||
const { File, DataType } = require('../classes/File.js') | ||
|
||
class Settings { | ||
constructor(accessToken, siteName) { | ||
this.accessToken = accessToken | ||
this.siteName = siteName | ||
} | ||
|
||
async get() { | ||
try { | ||
// retrieve _config.yml and social-media.yml | ||
const configResp = new Config(this.accessToken, this.siteName) | ||
const IsomerDataFile = new File(this.accessToken, this.siteName) | ||
const dataType = new DataType() | ||
IsomerDataFile.setFileType(dataType) | ||
|
||
const { content: config } = await configResp.read() | ||
const socialMediaResp = IsomerDataFile.read('social-media.yml').catch((err) => { | ||
// social-media.yml doesn't exist so we create a social-media.yml | ||
const content = { | ||
facebook: '', | ||
linkedin: '', | ||
twitter: '', | ||
youtube: '', | ||
instagram: '', | ||
} | ||
const socialMediaYml = Base64.encode(yaml.safeDump(content)) | ||
const { sha } = IsomerDataFile.create('social-media.yml', socialMediaYml) | ||
return { content, sha } | ||
}) | ||
const { content: socialMedia, sha: socialMediaSha } = await socialMediaResp | ||
|
||
// convert data to object form | ||
const configContent = yaml.safeLoad(Base64.decode(config)); | ||
const socialMediaContent = yaml.safeLoad(Base64.decode(socialMedia)); | ||
|
||
// retrieve only the relevant config fields | ||
const configFieldsRequired = { | ||
title: configContent.title, | ||
favicon: configContent.favicon, | ||
resources_name: configContent.resources_name, | ||
colors: configContent.colors, | ||
|
||
} | ||
|
||
return ({ configFieldsRequired, socialMediaContent, socialMediaSha }) | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
} | ||
|
||
async post(payload) { | ||
try { | ||
// setup | ||
const configResp = new Config(this.accessToken, this.siteName) | ||
const config = await configResp.read() | ||
const IsomerDataFile = new File(this.accessToken, this.siteName) | ||
const dataType = new DataType() | ||
IsomerDataFile.setFileType(dataType) | ||
|
||
// extract data | ||
const { | ||
socialMediaSettings, | ||
configSettings, | ||
socialMediaSha, | ||
} = payload | ||
|
||
// update config object | ||
const configContent = yaml.safeLoad(Base64.decode(config.content)); | ||
Object.keys(configSettings).forEach((setting) => (configContent[setting] = configSettings[setting])); | ||
|
||
// update files | ||
const newConfigContent = Base64.encode(yaml.safeDump(configContent)) | ||
const newSocialMediaContent = Base64.encode(yaml.safeDump(socialMediaSettings)) | ||
await configResp.update(newConfigContent, config.sha) | ||
await IsomerDataFile.update('social-media.yml', newSocialMediaContent, socialMediaSha) | ||
return | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
} | ||
} | ||
|
||
module.exports = { Settings } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const _ = require('lodash') | ||
const jwtUtils = require('../utils/jwt-utils') | ||
|
||
// Import Classes | ||
const { Settings } = require('../classes/Settings.js') | ||
|
||
router.get('/:siteName/settings', async function(req, res, next) { | ||
try { | ||
const { oauthtoken } = req.cookies | ||
let { access_token } = jwtUtils.verifyToken(oauthtoken) | ||
const { siteName } = req.params | ||
|
||
const settingsFile = new Settings(access_token, siteName) | ||
const settings = await settingsFile.get() | ||
res.status(200).json({ settings }) | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
}) | ||
|
||
router.post('/:siteName/settings', async function(req, res, next) { | ||
try { | ||
const { oauthtoken } = req.cookies | ||
let { access_token } = jwtUtils.verifyToken(oauthtoken) | ||
const { siteName } = req.params | ||
|
||
const settings = new Settings(access_token, siteName) | ||
await settings.post(req.body) | ||
res.status(200).send('OK') | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
}) | ||
|
||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters