Skip to content

Commit 0dbd8a1

Browse files
committed
Support for Hapi
1 parent 28d283a commit 0dbd8a1

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

README.md

+39-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ The list of supported frameworks matches [in-process-request](https://github.com
88
* Express.js v3
99
* Express.js v4
1010
* Express.js v5
11+
* Apollo Server v2
12+
* Hapi v19
1113
* Connect v3
1214
* Koa v2
1315

@@ -42,6 +44,7 @@ An additional header is injected into the request
4244
$ npm install lambda-request-handler
4345
```
4446

47+
### Express.js
4548
```javascript
4649
const express = require('express')
4750
const lambdaRequestHandler = require('lambda-request-handler')
@@ -63,7 +66,7 @@ module.exports = { handler }
6366

6467
If the above file in your Lambda source was called `index.js` then the name of the handler in the Lambda configuration is `index.handler`
6568

66-
### Advanced example with asynchronous setup
69+
#### Advanced example with asynchronous setup
6770

6871
Sometimes the application needs to read configuration from remote source before it can start processing requests. For example it may need to decrypt some secrets managed by KMS. For this use case a special helper `deferred` has been provided. It takes a factory function which returns a Promise that resolves to the app instance. The factory function will be called only once.
6972

@@ -95,3 +98,38 @@ const handler = lambdaRequestHandler.deferred(myAppPromise);
9598
module.exports = { handler }
9699

97100
```
101+
102+
### Hapi
103+
```javascript
104+
const Hapi = require('@hapi/hapi')
105+
const lambdaRequestHandler = require('lambda-request-handler')
106+
107+
// create custom listener for Hapi
108+
const myListener = new lambdaRequestHandler.HapiListener()
109+
110+
// Pass the custom listener to Hapi.server
111+
const server = Hapi.server({
112+
listener: myListener
113+
});
114+
115+
server.route({
116+
method: 'GET',
117+
path: '/',
118+
handler: (_request: any, _h: any) => {
119+
return 'Hello World!';
120+
}
121+
});
122+
123+
const myAppPromise = async () => {
124+
//wait for the server to initialize
125+
await server.start()
126+
// return the request listener function
127+
return myListener.handler
128+
};
129+
130+
const handler = lambdaRequestHandler.deferred(myAppPromise);
131+
132+
module.exports = { handler }
133+
```
134+
135+
If the above file in your Lambda source was called `index.js` then the name of the handler in the Lambda configuration is `index.handler`

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"main": "./dist/compile/lambda",
3535
"types": "./dist/compile/lambda.d.ts",
3636
"dependencies": {
37-
"in-process-request": "^0.0.10"
37+
"in-process-request": "^0.1.0"
3838
},
3939
"devDependencies": {
4040
"@types/compression": "^1.7.0",
@@ -48,7 +48,7 @@
4848
"express": "^4.17.1",
4949
"jest": "^25.5.4",
5050
"ts-jest": "^25.4.0",
51-
"ts-node": "^8.9.1",
51+
"ts-node": "^8.10.1",
5252
"typescript": "^3.8.3"
5353
}
5454
}

src/lambda.ts

+1
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ const handlerBuilder = (appFn: PromiseFactory<RequestListener>): handler.APIGate
4545

4646
const handler = (app: RequestListener) => handlerBuilder(() => Promise.resolve(app));
4747
handler.deferred = handlerBuilder;
48+
handler.HapiListener = inProcessRequestHandler.HapiListener;
4849

4950
export = handler;

yarn.lock

+8-8
Original file line numberDiff line numberDiff line change
@@ -1867,10 +1867,10 @@ imurmurhash@^0.1.4:
18671867
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
18681868
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
18691869

1870-
in-process-request@^0.0.10:
1871-
version "0.0.10"
1872-
resolved "https://registry.yarnpkg.com/in-process-request/-/in-process-request-0.0.10.tgz#40b7a7b7eaa05bd9f7e18effdfea6fe0f553bb13"
1873-
integrity sha512-vIoWQMYdzUlx94ElQzy6Ksvq3C+xrneU2+/nIvbPZxeYWkei/stC5eevZAdO88iNRbw82ur1d1Vc67tJhB+C6w==
1870+
in-process-request@^0.1.0:
1871+
version "0.1.0"
1872+
resolved "https://registry.yarnpkg.com/in-process-request/-/in-process-request-0.1.0.tgz#b4eb82eb2607e5fc8357f1fb755ccb1aac44dad4"
1873+
integrity sha512-aAGFWPFL0PCP5NFq9XtLMmECHzXGd0pdB+QwzzrYYmPrKMIRGFs/dQbkcurf/1dQiitXnfElz0eqbcFOv7rM6A==
18741874

18751875
inflight@^1.0.4:
18761876
version "1.0.6"
@@ -3764,10 +3764,10 @@ ts-jest@^25.4.0:
37643764
semver "6.x"
37653765
yargs-parser "18.x"
37663766

3767-
ts-node@^8.9.1:
3768-
version "8.9.1"
3769-
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.9.1.tgz#2f857f46c47e91dcd28a14e052482eb14cfd65a5"
3770-
integrity sha512-yrq6ODsxEFTLz0R3BX2myf0WBCSQh9A+py8PBo1dCzWIOcvisbyH6akNKqDHMgXePF2kir5mm5JXJTH3OUJYOQ==
3767+
ts-node@^8.10.1:
3768+
version "8.10.1"
3769+
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.10.1.tgz#77da0366ff8afbe733596361d2df9a60fc9c9bd3"
3770+
integrity sha512-bdNz1L4ekHiJul6SHtZWs1ujEKERJnHs4HxN7rjTyyVOFf3HaJ6sLqe6aPG62XTzAB/63pKRh5jTSWL0D7bsvw==
37713771
dependencies:
37723772
arg "^4.1.0"
37733773
diff "^4.0.1"

0 commit comments

Comments
 (0)