{"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  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, ...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        },\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,SAAS,aAAa;AACtB,SAAS,YAAY;AAUrB,eAAsB,oBAKpB;AAAA,EACA;AAAA,EACA,aAAa;AAAA,EACb,UAAU;AAAA,EACV;AAAA,EACA;AAAA,EACA;AAAA,EACA,SAAS;AAAA,EACT;AAAA,EACA;AAAA,EACA;AACF,GAAmD;AACjD,MAAI;AACF,UAAM,MAAM,IAAI,IAAI,KAAK,YAAY,IAAI,GAAG,OAAO;AAEnD,QAAI;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