Skip to content

Commit

Permalink
Merge pull request #2 from rebecca-lindsey/add-linting-and-tests
Browse files Browse the repository at this point in the history
Add linting and tests
  • Loading branch information
koobob authored Jul 14, 2023
2 parents 76d6c3d + e0bfff2 commit e2a6df8
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 75 deletions.
11 changes: 11 additions & 0 deletions .eslintrc.cjs
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",
},
};
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
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
128 changes: 59 additions & 69 deletions index.js
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')})
5 changes: 5 additions & 0 deletions index.test.js
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");
});
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default { transform: {} };
18 changes: 12 additions & 6 deletions package.json
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"
}
}
11 changes: 11 additions & 0 deletions server.js
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");
});

0 comments on commit e2a6df8

Please sign in to comment.