Skip to content

Commit e4eccb5

Browse files
authored
Merge pull request #38 from dbartholomae/use_strict_types
Use strict types
2 parents ec210fa + 1b584f1 commit e4eccb5

27 files changed

+372
-79
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ jobs:
6363
env:
6464
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
6565
- name: Bump versions
66-
run: node ./common/scripts/install-run-rush.js rush version --bump --target-branch main
66+
run: node ./common/scripts/install-run-rush.js version --bump --target-branch main
6767
- name: publish
6868
run: node ./common/scripts/install-run-rush.js publish -a -b main -p --set-access-level public --include-all

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,6 @@ jspm_packages/
6161
# Rush temporary files
6262
common/temp/
6363
**/.rush/temp/
64+
65+
# JetBrains IDEs
66+
.idea

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ export const handler = compose(
7171
})
7272
```
7373

74+
There's a [known issue with TypeScript](https://github.com/microsoft/TypeScript/issues/29904) that pipe and compose functions cannot
75+
infer types correctly if the innermost function is generic (in this case the last argument to `compose`).
76+
If you use TypeScript in strict mode, you can instead use the `composeHandler` function exported from `@lambda-middleware/compose`:
77+
78+
```typescript
79+
export const handler = composeHandler(
80+
someMiddleware(),
81+
someOtherMiddleware(),
82+
aThirdMiddleware(),
83+
() => {
84+
return {
85+
body: '',
86+
statusCode: 200
87+
}
88+
}
89+
)
90+
```
91+
7492
Composing middleware is equivalent to calling it nested:
7593
```typescript
7694
export const handler =
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@lambda-middleware/class-validator",
5+
"comment": "Make compatible with TypeScript strict mode",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@lambda-middleware/class-validator",
10+
"email": "[email protected]"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@lambda-middleware/compose",
5+
"comment": "Add composeHandler function",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@lambda-middleware/compose",
10+
"email": "[email protected]"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@lambda-middleware/http-header-normalizer",
5+
"comment": "Make compatible with TypeScript strict mode",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@lambda-middleware/http-header-normalizer",
10+
"email": "[email protected]"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@lambda-middleware/middy-adaptor",
5+
"comment": "Make compatible with TypeScript strict mode",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@lambda-middleware/middy-adaptor",
10+
"email": "[email protected]"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@lambda-middleware/utils",
5+
"comment": "Make compatible with TypeScript strict mode",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@lambda-middleware/utils",
10+
"email": "[email protected]"
11+
}

common/config/rush/pnpm-lock.yaml

Lines changed: 19 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/e2e-tests/examples/helloWorld.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "reflect-metadata";
22

33
import { classValidator } from "@lambda-middleware/class-validator";
4-
import { compose } from "@lambda-middleware/compose";
4+
import { composeHandler } from "@lambda-middleware/compose";
55
import { cors } from "@lambda-middleware/cors";
66
import { errorHandler } from "@lambda-middleware/http-error-handler";
77
import { jsonSerializer } from "@lambda-middleware/json-serializer";
@@ -11,6 +11,11 @@ import { IsString } from "class-validator";
1111

1212
// Define a validator for the body via class-validator
1313
class NameBody {
14+
constructor(firstName: string, lastName: string) {
15+
this.firstName = firstName;
16+
this.lastName = lastName;
17+
}
18+
1419
@IsString()
1520
public firstName: string;
1621

@@ -19,14 +24,16 @@ class NameBody {
1924
}
2025

2126
// This is your AWS handler
22-
async function helloWorld(event: { body: NameBody }) {
27+
async function helloWorld(event: {
28+
body: NameBody;
29+
}): Promise<{ message: string }> {
2330
// Thanks to the validation middleware you can be sure body is typed correctly
2431
return {
2532
message: `Hello ${event.body.firstName} ${event.body.lastName}`,
2633
};
2734
}
2835

29-
const wrapper = compose(
36+
export const handler = composeHandler(
3037
// add cors headers last so even error responses from the
3138
// errorHandler middleware have cors headers applied
3239
cors(),
@@ -48,7 +55,6 @@ const wrapper = compose(
4855
// and other middlewares might not be able to handle the modified event
4956
classValidator({
5057
bodyType: NameBody,
51-
})
52-
)(helloWorld);
53-
54-
export const handler = wrapper;
58+
}),
59+
helloWorld
60+
);

packages/class-validator/README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ This middleware is part of the [lambda middleware series](https://dbartholomae.g
2222
import "reflect-metadata";
2323

2424
import { classValidator } from '@lambda-middleware/class-validator'
25-
import { compose } from "@lambda-middleware/compose";
25+
import { composeHandler } from "@lambda-middleware/compose";
2626
import { errorHandler } from "@lambda-middleware/http-error-handler";
2727
import { IsString } from "class-validator";
2828

2929
// Define a validator for the body via class-validator
3030
class NameBody {
31+
constructor(firstName: string, lastName: string) {
32+
this.firstName = firstName;
33+
this.lastName = lastName;
34+
}
35+
3136
@IsString()
3237
public firstName: string;
3338

@@ -48,7 +53,7 @@ async function helloWorld(event: { body: NameBody }) {
4853
}
4954

5055
// Let's add middleware to our handler, then we will be able to attach middlewares to it
51-
export const handler = compose(
56+
export const handler = composeHandler(
5257
// The class validator throws validation errors from http-errors which are compatible with
5358
// the error handler middlewares for middy
5459
errorHandler(),
@@ -63,6 +68,7 @@ export const handler = compose(
6368
// to set it to true manually as the default for class-validator would be
6469
// false
6570
validator: {},
66-
})
67-
)(helloWorld);
71+
}),
72+
helloWorld
73+
);
6874
```

packages/class-validator/examples/helloWorld.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
import "reflect-metadata";
33

44
import { classValidator } from "../";
5-
import { compose } from "@lambda-middleware/compose";
5+
import { composeHandler } from "@lambda-middleware/compose";
66
import { errorHandler } from "@lambda-middleware/http-error-handler";
77
import { IsString } from "class-validator";
8+
import { APIGatewayProxyResult } from "aws-lambda";
89

910
// Define a validator for the body via class-validator
1011
class NameBody {
12+
constructor(firstName: string, lastName: string) {
13+
this.firstName = firstName;
14+
this.lastName = lastName;
15+
}
16+
1117
@IsString()
1218
public firstName: string;
1319

@@ -16,7 +22,9 @@ class NameBody {
1622
}
1723

1824
// This is your AWS handler
19-
async function helloWorld(event: { body: NameBody }) {
25+
async function helloWorld(event: {
26+
body: NameBody;
27+
}): Promise<APIGatewayProxyResult> {
2028
// Thanks to the validation middleware you can be sure body is typed correctly
2129
return {
2230
body: `Hello ${event.body.firstName} ${event.body.lastName}`,
@@ -28,7 +36,7 @@ async function helloWorld(event: { body: NameBody }) {
2836
}
2937

3038
// Let's add middleware to our handler, then we will be able to attach middlewares to it
31-
export const handler = compose(
39+
export const handler = composeHandler(
3240
// The class validator throws validation errors from http-errors which are compatible with
3341
// the error handler middlewares for middy
3442
errorHandler(),
@@ -43,5 +51,6 @@ export const handler = compose(
4351
// to set it to true manually as the default for class-validator would be
4452
// false
4553
validator: {},
46-
})
47-
)(helloWorld);
54+
}),
55+
helloWorld
56+
);

packages/class-validator/src/classValidator.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import { IsOptional, IsString } from "class-validator";
44
import { createEvent } from "@lambda-middleware/utils";
55

66
class NameBody {
7+
constructor(firstName: string, lastName: string) {
8+
this.firstName = firstName;
9+
this.lastName = lastName;
10+
}
11+
712
@IsString()
813
public firstName: string;
914

0 commit comments

Comments
 (0)