Skip to content

Commit ab15f1c

Browse files
authored
Merge pull request #20 from jeskew/feature/capture-metadata-in-generated-models
Add response metadata shape to output and exception interfaces
2 parents 6a9a954 + 09c1dcb commit ab15f1c

File tree

7 files changed

+104
-9
lines changed

7 files changed

+104
-9
lines changed

packages/service-types-generator/__tests__/Components/Type/Exception.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import { Exception } from "../../../lib/Components/Type/Exception";
2+
import { IndentedSection } from "../../../lib/Components/IndentedSection";
3+
import {
4+
METADATA_PROPERTY_IMPORT,
5+
OUTPUT_METADATA_PROPERTY
6+
} from "../../../lib/Components/Type/constants";
27

38
describe("Exception", () => {
49
it("should include standard exception members if not defined", () => {
@@ -11,7 +16,9 @@ describe("Exception", () => {
1116
});
1217

1318
expect(exception.toString()).toEqual(
14-
`/**
19+
`${METADATA_PROPERTY_IMPORT.toString()}
20+
21+
/**
1522
* <p>An exceptional state</p>
1623
*/
1724
export interface MyException {
@@ -29,6 +36,8 @@ export interface MyException {
2936
* <p>Human-readable description of the error.</p>
3037
*/
3138
message?: string;
39+
40+
${new IndentedSection(OUTPUT_METADATA_PROPERTY)}
3241
}`
3342
);
3443
});
@@ -51,7 +60,9 @@ export interface MyException {
5160
});
5261

5362
expect(exception.toString()).toEqual(
54-
`/**
63+
`${METADATA_PROPERTY_IMPORT.toString()}
64+
65+
/**
5566
* <p>An exceptional state</p>
5667
*/
5768
export interface MyException {
@@ -69,6 +80,8 @@ export interface MyException {
6980
* message property doc
7081
*/
7182
message?: string;
83+
84+
${new IndentedSection(OUTPUT_METADATA_PROPERTY)}
7285
}`
7386
);
7487
});
@@ -91,7 +104,9 @@ export interface MyException {
91104
});
92105

93106
expect(exception.toString()).toEqual(
94-
`/**
107+
`${METADATA_PROPERTY_IMPORT.toString()}
108+
109+
/**
95110
* <p>An exceptional state</p>
96111
*/
97112
export interface MyException {
@@ -109,6 +124,8 @@ export interface MyException {
109124
* name property doc
110125
*/
111126
name?: string;
127+
128+
${new IndentedSection(OUTPUT_METADATA_PROPERTY)}
112129
}`
113130
);
114131
});
@@ -131,7 +148,9 @@ export interface MyException {
131148
});
132149

133150
expect(exception.toString()).toEqual(
134-
`/**
151+
`${METADATA_PROPERTY_IMPORT.toString()}
152+
153+
/**
135154
* <p>An exceptional state</p>
136155
*/
137156
export interface MyException {
@@ -149,6 +168,8 @@ export interface MyException {
149168
* stack property doc
150169
*/
151170
stack?: string;
171+
172+
${new IndentedSection(OUTPUT_METADATA_PROPERTY)}
152173
}`
153174
);
154175
});

packages/service-types-generator/__tests__/Components/Type/Output.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import {
88
} from "@aws/service-model";
99
import { getMemberType } from "../../../lib/Components/Type/getMemberType";
1010
import { NonStreamingBlob, StreamingBlob } from "../../../__fixtures__";
11+
import { IndentedSection } from "../../../lib/Components/IndentedSection";
12+
import {
13+
METADATA_PROPERTY_IMPORT,
14+
OUTPUT_METADATA_PROPERTY
15+
} from "../../../lib/Components/Type/constants";
1116

1217
describe("Output", () => {
1318
it("should emit documentation and an empty interface for an empty structure", () => {
@@ -21,11 +26,13 @@ describe("Output", () => {
2126
});
2227

2328
expect(output.toString()).toEqual(
24-
`/**
29+
`${METADATA_PROPERTY_IMPORT.toString()}
30+
31+
/**
2532
* Operation output
2633
*/
2734
export interface ${name} {
28-
35+
${new IndentedSection(OUTPUT_METADATA_PROPERTY)}
2936
}`
3037
);
3138
});
@@ -45,14 +52,18 @@ export interface ${name} {
4552
});
4653

4754
expect(output.toString()).toEqual(
48-
`/**
55+
`${METADATA_PROPERTY_IMPORT.toString()}
56+
57+
/**
4958
* Operation output
5059
*/
5160
export interface ${name}<${GENERIC_STREAM_TYPE}> {
5261
/**
5362
* ${StreamingBlob.documentation}
5463
*/
5564
data?: ${getMemberType(StreamingBlob)};
65+
66+
${new IndentedSection(OUTPUT_METADATA_PROPERTY)}
5667
}`
5768
);
5869
});
@@ -75,14 +86,18 @@ export interface ${name}<${GENERIC_STREAM_TYPE}> {
7586
};
7687

7788
expect(new Output(output).toString()).toEqual(
78-
`/**
89+
`${METADATA_PROPERTY_IMPORT.toString()}
90+
91+
/**
7992
* ${output.documentation}
8093
*/
8194
export interface ${name}<${GENERIC_STREAM_TYPE}> {
8295
/**
8396
* ${dataMember.documentation}
8497
*/
8598
data?: ${getMemberType(dataMember.shape, dataMember)};
99+
100+
${new IndentedSection(OUTPUT_METADATA_PROPERTY)}
86101
}`
87102
);
88103
});
@@ -112,6 +127,7 @@ export interface ${name}<${GENERIC_STREAM_TYPE}> {
112127
`import {${getUnmarshalledShapeName(structure.name)}} from './${
113128
structure.name
114129
}';
130+
${METADATA_PROPERTY_IMPORT.toString()}
115131
116132
/**
117133
* ${outputShape.documentation}
@@ -121,6 +137,8 @@ export interface ${name} {
121137
* ${structure.documentation}
122138
*/
123139
data?: ${getMemberType(structure)};
140+
141+
${new IndentedSection(OUTPUT_METADATA_PROPERTY)}
124142
}`
125143
);
126144
});
@@ -158,6 +176,7 @@ export interface ${name} {
158176
`import {${getUnmarshalledShapeName(
159177
structureName
160178
)}} from './${structureName}';
179+
${METADATA_PROPERTY_IMPORT.toString()}
161180
162181
/**
163182
* ${inputShape.documentation}
@@ -167,6 +186,8 @@ export interface ${name} {
167186
* ${structureList.documentation}
168187
*/
169188
data?: ${getMemberType(structureList)};
189+
190+
${new IndentedSection(OUTPUT_METADATA_PROPERTY)}
170191
}`
171192
);
172193
});
@@ -215,6 +236,7 @@ export interface ${name} {
215236
keyStructure
216237
)}} from './${keyStructure}';
217238
import {${getUnmarshalledShapeName(valueStructure)}} from './${valueStructure}';
239+
${METADATA_PROPERTY_IMPORT.toString()}
218240
219241
/**
220242
* ${inputShape.documentation}
@@ -224,6 +246,8 @@ export interface ${name} {
224246
* ${structureMap.documentation}
225247
*/
226248
data?: ${getMemberType(structureMap)};
249+
250+
${new IndentedSection(OUTPUT_METADATA_PROPERTY)}
227251
}`
228252
);
229253
});

packages/service-types-generator/lib/Components/Type/Exception.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { Structure } from "./Structure";
22
import { SerializationType } from "@aws/types";
33
import { IndentedSection } from "../IndentedSection";
4+
import { getUnmarshalledShapeName } from "./helpers";
5+
import { Import } from "../Import";
6+
import {
7+
METADATA_PROPERTY_IMPORT,
8+
OUTPUT_METADATA_PROPERTY
9+
} from "./constants";
410

511
interface InnateMember {
612
memberName: string;
@@ -13,16 +19,26 @@ export class Exception extends Structure {
1319
const members = this.innateMembers
1420
.map(this.formatInnateMember, this)
1521
.concat(
16-
Object.keys(this.shape.members).map(this.getMemberDefinition, this)
22+
Object.keys(this.shape.members).map(this.getMemberDefinition, this),
23+
OUTPUT_METADATA_PROPERTY
1724
);
1825
return `
26+
${this.imports}
27+
1928
${this.docBlock(this.shape.documentation)}
2029
export interface ${this.shape.name} {
2130
${new IndentedSection(members.join("\n\n"))}
2231
}
2332
`.trim();
2433
}
2534

35+
private get imports(): string {
36+
return this.foreignShapes
37+
.map(shape => new Import(`./${shape}`, getUnmarshalledShapeName(shape)))
38+
.concat(METADATA_PROPERTY_IMPORT)
39+
.join("\n");
40+
}
41+
2642
private get innateMembers(): InnateMember[] {
2743
const { members } = this.shape;
2844
const innateMembers: InnateMember[] = [];

packages/service-types-generator/lib/Components/Type/Output.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { Structure } from "./Structure";
33
import { GENERIC_STREAM_TYPE } from "../../constants";
44
import { IndentedSection } from "../IndentedSection";
55
import { getUnmarshalledShapeName, hasStreamingBody } from "./helpers";
6+
import {
7+
METADATA_PROPERTY_IMPORT,
8+
OUTPUT_METADATA_PROPERTY
9+
} from "./constants";
610

711
export class Output extends Structure {
812
toString(): string {
@@ -16,6 +20,7 @@ export interface ${this.shape.name}${
1620
${new IndentedSection(
1721
Object.keys(this.shape.members)
1822
.map(this.getMemberDefinition, this)
23+
.concat(OUTPUT_METADATA_PROPERTY)
1924
.join("\n\n")
2025
)}
2126
}
@@ -25,6 +30,7 @@ ${new IndentedSection(
2530
private get imports(): string {
2631
return this.foreignShapes
2732
.map(shape => new Import(`./${shape}`, getUnmarshalledShapeName(shape)))
33+
.concat(METADATA_PROPERTY_IMPORT)
2834
.join("\n");
2935
}
3036
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Import } from "../Import";
2+
3+
const MD_PROP_ALIAS = "__ResponseMetadata__";
4+
5+
export const METADATA_PROPERTY_IMPORT = new Import(
6+
"@aws/types",
7+
`ResponseMetadata as ${MD_PROP_ALIAS}`
8+
);
9+
10+
export const OUTPUT_METADATA_PROPERTY = `
11+
/**
12+
* Metadata about the response received, including the HTTP status code, HTTP
13+
* headers, and any request identifiers recognized by the SDK.
14+
*/
15+
$metadata: ${MD_PROP_ALIAS};
16+
`.trim();

packages/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from "./credentials";
22
export * from "./http";
33
export * from "./crypto";
44
export * from "./protocol";
5+
export * from "./response";

packages/types/response.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export interface ResponseHeaders {
2+
readonly [index: string]: Array<string>;
3+
}
4+
5+
export interface ResponseMetadata {
6+
statusCode: number;
7+
requestId?: string;
8+
extendedRequestId?: string;
9+
cfId?: string;
10+
responseHeaders: ResponseHeaders;
11+
}

0 commit comments

Comments
 (0)