Skip to content
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

Add DefaultReadmeGenerator #721

Merged
merged 1 commit into from
Mar 22, 2023
Merged

Conversation

AndrewFossAWS
Copy link
Contributor

@AndrewFossAWS AndrewFossAWS commented Mar 17, 2023

Description of changes:

(See previous closed PR #584, opening a new PR from fork repo)

  • Added DefaultReadmeGenerator
  • Added createDefaultReadme in TypeScriptSettings (defaults to false)
  • This generates a default README.md if createDefaultReadme is set to true in smithy-build.json.
  • Validated README.md is created in both SDK and SSDK (via smithy-typescript-ssdk-demo)

See client and server README examples below:


weather

Description

SDK for JavaScript Weather Client for Node.js, Browser and React Native.

Provides weather forecasts.

Installing

To install the this package, simply type add or install weather
using your favorite package manager:

  • npm install weather
  • yarn add weather
  • pnpm add weather

Getting Started

Import

To send a request, you only need to import the WeatherClient and
the commands you need, for example GetCityCommand:

// CJS example
const { WeatherClient, GetCityCommand } = require("weather");
// ES6+ example
import { WeatherClient, GetCityCommand } from "weather";

Usage

To send a request, you:

  • Initiate client with configuration.
  • Initiate command with input parameters.
  • Call send operation on client with command object as input.
  • If you are using a custom http handler, you may call destroy() to close open connections.
// a client can be shared by different commands.
const client = new WeatherClient();

const params = { /** input parameters */ };
const command = new GetCityCommand(params);

Async/await

We recommend using await
operator to wait for the promise returned by send operation as follows:

// async/await.
try {
  const data = await client.send(command);
  // process data.
} catch (error) {
  // error handling.
} finally {
  // finally.
}

Async-await is clean, concise, intuitive, easy to debug and has better error handling
as compared to using Promise chains or callbacks.

Promises

You can also use Promise chaining
to execute send operation.

client.send(command).then(
  (data) => {
    // process data.
  },
  (error) => {
    // error handling.
  }
);

Promises can also be called using .catch() and .finally() as follows:

client
  .send(command)
  .then((data) => {
    // process data.
  })
  .catch((error) => {
    // error handling.
  })
  .finally(() => {
    // finally.
  });

Callbacks

We do not recommend using callbacks because of callback hell,
but they are supported by the send operation.

// callbacks.
client.send(command, (err, data) => {
  // process err and data.
});

Troubleshooting

When the service returns an exception, the error will include the exception information,
as well as response metadata (e.g. request id).

try {
  const data = await client.send(command);
  // process data.
} catch (error) {
  const { requestId, httpStatusCode } = error.$$metadata;
  console.log({ requestId, httpStatusCode });
  /**
   * The keys within exceptions are also parsed.
   * You can access them by specifying exception names:
   * if (error.name === 'SomeServiceException') {
   *     const value = error.specialKeyInException;
   * }
   */
}

@bootleg-service/server-bootleg

Description

JavaScript Server SDK for Bootleg

Installing

To install this package, simply type add or install @bootleg-service/server-bootleg
using your favorite package manager:

  • npm install @bootleg-service/server-bootleg
  • yarn add @bootleg-service/server-bootleg
  • pnpm add @bootleg-service/server-bootleg

Getting Started

Below is an example service handler created for the CreateVenue operation.

import { createServer, IncomingMessage, ServerResponse } from "http";
import { HttpRequest } from "@aws-sdk/protocol-http";
import {
  BootlegService as __BootlegService,
  CreateVenueInput,
  CreateVenueOutput,
  getBootlegServiceHandler
} from "@bootleg-service/server-bootleg";
import { convertEvent, convertResponse } from "@aws-smithy/server-node";

class BootlegService implements __BootlegService {
  CreateVenue(input: CreateVenueInput, request: HttpRequest): CreateVenueOutput {
    // Populate your business logic
  }
}

const serviceHandler = getBootlegServiceHandler(new BootlegService());

const server = createServer(async function (
  req: IncomingMessage,
  res: ServerResponse<IncomingMessage> & { req: IncomingMessage }
) {
  // Convert NodeJS's http request to an HttpRequest.
  const httpRequest = convertRequest(req);

  // Call the service handler, which will route the request to the GreetingService
  // implementation and then serialize the response to an HttpResponse.
  const httpResponse = await serviceHandler.handle(httpRequest);

  // Write the HttpResponse to NodeJS http's response expected format.
  return writeResponse(httpResponse, res);
});

server.listen(3000);

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@AndrewFossAWS AndrewFossAWS requested review from a team as code owners March 17, 2023 16:03
@AndrewFossAWS AndrewFossAWS merged commit 0d4eb34 into smithy-lang:main Mar 22, 2023
srchase pushed a commit that referenced this pull request Mar 23, 2023
srchase pushed a commit that referenced this pull request Jun 16, 2023
@agutoli
Copy link

agutoli commented Jul 17, 2024

@gosar @AndrewFossAWS

Hi everyone,

Firstly, thank you for this library; it has been incredibly useful. I'm setting up my Smithy models and noticed that the option createDefaultReadme=false seems to only work for the server generator. I tried disabling the README generation because the default content includes information primarily relevant to AWS SDK clients, which doesn't align with my use case.

Thanks again for your work on this library!
This is my config:

{
    "version": "1.0",
    "outputDirectory": "build/",
    "sources": ["models"],
    "maven": {
        "dependencies": [
            "software.amazon.smithy:smithy-model:1.50.0",
            "software.amazon.smithy:smithy-linters:1.50.0",
            "software.amazon.smithy:smithy-openapi:1.50.0",
            "software.amazon.smithy:smithy-openapi-traits:1.50.0",
            "software.amazon.smithy:smithy-aws-apigateway-openapi:1.50.0",
            "software.amazon.smithy:smithy-aws-traits:1.50.0",
            "software.amazon.smithy:smithy-aws-cloudformation:1.50.0",
            "software.amazon.smithy.typescript:smithy-typescript-codegen:0.21.1",
            "software.amazon.smithy.typescript:smithy-aws-typescript-codegen:0.21.1"
        ],
        "repositories": []
    },
    "projections": {
        "configuration": {
            "plugins": {
                "openapi": {
                    "service": "com.mycorp.api#Configuration",
                    "apiGatewayDefaults": "2023-08-11",
                    "protocol": "aws.protocols#restJson1",
                    "apiGatewayType" : "REST",
                    "disableCloudFormationSubstitution": true
                },
                "typescript-client-codegen": {
                    "package": "@mycorp/smithy-configuration-api-client",
                    "packageVersion": "0.0.4",
                    "packageManager": "npm",
                    "createDefaultReadme": false,
                    "generateTypeDoc": true,
                    "service": "com.mycorp.api#Configuration"
                },
                "typescript-server-codegen": {
                    "package": "@mycorp/smithy-configuration-api-server",
                    "packageVersion": "0.0.4",
                    "packageManager": "npm",
                    "disableDefaultValidation": true,
                    "createDefaultReadme": true,
                    "generateTypeDoc": true,
                    "service": "com.mycorp.api#Configuration",
                    "packageJson": {
                        "files": [
                            "dist-*/**",
                            "openapi.json"
                        ]
                    }
                }
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants