Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions smithy-templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@
".gitignore",
".gitattributes"
]
},
"smithy-typescript-quickstart": {
"documentation": "Quickstart example for Smithy TypeScript",
"path": "smithy-typescript-examples/quickstart-typescript",
"include": [
"config/",
"gradle/",
"gradlew",
"gradlew.bat",
".gitignore",
".gitattributes"
]
}
}
}
3 changes: 3 additions & 0 deletions smithy-typescript-examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Linting and Validation
The examples in this directory demonstrate the use of the [Smithy Typescript](https://github.com/smithy-lang/smithy-typescript) code generator.

41 changes: 41 additions & 0 deletions smithy-typescript-examples/quickstart-typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## Smithy-Typescript Quickstart

This project provides a template to get started using [Smithy TypeScript](https://github.com/smithy-lang/smithy-typescript/)
to create Typescript clients and servers.

For more information on this example, see the [Smithy Typescript Quickstart Guide](https://smithy.io/2.0/typescript/quickstart.html).

### Layout

- `client/`: Code generated client that can call the server.
- `smithy/`: Common package for the service API model. Shared by both client and server.
- `server/`: Code generated Server that implements stubbed operations code-generated from the service model.

### Usage

To create a new project from this template, use the [Smithy CLI](https://smithy.io/2.0/guides/smithy-cli/index.html)
`init` command as follows:

```console
smithy init -t smithy-typescript-quickstart
```

### Running and testing server

To run and test the server, navigate to the `server` directory and run the following:
```console
yarn setup && yarn start
```
This command will start the server on port `8888`.

Once the server is running, you may call the server using `curl`:

```console
curl -H "content-type: application/json" -d '{"coffeeType": "LATTE"}' -X POST localhost:8888/order
```

or, by running the client application in the `client` directory:

```console
yarn setup && yarn start
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

// Add repositories for all subprojects to resolve dependencies.
allprojects {
repositories {
mavenLocal()
mavenCentral()
}
}
// Add gradle tasks
// Custom clean task to remove all build artifacts and dependencies
tasks.register<Delete>("clean") {
delete(
"server/node_modules",
"server/dist",
"server/ssdk",
"client/node_modules",
"client/sdk",
)
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "client-repl",
"version": "0.0.1",
"description": "A REPL that can be used to experiment with the generated client.",
"main": "dist/index.js",
"scripts": {
"build-model": "cd .. && ./gradlew build",
"link-sdk": "ln -fs ../smithy/build/smithyprojections/smithy/source/typescript-client-codegen sdk",
"build-sdk": "cd sdk && yarn install && yarn build",
"setup": "yarn build-model && yarn link-sdk && yarn build-sdk && yarn install",
"start": "ts-node src/index.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
},
"dependencies": {
"@com.example/coffee-shop-client": "link:sdk",
"@types/node": "^20.12.13"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { CoffeeShop, CoffeeType, CreateOrderInput, GetOrderInput }
from '@com.example/coffee-shop-client'

const client = new CoffeeShop({
endpoint: {
protocol: 'http',
hostname: 'localhost',
port: 8888,
path: '/'
}
})

async function main() {
try {
// Create an order request
const createRequest: CreateOrderInput = {
coffeeType: CoffeeType.COLD_BREW
};

// Call the service to create an order
const createResponse = await client.createOrder(createRequest);
console.log(`Created request with id = ${createResponse.id}`);

// Get the order. Should still be in progress.
const getRequest: GetOrderInput = { id: createResponse.id };
const getResponse1 = await client.getOrder(getRequest);
console.log(`Got order with id = ${getResponse1.id}`);

// Give order some time to complete
console.log("Waiting for order to complete....");
await new Promise(resolve => setTimeout(resolve, 5000)); // 5 seconds

// Get the order again.
const getResponse2 = await client.getOrder(getRequest);
console.log(`Completed Order:{id:${getResponse2["id"]}, coffeeType:${getResponse2["coffeeType"]}, status:${getResponse2["status"]}}`);
} catch (error) {
console.error("An error occurred:", error);
}
}

main().catch(error => console.error("Unhandled error:", error));

Loading
Loading