Skip to content

Commit

Permalink
Refactor client classes to make their properties private and readonly
Browse files Browse the repository at this point in the history
where relevant.
  • Loading branch information
oleiade committed Nov 17, 2023
1 parent 6bf3253 commit 74dea77
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 63 deletions.
8 changes: 4 additions & 4 deletions src/internal/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
12 changes: 5 additions & 7 deletions src/internal/event-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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}
*/
Expand Down
21 changes: 5 additions & 16 deletions src/internal/kinesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/internal/kms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions src/internal/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }),
Expand Down Expand Up @@ -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
Expand All @@ -663,15 +665,15 @@ 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

/**
* 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
Expand All @@ -680,15 +682,15 @@ 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.
*/
contentMD5?: string

/**
* 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
Expand Down
14 changes: 3 additions & 11 deletions src/internal/secrets-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 16 additions & 11 deletions src/internal/sqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>)
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<string, string>
)
body = { ...body, ...attributeParameters }
}

if (typeof options.delaySeconds !== 'undefined') {
body = { ...body, DelaySeconds: options.delaySeconds };
body = { ...body, DelaySeconds: options.delaySeconds }
}

const signedRequest: SignedHTTPRequest = this.signature.sign(
Expand Down Expand Up @@ -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 }
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/internal/ssm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 74dea77

Please sign in to comment.