Skip to content

Commit 21681cb

Browse files
added Swagger-Openapispec todo:bringbackEC2
1 parent 64007aa commit 21681cb

File tree

6 files changed

+853
-190
lines changed

6 files changed

+853
-190
lines changed

apps/bank-webhook/package.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,27 @@
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",
6+
67
"scripts": {
7-
"build": "npx esbuild ./src/index.ts --bundle --platform=node --outfile=dist/index.js",
8-
"start": "node dist/index.js",
9-
"dev": "npm run build && npm run start"
10-
},
8+
"build": "tsc && npm run swagger:build",
9+
"start": "node dist/index.js",
10+
"dev": "npx ts-node ./src/index.ts",
11+
"swagger:build": "node scripts/generate-swagger.js"
12+
},
1113
"keywords": [],
1214
"author": "",
1315
"license": "ISC",
1416
"dependencies": {
1517
"@repo/db": "*",
1618
"@types/express": "^4.17.21",
17-
"esbuild": "^0.20.2",
19+
"esbuild": "^0.25.8",
1820
"express": "^4.19.1",
19-
"zod": "^3.23.8"
21+
"swagger-jsdoc": "^6.2.8",
22+
"swagger-ui-express": "^5.0.1",
23+
"zod": "^3.25.76"
24+
},
25+
"devDependencies": {
26+
"@types/swagger-jsdoc": "^6.0.4",
27+
"@types/swagger-ui-express": "^4.1.8"
2028
}
2129
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const swaggerJSDoc = require('swagger-jsdoc');
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
const swaggerOptions = {
6+
definition: {
7+
openapi: "3.0.0",
8+
info: {
9+
title: "VPay Bank Webhook API",
10+
version: "1.0.0",
11+
description: "API documentation for the bank webhook endpoint"
12+
}
13+
},
14+
apis: [path.join(__dirname, "../src/index.ts")], // Adjust if your file is elsewhere
15+
};
16+
17+
const swaggerSpec = swaggerJSDoc(swaggerOptions);
18+
fs.writeFileSync(path.join(__dirname, "../dist/swagger.json"), JSON.stringify(swaggerSpec, null, 2));
19+
console.log("Swagger JSON generated at dist/swagger.json");

apps/bank-webhook/src/index.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,49 @@ import express from "express";
22
import db from "@repo/db/client";
33
import { z } from "zod";
44

5-
5+
import swaggerUi from 'swagger-ui-express';
6+
import swaggerDocument from '../dist/swagger.json';
67
const app = express();
78

89
app.use(express.json())
910

11+
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
1012

1113
const paymentSchema = z.object({
1214
token: z.string(),
1315
user_identifier: z.string(),
1416
amount: z.string(),
1517
PaymentResponse:z.enum(["Success", "Failure"])
1618
});
19+
/**
20+
* @swagger
21+
* /hdfcWebhook:
22+
* post:
23+
* summary: HDFC webhook for payment response
24+
* requestBody:
25+
* required: true
26+
* content:
27+
* application/json:
28+
* schema:
29+
* type: object
30+
* properties:
31+
* token:
32+
* type: string
33+
* user_identifier:
34+
* type: string
35+
* amount:
36+
* type: string
37+
* PaymentResponse:
38+
* type: string
39+
* enum: [Success, Failure]
40+
* responses:
41+
* 200:
42+
* description: Payment recorded
43+
* 400:
44+
* description: Bad Request
45+
* 411:
46+
* description: Length Required
47+
*/
1748

1849
app.post("/hdfcWebhook", async (req, res) => {
1950

@@ -136,5 +167,7 @@ enum PaymentResponse {
136167
}
137168

138169
})
139-
console.log("server is running")
140-
app.listen(3003);
170+
app.listen(3003, () => {
171+
console.log("Server running at http://localhost:3003");
172+
console.log("Docs available at http://localhost:3003/api-docs");
173+
});

apps/bank-webhook/tsconfig.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
{
2-
"extends": "@repo/typescript-config/base.json",
2+
// "extends": "@repo/typescript-config/base.json",
33
"compilerOptions": {
4-
"outDir": "dist"
4+
"outDir": "dist",
5+
"module": "node16",
6+
"target": "es2019",
7+
"moduleResolution": "node16",
8+
"esModuleInterop": true,
9+
"strict": true,
10+
"skipLibCheck": true,
11+
"rootDir": "src",
12+
"resolveJsonModule": true,
513
},
614
"include": ["src"],
7-
"exclude": ["node_modules", "dist"]
15+
"exclude": ["node_modules"]
816
}

0 commit comments

Comments
 (0)