{
  "version": 3,
  "sources": ["../src/index.ts"],
  "sourcesContent": ["import fetch from 'cross-fetch';\nimport { stringify } from 'javascript-stringify';\n\nimport type { PathLike } from 'fs';\nimport type { FileHandle } from 'fs/promises';\nimport type { ChartConfiguration } from 'chart.js';\nimport type { Response } from 'cross-fetch';\n\nconst SPECIAL_FUNCTION_REGEX: RegExp = /['\"]__BEGINFUNCTION__(.*?)__ENDFUNCTION__['\"]/g;\n\nconst USER_AGENT = `quickchart-js/3.1.0`;\n\ninterface PostData {\n  chart: string;\n  width?: number;\n  height?: number;\n  format?: string;\n  version?: string;\n  backgroundColor?: string;\n  devicePixelRatio?: number;\n  key?: string;\n}\n\ninterface GradientFillOption {\n  offset: number;\n  color: string;\n}\n\ninterface GradientDimensionOption {\n  width?: number;\n  height?: number;\n}\n\nfunction doStringify(chartConfig: ChartConfiguration): string | undefined {\n  const str = stringify(chartConfig);\n  if (!str) {\n    return undefined;\n  }\n  return str.replace(SPECIAL_FUNCTION_REGEX, '$1');\n}\n\nfunction postJson(url: string, payload: PostData): Promise<Response> {\n  return fetch(url, {\n    method: 'POST',\n    headers: {\n      'User-Agent': USER_AGENT,\n      'Content-Type': 'application/json',\n    },\n    body: JSON.stringify(payload),\n  });\n}\n\nclass QuickChart {\n  private host: string;\n  private scheme: string;\n  private width: number;\n  private height: number;\n  private devicePixelRatio: number;\n  private backgroundColor: string;\n  private format: string;\n  private version: string;\n\n  private chart?: string;\n  private apiKey?: string;\n  private accountId?: string;\n\n  constructor(apiKey?: string, accountId?: string) {\n    this.apiKey = apiKey;\n    this.accountId = accountId;\n\n    this.host = 'quickchart.io';\n    this.scheme = 'https';\n\n    this.chart = undefined;\n    this.width = 500;\n    this.height = 300;\n    this.devicePixelRatio = 1.0;\n    this.backgroundColor = '#ffffff';\n    this.format = 'png';\n    this.version = '2.9.4';\n  }\n\n  setConfig(chartConfig: string | ChartConfiguration): QuickChart {\n    this.chart = typeof chartConfig === 'string' ? chartConfig : doStringify(chartConfig);\n    return this;\n  }\n\n  setHost(host: string): QuickChart {\n    this.host = host;\n    return this;\n  }\n\n  setScheme(scheme: string): QuickChart {\n    this.scheme = scheme;\n    return this;\n  }\n\n  setWidth(width: number): QuickChart {\n    this.width = width;\n    return this;\n  }\n\n  setHeight(height: number): QuickChart {\n    this.height = height;\n    return this;\n  }\n\n  setBackgroundColor(color: string): QuickChart {\n    this.backgroundColor = color;\n    return this;\n  }\n\n  setDevicePixelRatio(ratio: number): QuickChart {\n    this.devicePixelRatio = ratio;\n    return this;\n  }\n\n  setFormat(fmt: string): QuickChart {\n    this.format = fmt;\n    return this;\n  }\n\n  setVersion(version: string): QuickChart {\n    this.version = version;\n    return this;\n  }\n\n  isValid(): boolean {\n    if (!this.chart) {\n      return false;\n    }\n    return true;\n  }\n\n  private getBaseUrl(): string {\n    return `${this.scheme}://${this.host}`;\n  }\n\n  private getUrlObject(): URL {\n    if (!this.isValid()) {\n      throw new Error('You must call setConfig before getUrl');\n    }\n    const ret = new URL(`${this.getBaseUrl()}/chart`);\n    ret.searchParams.append('c', this.chart!);\n    ret.searchParams.append('w', String(this.width));\n    ret.searchParams.append('h', String(this.height));\n    ret.searchParams.append('ref', 'qc-js');\n    if (this.devicePixelRatio !== 1.0) {\n      ret.searchParams.append('devicePixelRatio', String(this.devicePixelRatio));\n    }\n    if (this.backgroundColor) {\n      ret.searchParams.append('bkg', this.backgroundColor);\n    }\n    if (this.format) {\n      ret.searchParams.append('f', this.format);\n    }\n    if (this.version) {\n      ret.searchParams.append('v', this.version);\n    }\n    if (this.apiKey) {\n      ret.searchParams.append('key', this.apiKey);\n    }\n    return ret;\n  }\n\n  getUrl(): string {\n    return this.getUrlObject().href;\n  }\n\n  getSignedUrl(): string {\n    if (!this.accountId || !this.apiKey) {\n      throw new Error(\n        'You must set accountId and apiKey in the QuickChart constructor to use getSignedUrl()',\n      );\n    }\n    const crypto = require('crypto');\n    const urlObj = this.getUrlObject();\n    const chartStr = urlObj.searchParams.get('c');\n\n    const signature = crypto.createHmac('sha256', this.apiKey).update(chartStr).digest('hex');\n    urlObj.searchParams.append('sig', signature);\n    urlObj.searchParams.append('accountId', this.accountId);\n    urlObj.searchParams.delete('key');\n    return urlObj.href;\n  }\n\n  getPostData(): PostData {\n    if (!this.isValid()) {\n      throw new Error('You must call setConfig creating post data');\n    }\n\n    const { width, height, chart, format, version, backgroundColor, devicePixelRatio, apiKey } =\n      this;\n    const postData: PostData = {\n      width,\n      height,\n      chart: chart!,\n    };\n    if (format) {\n      postData.format = format;\n    }\n    if (version) {\n      postData.version = version;\n    }\n    if (backgroundColor) {\n      postData.backgroundColor = backgroundColor;\n    }\n    if (devicePixelRatio) {\n      postData.devicePixelRatio = devicePixelRatio;\n    }\n    if (apiKey) {\n      postData.key = apiKey;\n    }\n    return postData;\n  }\n\n  async getShortUrl(): Promise<string> {\n    if (!this.isValid()) {\n      throw new Error('You must call setConfig before getUrl');\n    }\n    if (this.host !== 'quickchart.io') {\n      throw new Error('Short URLs must use quickchart.io host');\n    }\n\n    const resp = await postJson(`${this.getBaseUrl()}/chart/create`, this.getPostData());\n    if (!resp.ok) {\n      const quickchartError = resp.headers.get('x-quickchart-error');\n      const details = quickchartError ? `\\n${quickchartError}` : '';\n      throw new Error(`Chart shorturl creation failed with status code ${resp.status}${details}`);\n    }\n\n    const json = (await resp.json()) as undefined | { success?: boolean; url?: string };\n    if (!json || !json.success || !json.url) {\n      throw new Error('Received failure response from chart shorturl endpoint');\n    } else {\n      return json.url;\n    }\n  }\n\n  async toBinary(): Promise<Buffer> {\n    if (!this.isValid()) {\n      throw new Error('You must call setConfig before getUrl');\n    }\n\n    const resp = await postJson(`${this.getBaseUrl()}/chart`, this.getPostData());\n    if (!resp.ok) {\n      const quickchartError = resp.headers.get('x-quickchart-error');\n      const details = quickchartError ? `\\n${quickchartError}` : '';\n      throw new Error(`Chart creation failed with status code ${resp.status}${details}`);\n    }\n    const data = await resp.arrayBuffer();\n    return Buffer.from(data);\n  }\n\n  async toDataUrl(): Promise<string> {\n    const buf = await this.toBinary();\n    const b64buf = buf.toString('base64');\n    const type = this.format === 'svg' ? 'svg+xml' : 'png';\n    return `data:image/${type};base64,${b64buf}`;\n  }\n\n  async toFile(pathOrDescriptor: PathLike | FileHandle): Promise<void> {\n    const fs = require('fs');\n    const buf = await this.toBinary();\n    fs.writeFileSync(pathOrDescriptor, buf);\n  }\n\n  static getGradientFillHelper(\n    direction: string,\n    colors: string[],\n    dimensions?: GradientDimensionOption,\n  ): string {\n    return `__BEGINFUNCTION__getGradientFillHelper(${JSON.stringify(direction)}, ${JSON.stringify(\n      colors,\n    )}, ${JSON.stringify(dimensions)})__ENDFUNCTION__`;\n  }\n\n  static getGradientFill(\n    colorOptions: GradientFillOption[],\n    linearGradient: [number, number, number, number],\n  ): string {\n    return `__BEGINFUNCTION__getGradientFill(${JSON.stringify(colorOptions)}, ${JSON.stringify(\n      linearGradient,\n    )})__ENDFUNCTION__`;\n  }\n\n  static getImageFill(url: string): string {\n    return `__BEGINFUNCTION__getImageFill(${JSON.stringify(url)})__ENDFUNCTION__`;\n  }\n\n  static pattern = {\n    draw: function (\n      shapeType: string,\n      backgroundColor: string,\n      patternColor: string,\n      requestedSize: number,\n    ): string {\n      return `__BEGINFUNCTION__pattern.draw(${JSON.stringify(shapeType)}, ${JSON.stringify(\n        backgroundColor,\n      )}, ${JSON.stringify(patternColor)}, ${JSON.stringify(requestedSize)})__ENDFUNCTION__`;\n    },\n  };\n}\n\nexport default QuickChart;\n"],
  "mappings": "AAAA;AACA;AAOA,MAAM,yBAAiC;AAEvC,MAAM,aAAa;AAuBnB,qBAAqB,aAAqD;AACxE,QAAM,MAAM,UAAU;AACtB,MAAI,CAAC,KAAK;AACR,WAAO;AAAA;AAET,SAAO,IAAI,QAAQ,wBAAwB;AAAA;AAG7C,kBAAkB,KAAa,SAAsC;AACnE,SAAO,MAAM,KAAK;AAAA,IAChB,QAAQ;AAAA,IACR,SAAS;AAAA,MACP,cAAc;AAAA,MACd,gBAAgB;AAAA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