Skip to content

Commit

Permalink
refactor the version 1 APIS
Browse files Browse the repository at this point in the history
  • Loading branch information
GauravWalia19 committed Jun 8, 2020
1 parent e5fdfae commit a413196
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<img src="public/icons/treasureApiLogo.png" title="treasure api"/>
<img src="public/icons/treasureApisLogo.png" title="treasure api"/>
<h1 align="center">TREASURE APIS</h1>
<h4 align="center">
<img alt="GitHub top language" src="https://img.shields.io/github/languages/top/techous/treasureapis">
Expand Down
18 changes: 18 additions & 0 deletions docs/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const http = require('http');

http.get('http://localhost:5000/api/v1/search?find=da', (resp) => {
let data = '';

// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});

// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log(JSON.parse(data));
});

}).on("error", (err) => {
console.log("Error: " + err.message);
});
Binary file added public/icons/treasureApisLogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
110 changes: 72 additions & 38 deletions routes/api/v1/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
const express = require('express');
const router = express.Router();
const Appbase = require("appbase-js");

// excluding dotenv config from production
if (process.env.NODE_ENV !== 'production') require('dotenv').config()

// appbase reference for connection
var appbaseRef = Appbase({
url: process.env.APPBASE_URL,
app: process.env.APPBASE_APP,
Expand All @@ -24,90 +26,121 @@ var appbaseRef = Appbase({

/**
* API:
* url: /api/v1/search
* method: GET
* @endpoint: /api/v1/search
* @url: https://treasurejsapi.herokuapp.com/api/v1/search?find=m
*
* @method GET
* @access public
* @author techous
*
* request: https://treasurejsapi.herokuapp.com/api/v1/search?find=m
* query @param find
* query @param size
* @return response
**/
router.get('/',(req,res)=>{
if(handleInvalidRequest(req, res)){
let find = req.query.find.toUpperCase();
let size = req.query.size;
callAppbaseAPIv1(res, find, "name", size);
const {find, size} = req.query;
callAppbaseAPIv1(res, find.toUpperCase(), "name", size);
}
});

/**
* API:
* url: /api/v1/search/description
* method: GET
* @endpoint: /api/v1/search/description
* @url: https://treasurejsapi.herokuapp.com/api/v1/search/description?find=mo
*
* request: https://treasurejsapi.herokuapp.com/api/v1/search/description?find=mo
* @method GET
* @access public
* @author techous
*
* query @param find
* query @param size
* @return response
**/
router.get('/description',(req,res)=>{
if(handleInvalidRequest(req, res)){
let find = req.query.find.toUpperCase();
let size = req.query.size;
callAppbaseAPIv1(res, find, "description", size);
const {find, size} = req.query;
callAppbaseAPIv1(res, find.toUpperCase(), "description", size);
}
});

/**
* API:
* url: /api/v1/search/docs
* method: GET
* @endpoint: /api/v1/search/docs
* @url: https://treasurejsapi.herokuapp.com/api/v1/search/docs?find=mo
*
* @method GET
* @access public
* @author techous
*
* request: https://treasurejsapi.herokuapp.com/api/v1/search/docs?find=mo
* query @param find
* query @param size
* @return response
**/
router.get('/docs',(req,res)=>{
if(handleInvalidRequest(req, res)){
let find = req.query.find.toUpperCase();
let size = req.query.size;
callAppbaseAPIv1(res, find, "docs", size);
const {find, size} = req.query;
callAppbaseAPIv1(res, find.toUpperCase(), "docs", size);
}
});

/**
* API:
* url: /api/v1/search/website
* method: GET
* @endpoint: /api/v1/search/website
* @url https://treasurejsapi.herokuapp.com/api/v1/search/website?find=https
*
* request: https://treasurejsapi.herokuapp.com/api/v1/search/website?find=https
* @method GET
* @access public
* @author techous
*
* query @param find
* query @param size
* @return response
**/
router.get('/website',(req,res)=>{
if(handleInvalidRequest(req, res)){
let find = req.query.find.toUpperCase();
let size = req.query.size;
callAppbaseAPIv1(res, find, "website", size);
const {find, size} = req.query;
callAppbaseAPIv1(res, find.toUpperCase(), "website", size);
}
});

/**
* API:
* url: /api/search/github
* method: GET
* @endpoint: /api/search/github
* @url: https://treasurejsapi.herokuapp.com/api/v1/search/github?find=react
*
* @method GET
* @access public
* @author techous
*
* request: https://treasurejsapi.herokuapp.com/api/v1/search/github?find=react
* query @param find
* query @param size
* @return response
**/
router.get('/github', (req, res)=>{
if(handleInvalidRequest(req, res)){
let find = req.query.find.toUpperCase();
let size = req.query.size;
callAppbaseAPIv1(res, find, "github", size);
const {find, size} = req.query;
callAppbaseAPIv1(res, find.toUpperCase(), "github", size);
}
})

/**
* API:
* url: /api/search/other
* method: GET
* request: https://treasurejsapi.herokuapp.com/api/v1/search/other?find=react
* @endpoint: /api/search/other
* @url: https://treasurejsapi.herokuapp.com/api/v1/search/other?find=react
*
* @method GET
* @access public
* @author techous
*
* query @param find
* query @param size
* @return response
**/
router.get('/other', (req, res)=>{
if(handleInvalidRequest(req, res)){
let find = req.query.find.toUpperCase();
let size = req.query.size;
callAppbaseAPIv1(res, find, "other", size);
const {find, size} = req.query;
callAppbaseAPIv1(res, find.toUpperCase(), "other", size);
}
})

Expand All @@ -116,11 +149,12 @@ router.get('/other', (req, res)=>{
*
* @param req request to handle
* @param res response to send back
* @return
**/
function handleInvalidRequest(req, res){
if(!req.query || !req.query.find || req.body.find === '') // if the request don't contain the find field
if(!req.query.find || req.query.find === '') // if the request don't contain the find field
{
res.status(404).json({message : "Request parameters invalid"});
res.status(400).json({message : "Request Parameters Invalid"});
return false;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion routes/api/v2/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const mongoose = require('mongoose');
// excluding dotenv config from production
if (process.env.NODE_ENV !== 'production') require('dotenv').config()

mongoose.connect(process.env.MONGO_DB, {useNewUrlParser: true});
mongoose.connect(process.env.MONGO_DB, {useNewUrlParser: true,useUnifiedTopology: true,useCreateIndex: true});

const db = mongoose.connection;
db.on('error',console.error.bind(console, 'Connection error:'));
Expand Down

0 comments on commit a413196

Please sign in to comment.