diff --git a/.eslintrc.cjs b/.eslintrc.cjs new file mode 100644 index 0000000..61b9887 --- /dev/null +++ b/.eslintrc.cjs @@ -0,0 +1,11 @@ +module.exports = { + env: { + node: true, + jest: true, + }, + extends: "eslint:recommended", + parserOptions: { + ecmaVersion: "latest", + sourceType: "module", + }, +}; diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..c2bb7fd --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,40 @@ +name: CI + +on: + pull_request: + branches: + - main + push: + branches: + - main + +jobs: + lint: + runs-on: ubuntu-latest + + steps: + - name: "Checkout repository" + uses: actions/checkout@v3 + with: + persist-credentials: false + - name: "Use Node.js" + uses: actions/setup-node@v3 + with: + node-version: "16.x" + - run: npm install + - run: npm run lint + + test: + runs-on: ubuntu-latest + + steps: + - name: "Checkout repository" + uses: actions/checkout@v3 + with: + persist-credentials: false + - name: "Use Node.js" + uses: actions/setup-node@v3 + with: + node-version: "16.x" + - run: npm install + - run: npm test diff --git a/index.js b/index.js index 89960ab..7bf4518 100644 --- a/index.js +++ b/index.js @@ -1,103 +1,93 @@ -import 'dotenv/config' -import express from 'express' -import fs from 'fs' -import { createServer } from 'https' -import fetch from 'node-fetch' +import "dotenv/config"; +import express from "express"; +import fetch from "node-fetch"; +export const app = express(); - -const key = fs.readFileSync('./certs/key.pem'); -const cert = fs.readFileSync('./certs/cert.pem'); -const app = express() -const server = createServer({key: key, cert: cert}, app) - -const baseUrl = 'https://api.podium.com/v4/' -const refresToken = process.env.REFRESHTOKEN -const clientID = process.env.CLIENTID -const clientSecret = process.env.CLIENTSECRET -let token +const baseUrl = "https://api.podium.com/v4/"; +const refreshToken = process.env.REFRESHTOKEN; +const clientID = process.env.CLIENTID; +const clientSecret = process.env.CLIENTSECRET; app.use(express.urlencoded()); app.use(express.json()); //Retrieve all contacts -app.get('/', async (req, res) => { - try { - const token = await getTokenID(); +app.get("/", async (_req, res) => { + try { + const token = await getTokenID(); - if (token) { - const requestAPI = await fetch(`${baseUrl}/contacts`, { - Method: 'GET', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${token}` - } - }) + if (token) { + const requestAPI = await fetch(`${baseUrl}/contacts`, { + Method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + }); - const reqResponse = await requestAPI.json(); - return res.send(reqResponse) - } - - } catch (error) { - console.error('Error', error) - return res.sendStatus(error.response.status) + const reqResponse = await requestAPI.json(); + return res.send(reqResponse); } - -}) + } catch (error) { + console.error("Error", error); + return res.sendStatus(error.response.status); + } +}); //Create a contact for a specified location -app.post('/', async (req, res) => { +app.post("/", async (req, res) => { try { const token = await getTokenID(); let request; if (token) { - request = await fetch(`${baseUrl}/contacts`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${token}` - }, - body: JSON.stringify(req.body) - }) + request = await fetch(`${baseUrl}/contacts`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify(req.body), + }); - const reqResponse = await request.json(); - return res.send(reqResponse) + const reqResponse = await request.json(); + return res.send(reqResponse); } else { - return(res.send('No authorization token was found.')) + return res.send("No authorization token was found."); } - } catch(error) { - console.error(error) - return res.send(error) + } catch (error) { + console.error(error); + return res.send(error); } -}) +}); -async function getTokenID() { +export async function getTokenID() { const bodyData = { - 'client_id': clientID, - 'client_secret':clientSecret, - 'grant_type':'refresh_token', - 'refresh_token': refresToken, - } + client_id: clientID, + client_secret: clientSecret, + grant_type: "refresh_token", + refresh_token: refreshToken, + }; try { - const tokenRequest = await fetch('https://accounts.podium.com/oauth/token', { - method: 'POST', + const tokenRequest = await fetch( + "https://accounts.podium.com/oauth/token", + { + method: "POST", headers: { - 'Content-Type': 'application/json', + "Content-Type": "application/json", }, - body: JSON.stringify(bodyData) - }) + body: JSON.stringify(bodyData), + } + ); const tokenResponse = await tokenRequest.json(); - if(tokenResponse) { + if (tokenResponse) { return tokenResponse.access_token; } } catch (error) { - console.error(`Error retrieveing a new token, ${error}`) - return error + console.error(`Error retrieving a new token, ${error}`); + return error; } - } - -server.listen(3000, () => {console.log('Server is listening on port 3000')}) \ No newline at end of file diff --git a/index.test.js b/index.test.js new file mode 100644 index 0000000..8b1a648 --- /dev/null +++ b/index.test.js @@ -0,0 +1,5 @@ +import { getTokenID } from "./index.js"; + +it("validates existence of getTokenID", async () => { + expect(typeof getTokenID).toBe("function"); +}); diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..bbf04d7 --- /dev/null +++ b/jest.config.js @@ -0,0 +1 @@ +export default { transform: {} }; diff --git a/package.json b/package.json index 8e60804..343e698 100644 --- a/package.json +++ b/package.json @@ -1,26 +1,32 @@ { - "name": "podium-api", - "version": "1.0.0", + "name": "podium-api-sample-contacts", + "version": "1.1.0", "description": "A Podium demo app for CRUD contacts", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "lint": "eslint . --ext .js", + "test": "node --experimental-vm-modules ./node_modules/.bin/jest" }, "type": "module", "repository": { "type": "git", - "url": "git+https://github.com/jairoadi/podium-api-demo.git" + "url": "https://github.com/podium/podium-api-sample-contacts.git" }, "author": "Jairo Franco", "license": "ISC", "bugs": { - "url": "https://github.com/jairoadi/podium-api-demo/issues" + "url": "https://github.com/podium/podium-api-sample-contacts/issues" }, - "homepage": "https://github.com/jairoadi/podium-api-demo#readme", + "homepage": "https://github.com/podium/podium-api-sample-contacts#readme", "dependencies": { "axios": "^0.27.2", "body-parser": "^1.20.0", "dotenv": "^16.0.1", + "eslint": "^8.45.0", + "express": "^4.18.2", "node-fetch": "^3.2.4" + }, + "devDependencies": { + "jest": "^29.6.1" } } diff --git a/server.js b/server.js new file mode 100644 index 0000000..6f15f63 --- /dev/null +++ b/server.js @@ -0,0 +1,11 @@ +import { app } from "./index.js"; +import fs from "fs"; +import { createServer } from "https"; + +const key = fs.readFileSync("./certs/key.pem"); +const cert = fs.readFileSync("./certs/cert.pem"); +const server = createServer({ key: key, cert: cert }, app); + +server.listen(3000, () => { + console.log("Server is listening on port 3000"); +});