From 74dea7707876cca3a301756612513e6480a8378c Mon Sep 17 00:00:00 2001 From: oleiade Date: Fri, 17 Nov 2023 10:20:21 +0100 Subject: [PATCH] Refactor client classes to make their properties private and readonly where relevant. --- src/internal/client.ts | 8 ++++---- src/internal/event-bridge.ts | 12 +++++------- src/internal/kinesis.ts | 21 +++++---------------- src/internal/kms.ts | 7 +++---- src/internal/s3.ts | 16 +++++++++------- src/internal/secrets-manager.ts | 14 +++----------- src/internal/sqs.ts | 27 ++++++++++++++++----------- src/internal/ssm.ts | 6 +++--- 8 files changed, 48 insertions(+), 63 deletions(-) diff --git a/src/internal/client.ts b/src/internal/client.ts index 7936f2b..0e8c3f8 100644 --- a/src/internal/client.ts +++ b/src/internal/client.ts @@ -10,8 +10,8 @@ import { HTTPHeaders } from './http' * usage examples. */ export class AWSClient { - awsConfig: AWSConfig - serviceName: string + readonly awsConfig: AWSConfig + readonly serviceName: string private _endpoint?: Endpoint @@ -66,6 +66,6 @@ export class AWSClient { * Type alias representing the result of an AWSClient.buildRequest call */ export interface AWSRequest { - url: string - headers: HTTPHeaders + readonly url: string + readonly headers: HTTPHeaders } diff --git a/src/internal/event-bridge.ts b/src/internal/event-bridge.ts index 4564326..aa8ab88 100644 --- a/src/internal/event-bridge.ts +++ b/src/internal/event-bridge.ts @@ -12,11 +12,9 @@ import { HTTPHeaders, HTTPMethod } from './http' * Class allowing to interact with Amazon AWS's Event Bridge service */ export class EventBridgeClient extends AWSClient { - method: HTTPMethod - - commonHeaders: HTTPHeaders - - signature: SignatureV4 + private readonly signature: SignatureV4 + private readonly method: HTTPMethod + private readonly commonHeaders: HTTPHeaders constructor(awsConfig: AWSConfig) { super(awsConfig, 'events') @@ -41,8 +39,8 @@ export class EventBridgeClient extends AWSClient { /** * Sends custom events to Amazon EventBridge so that they can be matched to rules. - * - * @param {PutEventsInput} input - The input for the PutEvents operation. + * + * @param {PutEventsInput} input - The input for the PutEvents operation. * @throws {EventBridgeServiceError} * @throws {InvalidSignatureError} */ diff --git a/src/internal/kinesis.ts b/src/internal/kinesis.ts index 2e27dbc..8e40f2c 100644 --- a/src/internal/kinesis.ts +++ b/src/internal/kinesis.ts @@ -18,20 +18,9 @@ https://docs.aws.amazon.com/kinesis/latest/APIReference/API_Operations.html * Allows interacting with the Kinesis API. */ export class KinesisClient extends AWSClient { - /** - * The SignatureV4 object used to sign requests. - */ - signature: SignatureV4 - - /** - * The common headers that are used for all requests. - */ - commonHeaders: HTTPHeaders - - /** - * The version of the Kinesis API that is used for all requests. - */ - serviceVersion: string + private readonly signature: SignatureV4 + private readonly commonHeaders: HTTPHeaders + private readonly serviceVersion: string /** * A constructor function that creates a new instance of the Kinesis class. @@ -583,8 +572,8 @@ export class GetRecordsResponse { * The number of milliseconds the GetRecords response is from the * tip of the stream, indicating how far behind current time the * consumer is. - * - * A value of zero indicates that record processing is caught + * + * A value of zero indicates that record processing is caught * up, and there are no new records to process at this moment. */ millisBehindLatest: number diff --git a/src/internal/kms.ts b/src/internal/kms.ts index 979bb38..c56ace1 100644 --- a/src/internal/kms.ts +++ b/src/internal/kms.ts @@ -12,10 +12,9 @@ import { InvalidSignatureError, SignatureV4 } from './signature' * Class allowing to interact with Amazon AWS's KMS service */ export class KMSClient extends AWSClient { - method: HTTPMethod - commonHeaders: HTTPHeaders - - signature: SignatureV4 + private readonly signature: SignatureV4 + private readonly method: HTTPMethod + private readonly commonHeaders: HTTPHeaders /** * Create a KMSClient diff --git a/src/internal/s3.ts b/src/internal/s3.ts index 309cfa1..25a1c56 100644 --- a/src/internal/s3.ts +++ b/src/internal/s3.ts @@ -10,7 +10,7 @@ import { InvalidSignatureError, SignatureV4 } from './signature' /** Class allowing to interact with Amazon AWS's S3 service */ export class S3Client extends AWSClient { - signature: SignatureV4 + private readonly signature: SignatureV4 /** * Create a S3Client @@ -222,7 +222,9 @@ export class S3Client extends AWSClient { path: `/${bucketName}/${objectKey}`, headers: { Host: this.endpoint.host, - ...(params?.contentDisposition && { 'Content-Disposition': params.contentDisposition }), + ...(params?.contentDisposition && { + 'Content-Disposition': params.contentDisposition, + }), ...(params?.contentEncoding && { 'Content-Encoding': params.contentEncoding }), ...(params?.contentLength && { 'Content-Length': params.contentLength }), ...(params?.contentMD5 && { 'Content-MD5': params.contentMD5 }), @@ -654,7 +656,7 @@ type StorageClass = export interface PutObjectParams { /** * Specifies presentational information for the object. - * + * * For more information, see https://www.rfc-editor.org/rfc/rfc6266#section-4. */ contentDisposition?: string @@ -663,7 +665,7 @@ export interface PutObjectParams { * Specifies what content encodings have been applied to the object and thus * what decoding mechanisms must be applied to obtain the media-type referenced * by the ContentType option. - * + * * For more information, see https://www.rfc-editor.org/rfc/rfc9110.html#field.content-encoding. */ contentEncoding?: string @@ -671,7 +673,7 @@ export interface PutObjectParams { /** * Size of the body in bytes. This parameter is useful when the size of the body cannot be * determined automatically. - * + * * For more information, see https://www.rfc-editor.org/rfc/rfc9110.html#name-content-length. */ contentLength?: string @@ -680,7 +682,7 @@ export interface PutObjectParams { * The base64-encoded 128-bit MD5 digest of the message (without the headers) according to RFC 1864. * This header can be used as a message integrity check to verify that the data is the same data that * was originally sent. - * + * * Although it is optional, we recommend using the Content-MD5 mechanism as an end-to-end integrity * check. */ @@ -688,7 +690,7 @@ export interface PutObjectParams { /** * A standard MIME type describing the format of the contents. - * + * * For more information, see https://www.rfc-editor.org/rfc/rfc9110.html#name-content-type. */ contentType?: string diff --git a/src/internal/secrets-manager.ts b/src/internal/secrets-manager.ts index a675c40..7f9026f 100644 --- a/src/internal/secrets-manager.ts +++ b/src/internal/secrets-manager.ts @@ -13,17 +13,9 @@ import { InvalidSignatureError, SignatureV4 } from './signature' * Class allowing to interact with Amazon AWS's SecretsManager service */ export class SecretsManagerClient extends AWSClient { - /** - * HTTP Method to use when interacting with the Secrets Manager service. - */ - method: HTTPMethod - - /** - * HTTP headers to use accross all requests to the Secrets Manager service. - */ - commonHeaders: HTTPHeaders - - signature: SignatureV4 + private readonly signature: SignatureV4 + private readonly method: HTTPMethod + private readonly commonHeaders: HTTPHeaders /** * Create a SecretsManagerClient diff --git a/src/internal/sqs.ts b/src/internal/sqs.ts index 2bc4c5e..57c4a73 100644 --- a/src/internal/sqs.ts +++ b/src/internal/sqs.ts @@ -71,19 +71,24 @@ export class SQSClient extends AWSClient { * See https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessage.html#SQS-SendMessage-request-MessageAttributes * for more information. */ - const attributeParameters = Object.entries(options.messageAttributes).reduce((params, [name, attribute], i) => { - const valueParameterSuffix = attribute.type === 'Binary' ? 'BinaryValue' : 'StringValue' - return Object.assign(params, { - [`MessageAttribute.${i + 1}.Name`]: name, - [`MessageAttribute.${i + 1}.Value.${valueParameterSuffix}`]: attribute.value, - [`MessageAttribute.${i + 1}.Value.DataType`]: attribute.type - }) - }, {} as Record) - body = { ...body, ...attributeParameters }; + const attributeParameters = Object.entries(options.messageAttributes).reduce( + (params, [name, attribute], i) => { + const valueParameterSuffix = + attribute.type === 'Binary' ? 'BinaryValue' : 'StringValue' + return Object.assign(params, { + [`MessageAttribute.${i + 1}.Name`]: name, + [`MessageAttribute.${i + 1}.Value.${valueParameterSuffix}`]: + attribute.value, + [`MessageAttribute.${i + 1}.Value.DataType`]: attribute.type, + }) + }, + {} as Record + ) + body = { ...body, ...attributeParameters } } if (typeof options.delaySeconds !== 'undefined') { - body = { ...body, DelaySeconds: options.delaySeconds }; + body = { ...body, DelaySeconds: options.delaySeconds } } const signedRequest: SignedHTTPRequest = this.signature.sign( @@ -249,7 +254,7 @@ export interface SendMessageOptions { * The message attributes */ messageAttributes?: { - [name: string]: { type: 'String' | 'Number' | 'Binary', value: string } + [name: string]: { type: 'String' | 'Number' | 'Binary'; value: string } } /** diff --git a/src/internal/ssm.ts b/src/internal/ssm.ts index ddbe354..a79a939 100644 --- a/src/internal/ssm.ts +++ b/src/internal/ssm.ts @@ -12,9 +12,9 @@ import { InvalidSignatureError, SignatureV4 } from './signature' * Class allowing to interact with Amazon AWS's Systems Manager service */ export class SystemsManagerClient extends AWSClient { - method: HTTPMethod - commonHeaders: HTTPHeaders - signature: SignatureV4 + private readonly signature: SignatureV4 + private readonly method: HTTPMethod + private readonly commonHeaders: HTTPHeaders /** * Create a SystemsManagerClient