Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Yuan committed Nov 3, 2023
1 parent 85b4deb commit 46d21db
Showing 1 changed file with 98 additions and 4 deletions.
102 changes: 98 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,106 @@ build/smithy/source/typescript-client-codegen/
└── typedoc.json
```
TODO What is missing and how to add it? (what customizations do users need to write themselves? Are there example we can point them to?)
#### Code generation implementations not included
- Protocols
- Endpoint resolution
Smithy TypeScript provides default code generation implementations for generating TypeScript clients, but also requires customers to either implement or consume certain implementations where there is no default.
##### Publishing a Client SDK package
For Smithy TypeScript clients, the main implementations not provided are protocol generators and handling endpoint resolution (see [the TypeScript code generation section](#typescript-code-generation)).
> If there are items missing from this section, feel free to [create an issue](https://github.com/awslabs/smithy-typescript/issues/new).
##### Protocols
Protocols define how operation inputs and outputs are serialized and deserialized on the wire. This behavior can be defined in Smithy through [protocol traits](https://smithy.io/2.0/spec/protocol-traits.html) with corresponding implementations of [the `ProtocolGenerator` interface](smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/ProtocolGenerator.java) in Smithy TypeScript. Besides the `ProtocolGenerator` interface, Smithy TypeScript has additional abstract classes that partially implement the `ProtocolGenerator` interface and can be extended: [`HttpBindingProtocolGenerator` for HTTP binding protocols](smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java) and [`HttpRpcProtocolGenerator` for HTTP RPC protocols](smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpRpcProtocolGenerator.java).
Once a `ProtocolGenerator` is implemented, the implementation can be registered through a `TypeScriptIntegration`:
- `TypeScriptIntegration` with `ProtocolGenerator` implementation:
```java
// src/main/java/typescript/example/client/gradle/ExampleClientProtocolGeneratorIntegration.java
package typescript.example.client.gradle;
// ...
public class ExampleClientProtocolGeneratorIntegration implements TypeScriptIntegration {
// ProtocolGenerator implementation is inline for brevity, but should be in its own file
private static class ExampleClientProtocolGenerator implements ProtocolGenerator {
// Implement ProtocolGenerator methods
}
@Override
public List<ProtocolGenerator> getProtocolGenerators() {
return List.of(new ExampleClientProtocolGenerator());
}
}
```
- Registering the `TypeScriptIntegration`:
```java
// src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration
typescript.example.client.gradle.ExampleClientProtocolGeneratorIntegration
```

See [the section on customizations via `TypeScriptIntegration` for more details](#customizations-via-typescriptintegration).

> Note: [AWS SDK for JavaScript v3](https://github.com/aws/aws-sdk-js-v3), a customer of Smithy TypeScript, implements the [AWS protocols](https://smithy.io/2.0/aws/protocols/index.html) and can be consumed by adding the `software.amazon.smithy.typescript:smithy-aws-typescript-codegen` dependency.

##### Endpoint resolution

Endpoint resolution is not implemented by default due to the inherent complexity. By default, if no endpoint resolution is provided, customers using the client will have to explicity provide an endpoint provider.

```typescript
import {
$SERVICEClient,
} from "..."; // example client package

// Without providing the endpoint, a "No valid endpoint provider available." error will be thrown
const individualClient = new $SERVICEClient({
endpoint: async () => "https://www.example.com",
});
```

To provide a default endpoint provider, implementing the `TypeScriptIntegration::getRuntimeConfigWriters()` method should suffice:

- `TypeScriptIntegration` with `getRuntimeConfigWriters()` implementation:
```java
// src/main/java/typescript/example/client/gradle/ExampleClientEndpointResolutionIntegration.java
package typescript.example.client.gradle;

// ...

public class ExampleClientEndpointResolutionIntegration implements TypeScriptIntegration {
@Override
public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
LanguageTarget target
) {
// Runtime config value also be specified per platform by using the `target` argument, e.g.
// if (target.equals(LanguageTarget.NODE)) { ... }

// This example provides an arbitrary endpoint provider based on random numbers
return Map.of("endpoint", w -> w.write("""
async () => {
// Generate number between 1-10
const n = Math.floor(Math.random() * 11);
// Return domain based on random number
return n < 5
? "https://www.example.com"
: "https://www.amazon.com";
}"""));
}
}
```
- Registering the `TypeScriptIntegration`:
```java
// src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration
typescript.example.client.gradle.ExampleClientEndpointResolutionIntegration
```
See [the section on customizations via `TypeScriptIntegration` for more details](#customizations-via-typescriptintegration).
#### Publishing a Client SDK package
> Note: There is no prescribed way to publish NPM packages since there are many ways to maintain SDKs. Some publishing tools include using [`npm publish`](https://docs.npmjs.com/cli/v8/commands/npm-publish) or [`yarn publish`](https://classic.yarnpkg.com/lang/en/docs/cli/publish/) directly, or managing a monorepo with tools like [`turbo`](https://turbo.build/repo/docs/handbook/publishing-packages). This section provides tips for how a general publishing workflow could work.
Expand Down

0 comments on commit 46d21db

Please sign in to comment.