Skip to content

Commit

Permalink
chore: rate limit for api (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
shivam-sharma7 authored Oct 20, 2024
1 parent f887c10 commit 79214d1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ This is a lightweight and timezone-aware date utility package built on top of th
- Calculate the time difference (in hours) between two timezones.
- Supports for both JavaScript and TypeScript developer.

There are many more features available in the package.

## Download/install

[Download/install instructions](https://shivam-sharma7.github.io/world-clockify/#/./guide/installation)

## Documentation

- [General documentation](https://shivam-sharma7.github.io/world-clockify/#/)
- [API documentation](https://shivam-sharma7.github.io/world-clockify/#/./api/Introduction)

## Development

Expand Down
16 changes: 16 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"dotenv": "^16.4.5",
"express": "^4.21.1",
"express-rate-limit": "^7.4.1",
"world-clockify": "^1.4.3"
},
"devDependencies": {
Expand Down
10 changes: 10 additions & 0 deletions api/src/middleware/ratelimit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { rateLimit } from 'express-rate-limit';

export const limiter = rateLimit({
windowMs: 5 * 60 * 1000, // 5 minutes
limit: 10, // each IP can make up to 10 requests per `windowsMs` (5 minutes)
standardHeaders: true, // add the `RateLimit-*` headers to the response
legacyHeaders: false, // remove the `X-RateLimit-*` headers from the response
message: 'Too many requests, please try again later',
statusCode: 429,
});
13 changes: 7 additions & 6 deletions api/src/routes/timezone.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ import {
fetchSupportedCalendar,
countDownToEvent,
} from '../controllers/timezone.controller';
import { limiter } from '../middleware/ratelimit';

const router = express.Router();

router.get('/currenttime', fetchCurrentTimeInZone);
router.get('/supportedtimezone', fetchSupportedTimezones);
router.get('/supportedtimezone', limiter, fetchSupportedTimezones);
router.get('/currency', fetchSupportedCrrency);
router.get('/calendar', fetchSupportedCalendar);
router.get('/converttimezone', fetchConvertedTime);
router.get('/converttimezone', limiter, fetchConvertedTime);
router.get('/timedifference', fetchTimeDifference);
router.get('/calculateduration', calculateDurationBetweenDates);
router.get('/formattimezone', formatDateInTimezone);
router.get('/formatdatelocale', formatDateLocale);
router.get('/countdownevent', countDownToEvent);
router.get('/calculateduration', limiter, calculateDurationBetweenDates);
router.get('/formattimezone', limiter, formatDateInTimezone);
router.get('/formatdatelocale', limiter, formatDateLocale);
router.get('/countdownevent', limiter, countDownToEvent);

export default router;

0 comments on commit 79214d1

Please sign in to comment.