Skip to content

Commit 0043855

Browse files
committed
add examples
1 parent 0db7030 commit 0043855

File tree

13 files changed

+253
-3
lines changed

13 files changed

+253
-3
lines changed

__mocks__/epsagon.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,3 @@ const epsagon = jest.genMockFromModule('epsagon');
44
epsagon.label = function(key: string) {};
55

66
module.exports = epsagon;
7-
// module.exports = (function() {
8-
// throw new Error();
9-
// })();

examples/typescript/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# package directories
2+
node_modules
3+
jspm_packages
4+
5+
# Serverless directories
6+
.serverless
7+
8+
# Webpack directories
9+
.webpack

examples/typescript/handler.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { APIGatewayProxyHandler } from 'aws-lambda';
2+
import 'source-map-support/register';
3+
4+
export const hello: APIGatewayProxyHandler = async (event, _context) => {
5+
return {
6+
statusCode: 200,
7+
body: JSON.stringify({
8+
message: 'Go Serverless Webpack (Typescript) v1.0! Your function executed successfully!',
9+
input: event,
10+
}, null, 2),
11+
};
12+
}

examples/typescript/handlers/api.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { api, ApiSignature } from '@manwaring/lambda-wrapper';
2+
3+
export const handler = api(async ({ event, success, error }: ApiSignature) => {
4+
try {
5+
return success(event);
6+
} catch (err) {
7+
return error(err);
8+
}
9+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { authorizer, AuthorizerSignature } from '@manwaring/lambda-wrapper';
2+
3+
export const handler = authorizer(async ({ token, valid, invalid, error }: AuthorizerSignature) => {
4+
try {
5+
if (!token) {
6+
return invalid('Missing token');
7+
} else {
8+
// TODO validate the authentication token
9+
const jwt = {
10+
sub: '1234567890',
11+
name: 'John Doe',
12+
iat: 1516239022
13+
};
14+
return valid(jwt);
15+
}
16+
} catch (err) {
17+
return error(err);
18+
}
19+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { S3 } from 'aws-sdk';
2+
import { cloudFormation, CloudFormationSignature } from '@manwaring/lambda-wrapper';
3+
4+
const s3 = new S3({ apiVersion: '2006-03-01', region: 'us-east-1' });
5+
6+
export const handler = cloudFormation(async ({ event, success, failure }: CloudFormationSignature) => {
7+
try {
8+
if (event.RequestType.toUpperCase() === 'DELETE') {
9+
await emptyBucket(event.ResourceProperties.BucketName);
10+
}
11+
return success();
12+
} catch (err) {
13+
return failure(err);
14+
}
15+
});
16+
17+
async function emptyBucket(bucket: string): Promise<void> {
18+
const objects = await listBucketObjects(bucket);
19+
await removeBucketObjects(bucket, objects);
20+
}
21+
22+
function listBucketObjects(Bucket: string) {
23+
const params = { Bucket };
24+
console.log('Listing bucket objects with params', params);
25+
return s3.listObjectsV2(params).promise();
26+
}
27+
28+
function removeBucketObjects(Bucket: string, objects) {
29+
const params = {
30+
Bucket,
31+
Delete: {
32+
Objects: objects.Contents.map(object => {
33+
return { Key: object.Key };
34+
})
35+
}
36+
};
37+
console.log('Deleting objects from bucket with params', params);
38+
return s3.deleteObjects(params).promise();
39+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { dynamodbStream } from '@manwaring/lambda-wrapper';
2+
3+
export const handler = dynamodbStream(async ({ success, error }: DynamoDBStreamSignature) => {
4+
try {
5+
return success();
6+
} catch (err) {
7+
return error(err);
8+
}
9+
});

examples/typescript/handlers/sns.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { sns, SnsSignature } from '@manwaring/lambda-wrapper';
2+
3+
export const handler = sns(async ({ event, success, error }: SnsSignature) => {
4+
try {
5+
return success();
6+
} catch (err) {
7+
return error(err);
8+
}
9+
});

examples/typescript/package-lock.json

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

examples/typescript/package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "typescript",
3+
"version": "1.0.0",
4+
"description": "Serverless webpack example using Typescript",
5+
"main": "handler.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"dependencies": {
10+
"@manwaring/lambda-wrapper": "^0.3.8",
11+
"source-map-support": "^0.5.10"
12+
},
13+
"devDependencies": {
14+
"@types/aws-lambda": "^8.10.17",
15+
"@types/node": "^10.12.18",
16+
"serverless-webpack": "^5.2.0",
17+
"ts-loader": "^5.3.3",
18+
"typescript": "^3.2.4",
19+
"webpack": "^4.29.0"
20+
},
21+
"author": "The serverless webpack authors (https://github.com/elastic-coders/serverless-webpack)",
22+
"license": "MIT"
23+
}

0 commit comments

Comments
 (0)