Skip to content

Commit 1d864e7

Browse files
authored
Merge pull request #1 from phenixcoder/feature/logger-advanced
feat(logger): added improved logger
2 parents cb21379 + 9be5ee3 commit 1d864e7

File tree

11 files changed

+141
-20
lines changed

11 files changed

+141
-20
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,4 @@ dist
125125
### Node Patch ###
126126
# Serverless Webpack directories
127127
.webpack/
128-
app/**/*.js
129-
app/**/*.d.ts
128+
build

Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
FROM public.ecr.aws/lambda/nodejs:14
22

3-
COPY . ${LAMBDA_TASK_ROOT}
4-
3+
COPY ./build ${LAMBDA_TASK_ROOT}
54
RUN npm ci --only=production
65

76
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)

dev-server.js

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const express = require('express')
22
const { existsSync } = require('fs')
3-
const { handler } = require('./app/index')
3+
const { handler } = require('./build/src/index')
44
const newman = require('newman')
55

66
const app = express();
@@ -10,7 +10,7 @@ let server = null;
1010

1111
app.post('/2015-03-31/functions/function/invocations', async (req, res) => {
1212
try {
13-
const body = req.body ? JSON.parse(req.body) : req;
13+
const body = req.body ? JSON.parse(req.body) : ExpressToAWSAPIGatewayProxyEventV2(req);
1414
handler(body, {}).then(result => {
1515
res.send(result);
1616
}).catch(error => console.error(error));
@@ -20,7 +20,12 @@ app.post('/2015-03-31/functions/function/invocations', async (req, res) => {
2020
})
2121

2222
app.all('/api', async (req, res) => {
23-
handler(req, {}).then(result => {
23+
handler(ExpressToAWSAPIGatewayProxyEventV2(req), {}).then(result => {
24+
res.send(result);
25+
}).catch(error => console.error(error));
26+
});
27+
app.all('/__health', async (req, res) => {
28+
handler(ExpressToAWSAPIGatewayProxyEventV2(req), {}).then(result => {
2429
res.send(result);
2530
}).catch(error => console.error(error));
2631
});
@@ -34,8 +39,8 @@ server = app.listen(port, () => {
3439
if (process.argv[2]) {
3540
const testFilename = process.argv[3] || 'service-collection.postman_collection.json'
3641

37-
console.log('Running Newman tests');
3842
if (!existsSync(`./${testFilename}`)) {
43+
console.log('Running Newman tests');
3944
console.error(`Error: test collection not found
4045
./${testFilename}
4146
@@ -58,4 +63,86 @@ server = app.listen(port, () => {
5863
process.exit();
5964
})
6065
}
61-
})
66+
})
67+
68+
function ExpressToAWSAPIGatewayProxyEventV2(request) {
69+
return {
70+
"resource": request.path,
71+
"path": request.path,
72+
"httpMethod": request.method,
73+
"headers": {
74+
"header1": "value1",
75+
"header2": "value2"
76+
},
77+
"multiValueHeaders": {
78+
"header1": [
79+
"value1"
80+
],
81+
"header2": [
82+
"value1",
83+
"value2"
84+
]
85+
},
86+
"queryStringParameters": {
87+
"parameter1": "value1",
88+
"parameter2": "value"
89+
},
90+
"multiValueQueryStringParameters": {
91+
"parameter1": [
92+
"value1",
93+
"value2"
94+
],
95+
"parameter2": [
96+
"value"
97+
]
98+
},
99+
"requestContext": {
100+
"accountId": "123456789012",
101+
"apiId": "id",
102+
"authorizer": {
103+
"claims": null,
104+
"scopes": null
105+
},
106+
"domainName": "id.execute-api.us-east-1.amazonaws.com",
107+
"domainPrefix": "id",
108+
"extendedRequestId": "request-id",
109+
"httpMethod": "GET",
110+
"identity": {
111+
"accessKey": null,
112+
"accountId": null,
113+
"caller": null,
114+
"cognitoAuthenticationProvider": null,
115+
"cognitoAuthenticationType": null,
116+
"cognitoIdentityId": null,
117+
"cognitoIdentityPoolId": null,
118+
"principalOrgId": null,
119+
"sourceIp": "IP",
120+
"user": null,
121+
"userAgent": "user-agent",
122+
"userArn": null,
123+
"clientCert": {
124+
"clientCertPem": "CERT_CONTENT",
125+
"subjectDN": "www.example.com",
126+
"issuerDN": "Example issuer",
127+
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
128+
"validity": {
129+
"notBefore": "May 28 12:30:02 2019 GMT",
130+
"notAfter": "Aug 5 09:36:04 2021 GMT"
131+
}
132+
}
133+
},
134+
"path": "/my/path",
135+
"protocol": "HTTP/1.1",
136+
"requestId": "id=",
137+
"requestTime": "04/Mar/2020:19:15:17 +0000",
138+
"requestTimeEpoch": 1583349317135,
139+
"resourceId": null,
140+
"resourcePath": "/my/path",
141+
"stage": "$default"
142+
},
143+
"pathParameters": null,
144+
"stageVariables": null,
145+
"body": "Hello from Lambda!",
146+
"isBase64Encoded": false
147+
}
148+
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"scripts": {
2525
"start": "tsc --watch --preserveWatchOutput & nodemon dev-server.js -e js -w ./app",
2626
"build": "tsc",
27-
"test": "find app -wholename '*.d.ts' -delete && find app -wholename '*.js' -delete && jest",
27+
"build:image": "./scripts/gen-build",
28+
"test": "jest",
2829
"test:watch": "npm t -- --watch",
2930
"test:newman": "npm run build && node dev-server.js --test",
3031
"lint": "tslint --fix -p ./tsconfig.json",
@@ -33,7 +34,7 @@
3334
},
3435
"husky": {
3536
"hooks": {
36-
"pre-commit": "tsdx lint"
37+
"pre-commit": "npm run lint && npm run prettier"
3738
}
3839
},
3940
"prettier": {

scripts/gen-build

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
rm -rf ./build
4+
npm run build
5+
cp ./package-lock.json ./build
6+
cd ./build
7+
mv src app
8+
../scripts/gen-prod-package-json package.json package.json
9+
find . -wholename '*.d.ts' -delete
10+
find . -wholename '*.test.js' -delete
11+
cd ..

scripts/gen-prod-package-json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/env node
2+
3+
const { readFileSync, writeFileSync } = require("fs");
4+
5+
const packageString = readFileSync(process.argv[2]);
6+
7+
const { name, version, dependencies, engines, keywords, repository } = JSON.parse(packageString);
8+
9+
const prodJSON = {
10+
name,
11+
version,
12+
dependencies,
13+
engines,
14+
keywords,
15+
repository
16+
};
17+
18+
19+
console.log(prodJSON);
20+
if (process.argv[3]) {
21+
writeFileSync(process.argv[3], JSON.stringify(prodJSON, null, ' '));
22+
}

app/index.test.ts renamed to src/index.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ describe('Index Handler', () => {
9090
const response = await handler(mockRequest(), mockContext());
9191
expect(response).toStrictEqual({
9292
body: JSON.stringify({
93-
hello: 'world',
94-
foo: 'bar',
93+
version,
94+
name,
95+
event: mockRequest(),
96+
context: mockContext(),
9597
}),
9698
headers: {
9799
'content-type': 'text/json',

app/index.ts renamed to src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ export const handler = async (
3030
'content-type': 'text/json',
3131
},
3232
body: JSON.stringify({
33-
hello: 'world',
34-
foo: 'bar',
33+
version: pkg.version,
34+
name: pkg.name,
35+
event,
36+
context: _context,
3537
}),
3638
};
3739
};
File renamed without changes.

app/lib/logger.ts renamed to src/lib/logger.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ const format = winston.format.combine(
3232
)
3333
);
3434

35-
const transports = [
36-
new winston.transports.Console()
37-
];
35+
const transports = [new winston.transports.Console()];
3836

3937
const Logger = winston.createLogger({
4038
level: level(),

0 commit comments

Comments
 (0)