Skip to content

Commit 5c57ac2

Browse files
authored
Merge pull request #127 from smithy-lang/joewyz/typescript-example
Add typescript quick start example
2 parents 189a48b + c64f5eb commit 5c57ac2

19 files changed

+2010
-0
lines changed

smithy-templates.json

+12
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@
151151
".gitignore",
152152
".gitattributes"
153153
]
154+
},
155+
"smithy-typescript-quickstart": {
156+
"documentation": "Quickstart example for Smithy TypeScript",
157+
"path": "smithy-typescript-examples/quickstart-typescript",
158+
"include": [
159+
"config/",
160+
"gradle/",
161+
"gradlew",
162+
"gradlew.bat",
163+
".gitignore",
164+
".gitattributes"
165+
]
154166
}
155167
}
156168
}

smithy-typescript-examples/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Linting and Validation
2+
The examples in this directory demonstrate the use of the [Smithy Typescript](https://github.com/smithy-lang/smithy-typescript) code generator.
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## Smithy-Typescript Quickstart
2+
3+
This project provides a template to get started using [Smithy TypeScript](https://github.com/smithy-lang/smithy-typescript/)
4+
to create Typescript clients and servers.
5+
6+
For more information on this example, see the [Smithy Typescript Quickstart Guide](https://smithy.io/2.0/typescript/quickstart.html).
7+
8+
### Layout
9+
10+
- `client/`: Code generated client that can call the server.
11+
- `smithy/`: Common package for the service API model. Shared by both client and server.
12+
- `server/`: Code generated Server that implements stubbed operations code-generated from the service model.
13+
14+
### Usage
15+
16+
To create a new project from this template, use the [Smithy CLI](https://smithy.io/2.0/guides/smithy-cli/index.html)
17+
`init` command as follows:
18+
19+
```console
20+
smithy init -t smithy-typescript-quickstart
21+
```
22+
23+
### Running and testing server
24+
25+
To run and test the server, navigate to the `server` directory and run the following:
26+
```console
27+
yarn setup && yarn start
28+
```
29+
This command will start the server on port `8888`.
30+
31+
Once the server is running, you may call the server using `curl`:
32+
33+
```console
34+
curl -H "content-type: application/json" -d '{"coffeeType": "LATTE"}' -X POST localhost:8888/order
35+
```
36+
37+
or, by running the client application in the `client` directory:
38+
39+
```console
40+
yarn setup && yarn start
41+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
// Add repositories for all subprojects to resolve dependencies.
3+
allprojects {
4+
repositories {
5+
mavenLocal()
6+
mavenCentral()
7+
}
8+
}
9+
// Add gradle tasks
10+
// Custom clean task to remove all build artifacts and dependencies
11+
tasks.register<Delete>("clean") {
12+
delete(
13+
"server/node_modules",
14+
"server/dist",
15+
"server/ssdk",
16+
"client/node_modules",
17+
"client/sdk",
18+
)
19+
}
20+
21+
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "client-repl",
3+
"version": "0.0.1",
4+
"description": "A REPL that can be used to experiment with the generated client.",
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"build-model": "cd .. && ./gradlew build",
8+
"link-sdk": "ln -fs ../smithy/build/smithyprojections/smithy/source/typescript-client-codegen sdk",
9+
"build-sdk": "cd sdk && yarn install && yarn build",
10+
"setup": "yarn build-model && yarn link-sdk && yarn build-sdk && yarn install",
11+
"start": "ts-node src/index.ts"
12+
},
13+
"keywords": [],
14+
"author": "",
15+
"license": "ISC",
16+
"devDependencies": {
17+
"ts-node": "^10.9.2",
18+
"typescript": "^5.4.5"
19+
},
20+
"dependencies": {
21+
"@com.example/coffee-shop-client": "link:sdk",
22+
"@types/node": "^20.12.13"
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { CoffeeShop, CoffeeType, CreateOrderInput, GetOrderInput }
2+
from '@com.example/coffee-shop-client'
3+
4+
const client = new CoffeeShop({
5+
endpoint: {
6+
protocol: 'http',
7+
hostname: 'localhost',
8+
port: 8888,
9+
path: '/'
10+
}
11+
})
12+
13+
async function main() {
14+
try {
15+
// Create an order request
16+
const createRequest: CreateOrderInput = {
17+
coffeeType: CoffeeType.COLD_BREW
18+
};
19+
20+
// Call the service to create an order
21+
const createResponse = await client.createOrder(createRequest);
22+
console.log(`Created request with id = ${createResponse.id}`);
23+
24+
// Get the order. Should still be in progress.
25+
const getRequest: GetOrderInput = { id: createResponse.id };
26+
const getResponse1 = await client.getOrder(getRequest);
27+
console.log(`Got order with id = ${getResponse1.id}`);
28+
29+
// Give order some time to complete
30+
console.log("Waiting for order to complete....");
31+
await new Promise(resolve => setTimeout(resolve, 5000)); // 5 seconds
32+
33+
// Get the order again.
34+
const getResponse2 = await client.getOrder(getRequest);
35+
console.log(`Completed Order:{id:${getResponse2["id"]}, coffeeType:${getResponse2["coffeeType"]}, status:${getResponse2["status"]}}`);
36+
} catch (error) {
37+
console.error("An error occurred:", error);
38+
}
39+
}
40+
41+
main().catch(error => console.error("Unhandled error:", error));
42+

0 commit comments

Comments
 (0)