Skip to content

Commit ac837b1

Browse files
committed
Add a package for extracting metadata from responses
1 parent 63ba221 commit ac837b1

File tree

6 files changed

+135
-4
lines changed

6 files changed

+135
-4
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.js
2+
*.js.map
3+
*.d.ts
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import {extractMetadata} from "../index";
2+
import {HttpResponse} from "@aws/types";
3+
4+
describe('extractMetadata', () => {
5+
const response: HttpResponse<string> = {
6+
statusCode: 200,
7+
headers: {
8+
Foo: 'bar',
9+
Fizz: 'buzz',
10+
Snap: 'crackle, pop',
11+
}
12+
};
13+
14+
it('should extract the status code from responses', () => {
15+
expect(extractMetadata(response).statusCode).toBe(response.statusCode);
16+
});
17+
18+
it('should extract and downcase headers', () => {
19+
expect(extractMetadata(response).responseHeaders).toEqual({
20+
foo: response.headers.Foo,
21+
fizz: response.headers.Fizz,
22+
snap: response.headers.Snap,
23+
});
24+
});
25+
26+
it('should extract the request ID from the standard header', () => {
27+
expect(extractMetadata({
28+
...response,
29+
headers: {
30+
'X-Amz-Request-ID': 'id',
31+
}
32+
}).requestId).toBe('id');
33+
});
34+
35+
it('should extract the request ID from the alternate header', () => {
36+
expect(extractMetadata({
37+
...response,
38+
headers: {
39+
'X-Amz-RequestId': 'id',
40+
}
41+
}).requestId).toBe('id');
42+
});
43+
44+
it('should prefer the request ID from the standard header', () => {
45+
expect(extractMetadata({
46+
...response,
47+
headers: {
48+
'X-Amz-Request-ID': 'id',
49+
'X-Amz-RequestId': 'alt_id',
50+
}
51+
}).requestId).toBe('id');
52+
});
53+
54+
it('should extract the extended request ID from the standard header', () => {
55+
expect(extractMetadata({
56+
...response,
57+
headers: {
58+
'X-Amz-ID-2': 'extendedId',
59+
}
60+
}).extendedRequestId).toBe('extendedId');
61+
});
62+
63+
it('should extract the CloudFront ID from the standard header', () => {
64+
expect(extractMetadata({
65+
...response,
66+
headers: {
67+
'X-Amz-CF-ID': 'cfId',
68+
}
69+
}).cfId).toBe('cfId');
70+
});
71+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {
2+
HeaderBag,
3+
HttpResponse,
4+
ResponseMetadata,
5+
} from "@aws/types";
6+
7+
const REQUEST_ID_HEADER = 'x-amz-request-id';
8+
const REQUEST_ID_ALT_HEADER = 'x-amz-requestid';
9+
const EXTENDED_REQUEST_ID_HEADER = 'x-amz-id-2';
10+
const CF_ID_HEADER = 'x-amz-cf-id';
11+
12+
export function extractMetadata(
13+
{headers, statusCode}: HttpResponse<any>
14+
): ResponseMetadata {
15+
const responseHeaders: HeaderBag = Object.keys(headers)
16+
.reduce((lowercase: HeaderBag, headerName: string) => {
17+
lowercase[headerName.toLowerCase()] = headers[headerName];
18+
return lowercase;
19+
}, {});
20+
21+
return {
22+
statusCode,
23+
responseHeaders,
24+
requestId: responseHeaders[REQUEST_ID_HEADER]
25+
|| responseHeaders[REQUEST_ID_ALT_HEADER],
26+
extendedRequestId: responseHeaders[EXTENDED_REQUEST_ID_HEADER],
27+
cfId: responseHeaders[CF_ID_HEADER],
28+
};
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "@aws/response-metadata-extractor",
3+
"private": true,
4+
"version": "0.0.1",
5+
"description": "Provides a function for extracting metadata from responses",
6+
"scripts": {
7+
"pretest": "tsc",
8+
"test": "jest"
9+
},
10+
"author": "[email protected]",
11+
"license": "UNLICENSED",
12+
"main": "index.js",
13+
"dependencies": {
14+
"@aws/types": "^0.0.1"
15+
},
16+
"devDependencies": {
17+
"@types/jest": "^20.0.2",
18+
"jest": "^20.0.4",
19+
"typescript": "^2.3"
20+
}
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"declaration": true,
6+
"strict": true,
7+
"sourceMap": true
8+
}
9+
}

packages/types/response.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
export interface ResponseHeaders {
2-
readonly [index: string]: Array<string>;
3-
}
1+
import {HeaderBag} from './http';
42

53
export interface ResponseMetadata {
64
statusCode: number;
75
requestId?: string;
86
extendedRequestId?: string;
97
cfId?: string;
10-
responseHeaders: ResponseHeaders;
8+
responseHeaders: HeaderBag;
119
}

0 commit comments

Comments
 (0)