Saltar al contenido principal

AWS - Visión General

Paquete: @tiendana/utils

Funciones para generar URLs de assets alojados en infraestructura AWS de Tiendana (CloudFront, S3, CDN público).

Configuración​

class AwsAssetsResources {
static cloudfrontId = 'd320eox725dcq1';
static s3BucketName = 'tiendana';
static cdnPublicDomain = 'cdn-public.tiendana.com';
}

Funciones Disponibles​

URLs Básicas​

  • getCloudfrontUrl(key) - URL de CloudFront para un key específico
  • getS3Url(key) - URL de S3 para un key específico
  • getCdnPublicUrl(key) - URL del CDN público para un key específico

URLs desde Attachments​

Funciones que extraen el key de un objeto Attachment y generan la URL correspondiente:

  • getCloudfrontUrlAttachment(attachment) - CloudFront URL desde Attachment
  • getS3UrlAttachment(attachment) - S3 URL desde Attachment
  • getCdnPublicUrlAttachment(attachment) - CDN público URL desde Attachment

Prioridad de keys: key5 > key4 > key

Firmas​

function getCloudfrontUrl<T extends string | null | undefined>(
key: T
): T extends string ? string : null

function getCloudfrontUrlAttachment(
attachment?: Attachment | null
): string | null

function getS3Url<T extends string | null | undefined>(
key: T
): T extends string ? string : null

function getS3UrlAttachment(
attachment?: Attachment | null
): string | null

function getCdnPublicUrl<T extends string | null | undefined>(
key: T
): T extends string ? string : null

function getCdnPublicUrlAttachment(
attachment?: Attachment | null
): string | null

Ejemplos de Uso​

URLs Básicas​

import { getCloudfrontUrl, getS3Url, getCdnPublicUrl } from '@tiendana/utils';

// CloudFront (CDN con caché)
const imageUrl = getCloudfrontUrl('products/image.jpg');
// 'https://d320eox725dcq1.cloudfront.net/products/image.jpg'

// S3 directo
const documentUrl = getS3Url('documents/file.pdf');
// 'https://tiendana.s3.amazonaws.com/documents/file.pdf'

// CDN público
const iconUrl = getCdnPublicUrl('icons/logo.svg');
// 'https://cdn-public.tiendana.com/icons/logo.svg'

// Null safety
getCloudfrontUrl(null); // null
getCloudfrontUrl(undefined); // null

Desde Attachments​

import { getCloudfrontUrlAttachment } from '@tiendana/utils';
import { Attachment } from '@tiendana/api-types';

const attachment: Attachment = {
key5: 'products/images/photo-optimized.jpg',
key4: 'products/images/photo-medium.jpg',
key: 'products/images/photo.jpg'
};

const url = getCloudfrontUrlAttachment(attachment);
// Usa key5 primero: 'https://d320eox725dcq1.cloudfront.net/products/images/photo-optimized.jpg'

// Sin attachment
getCloudfrontUrlAttachment(null); // null
getCloudfrontUrlAttachment(undefined); // null

Cuándo Usar Cada Función​

FunciónUso
getCloudfrontUrlImágenes, videos, assets estáticos con caché
getS3UrlArchivos privados, generación de presigned URLs
getCdnPublicUrlAssets públicos compartidos, fuentes, iconos

Tipos​

interface Attachment {
key?: string | null;
key4?: string | null; // Resolución media
key5?: string | null; // Resolución optimizada
// ... otros campos
}

Ver También​