Skip to content

Commit a51fa0e

Browse files
committed
Update model to use REST bindings and add more settings
1 parent f2aa3af commit a51fa0e

File tree

3 files changed

+99
-9
lines changed

3 files changed

+99
-9
lines changed

smithy-typescript-codegen-test/model/main.smithy

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
$version: "0.4.0"
2-
namespace example.weather.foo
2+
namespace example.weather
33

44
/// Provides weather forecasts.
5-
@paginated(inputToken: "nextToken", outputToken: "nextToken",
6-
pageSize: "pageSize")
5+
@paginated(inputToken: "nextToken", outputToken: "nextToken", pageSize: "pageSize")
76
service Weather {
87
version: "2006-03-01",
98
resources: [City],
@@ -14,25 +13,32 @@ resource City {
1413
identifiers: { cityId: CityId },
1514
read: GetCity,
1615
list: ListCities,
17-
resources: [Forecast],
16+
resources: [Forecast, CityImage],
1817
}
1918

2019
resource Forecast {
2120
identifiers: { cityId: CityId },
2221
read: GetForecast,
2322
}
2423

24+
resource CityImage {
25+
identifiers: { cityId: CityId },
26+
read: GetCityImage,
27+
}
28+
2529
// "pattern" is a trait.
2630
@pattern("^[A-Za-z0-9 ]+$")
2731
string CityId
2832

2933
@readonly
34+
@http(method: "GET", uri: "/cities/{cityId}")
3035
operation GetCity(GetCityInput) -> GetCityOutput errors [NoSuchResource]
3136

3237
structure GetCityInput {
3338
// "cityId" provides the identifier for the resource and
3439
// has to be marked as required.
3540
@required
41+
@httpLabel
3642
cityId: CityId
3743
}
3844

@@ -59,6 +65,7 @@ structure CityCoordinates {
5965

6066
/// Error encountered when no resource could be found.
6167
@error("client")
68+
@httpError(404)
6269
structure NoSuchResource {
6370
/// The type of resource that was not found.
6471
@required
@@ -69,10 +76,14 @@ structure NoSuchResource {
6976
// return truncated results.
7077
@readonly
7178
@paginated(items: "items")
79+
@http(method: "GET", uri: "/cities")
7280
operation ListCities(ListCitiesInput) -> ListCitiesOutput
7381

7482
structure ListCitiesInput {
83+
@httpQuery("nextToken")
7584
nextToken: String,
85+
86+
@httpQuery("pageSize")
7687
pageSize: Integer
7788
}
7889

@@ -102,6 +113,7 @@ structure CitySummary {
102113
}
103114

104115
@readonly
116+
@http(method: "GET", uri: "/current-time")
105117
operation GetCurrentTime() -> GetCurrentTimeOutput
106118

107119
structure GetCurrentTimeOutput {
@@ -110,12 +122,14 @@ structure GetCurrentTimeOutput {
110122
}
111123

112124
@readonly
125+
@http(method: "GET", uri: "/cities/{cityId}/forecast")
113126
operation GetForecast(GetForecastInput) -> GetForecastOutput
114127

115128
// "cityId" provides the only identifier for the resource since
116129
// a Forecast doesn't have its own.
117130
structure GetForecastInput {
118131
@required
132+
@httpLabel
119133
cityId: CityId,
120134
}
121135

@@ -145,3 +159,20 @@ map StringMap {
145159
key: String,
146160
value: String,
147161
}
162+
163+
@readonly
164+
@http(method: "GET", uri: "/cities/{cityId}/image")
165+
operation GetCityImage(GetCityImageInput) -> GetCityImageOutput errors [NoSuchResource]
166+
167+
structure GetCityImageInput {
168+
@required @httpLabel
169+
cityId: CityId
170+
}
171+
172+
structure GetCityImageOutput {
173+
@httpPayload
174+
image: CityImageData,
175+
}
176+
177+
@streaming
178+
blob CityImageData

smithy-typescript-codegen-test/smithy-build.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
"version": "1.0",
33
"plugins": {
44
"typescript-codegen": {
5-
"service": "example.weather.foo#Weather",
5+
"service": "example.weather#Weather",
66
"package": "weather",
7-
"target": "node"
7+
"packageVersion": "0.0.1",
8+
"packageJson": {
9+
"license": "Apache-2.0"
10+
}
811
}
912
}
10-
}
13+
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptSettings.java

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,15 @@
3030
public final class TypeScriptSettings {
3131

3232
private static final String PACKAGE = "package";
33+
private static final String PACKAGE_DESCRIPTION = "packageDescription";
34+
private static final String PACKAGE_VERSION = "packageVersion";
35+
private static final String PACKAGE_JSON = "packageJson";
3336
private static final String SERVICE = "service";
3437

3538
private String packageName;
39+
private String packageDescription;
40+
private String packageVersion;
41+
private ObjectNode packageJson;
3642
private ShapeId service;
3743
private ObjectNode pluginSettings = Node.objectNode();
3844

@@ -44,9 +50,14 @@ public final class TypeScriptSettings {
4450
*/
4551
public static TypeScriptSettings from(ObjectNode config) {
4652
TypeScriptSettings settings = new TypeScriptSettings();
47-
config.warnIfAdditionalProperties(Arrays.asList(PACKAGE, SERVICE));
48-
settings.setPackageName(config.expectStringMember(PACKAGE).getValue());
53+
config.warnIfAdditionalProperties(Arrays.asList(
54+
PACKAGE, PACKAGE_DESCRIPTION, PACKAGE_JSON, PACKAGE_VERSION, SERVICE));
4955
settings.setService(config.expectStringMember(SERVICE).expectShapeId());
56+
settings.setPackageName(config.expectStringMember(PACKAGE).getValue());
57+
settings.setPackageVersion(config.expectStringMember(PACKAGE_VERSION).getValue());
58+
settings.setPackageDescription(config.getStringMemberOrDefault(
59+
PACKAGE_DESCRIPTION, settings.getPackageName() + " client"));
60+
settings.packageJson = config.getObjectMember(PACKAGE_JSON).orElse(Node.objectNode());
5061
settings.setPluginSettings(config);
5162
return settings;
5263
}
@@ -104,4 +115,49 @@ public ServiceShape getService(Model model) {
104115
.asServiceShape()
105116
.orElseThrow(() -> new CodegenException("Shape is not a Service: " + getService()));
106117
}
118+
119+
/**
120+
* Gets the description of the package that will be placed in the
121+
* "description" field of the generated package.json.
122+
*
123+
* @return Returns the description.
124+
*/
125+
public String getPackageDescription() {
126+
return packageDescription;
127+
}
128+
129+
public void setPackageDescription(String packageDescription) {
130+
this.packageDescription = packageDescription;
131+
}
132+
133+
/**
134+
* Gets the version of the generated package that will be used with the
135+
* generated package.json file.
136+
*
137+
* @return Returns the package version.
138+
*/
139+
public String getPackageVersion() {
140+
return packageVersion;
141+
}
142+
143+
public void setPackageVersion(String packageVersion) {
144+
this.packageVersion = packageVersion;
145+
}
146+
147+
/**
148+
* Gets a chunk of custom properties to merge into the generated
149+
* package.json file.
150+
*
151+
* <p>This JSON is used to provide any property present in the
152+
* package.json file that isn't captured by any other settings.
153+
*
154+
* @return Returns the custom package JSON.
155+
*/
156+
public ObjectNode getPackageJson() {
157+
return packageJson;
158+
}
159+
160+
public void setPackageJson(ObjectNode packageJson) {
161+
this.packageJson = packageJson;
162+
}
107163
}

0 commit comments

Comments
 (0)