Skip to content

feat: PDF upload handling example. applicable to other file uploads. #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions typescript/inbound/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ yarn-error.log*
# Ignore OS-specific files
.DS_Store
Thumbs.db

/uploads/*.pdf
7 changes: 7 additions & 0 deletions typescript/inbound/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ cd hookdeck-ts-inbound
npm i
npm run dev
```

## Test uploading files

```sh
curl --location 'YOUR_HOOKDECK_URL' \
--form 'pdf=@"example-files/test.pdf"'
```
Binary file added typescript/inbound/example-files/test.pdf
Binary file not shown.
31 changes: 30 additions & 1 deletion typescript/inbound/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare module 'express-serve-static-core' {

import dotenv from "dotenv";
import { verifyWebhookSignature } from "@hookdeck/sdk/webhooks";
import multer from "multer";

dotenv.config();

Expand Down Expand Up @@ -44,11 +45,39 @@ app.use(express.json({

const port = process.env.PORT || 3030;

// PDF upload handling example
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads')
},
filename: function (req, file, cb) {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
cb(null, `${file.fieldname}-${uniqueSuffix}.${file.originalname.split('.').pop()}`)
}
})

const upload = multer({ storage: storage })

app.post("/uploads", verify, upload.single('pdf'), (req: Request, res: Response) => {
console.log({
webhook_received: new Date().toISOString(),
path: req.path,
body: req.body,
});

const pdf = req.body.pdf;
console.log({ pdf });

res.json({ status: "ACCEPTED" });
});

// Catch all general example

app.post("*", verify, (req: Request, res: Response) => {
console.log({
webhook_received: new Date().toISOString(),
path: req.path,
body: req.body
body: req.body,
});

res.json({ status: "ACCEPTED" });
Expand Down
163 changes: 159 additions & 4 deletions typescript/inbound/package-lock.json

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

2 changes: 2 additions & 0 deletions typescript/inbound/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
"@hookdeck/sdk": "^0.4.0",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"multer": "^1.4.5-lts.1",
"nodemon": "^3.0.1"
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/multer": "^1.4.12",
"ts-node-dev": "^2.0.0",
"typescript": "^5.2.2"
}
Expand Down
Empty file.