{"version":3,"file":"thumbor-client.umd.cjs","sources":["../src/helpers/index.ts","../src/lib/enums.ts","../src/lib/thumbor.ts","../src/lib/index.ts"],"sourcesContent":["export function isDefined<T>(value: T) {\n  return typeof value !== 'undefined'\n}\n","/**\n * Enum representing different fitting types for an element.\n */\nexport enum FitInType {\n  /**\n   * Default fitting type.\n   */\n  DEFAULT = 'DEFAULT',\n  /**\n   * Full fitting type.\n   */\n  FULL = 'FULL',\n  /**\n   * Adaptive fitting type.\n   */\n  ADAPTIVE = 'ADAPTIVE'\n}\n\n/**\n * Enum representing vertical positions.\n */\nexport enum VerticalPosition {\n  /**\n   * Top vertical position.\n   */\n  TOP = 'TOP',\n  /**\n   * Middle vertical position.\n   */\n  MIDDLE = 'MIDDLE',\n  /**\n   * Bottom vertical position.\n   */\n  BOTTOM = 'BOTTOM'\n}\n\n/**\n * Enum representing horizontal positions.\n */\nexport enum HorizontalPosition {\n  /**\n   * Left horizontal position.\n   */\n  LEFT = 'LEFT',\n  /**\n   * Center horizontal position.\n   */\n  CENTER = 'CENTER',\n  /**\n   * Right horizontal position.\n   */\n  RIGHT = 'RIGHT'\n}\n","import crypto from 'crypto'\nimport { isDefined } from '../helpers'\nimport { Parameters, ThumborClientOptions, WindowSizeAndPosition } from './types.ts'\nimport { FitInType, HorizontalPosition, VerticalPosition } from './enums.ts'\n\n/**\n * Class representing a Thumbor client for generating image URLs.\n */\nexport class Thumbor {\n  private readonly url: string\n  private readonly key?: string\n  private parameters: Parameters = this.defaultParameters()\n\n  /**\n   * Constructs a new Thumbor instance.\n   * @param url The URL of the Thumbor server.\n   * @param key Optional security key for accessing the Thumbor server.\n   */\n  constructor({ url, key }: ThumborClientOptions) {\n    this.url = url\n    this.key = key\n  }\n\n  /**\n   * Creates default parameters.\n   * @returns Default parameters object.\n   */\n  public defaultParameters(): Parameters {\n    return {\n      imagePath: '',\n      width: 0,\n      height: 0,\n      smart: false,\n      trimFlag: false,\n      withFlipHorizontally: false,\n      withFlipVertically: false,\n      filtersCalls: []\n    }\n  }\n\n  /**\n   * Sets the image path from a URL.\n   * @param url The URL of the image.\n   * @returns The Thumbor instance.\n   */\n  public fromUrl(url: string) {\n    this.parameters.imagePath = url\n    return this\n  }\n\n  /**\n   * Sets the path of the image.\n   * @param path The path of the image.\n   * @returns The Thumbor instance.\n   */\n  public setPath(path: string) {\n    this.parameters.imagePath = (path.startsWith('/')) ? path.slice(1, path.length) : path\n    return this\n  }\n\n  /**\n   * Resizes the image.\n   * @param width The width of the image.\n   * @param height The height of the image.\n   * @returns The Thumbor instance.\n   */\n  public resize(width: Parameters['width'], height: Parameters['height']) {\n    this.parameters.width = width\n    this.parameters.height = height\n    this.parameters.fitInType = undefined\n    return this\n  }\n\n  /**\n   * Sets smart cropping flag.\n   * @param smartCrop Flag indicating whether to use smart cropping.\n   * @returns The Thumbor instance.\n   */\n  public smartCrop(smartCrop: boolean = true) {\n    this.parameters.smart = smartCrop\n    return this\n  }\n\n  /**\n   * Sets trim flag.\n   * @returns The Thumbor instance.\n   */\n  public trim() {\n    this.parameters.trimFlag = true\n    return this\n  }\n\n  /**\n   * Sets fit-in parameters.\n   * @param width The width to fit in.\n   * @param height The height to fit in.\n   * @param type The fitting type.\n   * @returns The Thumbor instance.\n   */\n  public fitIn(width: number, height: number, type = FitInType.DEFAULT) {\n    this.parameters.width = width\n    this.parameters.height = height\n    this.parameters.fitInType = type\n    return this\n  }\n\n  /**\n   * Flips the image horizontally.\n   * @returns The Thumbor instance.\n   */\n  public flipHorizontally() {\n    this.parameters.withFlipHorizontally = true\n    return this\n  }\n\n  /**\n   * Flips the image vertically.\n   * @returns The Thumbor instance.\n   */\n  public flipVertically() {\n    this.parameters.withFlipVertically = true\n    return this\n  }\n\n  /**\n   * Sets horizontal alignment.\n   * @param halign The horizontal alignment value.\n   * @returns The Thumbor instance.\n   */\n  public halign(halign: HorizontalPosition) {\n    this.parameters.halignValue = halign\n    return this\n  }\n\n  /**\n   * Sets vertical alignment.\n   * @param valign The vertical alignment value.\n   * @returns The Thumbor instance.\n   */\n  public valign(valign: VerticalPosition) {\n    this.parameters.valignValue = valign\n    return this\n  }\n\n  /**\n   * Adds a filter call for image processing.\n   * @param filterCall The filter call.\n   * @returns The Thumbor instance.\n   */\n  public filter(filterCall: string) {\n    this.parameters.filtersCalls.push(filterCall)\n    return this\n  }\n\n  /**\n   * Sets crop values for the image.\n   * @param crop The crop values.\n   * @returns The Thumbor instance.\n   */\n  public crop(crop: WindowSizeAndPosition) {\n    this.parameters.cropValues = crop\n    return this\n  }\n\n  /**\n   * Builds the URL for the image with applied operations.\n   * @returns The generated image URL.\n   */\n  public buildURL() {\n    const operation = this.getOperationPath()\n    const dataToEncrypt = operation + '/' + this.parameters.imagePath\n\n    if (this.key) {\n      const digest = crypto\n        .createHmac('sha1', this.key)\n        .update(dataToEncrypt)\n        .digest('base64')\n        .replace(/\\+/g, '-').replace(/\\//g, '_')\n\n      this.parameters = this.defaultParameters()\n\n      return this.url + '/' + digest + '/' + dataToEncrypt\n    }\n\n    this.parameters = this.defaultParameters()\n\n    return this.url + '/unsafe/' + dataToEncrypt\n  }\n\n  /**\n   * Constructs URL parts based on parameters.\n   * @returns Array of URL parts.\n   */\n  private getURLParts() {\n    const parts = []\n\n    if (this.parameters.trimFlag) {\n      parts.push('trim')\n    }\n\n    if (this.parameters.cropValues) {\n      parts.push(\n        this.parameters.cropValues.left.toString() +\n        'x' + this.parameters.cropValues.top.toString() +\n        ':' + this.parameters.cropValues.right.toString() +\n        'x' + this.parameters.cropValues.bottom.toString()\n      )\n    }\n\n    switch (this.parameters.fitInType) {\n      case FitInType.DEFAULT:\n        parts.push('fit-in')\n        break\n      case FitInType.FULL:\n        parts.push('full-fit-in')\n        break\n      case FitInType.ADAPTIVE:\n        parts.push('adaptative-fit-in')\n        break\n      default:\n        break\n    }\n\n    if (\n      isDefined(this.parameters.width) ||\n      isDefined(this.parameters.height) ||\n      this.parameters.withFlipHorizontally ||\n      this.parameters.withFlipVertically\n    ) {\n      let sizeString = ''\n\n      if (this.parameters.withFlipHorizontally) {\n        sizeString += '-'\n      }\n\n      sizeString += this.parameters.width\n\n      sizeString += 'x'\n\n      if (this.parameters.withFlipVertically) {\n        sizeString += '-'\n      }\n\n      sizeString += this.parameters.height\n\n      parts.push(sizeString)\n    }\n\n    if (this.parameters.halignValue) {\n      parts.push(this.parameters.halignValue)\n    }\n\n    if (this.parameters.valignValue) {\n      parts.push(this.parameters.valignValue)\n    }\n\n    if (this.parameters.smart) {\n      parts.push('smart')\n    }\n\n    if (this.parameters.filtersCalls.length > 0) {\n      parts.push('filters:' + this.parameters.filtersCalls.join(':'))\n    }\n\n    return parts\n  }\n\n  /**\n   * Constructs the operation path for the URL.\n   * @returns The constructed operation path.\n   */\n  private getOperationPath() {\n    const parts = this.getURLParts()\n\n    if (parts.length === 0) {\n      return ''\n    }\n\n    return parts.join('/')\n  }\n}\n","import { Thumbor } from './thumbor.ts'\nimport { ThumborClientOptions } from './types.ts'\n\n/**\n * Factory function to create a Thumbor instance with given options.\n * @param options Options for configuring the Thumbor instance.\n * @returns A new instance of Thumbor.\n */\nexport function createThumbor(options: ThumborClientOptions) {\n  return new Thumbor(options)\n}\n"],"names":["isDefined","value","FitInType","Thumbor","url","key","__publicField","path","width","height","smartCrop","type","halign","valign","filterCall","crop","dataToEncrypt","digest","crypto","parts","sizeString","createThumbor","options"],"mappings":"+aAAO,SAASA,EAAaC,EAAU,CACrC,OAAO,OAAOA,EAAU,GAC1B,CCCY,IAAAC,GAAAA,IAIVA,EAAA,QAAU,UAIVA,EAAA,KAAO,OAIPA,EAAA,SAAW,WAZDA,IAAAA,GAAA,CAAA,CAAA,ECKL,MAAMC,CAAQ,CAUnB,YAAY,CAAE,IAAAC,EAAK,IAAAC,GAA6B,CAT/BC,EAAA,YACAA,EAAA,YACTA,EAAA,kBAAyB,KAAK,kBAAkB,GAQtD,KAAK,IAAMF,EACX,KAAK,IAAMC,CAAA,CAON,mBAAgC,CAC9B,MAAA,CACL,UAAW,G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