Skip to content

Commit ee06835

Browse files
authored
Fetch items by id, including children comments (#2)
* Groundwork for dynamic API endpoint * Process path parameter and fetch data based on request * Include tslint * Initial attempt to fetch child comments * Do not fetch kids if item is null
1 parent 677186f commit ee06835

9 files changed

+308
-12
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@
66
Attempts to incorporate `webpack` have failed. Consider reviewing the commit history to see the various configurations that did not work. Simple functions operated without issue, but functions with Firebase functionality fail. The issue appeared to be caused by async/await functionality, but Promises in general had issues.
77

88
At some point in the future, `dom` should be removed as a library. Additionally, `webpack` should be included. `webpack` is important due to its ability to reduce bundle sizes and streamline code generation. The bundle size without webpack is ~16MB for ANY function. With webpack, the `hello` function drops to ~3KB while hn-api drops to ~350KB.
9+
10+
## Future Reference
11+
Do not use `console.dir()` with AWS. The logs are difficult to decipher (line breaks prevent useful grouping).

data/api-request-event-real.json

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"resource:": "/api/v1/item/{id}",
3+
"path:": "/api/v1/item/12345",
4+
"httpMethod:": "GET",
5+
"headers:": null,
6+
"multiValueHeaders:": null,
7+
"queryStringParameters:": null,
8+
"multiValueQueryStringParameters:": null,
9+
"pathParameters:": {
10+
"id:": "12345"
11+
},
12+
"stageVariables:": null,
13+
"requestContext:": {
14+
"path:": "/api/v1/item/{id}",
15+
"accountId:": "729037857495",
16+
"resourceId:": "zuwcux",
17+
"stage:": "test-invoke-stage",
18+
"domainPrefix:": "testPrefix",
19+
"requestId:": "10891e3b-1c4a-11e9-8662-67f5384b099c",
20+
"identity:": {
21+
"cognitoIdentityPoolId:": null,
22+
"cognitoIdentityId:": null,
23+
"apiKey:": "test-invoke-api-key",
24+
"cognitoAuthenticationType:": null,
25+
"userArn:": "arn:aws:sts::729037857495:assumed-role/OrganizationAccountAccessRole/chandler",
26+
"apiKeyId:": "test-invoke-api-key-id",
27+
"userAgent:": "aws-internal/3 aws-sdk-java/1.11.465 Linux/4.9.137-0.1.ac.218.74.329.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.192-b12 java/1.8.0_192",
28+
"accountId:": "729037857495",
29+
"caller:": "AROAJFXXJ5BJW7Q32FK52:chandler",
30+
"sourceIp:": "test-invoke-source-ip",
31+
"accessKey:": "ASIA2TPQK4LLZJDJIRUX",
32+
"cognitoAuthenticationProvider:": null,
33+
"user:": "AROAJFXXJ5BJW7Q32FK52:chandler"
34+
},
35+
"domainName:": "testPrefix.testDomainName",
36+
"resourcePath:": "/api/v1/item/{id}",
37+
"httpMethod:": "GET",
38+
"extendedRequestId:": "Txr6qE39PHcF7Fw=",
39+
"apiId:": "jo0norcal4"
40+
},
41+
"body:": null,
42+
"isBase64Encoded:": false
43+
}

data/api-request-event.json

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"body": "eyJ0ZXN0IjoiYm9keSJ9",
3+
"resource": "/{proxy+}",
4+
"path": "/path/to/resource",
5+
"httpMethod": "POST",
6+
"isBase64Encoded": true,
7+
"queryStringParameters": {
8+
"foo": "bar"
9+
},
10+
"pathParameters": {
11+
"proxy": "/path/to/resource",
12+
"id": "8863"
13+
},
14+
"stageVariables": {
15+
"baz": "qux"
16+
},
17+
"headers": {
18+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
19+
"Accept-Encoding": "gzip, deflate, sdch",
20+
"Accept-Language": "en-US,en;q=0.8",
21+
"Cache-Control": "max-age=0",
22+
"CloudFront-Forwarded-Proto": "https",
23+
"CloudFront-Is-Desktop-Viewer": "true",
24+
"CloudFront-Is-Mobile-Viewer": "false",
25+
"CloudFront-Is-SmartTV-Viewer": "false",
26+
"CloudFront-Is-Tablet-Viewer": "false",
27+
"CloudFront-Viewer-Country": "US",
28+
"Host": "1234567890.execute-api.us-west-2.amazonaws.com",
29+
"Upgrade-Insecure-Requests": "1",
30+
"User-Agent": "Custom User Agent String",
31+
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
32+
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
33+
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
34+
"X-Forwarded-Port": "443",
35+
"X-Forwarded-Proto": "https"
36+
},
37+
"requestContext": {
38+
"accountId": "123456789012",
39+
"resourceId": "123456",
40+
"stage": "prod",
41+
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
42+
"requestTime": "09/Apr/2015:12:34:56 +0000",
43+
"requestTimeEpoch": 1428582896000,
44+
"identity": {
45+
"cognitoIdentityPoolId": null,
46+
"accountId": null,
47+
"cognitoIdentityId": null,
48+
"caller": null,
49+
"accessKey": null,
50+
"sourceIp": "127.0.0.1",
51+
"cognitoAuthenticationType": null,
52+
"cognitoAuthenticationProvider": null,
53+
"userArn": null,
54+
"userAgent": "Custom User Agent String",
55+
"user": null
56+
},
57+
"path": "/prod/path/to/resource",
58+
"resourcePath": "/{proxy+}",
59+
"httpMethod": "POST",
60+
"apiId": "1234567890",
61+
"protocol": "HTTP/1.1"
62+
}
63+
}

package-lock.json

+112
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
"version": "0.1.0",
44
"description": "Serverless webpack example using Typescript",
55
"scripts": {
6+
"lint": "tslint --project .",
67
"compile": "tsc",
8+
"build": "npm run lint && npm run compile",
79
"compile:watch": "npm run compile -- --watch",
8-
"predeploy:dev": "npm run compile",
10+
"predeploy:dev": "npm run build",
911
"deploy:dev": "serverless deploy --stage dev",
1012
"preoffline": "npm run compile",
1113
"offline": "serverless offline",
@@ -21,6 +23,7 @@
2123
"serverless": "^1.36.1",
2224
"serverless-offline": "^4.1.4",
2325
"source-map-support": "^0.5.6",
26+
"tslint": "^5.12.1",
2427
"typescript": "^3.2.4"
2528
},
2629
"author": "Chandler Wall",

serverless.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ functions:
1818
events:
1919
- http:
2020
method: get
21-
path: hello
21+
path: api/v1/meta/health
2222
hn-api:
2323
handler: dist/hn-api.hn
2424
events:
2525
- http:
2626
method: get
27-
path: hn
27+
path: api/v1/item/{id}

src/hello.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import {APIGatewayProxyHandler} from "aws-lambda";
22

33
export const hello: APIGatewayProxyHandler = async (event, context) => {
4+
// TODO - This function needs to be come a health check.
45
return {
56
statusCode: 200,
67
body: JSON.stringify({
78
message: "Go Serverless Webpack (Typescript) v1.0! Your function executed successfully!",
89
input: event,
9-
context: context,
10+
context,
1011
}),
1112
};
1213
};

0 commit comments

Comments
 (0)