// Copyright Abridged, Inc. 2023,2024. All Rights Reserved. // Node module: @collabland/aws // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT import { DetectDocumentTextCommand, DetectDocumentTextCommandInput, DetectDocumentTextCommandOutput, S3Object, Textract, TextractClientConfig, } from '@aws-sdk/client-textract'; import {loggers} from '@collabland/common'; import { XRayInstrumented, XRayServiceClient, getDefaultConfig, } from '../index.js'; const {debug} = loggers('collabland:aws:textract:trace'); export class TextractServiceClient implements XRayInstrumented { readonly textract: Textract; xray?: XRayServiceClient; constructor(options?: TextractClientConfig) { this.textract = new Textract({ ...getDefaultConfig(), ...options, region: 'us-east-1', }); } enableXRay() { //TODO: Need to enable X-Ray, please see https://github.com/abridged/collabland-monorepo/issues/1673 // this.xray?.addMiddleware(this.textract, 'textract'); } async detectDocumentText( bufferOrS3Object: Buffer | S3Object, ): Promise { const input: DetectDocumentTextCommandInput = { Document: { ...(Buffer.isBuffer(bufferOrS3Object) ? {Bytes: bufferOrS3Object} : {S3Object: bufferOrS3Object}), }, }; debug('Calling DetectDocumentText for the following document: %O', input); const command = new DetectDocumentTextCommand(input); const response = await this.textract.send(command); debug('DetectDocumentText output: %O', response); return response; } }