From 2ec6fe390594557e99c1a21ca01a5d5a8721ea5c Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 13:28:36 -0600 Subject: [PATCH 01/17] Separate server and index --- index.js | 128 +++++++++++++++++++++++++----------------------------- server.js | 11 +++++ 2 files changed, 70 insertions(+), 69 deletions(-) create mode 100644 server.js diff --git a/index.js b/index.js index 89960ab..0686d2f 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 refresToken = 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: refresToken, + }; 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 retrieveing 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/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"); +}); From 89fd4b342f8407fc253b0ac4b817f510cc9d804a Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 13:28:45 -0600 Subject: [PATCH 02/17] Add basic test --- index.test.js | 5 +++++ jest.config.js | 1 + package.json | 14 +++++++++----- 3 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 index.test.js create mode 100644 jest.config.js 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..4e8f74a 100644 --- a/package.json +++ b/package.json @@ -1,26 +1,30 @@ { - "name": "podium-api", + "name": "podium-api-sample-contacts", "version": "1.0.0", "description": "A Podium demo app for CRUD contacts", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "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", + "express": "^4.18.2", "node-fetch": "^3.2.4" + }, + "devDependencies": { + "jest": "^29.6.1" } } From 63e998f32bf818b15898aff13dcc1b9b071626ed Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 13:34:09 -0600 Subject: [PATCH 03/17] Add linting --- .eslintrc.cjs | 24 ++++++++++++++++++++++++ package.json | 2 ++ 2 files changed, 26 insertions(+) create mode 100644 .eslintrc.cjs diff --git a/.eslintrc.cjs b/.eslintrc.cjs new file mode 100644 index 0000000..4fd2da7 --- /dev/null +++ b/.eslintrc.cjs @@ -0,0 +1,24 @@ +module.exports = { + env: { + browser: true, + es2021: true, + }, + extends: "eslint:recommended", + overrides: [ + { + env: { + jest: true, + node: true, + }, + files: [".eslintrc.{js,cjs}"], + parserOptions: { + sourceType: "script", + }, + }, + ], + parserOptions: { + ecmaVersion: "latest", + sourceType: "module", + }, + rules: {}, +}; diff --git a/package.json b/package.json index 4e8f74a..7ff78fe 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "A Podium demo app for CRUD contacts", "main": "index.js", "scripts": { + "lint": "eslint . --ext .js", "test": "node --experimental-vm-modules ./node_modules/.bin/jest" }, "type": "module", @@ -21,6 +22,7 @@ "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" }, From 82529100152adcd445df8f54d1e55e9c45143c95 Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 13:56:02 -0600 Subject: [PATCH 04/17] Correct estlintrc file --- .eslintrc.cjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 4fd2da7..622739c 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -10,9 +10,9 @@ module.exports = { jest: true, node: true, }, - files: [".eslintrc.{js,cjs}"], + files: ["**/*.test.js", "**/*.test.jsx", ".eslintrc.{js,cjs}"], parserOptions: { - sourceType: "script", + sourceType: "module", }, }, ], From 79e3db1acd6bab14297df149a0b5d5fe2064c5ec Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 14:00:51 -0600 Subject: [PATCH 05/17] Remove unnecessary configs from eslintrc --- .eslintrc.cjs | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 622739c..61b9887 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -1,24 +1,11 @@ module.exports = { env: { - browser: true, - es2021: true, + node: true, + jest: true, }, extends: "eslint:recommended", - overrides: [ - { - env: { - jest: true, - node: true, - }, - files: ["**/*.test.js", "**/*.test.jsx", ".eslintrc.{js,cjs}"], - parserOptions: { - sourceType: "module", - }, - }, - ], parserOptions: { ecmaVersion: "latest", sourceType: "module", }, - rules: {}, }; From 64c595fe0d83292fe21d8220ac08ffd39e4d267a Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 14:08:30 -0600 Subject: [PATCH 06/17] Add ci workflow --- .github/workflows/ci.yml | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..ab46696 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,36 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + setup: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Use Node.js + uses: actions/setup-node@v3 + with: + node-version: "16.x" + - run: npm ci + - run: npm run build --if-present + + test: + runs-on: ubuntu-latest + + needs: setup + + steps: + - run: npm test + + lint: + runs-on: ubuntu-latest + + needs: setup + + steps: + - run: npm run lint From 268bcb1c4b26e7e2f87f4b9bcf4648eab7dbc2b8 Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 14:13:52 -0600 Subject: [PATCH 07/17] Update when CI yml will be triggered --- .github/workflows/ci.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ab46696..ea4754f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,10 +1,12 @@ name: CI on: - push: - branches: [main] pull_request: - branches: [main] + branches: + - main + push: + branches: + - main jobs: setup: From 6ac31cc24ed408254b593fea6d3a1d65c81900cb Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 14:18:33 -0600 Subject: [PATCH 08/17] Test alternate ci triggers --- .github/workflows/ci.yml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ea4754f..72edb1e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,12 +1,13 @@ name: CI on: - pull_request: - branches: - - main - push: - branches: - - main + [workflow_dispatch] + # pull_request: + # branches: + # - main + # push: + # branches: + # - main jobs: setup: From 7f07a6299d8009cebfa5cf7d78cf75f643c3be6c Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 14:28:30 -0600 Subject: [PATCH 09/17] Swap CI settings back --- .github/workflows/ci.yml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 72edb1e..ea4754f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,13 +1,12 @@ name: CI on: - [workflow_dispatch] - # pull_request: - # branches: - # - main - # push: - # branches: - # - main + pull_request: + branches: + - main + push: + branches: + - main jobs: setup: From acda95b72459a495b67208f9bc4a5f385b296775 Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 14:31:50 -0600 Subject: [PATCH 10/17] Correct typos --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 0686d2f..7bf4518 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ import fetch from "node-fetch"; export const app = express(); const baseUrl = "https://api.podium.com/v4/"; -const refresToken = process.env.REFRESHTOKEN; +const refreshToken = process.env.REFRESHTOKEN; const clientID = process.env.CLIENTID; const clientSecret = process.env.CLIENTSECRET; @@ -66,7 +66,7 @@ export async function getTokenID() { client_id: clientID, client_secret: clientSecret, grant_type: "refresh_token", - refresh_token: refresToken, + refresh_token: refreshToken, }; try { @@ -87,7 +87,7 @@ export async function getTokenID() { return tokenResponse.access_token; } } catch (error) { - console.error(`Error retrieveing a new token, ${error}`); + console.error(`Error retrieving a new token, ${error}`); return error; } } From a55e7f660aec6ef6d3a7ed4942caf54327542931 Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 14:36:01 -0600 Subject: [PATCH 11/17] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7ff78fe..343e698 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "podium-api-sample-contacts", - "version": "1.0.0", + "version": "1.1.0", "description": "A Podium demo app for CRUD contacts", "main": "index.js", "scripts": { From 52b56544fa67388bd2d786ae356792f9ba487cbb Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 14:54:23 -0600 Subject: [PATCH 12/17] Fix CI pipeline --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ea4754f..e288a0d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: uses: actions/setup-node@v3 with: node-version: "16.x" - - run: npm ci + - run: npm install - run: npm run build --if-present test: From 9c1e3c562ed0d761551ede4242859336ef9053fb Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 15:02:45 -0600 Subject: [PATCH 13/17] Adjust ci --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e288a0d..2933615 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,13 +13,13 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Use Node.js + - name: "Checkout repository" + uses: actions/checkout@v3 + - name: "Use Node.js" uses: actions/setup-node@v3 with: node-version: "16.x" - run: npm install - - run: npm run build --if-present test: runs-on: ubuntu-latest From 892d2b6b6e2c25c1bd4e577e44dbba499a88eb43 Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 15:05:50 -0600 Subject: [PATCH 14/17] Adjust ci to not persist credentials --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2933615..f9e89da 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,6 +15,8 @@ jobs: steps: - name: "Checkout repository" uses: actions/checkout@v3 + with: + persist-credentials: false - name: "Use Node.js" uses: actions/setup-node@v3 with: From e2e6d9d9805f4d762a6e8ed0ed30638392bc54cb Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 15:10:04 -0600 Subject: [PATCH 15/17] Add additional steps to CI --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f9e89da..eae0d9d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,6 +22,10 @@ jobs: with: node-version: "16.x" - run: npm install + - name: npm init + run: npm init -y + - name: Run build task + run: npm run build --if-present test: runs-on: ubuntu-latest From c6c6c9f474b98c847d68fd978936c53350e9f4bb Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 15:11:50 -0600 Subject: [PATCH 16/17] Attempt consolidating jobs --- .github/workflows/ci.yml | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eae0d9d..807d1fa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,23 +22,21 @@ jobs: with: node-version: "16.x" - run: npm install - - name: npm init - run: npm init -y - - name: Run build task - run: npm run build --if-present + - run: npm test + - run: npm run lint - test: - runs-on: ubuntu-latest + # test: + # runs-on: ubuntu-latest - needs: setup + # needs: setup - steps: - - run: npm test + # steps: + # - run: npm test - lint: - runs-on: ubuntu-latest + # lint: + # runs-on: ubuntu-latest - needs: setup + # needs: setup - steps: - - run: npm run lint + # steps: + # - run: npm run lint From e0bfff264f8cf00d2324342222f84b3fbae7353b Mon Sep 17 00:00:00 2001 From: Rebecca Lindsey Date: Fri, 14 Jul 2023 15:15:11 -0600 Subject: [PATCH 17/17] Split up jobs --- .github/workflows/ci.yml | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 807d1fa..c2bb7fd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,7 +9,7 @@ on: - main jobs: - setup: + lint: runs-on: ubuntu-latest steps: @@ -22,21 +22,19 @@ jobs: with: node-version: "16.x" - run: npm install - - run: npm test - run: npm run lint - # test: - # runs-on: ubuntu-latest - - # needs: setup - - # steps: - # - run: npm test - - # lint: - # runs-on: ubuntu-latest - - # needs: setup + test: + runs-on: ubuntu-latest - # steps: - # - run: npm run lint + 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