{"version":3,"sources":["../../src/shared/shared.actions.ts","../../src/shared/shared.types.ts","../../src/modules/subscription/subscription.action.ts","../../src/modules/subscription/index.ts"],"sourcesContent":["import { fetch } from \"undici\";\nimport { join } from \"node:path\";\n\nimport { LemonsqueezyDataType } from \"~/shared\";\n\nimport type {\n  BaseLemonsqueezyResponse,\n  LemonsqueezyOptions,\n  PaginatedBaseLemonsqueezyResponse,\n} from \"~/shared\";\n\nexport async function requestLemonSqueeze<\n  TResponse extends\n    | BaseLemonsqueezyResponse<any>\n    | PaginatedBaseLemonsqueezyResponse<any>,\n  TData extends Record<string, any> = Record<string, any>\n>({\n  apiKey,\n  apiVersion = \"v1\",\n  baseUrl = \"https://api.lemonsqueezy.com\",\n  data,\n  headers,\n  include,\n  method = \"GET\",\n  page,\n  params,\n  path,\n}: LemonsqueezyOptions<TData>): Promise<TResponse> {\n  try {\n    const url = new URL(join(apiVersion, path), baseUrl);\n\n    if (include)\n      url.searchParams.append(\n        \"include\",\n        include.map((i) => LemonsqueezyDataType[i]).join(\",\")\n      );\n\n    if (page) url.searchParams.append(\"page\", page.toString());\n\n    if (params && method === \"GET\")\n      Object.entries(params).forEach(([key, value]) =>\n        url.searchParams.append(key, value)\n      );\n\n    const response = await fetch(url.href, {\n      headers: {\n        Accept: \"application/vnd.api+json\",\n        Authorization: `Bearer ${apiKey}`,\n        \"Content-Type\": \"application/vnd.api+json\",\n        ...headers,\n      },\n      method,\n      ...(data && method !== \"GET\"\n        ? {\n            body: JSON.stringify(data),\n          }\n        : {}),\n    });\n    if (!response.ok) {\n      const errorsJson = (await response.json()) as {\n        errors: Array<{\n          detail: string;\n          status: number;\n          title: string;\n        }>;\n      };\n      throw {\n        status: response.status,\n        message: response.statusText,\n        errors: errorsJson.errors,\n      };\n    }\n\n    const json = (await response.json()) as TResponse;\n    if (json.errors && json.errors.length > 0) throw json;\n\n    return json;\n  } catch (error) {\n    throw error;\n  }\n}\n","import type { RequestInit } from \"undici\";\n\nexport interface SharedModuleOptions {\n  apiKey: string;\n  page?: number;\n  include?: Array<keyof typeof LemonsqueezyDataType>;\n}\n\nexport interface SharedLemonsqueezyOptions {\n  apiVersion?: \"v1\";\n  baseUrl?: string;\n}\n\nexport interface LemonsqueezyOptions<\n  TData extends Record<string, any> = Record<string, any>\n> extends Omit<RequestInit, \"body\">,\n    SharedLemonsqueezyOptions,\n    SharedModuleOptions {\n  data?: TData;\n  params?: Record<string, any>;\n  method?:\n    | \"CONNECT\"\n    | \"DELETE\"\n    | \"GET\"\n    | \"HEAD\"\n    | \"OPTIONS\"\n    | \"PATCH\"\n    | \"POST\"\n    | \"PUT\"\n    | \"TRACE\";\n  path: string;\n}\n\nexport enum LemonsqueezyDataType {\n  checkouts = \"checkouts\",\n  customers = \"customers\",\n  discounts = \"discounts\",\n  files = \"files\",\n  license_key_instances = \"license-key-instances\",\n  license_keys = \"license-keys\",\n  order_items = \"order-items\",\n  orders = \"orders\",\n  products = \"products\",\n  stores = \"stores\",\n  subscriptions = \"subscriptions\",\n  subscription_invoices = \"subscription-invoices\",\n  users = \"users\",\n  variants = \"variants\",\n}\n\nexport interface BaseLemonsqueezyResponse<\n  TData,\n  TLinks = {\n    self: string;\n  }\n> {\n  data: TData;\n  errors?: Array<{\n    detail: string;\n    status: string | number;\n    title: string;\n  }>;\n  jsonapi: {\n    version: string;\n  };\n  links: TLinks;\n}\n\nexport interface PaginatedBaseLemonsqueezyResponse<\n  TData,\n  TLinks = {\n    first: string;\n    last: string;\n  }\n> extends BaseLemonsqueezyResponse<TData, TLinks> {\n  meta: {\n    page: {\n      currentPage: number;\n      from: number;\n      lastPage: number;\n      perPage: number;\n      to: number;\n      total: number;\n    };\n  };\n}\n","import { LemonsqueezyDataType } from \"~/shared\";\nimport { requestLemonSqueeze } from \"~/shared\";\n\nimport type {\n  ListAllSubscriptionsOptions,\n  ListAllSubscriptionsResult,\n  RetrieveSubscriptionOptions,\n  RetrieveSubscriptionResult,\n  UpdateSubscriptionOptions,\n  UpdateSubscriptionResult,\n} from \"./subscription.types\";\nimport type { SharedModuleOptions } from \"~/shared\";\n\n/**\n * List all subscriptions\n *\n * @description Returns a paginated list of subscriptions\n *\n * @docs https://docs.lemonsqueezy.com/api/subscriptions#list-all-subscriptions\n *\n * @param {Object} [options]\n *\n * @returns Returns a paginated list of subscription objects ordered by `created_at` (descending)\n */\nexport async function listAllSubscriptions(\n  options: ListAllSubscriptionsOptions & SharedModuleOptions\n): Promise<ListAllSubscriptionsResult> {\n  const { orderId, orderItemId, productId, storeId, variantId, ...rest } =\n    options;\n\n  return requestLemonSqueeze<ListAllSubscriptionsResult>({\n    params: {\n      ...(orderId ? { order_id: orderId } : {}),\n      ...(orderItemId ? { order_item_id: orderItemId } : {}),\n      ...(productId ? { product_id: productId } : {}),\n      ...(storeId ? { store_id: storeId } : {}),\n      ...(variantId ? { variant_id: variantId } : {}),\n    },\n    path: \"/subscriptions\",\n    ...rest,\n  });\n}\n\n/**\n * Retrieve subscription\n *\n * @description Retrieves the subscription with the given ID\n *\n * @docs https://docs.lemonsqueezy.com/api/subscriptions#retrieve-a-subscription\n *\n * @param {String} options.id - The ID of the subscription to retrieve\n *\n * @returns A subscription object\n */\nexport async function retrieveSubscription(\n  options: RetrieveSubscriptionOptions & SharedModuleOptions\n): Promise<RetrieveSubscriptionResult> {\n  const { id, ...rest } = options;\n\n  return requestLemonSqueeze<RetrieveSubscriptionResult>({\n    path: `/subscriptions/${id}`,\n    ...rest,\n  });\n}\n\n/**\n * Update subscription\n *\n * @description Update an existing subscription to specific parameters. With this endpoint, you can:\n *\n *  - Upgrade & Downgrade a subscripion to a different Product or Variant\n *  - Change payment pause collection behaviour\n *  - Update the billing date for when payments are collected\n *\n * When changing the plan of a subscription, we prorate the charge for the next billing cycle.\n * For example, if a customer buys your product on April 1st for $50, they'll be charged $50 immediately.\n * If on April 15th they upgrade to your $100 product, on May 1st they'll be charged $125.\n * This is made up of $100 for renewing, $50 of used time on your upgraded $100 plan from April 15th - May 1st, and then a credited -$25 for unused time on your $50 plan.\n *\n * If downgrading a subscription, we'll issue a credit which is then applied on the next invoice.\n *\n * Changing a subscription plan may change the billing date or charge immediately if:\n *\n *  - The variant has a different billing cycle (from monthly to yearly, etc)\n *  - The subscription is no longer free, or is now free\n *  - A trial starts or ends\n *\n * @docs https://docs.lemonsqueezy.com/api/subscriptions#update-a-subscription\n *\n * @param {String} options.id - The ID of the subscription to retrieve\n *\n * @returns A subscription object\n */\nexport async function updateSubscription(\n  options: UpdateSubscriptionOptions & SharedModuleOptions\n): Promise<UpdateSubscriptionResult> {\n  const { billingAnchor, cancelled, id, pause, productId, variantId, invoiceImmediately, disableProrations,  ...rest } =\n    options;\n\n  return requestLemonSqueeze<UpdateSubscriptionResult>({\n    data: {\n      data: {\n        attributes: {\n          billing_anchor: billingAnchor,\n          cancelled,\n          pause,\n          product_id: productId,\n          variant_id: variantId,\n          invoice_immediately: invoiceImmediately,\n          disable_prorations: disableProrations\n        },\n        id,\n        type: LemonsqueezyDataType.subscriptions,\n      },\n    },\n    path: `/subscriptions/${id}`,\n    method: \"PATCH\",\n    ...rest,\n  });\n}\n","import {\n  listAllSubscriptions,\n  retrieveSubscription,\n  updateSubscription,\n} from \"./subscription.action\";\n\nexport { listAllSubscriptions, retrieveSubscription, updateSubscription };\n\nexport type {\n  LemonsqueezySubscription,\n  ListAllSubscriptionsOptions,\n  ListAllSubscriptionsResult,\n  RetrieveSubscriptionOptions,\n  RetrieveSubscriptionResult,\n  UpdateSubscriptionOptions,\n  UpdateSubscriptionResult,\n} from \"./subscription.types\";\n\nexport default {\n  listAllSubscriptions,\n  retrieveSubscription,\n  updateSubscription,\n} as const;\n"],"mappings":"AAAA,OAAS,SAAAA,MAAa,SACtB,OAAS,QAAAC,MAAY,YAUrB,eAAsBC,EAKpB,CACA,OAAAC,EACA,WAAAC,EAAa,KACb,QAAAC,EAAU,+BACV,KAAAC,EACA,QAAAC,EACA,QAAAC,EACA,OAAAC,EAAS,MACT,KAAAC,EACA,OAAAC,EACA,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BC,EACqC,CACrC,GAAM,CAAE,QAAAC,EAAS,YAAAC,EAAa,UAAAC,EAAW,QAAAC,EAAS,UAAAC,EAAW,GAAGC,CAAK,EACnEN,EAEF,OAAOO,EAAgD,CACrD,OAAQ,CACN,GAAIN,EAAU,CAAE,SAAUA,CAAQ,EAAI,CAAC,EACvC,GAAIC,EAAc,CAAE,cAAeA,CAAY,EAAI,CAAC,EACpD,GAAIC,EAAY,CAAE,WAAYA,CAAU,EAAI,CAAC,EAC7C,GAAIC,EAAU,CAAE,SAAUA,CAAQ,EAAI,CAAC,EACvC,GAAIC,EAAY,CAAE,WAAYA,CAAU,EAAI,CAAC,CAC/C,EACA,KAAM,iBACN,GAAGC,CACL,CAAC,CACH,CAaA,eAAsBE,EACpBR,EACqC,CACrC,GAAM,CAAE,GAAAS,EAAI,GAAGH,CAAK,EAAIN,EAExB,OAAOO,EAAgD,CACrD,KAAM,kBAAkBE,CAAE,GAC1B,GAAGH,CACL,CAAC,CACH,CA8BA,eAAsBI,EACpBV,EACmC,CACnC,GAAM,CAAE,cAAAW,EAAe,UAAAC,EAAW,GAAAH,EAAI,MAAAI,EAAO,UAAAV,EAAW,UAAAE,EAAW,mBAAAS,EAAoB,kBAAAC,EAAoB,GAAGT,CAAK,EACjHN,EAEF,OAAOO,EAA8C,CACnD,KAAM,CACJ,KAAM,CACJ,WAAY,CACV,eAAgBI,EAChB,UAAAC,EACA,MAAAC,EACA,WAAYV,EACZ,WAAYE,EACZ,oBAAqBS,EACrB,mBAAoBC,CACtB,EACA,GAAAN,EACA,oBACF,CACF,EACA,KAAM,kBAAkBA,CAAE,GAC1B,OAAQ,QACR,GAAGH,CACL,CAAC,CACH,CCrGA,IAAOU,EAAQ,CACb,qBAAAC,EACA,qBAAAC,EACA,mBAAAC,CACF","names":["fetch","join","requestLemonSqueeze","apiKey","apiVersion","baseUrl","data","headers","include","method","page","params","path","url","join","i","LemonsqueezyDataType","key","value","response","fetch","errorsJson","json","error","LemonsqueezyDataType","listAllSubscriptions","options","orderId","orderItemId","productId","storeId","variantId","rest","requestLemonSqueeze","retrieveSubscription","id","updateSubscription","billingAnchor","cancelled","pause","invoiceImmediately","disableProrations","subscription_default","listAllSubscriptions","retrieveSubscription","updateSubscription"]}