Skip to content

Commit

Permalink
[Feature] : Baseline Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SwagataChaudhuri committed Feb 25, 2024
0 parents commit 6b92645
Show file tree
Hide file tree
Showing 26 changed files with 6,791 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TIMEOUT_IN_MILLISECONDS=<TIMEOUT_IN_MILLISECONDS>
25 changes: 25 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
45 changes: 45 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Build

on:
push:
branches:
- main
- develop
- feature/*
pull_request:
types:
- opened
- synchronize
- reopened
workflow_dispatch:

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Step 1 -> Code Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Step 2 -> Setup Node JS
uses: actions/setup-node@v3
with:
node-version: "18"
- name: Step 3 -> Cache Node JS Packages
uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-node-
${{ runner.OS }}-
- name: Step 4 -> Install packages
run: npm install --registry=https://registry.npmjs.org
- name: Step 5 -> Run Tests & Generate Coverqge Report
run: npm run test:cov
- name: Step 6 -> Upload Coverage Report
run: |
bash <(curl -s https://codecov.io/bash)
env:
CODECOV_TOKEN: ${{secrets.CODECOV_TOKEN}}
39 changes: 39 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Compiled Output
/dist
/node_modules

# Environment Secrets
.env
secrets.json

# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# OS
.DS_Store

# Tests
/coverage
/.nyc_output

# IDEs and Editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node-terminal",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"cwd": "${workspaceRoot}",
"command": "npm run start:dev",
}
]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Swagata Chaudhuri

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
105 changes: 105 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# **NestJS - Timeout**

![Compile and build](https://github.com/SwagataChaudhuri/NestJS-Timeout/actions/workflows/build.yml/badge.svg)
[![codecov](https://codecov.io/github/SwagataChaudhuri/NestJS-Timeout/branch/main/graph/badge.svg?token=B0TFMBOQM2)](https://codecov.io/github/SwagataChaudhuri/NestJS-AzureAD-Authentication)
![Prettier](https://img.shields.io/badge/Code%20style-prettier-informational?logo=prettier&logoColor=white)
[![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](./LICENSE)

Sample implementation to demonstrate gateway timeout in NestJS via interceptors.

---

## **Description**

This repository contains a sample implementation of `NestJS` gateway timeout via interceptors. The application exposes a simple REST API which has two endpoints.
The first endpoint `/greetings` is configured to return a response immediately.
The second endpoint `/greetings/delayed` is configured to return a respose with a random delay between 1 to 5 seconds. In case the delay is more than the value of `TIMEOUT_IN_MILLISECONDS`, the application will return a `504 Gateway Timeout` error.

---

## **Installation**

```bash
$ pnpm install
```
---

## **Configuration**

This application has a .env.example file. Please create a .env file and update the values as per the desired requirement. The below table describes the environment variables used in the application:

| Environment Variable | Description |
| -------------------------- | ------------------------------ |
| `TIMEOUT_IN_MILLISECONDS` | Timeout Value in Milliseconds |

---

## **Running the application**

The application can be run in two modes, `development` and `production`. The `development` mode supports hot reloading which is beneficial during development. The `production` mode is optimized for performance.

### **Development Mode**

Execute the below command to run the application in development mode:

```bash
$ pnpm run start:dev
```

### **Production Mode**

Execute the below command to run the application in production mode:

```
$ pnpm run start:prod
```

## **Testing**

The application has tests configured using `Jest`. The tests are located in the `test` directory. The tests are also configured to generate coverage reports. The coverage reports are generated in the `coverage` directory.

### **Unit Tests**

Execute the below command to run the unit tests:

```bash
$ pnpm run test
```
### **Coverage Reports**

Execute the below command to run the tests and generate the coverage reports:

```bash
$ pnpm run test:cov
```
---

## **License**

The application and all associated source code are distributed under the [MIT License](LICENSE).

---

## **Author**

[Swagata Chaudhuri]()

---

## **Support**

In case you find the project helpful, please consider supporting by ⭐ the project.

---

## **Contributing**

Contributions are welcome! Please feel free to submit a Pull Request in case you find any issues with the code.

---

## **Acknowledgements**

- [NestJS](https://nestjs.com/)
- [Jest](https://jestjs.io/)
- [TypeScript](https://www.typescriptlang.org/)
10 changes: 10 additions & 0 deletions nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true,
"builder": "swc",
"typeCheck": true
}
}
99 changes: 99 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"name": "nestjs-timeout",
"version": "1.0.0",
"description": "nestjs gateway timeout implementation using interceptors",
"author": "swagata chaudhuri",
"private": true,
"license": "MIT",
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^10.3.3",
"@nestjs/config": "^3.2.0",
"@nestjs/core": "^10.3.3",
"@nestjs/platform-express": "^10.3.3",
"@nestjs/swagger": "^7.3.0",
"@nestjs/terminus": "^10.2.3",
"reflect-metadata": "^0.2.1",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@nestjs/cli": "^10.3.2",
"@nestjs/schematics": "^10.1.1",
"@nestjs/testing": "^10.3.3",
"@swc/cli": "^0.3.9",
"@swc/core": "^1.4.2",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.20",
"@types/supertest": "^6.0.2",
"@typescript-eslint/eslint-plugin": "^7.0.2",
"@typescript-eslint/parser": "^7.0.2",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"source-map-support": "^0.5.21",
"supertest": "^6.3.4",
"ts-jest": "^29.1.2",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.3.3"
},
"jest": {
"testTimeout": 200000,
"modulePaths": [
"<rootDir>"
],
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"coverageReporters": [
"html",
"text",
"cobertura"
],
"coveragePathIgnorePatterns": [
"<rootDir>/dist/",
".*\\main\\.ts",
".module.ts$",
".spec.ts$"
],
"coverageDirectory": "../coverage",
"rootDir": "src",
"testRegex": [
".*\\.spec\\.ts$",
".*\\.e2e-spec\\.ts$"
],
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s",
"!**/node_modules/**",
"!**/dist/**",
"!**/*.dto.(t|j)s",
"!**/*.stub.(t|j)s",
"!**/*.strategy.(t|j)s",
"!**/*.decorator.(t|j)s",
"!**/*.interceptor.(t|j)s"
],
"testEnvironment": "node"
}
}
Loading

0 comments on commit 6b92645

Please sign in to comment.