-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
62 lines (50 loc) · 2.04 KB
/
app.ts
File metadata and controls
62 lines (50 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import { Input, Output } from './interfaces';
import axios from 'axios';
import * as cheerio from 'cheerio';
/**
*
* Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
* @param {Object} event - API Gateway Lambda Proxy Input Format
*
* Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
* @returns {Object} object - API Gateway Lambda Proxy Output Format
*
*/
const BUCKET = 'lambda-url-to-html-storage';
const s3Client = new S3Client({ region: 'us-east-1' });
const storeHtmlFile = async (content: string, name: string): Promise<string> => {
const key = `${name}.html`;
const cmd = new PutObjectCommand({
Bucket: BUCKET,
Key: key,
Body: Buffer.from(content),
ContentType: 'text/html'
});
await s3Client.send(cmd);
return `https://${BUCKET}.s3.amazonaws.com/${key}`;
};
const getErrorMessage = (error: unknown) => {
return error instanceof Error ? error.message : String(error);
};
export const lambdaHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
const response: APIGatewayProxyResult = { statusCode: 200, body: '' };
const output: Output = { title: '', s3_url: '', error: '' };
try {
const eventBody = event.queryStringParameters as object as Input;
if (!eventBody.name || !eventBody.url) {
throw Error('name and url are required.');
}
console.log(JSON.stringify(eventBody));
const res = await axios.get(eventBody.url);
output.title = cheerio.load(res.data)('head > title').text().trim();
output.s3_url = await storeHtmlFile(res.data, eventBody.name);
} catch (err) {
response.statusCode = 500;
output.error = getErrorMessage(err);
}
response.body = JSON.stringify(output);
console.log(JSON.stringify(response));
return response;
};