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O,MAAX,IAAI,EAAQ,GAAG,CAAC,QAAQ,EAAE,EAAE;YACjD,MAAM;YACN,OAAO,EAAE;gBACP,cAAc,EAAE,kBAAkB;gBAClC,GAAG,OAAO;aACX;SACF,CAAC,CAAC;QAEH,MAAM,YAAY,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QAC3C,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;YAChB,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,YAA4B,CAAC;YACxD,MAAM,IAAI,KAAK,CAAC,uBAAuB,OAAO,YAAY,KAAK,EAAE,CAAC,CAAC;SACpE;QAED,OAAO,YAAsB,CAAC;KAC/B;IAAC,OAAO,CAAC,EAAE;QACV,MAAM,YAAY,GAChB,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,eAAe,CAAC,CAAC;QAExE,MAAM,IAAI,wBAAwB,CAChC,2BAA2B,YAAY,EAAE,CAC1C,CAAC;KACH;AACH,CAAC,gDAED,KAAK;IACH,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,cAAc,EAAE,CAAC;IAC1D,OAAO,EAAE,aAAa,EAAE,UAAU,WAAW,EAAE,EAAE,CAAC;AACpD,CAAC","sourcesContent":["import { getEnvUrls, type Env } from './constants';\nimport { SubscriptionServiceError } from './errors';\nimport type {\n  AuthUtils,\n  GetSubscriptionsResponse,\n  ISubscriptionService,\n} from './types';\n\nexport type SubscriptionServiceConfig = {\n  env: Env;\n  auth: AuthUtils;\n  fetchFn: typeof globalThis.fetch;\n};\n\ntype ErrorMessage = {\n  message: string;\n  error: string;\n};\n\nexport const SUBSCRIPTION_URL = (env: Env, path: string) =>\n  `${getEnvUrls(env).subscriptionApiUrl}/api/v1/${path}`;\n\nexport class SubscriptionService implements ISubscriptionService {\n  readonly #env: Env;\n\n  readonly #fetch: typeof globalThis.fetch;\n\n  public authUtils: AuthUtils;\n\n  constructor(config: SubscriptionServiceConfig) {\n    this.#env = config.env;\n    this.authUtils = config.auth;\n    this.#fetch = config.fetchFn;\n  }\n\n  async getSubscriptions(): Promise<GetSubscriptionsResponse> {\n    const path = 'subscriptions';\n    return await this.#makeRequest(path);\n  }\n\n  async cancelSubscription(params: { subscriptionId: string }): Promise<void> {\n    const path = `subscriptions/${params.subscriptionId}`;\n    return await this.#makeRequest(path, 'DELETE');\n  }\n\n  async #makeRequest<Result>(\n    path: string,\n    method: 'GET' | 'POST' | 'DELETE' | 'PUT' | 'PATCH' = 'GET',\n  ): Promise<Result> {\n    try {\n      const headers = await this.#getAuthorizationHeader();\n      const url = new URL(SUBSCRIPTION_URL(this.#env, path));\n\n      const response = await this.#fetch(url.toString(), {\n        method,\n        headers: {\n          'Content-Type': 'application/json',\n          ...headers,\n        },\n      });\n\n      const responseBody = await response.json();\n      if (!response.ok) {\n        const { message, error } = responseBody as ErrorMessage;\n        throw new Error(`HTTP error message: ${message}, error: ${error}`);\n      }\n\n      return responseBody as Result;\n    } catch (e) {\n      const errorMessage =\n        e instanceof Error ? e.message : JSON.stringify(e ?? 'unknown error');\n\n      throw new SubscriptionServiceError(\n        `failed to make request. ${errorMessage}`,\n      );\n    }\n  }\n\n  async #getAuthorizationHeader(): Promise<{ Authorization: string }> {\n    const accessToken = await this.authUtils.getAccessToken();\n    return { Authorization: `Bearer ${accessToken}` };\n  }\n}\n"]}