generated from jairoadi/podium-api-demo-contacts
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from rebecca-lindsey/add-linting-and-tests
Add linting and tests
- Loading branch information
Showing
7 changed files
with
139 additions
and
75 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,11 @@ | ||
module.exports = { | ||
env: { | ||
node: true, | ||
jest: true, | ||
}, | ||
extends: "eslint:recommended", | ||
parserOptions: { | ||
ecmaVersion: "latest", | ||
sourceType: "module", | ||
}, | ||
}; |
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,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 |
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 |
---|---|---|
@@ -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')}) |
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,5 @@ | ||
import { getTokenID } from "./index.js"; | ||
|
||
it("validates existence of getTokenID", async () => { | ||
expect(typeof getTokenID).toBe("function"); | ||
}); |
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 @@ | ||
export default { transform: {} }; |
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 |
---|---|---|
@@ -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" | ||
} | ||
} |
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,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"); | ||
}); |