-
Notifications
You must be signed in to change notification settings - Fork 5.7k
JavaScript code example standards
The code example team standards covers the guidelines that are common to all projects.
In the case of single-action examples, create the client inside the action:
import { CreateBucketCommand, S3Client } from "@aws-sdk/client-s3";
export const main = async () => {
const client = new S3Client({});
const command = new CreateBucketCommand({
// The name of the bucket. Bucket names are unique and have several other constraints.
// See https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html
Bucket: "bucket-name",
});
try {
const { Location } = await client.send(command);
console.log(`Bucket created with location ${Location}`);
} catch (err) {
console.error(err);
}
};
In the case of Scenarios, the client should be created externally and passed in to the Scenario state:
export const ec2Scenario = new Scenario(
"EC2",
[],
{ ec2Client: new EC2Client({}), errors: [] },
);
-
javascriptv3
is considered the project root. - All examples exist under
example_code
. - Each directory under
example_code
corresponds to an AWS service. - Directory names should be lowercase with underscores.
- File names should be lowercase with dashes.
-
cross-services
is a special directory for examples that use multiple services. - Example directory structure:
-
{service}/ actions/ {action-name}.js scenarios/ web/ {web-scenario-name}/ {scenario-name}.js {scenario_folder}/ {scenario-file}.js tests/ {integ-test-name}.integration.test.js {unit-test-name}.unit.test.js hello.js package.json README.md vite.config.js
-
The javascriptv3
directory includes a pre-commit hook that will format and run tests. Configure this by following the instructions in the git-hooks README
When an file needs to be run from the command line, add the following code to the bottom. This ensures the file can be run directly, or imported.
// Call function if run directly
import { fileURLToPath } from "url";
if (process.argv[1] === fileURLToPath(import.meta.url)) {
example();
}
Previous versions of the AWS SDK for JavaScript were less modularized. The latest versions rely heavily on modular packages. Import only the clients and commands needed.
import { ListBucketsCommand, S3Client } from "@aws-sdk/client-s3";
const client = new S3Client({});
export const helloS3 = async () => {
const command = new ListBucketsCommand({});
const { Buckets } = await client.send(command);
console.log("Buckets: ");
console.log(Buckets.map((bucket) => bucket.Name).join("\n"));
return Buckets;
};
Some examples are more complicated than just singular client calls. In cases like these, there should be a balance between education and engineering. javascriptv3/example_code/libs/scenario
is a module that provides a framework for setting up more complex examples. Use this framework when an example is more than a few steps.
For guidance on using the scenario framework, see javascriptv3/example_code/libs/scenario/scenario-example.js
.
- About the AWS SDK Code Examples repo
- Code quality guidelines - testing and linting
- Code comment guidelines
- Sample files guidelines
- Cross-service example guidelines
- README templates
-
Code example standards
- General standards
- .NET code example standards
- C++ code example standards
- CLI/Bash code example standards
- Go code example standards
- Kotlin code example standards
- Java code example standards
- JavaScript code example standards
- PHP code example standards
- Python code example standards
- Swift code example standards
- SAP-ABAP code example standards
- Ruby code example standards
- Rust code example standards