generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 14
Add typescript quick start example #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
41
smithy-typescript-examples/quickstart-typescript/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ``` |
22 changes: 22 additions & 0 deletions
22
smithy-typescript-examples/quickstart-typescript/build.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| ) | ||
| } | ||
|
|
||
|
|
||
|
|
24 changes: 24 additions & 0 deletions
24
smithy-typescript-examples/quickstart-typescript/client/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
smithy-typescript-examples/quickstart-typescript/client/src/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.