@rempays/layer-core
v1.0.1-beta.0
Published
Core utilities layer for RemPays platform with AWS services integration (Cognito, S3, Secrets Manager, Textract, Facebook API)
Maintainers
Readme
@rempays/layer-core
Core utilities layer for RemPays platform with AWS services integration (Cognito, S3, Secrets Manager, Textract, Facebook API).
Installation
npm install @rempays/layer-coreModules
1. Cognito Service
Authentication and user management with AWS Cognito.
import { CognitoService } from '@rempays/layer-core/cognito';
const cognito = new CognitoService({
userPoolId: 'us-east-1_xxxxx',
clientId: 'xxxxx',
region: 'us-east-1'
});
// Register user
const result = await cognito.signUp({
phoneNumber: '+1234567890',
email: '[email protected]',
password: 'SecurePass123'
});
// Confirm registration with OTP
await cognito.confirmSignUp('+1234567890', '123456');
// Sign in with custom auth
const authResponse = await cognito.signInCustomAuth({
phoneNumber: '+1234567890'
});
// Verify OTP code
const tokens = await cognito.respondToAuthChallenge({
phoneNumber: '+1234567890',
code: '123456',
session: authResponse.session
});
// Get user info
const user = await cognito.getUser(tokens.accessToken!);
// Refresh token
const newTokens = await cognito.refreshToken({
refreshToken: tokens.refreshToken!
});Available Methods
CognitoService
signUp(params)- Register new userconfirmSignUp(phoneNumber, code)- Confirm registration with OTPsignInCustomAuth(params)- Sign in with custom authenticationsignInSRP(params)- Sign in with SRPrespondToAuthChallenge(params)- Verify OTP coderefreshToken(params)- Refresh access tokensgetUser(accessToken)- Get authenticated user infoadminGetUser(phoneNumber)- Get user info (admin)adminInitiateAuth(phoneNumber)- Initiate auth as admin
Environment Variables
COGNITO_USER_POOL_ID=us-east-1_XXXXXXXXX
COGNITO_CLIENT_ID=XXXXXXXXXXXXXXXXXXXXXXXXXX
AWS_REGION=us-east-1IAM Permissions Required
Your Lambda or application needs these permissions:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cognito-idp:SignUp",
"cognito-idp:ConfirmSignUp",
"cognito-idp:InitiateAuth",
"cognito-idp:RespondToAuthChallenge",
"cognito-idp:GetUser",
"cognito-idp:AdminGetUser",
"cognito-idp:AdminInitiateAuth"
],
"Resource": "arn:aws:cognito-idp:REGION:ACCOUNT_ID:userpool/USER_POOL_ID"
}
]
}Development
# Install dependencies
npm install
# Build
npm run build
# Watch mode
npm run devLicense
UNLICENSED
2. Secrets Manager
Manage secrets from AWS Secrets Manager.
import { getSecret, getSecrets, getSecretJSON } from '@rempays/layer-core/secrets';
// Get single secret
const apiKey = await getSecret('api_key');
// Get multiple secrets
const secrets = await getSecrets(['api_key', 'db_password']);
// Get JSON secret
const config = await getSecretJSON<{ host: string; port: number }>('db_config');3. S3 Service
File operations with AWS S3.
import { S3Service } from '@rempays/layer-core/s3';
// Upload file
const s3Uri = await S3Service.uploadFile({
key: 'documents/file.pdf',
body: buffer,
contentType: 'application/pdf',
metadata: { userId: '123' }
});
// Download file
const fileBuffer = await S3Service.downloadFile('documents/file.pdf');
// Generate document key
const key = S3Service.generateDocumentKey({
chatId: 'chat123',
messageId: 'msg456',
extension: 'pdf'
});4. Textract Service
Extract text from documents using AWS Textract.
import { TextractService } from '@rempays/layer-core/textract';
// Analyze document from S3
const text = await TextractService.analyzeDocumentFromS3('my-bucket', 'document.pdf');
// Analyze document from bytes
const imageBytes = new Uint8Array(buffer);
const extractedText = await TextractService.analyzeDocumentFromBytes(imageBytes);5. Facebook API (WhatsApp)
Send messages via WhatsApp Business API.
import { FacebookApi } from '@rempays/layer-core/facebook-api';
// Send text message
await FacebookApi.sendText({
to: '+1234567890',
body: 'Hello from RemPays!',
previewUrl: true
});
// Send template
await FacebookApi.sendTemplate({
to: '+1234567890',
name: 'welcome_template',
languageCode: 'en',
components: []
});
// Send interactive buttons
await FacebookApi.sendInteractiveButtons({
to: '+1234567890',
body: 'Choose an option:',
buttons: [
{ type: 'reply', id: 'opt1', title: 'Option 1' },
{ type: 'reply', id: 'opt2', title: 'Option 2' }
]
});
// Send interactive list
await FacebookApi.sendInteractiveList({
to: '+1234567890',
body: 'Select from menu:',
buttonText: 'View Menu',
sections: [
{
title: 'Main Options',
rows: [
{ id: '1', title: 'Option 1', description: 'Description 1' },
{ id: '2', title: 'Option 2', description: 'Description 2' }
]
}
]
});
// Send image
await FacebookApi.sendImage({
to: '+1234567890',
link: 'https://example.com/image.jpg',
caption: 'Check this out!'
});
// Send document
await FacebookApi.sendDocument({
to: '+1234567890',
link: 'https://example.com/document.pdf',
filename: 'invoice.pdf',
caption: 'Your invoice'
});
// Mark as read
await FacebookApi.markAsRead({ messageId: 'msg_id' });Available Methods Summary
CognitoService
signUp(params)- Register new userconfirmSignUp(phoneNumber, code)- Confirm registration with OTPsignInCustomAuth(params)- Sign in with custom authenticationsignInSRP(params)- Sign in with SRPrespondToAuthChallenge(params)- Verify OTP coderefreshToken(params)- Refresh access tokensgetUser(accessToken)- Get authenticated user infoadminGetUser(phoneNumber)- Get user info (admin)adminInitiateAuth(phoneNumber)- Initiate auth as admin
Secrets
getSecret(keyName, environment?)- Get single secretgetSecrets(keyNames, environment?)- Get multiple secretsgetSecretJSON<T>(keyName, environment?)- Get and parse JSON secret
S3Service
uploadFile(params)- Upload file to S3downloadFile(key)- Download file from S3generateDocumentKey(params)- Generate unique document key
TextractService
analyzeDocumentFromS3(bucket, key)- Extract text from S3 documentanalyzeDocumentFromBytes(bytes)- Extract text from byte array
FacebookApi
sendText(params)- Send text messagesendTemplate(params)- Send template messagesendInteractiveButtons(params)- Send interactive buttonssendInteractiveList(params)- Send interactive listsendImage(params)- Send imagesendDocument(params)- Send documentsendLocation(params)- Send locationmarkAsRead(params)- Mark message as readsetTyping(params)- Set typing indicator
