-
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: add NetlifyToml class for retrieving netlify.toml file through Github API * feat: add netlifyToml route file for retrieving, decoding, and sending netlify.toml * feat: adds netlifyTomlRouter and auth
- Loading branch information
Showing
4 changed files
with
81 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const axios = require('axios') | ||
const validateStatus = require('../utils/axios-utils') | ||
|
||
// Import error | ||
const { NotFoundError } = require('../errors/NotFoundError') | ||
|
||
const GITHUB_ORG_NAME = process.env.GITHUB_ORG_NAME | ||
const BRANCH_REF = process.env.BRANCH_REF | ||
|
||
class NetlifyToml { | ||
constructor(accessToken, siteName) { | ||
this.accessToken = accessToken | ||
this.siteName = siteName | ||
} | ||
|
||
async read() { | ||
try { | ||
const endpoint = `https://api.github.com/repos/${GITHUB_ORG_NAME}/${this.siteName}/contents/netlify.toml` | ||
|
||
const params = { | ||
"ref": BRANCH_REF, | ||
} | ||
|
||
const resp = await axios.get(endpoint, { | ||
validateStatus, | ||
params, | ||
headers: { | ||
Authorization: `token ${this.accessToken}`, | ||
"Content-Type": "application/json" | ||
} | ||
}) | ||
|
||
if (resp.status === 404) throw new NotFoundError ('netlify.toml file does not exist') | ||
|
||
const { content, sha } = resp.data | ||
return { content, sha } | ||
|
||
} catch (err) { | ||
throw err | ||
} | ||
} | ||
} | ||
|
||
module.exports = { NetlifyToml } |
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,32 @@ | ||
const express = require('express') | ||
const router = express.Router() | ||
const toml = require('toml') | ||
|
||
// Import middleware | ||
const { attachRouteHandlerWrapper } = require('../middleware/routeHandler') | ||
|
||
// Import classes | ||
const { NetlifyToml } = require('../classes/NetlifyToml') | ||
|
||
// List resources | ||
async function getNetlifyToml (req, res, next) { | ||
const { accessToken } = req | ||
const { siteName } = req.params | ||
|
||
const netlifyTomlFile = new NetlifyToml(accessToken, siteName) | ||
|
||
const { content } = await netlifyTomlFile.read() | ||
|
||
// Convert to readable form | ||
const netlifyTomlReadableContent = toml.parse(Base64.decode(content)) | ||
|
||
// Headers is an array of objects, specifying a set of access rules for each specified path | ||
// Under our current assumption, we apply the first set of access rules to all paths | ||
const netlifyTomlHeaderValues = netlifyTomlReadableContent.headers[0].values | ||
|
||
res.status(200).json({ netlifyTomlHeaderValues }) | ||
} | ||
|
||
router.get('/:siteName/netlify-toml', attachRouteHandlerWrapper(getNetlifyToml)) | ||
|
||
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