Skip to content

Commit b8a08b8

Browse files
author
Gonzalo Garcia Jaubert
authored
Merge pull request #5 from Optum/multipart
Multipart form-data requests
2 parents 4452d93 + daa36e2 commit b8a08b8

34 files changed

+741
-258
lines changed

README.md

Lines changed: 108 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Booster rocket webhook Azure
22

33
## Description
4-
This Rocket adds a Webhook to your Booster application. When the webhook is called, a function will be executed in your Booster application with request as a parameter.
4+
This Rocket adds HTTP endpoints to your Booster application. When an endpoint is called, a function will be executed in your Booster application with request as a parameter.
55

66
## Supported Providers
77

@@ -19,11 +19,11 @@ Add your rocket to your application in the configuration file.
1919
function buildBoosterWebhook(config: BoosterConfig): BoosterWebhook {
2020
return new BoosterWebhook(config, [
2121
{
22-
origin: 'test',
22+
route: 'test',
2323
handlerClass: TestHandler,
2424
},
2525
{
26-
origin: 'other',
26+
route: 'clients/other',
2727
handlerClass: FacebookHandler,
2828
},
2929
])
@@ -44,8 +44,15 @@ For Local
4444

4545
### Parameters
4646

47-
* origin: Identify the webhook. It will be also the name of the endpoint that will be created
47+
* route: The endpoint that will be created. It is possible to define nested routes.
4848
* handlerClass: A class with a `handle` method to handle the request
49+
* multiPartConfig: multipart/form-data configuration
50+
* defParamCharset: the default character set to use for values of part header parameters (e.g. filename)
51+
* preservePath: If paths in filenames from file parts in a 'multipart/form-data' request shall be preserved.
52+
* limits:
53+
* fileSize: the max file size (in bytes)
54+
* files: the max number of file fields.
55+
* parts: the max number of parts (fields + files).
4956

5057
The `handle` method should be like this one:
5158

@@ -60,15 +67,16 @@ export class TestHandler {
6067
throw new InvalidParameterError("Error message");
6168
}
6269
return Promise.resolve({
63-
body: { name: "my_name" }
70+
body: { name: "my_name" },
71+
responseType: WebhookResponseType.json,
6472
});
6573
}
6674
}
6775
```
6876

6977
## Return type
7078

71-
Handle methods return a promise of WebhookHandlerReturnType or void. This object contains the headers and body to be returned as response. Example:
79+
Handle methods return a promise of WebhookHandlerReturnType or void. This object contains the headers and body to be returned as response. You need to specify the responseType. Example:
7280

7381
```typescript
7482
public static async handle(
@@ -80,10 +88,55 @@ Handle methods return a promise of WebhookHandlerReturnType or void. This object
8088
headers: {
8189
Test: 'test header',
8290
},
91+
responseType: WebhookResponseType.text,
8392
})
8493
}
8594
```
8695

96+
To return a file, set the responseType to `WebhookResponseType.file`. Specify the name and mime type in the `file` field.
97+
98+
Example:
99+
100+
```typescript
101+
const result: WebhookHandlerReturnType = {
102+
body: file,
103+
file: {
104+
name: '1.txt',
105+
mimeType: WebhookResponseType.text,
106+
},
107+
responseType: WebhookResponseType.file,
108+
}
109+
```
110+
111+
## Authorization
112+
113+
To define with roles can access each point use the `authorize` configuration parameter as the `authorize` parameter in Booster.
114+
115+
Example:
116+
117+
```typescript
118+
{
119+
route: 'all',
120+
handlerClass: AllHandlerDispatcher,
121+
authorize: 'all',
122+
},
123+
{
124+
route: 'test/roles',
125+
handlerClass: AdminHandlerDispatcher,
126+
authorize: [Admin],
127+
},
128+
{
129+
route: 'custom',
130+
handlerClass: CustomHandlerDispatcher,
131+
authorize: (currentUser?: UserEnvelope, request?: WebhookRequest): Promise<void> => {
132+
if (currentUser?.username !== 'test@test.com') {
133+
return Promise.reject('Unexpected user')
134+
}
135+
return Promise.resolve()
136+
},
137+
},
138+
```
139+
87140
## Usage
88141

89142
```shell
@@ -113,6 +166,55 @@ The webhookEventInterface object will be similar to this one:
113166
}
114167
```
115168

169+
170+
## Multipart/form-data requests
171+
172+
Endpoints accepts multipart/form-data requests. The `WebhookEvent` will contain a new `multiPart` object with the files and fields in the request. Example:
173+
174+
```json
175+
{
176+
origin: 'test',
177+
method: 'POST',
178+
url: '/test?param1=testvalue',
179+
originalUrl: '/webhook/test?param1=testvalue',
180+
headers: {
181+
accept: '*/*',
182+
'cache-control': 'no-cache',
183+
host: 'localhost:3000',
184+
'accept-encoding': 'gzip, deflate, br',
185+
connection: 'keep-alive',
186+
'content-length': '0'
187+
},
188+
query: {
189+
param1: 'testvalue'
190+
},
191+
params: {},
192+
rawBody: undefined,
193+
body: {},
194+
multipart: {
195+
"files": [
196+
{
197+
name: "file1",
198+
bufferFile: Buffer(11),
199+
filename: "file1.txt",
200+
encoding: "7bit",
201+
mimeType: "text/plain"
202+
}
203+
],
204+
"fields": [
205+
{
206+
name: "parameterName",
207+
value: "value",
208+
nameTruncated: false,
209+
valueTruncated: false,
210+
encoding: "7bit",
211+
...
212+
}
213+
]
214+
}
215+
}
216+
```
217+
116218
## Compile
117219

118220
> npm install

0 commit comments

Comments
 (0)