first push - rest API with handlers attached to methods

This commit is contained in:
Kfir Dayan 2023-04-24 20:27:36 +03:00
commit 91ee1479b0
10 changed files with 1704 additions and 0 deletions

1
.env.example Normal file
View file

@ -0,0 +1 @@
GEOCODING_API_KEY='your_api_key'

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
node_modules
.env
dist

1590
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

26
package.json Normal file
View file

@ -0,0 +1,26 @@
{
"name": "dropit",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon dist/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/body-parser": "^1.19.2",
"@types/express": "^4.17.17",
"axios": "^1.3.6",
"body-parser": "^1.20.2",
"express": "^4.18.2"
},
"devDependencies": {
"@types/node": "^18.16.0",
"nodemon": "^2.0.22",
"ts-node-dev": "^2.0.0",
"typescript": "^5.0.4"
}
}

10
src/app.ts Normal file
View file

@ -0,0 +1,10 @@
import express from 'express';
import routes from './routes';
const app = express();
app.use(express.json());
app.use(routes);
export default app;

19
src/geocoding.ts Normal file
View file

@ -0,0 +1,19 @@
import axios from 'axios';
const dotenv = require('dotenv');
dotenv.config();
const GEOCODING_API_KEY = process.env.GEOCODING_API_KEY;
export interface Address {
street: string;
line1: string;
line2: string;
country: string;
postcode: string;
}
export const resolveAddress = async (searchTerm: string): Promise<Address> => {
const response = await axios.get(`https://api.geoapify.com/v1/geocode/search?text=${searchTerm}&apiKey=${GEOCODING_API_KEY}`);
return response.data.features[0].properties;
};

26
src/handlers.ts Normal file
View file

@ -0,0 +1,26 @@
import { Request, Response } from 'express';
export const resolveAddressHandler = (req: Request, res: Response) => {
// TODO: Implement resolve address functionality using
};
export const timeslotsHandler = (req: Request, res: Response) => {
// TODO: Implement timeslots functionality
};
export const deliveriesHandler = (req: Request, res: Response) => {
// TODO: Implement deliveries functionality
};
export const cancelDeliveryHandler = (req: Request, res: Response) => {
// TODO: Implement cancel delivery functionality
};
export const dailyDeliveriesHandler = (req: Request, res: Response) => {
// TODO: Implement daily deliveries functionality
};
export const weeklyDeliveriesHandler = (req: Request, res: Response) => {
// TODO: Implement weekly deliveries functionality
};

7
src/index.ts Normal file
View file

@ -0,0 +1,7 @@
import app from './app';
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

13
src/routes.ts Normal file
View file

@ -0,0 +1,13 @@
import express from 'express';
import { resolveAddressHandler, timeslotsHandler, deliveriesHandler, cancelDeliveryHandler, dailyDeliveriesHandler, weeklyDeliveriesHandler } from './handlers';
const router = express.Router();
router.post('/resolve-address', resolveAddressHandler);
router.get('/deliveries/daily', dailyDeliveriesHandler);
router.get('/deliveries/weekly', weeklyDeliveriesHandler);
router.post('/timeslots', timeslotsHandler);
router.post('/deliveries', deliveriesHandler);
router.delete('/deliveries/:id', cancelDeliveryHandler);
export default router;

9
tsconfig.json Normal file
View file

@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "dist",
"esModuleInterop": true
},
"include": ["src/**/*"]
}