Skip to content

Commit 3ce6dce

Browse files
committed
Refactor AwsTextractService to encapsulate client creation logic
- Introduced a private method createTextractClient in backend/src/services/aws-textract.service.ts to streamline the initialization of the AWS Textract client. - Removed redundant code from the constructor, enhancing readability and maintainability. - Improved logging for client initialization without exposing sensitive credentials.
1 parent b07f552 commit 3ce6dce

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

backend/src/services/aws-textract.service.ts

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,7 @@ export class AwsTextractService {
2727

2828
constructor(private readonly configService: ConfigService) {
2929
try {
30-
const region = this.configService.get<string>('aws.region') || 'us-east-1';
31-
const accessKeyId = this.configService.get<string>('aws.aws.accessKeyId');
32-
const secretAccessKey = this.configService.get<string>('aws.aws.secretAccessKey');
33-
const sessionToken = this.configService.get<string>('aws.aws.sessionToken');
34-
35-
// Create client config with required region
36-
const clientConfig: any = { region };
37-
38-
// Only add credentials if explicitly provided
39-
if (accessKeyId && secretAccessKey) {
40-
clientConfig.credentials = {
41-
accessKeyId,
42-
secretAccessKey,
43-
...(sessionToken && { sessionToken }),
44-
};
45-
}
46-
47-
// Initialize AWS Textract client with more robust config
48-
this.client = new TextractClient(clientConfig);
49-
50-
// Log credential configuration for debugging (without exposing actual credentials)
51-
this.logger.log(
52-
`AWS Textract client initialized with region ${region} and credentials ${accessKeyId ? '(provided)' : '(missing)'}, session token ${sessionToken ? '(provided)' : '(not provided)'}`,
53-
);
30+
this.client = this.createTextractClient();
5431

5532
// Initialize rate limiter (10 requests per minute per IP by default)
5633
const requestsPerMinute =
@@ -68,6 +45,39 @@ export class AwsTextractService {
6845
}
6946
}
7047

48+
/**
49+
* Creates and configures the AWS Textract client
50+
* @returns Configured TextractClient instance
51+
*/
52+
private createTextractClient(): TextractClient {
53+
const region = this.configService.get<string>('aws.region') || 'us-east-1';
54+
const accessKeyId = this.configService.get<string>('aws.aws.accessKeyId');
55+
const secretAccessKey = this.configService.get<string>('aws.aws.secretAccessKey');
56+
const sessionToken = this.configService.get<string>('aws.aws.sessionToken');
57+
58+
// Create client config with required region
59+
const clientConfig: any = { region };
60+
61+
// Only add credentials if explicitly provided
62+
if (accessKeyId && secretAccessKey) {
63+
clientConfig.credentials = {
64+
accessKeyId,
65+
secretAccessKey,
66+
...(sessionToken && { sessionToken }),
67+
};
68+
}
69+
70+
// Initialize AWS Textract client with more robust config
71+
const client = new TextractClient(clientConfig);
72+
73+
// Log credential configuration for debugging (without exposing actual credentials)
74+
this.logger.log(
75+
`AWS Textract client initialized with region ${region} and credentials ${accessKeyId ? '(provided)' : '(missing)'}, session token ${sessionToken ? '(provided)' : '(not provided)'}`,
76+
);
77+
78+
return client;
79+
}
80+
7181
/**
7282
* Extract text from a medical lab report image or PDF
7383
* @param fileBuffer The file buffer containing the image or PDF

0 commit comments

Comments
 (0)