{"version":3,"sources":["../src/index.ts","../src/UmamiApiClient.ts","../src/defaults.ts","../src/classes/website.ts","../src/classes/user-account.ts"],"sourcesContent":["\"use strict\";\nimport UmamiApiClient from \"./UmamiApiClient\";\nexport default UmamiApiClient;\n","import axios, {\n\ttype AxiosInstance,\n\ttype AxiosResponse,\n\ttype InternalAxiosRequestConfig,\n} from \"axios\";\nimport { Website, type WebsiteData } from \"./classes/website\";\nimport { UserAccount, type UserAccountData } from \"./classes/user-account\";\nimport { DEFAULT_HTTP_CLIENT_TIMEOUT_MS, DEFAULT_USER_AGENT } from \"./defaults\";\n\ninterface AuthData {\n\ttoken: string;\n\tuser: {\n\t\tuserId: number;\n\t\tusername: string;\n\t\tisAdmin: boolean;\n\t\taccountUuid: string;\n\t\tiat?: number;\n\t\tshareToken?: string;\n\t};\n}\n\ninterface PageViewPayload {\n\twebsite: string;\n\turl: string;\n\treferrer?: string;\n\thostname: string;\n\tlanguage?: string;\n\tscreen?: string;\n}\n\ninterface EventPayload {\n\twebsite: string;\n\turl: string;\n\treferrer?: string;\n\thostname: string;\n\tlanguage?: string;\n\tscreen?: string;\n\tevent_name: string;\n\tevent_data: string;\n}\n\ntype CollectPayload = PageViewPayload | EventPayload;\n\nexport default class UmamiApiClient {\n\tprivate readonly _axios: AxiosInstance;\n\tprivate readonly _auth: Promise<AxiosResponse<AuthData>>;\n\tprivate _lastAuthCheck: number = Date.now();\n\n\tpublic async getCurrentUser() {\n\t\treturn (await this._auth).data.user;\n\t}\n\n\t/**\n\t * @param server The Umami installation hostname (e.g. app.umami.is). The protocol, if present, will be removed.\n\t * @param username Username of the user you want to use.\n\t * @param password Password of the user you want to use.\n\t * @returns An authenticated class instance\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/auth/login.js Relevant Umami source code}\n\t */\n\tconstructor(server: string, username: string, password: string) {\n\t\tif (!server) throw new Error(\"A server hostname is required\");\n\t\tserver = server.replace(/https?:\\/\\//, \"\").replace(/\\/$/, \"\");\n\t\tif (!username || !password) throw new Error(\"A username and a password are required\");\n\n\t\tthis._axios = axios.create({\n\t\t\tbaseURL: `https://${server}/api`,\n\t\t\ttimeout: DEFAULT_HTTP_CLIENT_TIMEOUT_MS,\n\t\t});\n\n\t\tthis._axios.interceptors.request.use(this._verifyAuth.bind(this));\n\n\t\tthis._auth = this._axios.post(\"/auth/login\", { username, password });\n\t}\n\n\tprivate async _verifyAuth(config: InternalAxiosRequestConfig) {\n\t\tif (config.url == \"/auth/login\" || config.url == \"/collect\") return config;\n\n\t\tconst auth = await this._auth;\n\n\t\tconfig.headers[\"Authorization\"] = `Bearer ${auth.data.token}`;\n\n\t\tif (config.url == \"/auth/verify\") return config;\n\n\t\tif (this._lastAuthCheck + 60 * 60 * 1000 < Date.now()) {\n\t\t\tthis._lastAuthCheck = Date.now();\n\n\t\t\ttry {\n\t\t\t\tawait this._axios.get(\"/auth/verify\");\n\t\t\t} catch (error) {\n\t\t\t\tconsole.error({ axiosConfig: config });\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t}\n\n\t\treturn config;\n\t}\n\n\t/**\n\t * Collects a pageview\n\t * @param type The type of event to send\n\t * @param payload The payload of the pageview\n\t * @param userAgent Value of the User-Agent header. Necessary for platform detection. Defaults to Firefox on Mac OS on a laptop\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/collect.js#L75 Relevant Umami source code}\n\t */\n\tpublic async collect(\n\t\ttype: \"pageview\",\n\t\tpayload: PageViewPayload,\n\t\tuserAgent?: string,\n\t): Promise<string>;\n\t/**\n\t * Collects an event\n\t * @param type The type of event to send\n\t * @param payload The payload of the event\n\t * @param userAgent Value of the User-Agent header. Necessary for platform detection. Defaults to Firefox on Mac OS on a laptop\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/collect.js#L77 Relevant Umami source code}\n\t */\n\tpublic async collect(type: \"event\", payload: EventPayload, userAgent?: string): Promise<string>;\n\tpublic async collect(\n\t\ttype: \"pageview\" | \"event\",\n\t\tpayload: CollectPayload,\n\t\tuserAgent: string = DEFAULT_USER_AGENT,\n\t) {\n\t\tif (!userAgent) throw new Error(\"A user agent is required. See https://umami.is/docs/api\");\n\n\t\tconst { data } = await this._axios.post<string>(\n\t\t\t\"/collect\",\n\t\t\t{ type, payload },\n\t\t\t{ headers: { \"User-Agent\": userAgent } },\n\t\t);\n\t\treturn data;\n\t}\n\n\t/**\n\t * Collects a pageview\n\t * @param server The Umami installation hostname (e.g. app.umami.is). The protocol, if present, will be removed.\n\t * @param type The type of event to send\n\t * @param payload The payload of the pageview\n\t * @param userAgent Value of the User-Agent header. Necessary for platform detection. Defaults to Firefox on Mac OS on a laptop\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/collect.js#L75 Relevant Umami source code}\n\t */\n\tpublic static async collect(\n\t\tserver: string,\n\t\ttype: \"pageview\",\n\t\tpayload: PageViewPayload,\n\t\tuserAgent?: string,\n\t): Promise<string>;\n\t/**\n\t * Collects an event\n\t * @param server The Umami installation hostname (e.g. app.umami.is). The protocol, if present, will be removed.\n\t * @param type The type of event to send\n\t * @param payload The payload of the event\n\t * @param userAgent Value of the User-Agent header. Necessary for platform detection. Defaults to Firefox on Mac OS on a laptop\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/collect.js#L77 Relevant Umami source code}\n\t */\n\tpublic static async collect(\n\t\tserver: string,\n\t\ttype: \"event\",\n\t\tpayload: EventPayload,\n\t\tuserAgent?: string,\n\t): Promise<string>;\n\tpublic static async collect(\n\t\tserver: string,\n\t\ttype: \"pageview\" | \"event\",\n\t\tpayload: CollectPayload,\n\t\tuserAgent: string = DEFAULT_USER_AGENT,\n\t) {\n\t\tif (!server) throw new Error(\"A server is required.\");\n\t\tserver = server.replace(/https?:\\/\\//, \"\").replace(/\\/$/, \"\");\n\n\t\tif (!userAgent) throw new Error(\"A user agent is required. See https://umami.is/docs/api\");\n\n\t\tconst { data } = await axios.post<string>(\n\t\t\t`https://${server}/api/collect`,\n\t\t\t{ type, payload },\n\t\t\t{ headers: { \"User-Agent\": userAgent } },\n\t\t);\n\t\treturn data;\n\t}\n\n\t/**\n\t * Gets tracked websites\n\t * @param options.include_all Whether or not to include all websites (admin only)\n\t * @param options.user_id The user to query websites from (admin only, if not your own user id)\n\t * @returns An array of tracked websites\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/websites/index.js#L20-L31 Relevant Umami source code}\n\t */\n\tpublic async getWebsites(options?: { include_all?: boolean; user_id?: number }) {\n\t\tconst { data } = await this._axios.get<WebsiteData[]>(\"/websites\", {\n\t\t\tparams: options,\n\t\t});\n\t\treturn data.map((data) => new Website(this, this._axios, data));\n\t}\n\n\t/**\n\t * Gets the first website that gets returned by Umami\n\t * @returns The first website\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/websites/index.js Relevant Umami source code}\n\t */\n\tpublic async getWebsite(): Promise<Website>;\n\t/**\n\t * Gets a website by its UUID (not ID)\n\t * @param websiteUuid The website's UUID (not ID)\n\t * @returns The website\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/websites/[id]/index.js Relevant Umami source code}\n\t */\n\tpublic async getWebsite(websiteUuid: string): Promise<Website>;\n\tpublic async getWebsite(websiteUuid?: string) {\n\t\tif (websiteUuid === undefined) {\n\t\t\tconst websites = await this.getWebsites();\n\t\t\treturn new Website(this, this._axios, websites[0]);\n\t\t}\n\n\t\tconst { data } = await this._axios.get<WebsiteData>(`/websites/${websiteUuid}`);\n\t\treturn new Website(this, this._axios, data);\n\t}\n\n\t/**\n\t * Gets a website by a property\n\t * @param key The property to check\n\t * @param value The value to check the property against\n\t * @returns The website\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/websites/index.js Relevant Umami source code}\n\t *\n\t * @example\n\t * Get a website by domain name\n\t * ```ts\n\t * const website = await instance.getWebsiteBy(\"domain\", \"example.com\");\n\t * ```\n\t */\n\tpublic async getWebsiteBy(key: keyof Website, value: string | number) {\n\t\tif (key == \"shareId\") {\n\t\t\tconst { data } = await this._axios.get<{ id: string; token: string }>(`/share/${value}`);\n\t\t\treturn await this.getWebsite(data.id);\n\t\t}\n\n\t\tif (key == \"websiteUuid\") {\n\t\t\treturn await this.getWebsite(value as string);\n\t\t}\n\n\t\tconst websites = await this.getWebsites();\n\t\tconst website = websites.find((website) => website[key] == value);\n\t\tif (!website) {\n\t\t\tthrow Error(\"Could not find website\");\n\t\t}\n\t\treturn website;\n\t}\n\n\t/**\n\t * Creates a new website and returns its information.\n\t * @param options.domain The domain name of the website (e.g. umami.is)\n\t * @param options.name The name of the website (usually the same as the domain)\n\t * @param options.owner The website's owner's ID (by default, the logged-in's user's)\n\t * @param options.enableShareUrl Whether or not to enable public sharing.\n\t * @returns The new website's information\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/websites/index.js#L33-L47 Relevant Umami source code}\n\t */\n\tpublic async createWebsite(options: {\n\t\tdomain: string;\n\t\tname: string;\n\t\towner?: number;\n\t\tenableShareUrl?: boolean;\n\t}) {\n\t\tif (!options.owner) {\n\t\t\tconst currentUser = await this.getCurrentUser();\n\t\t\toptions = {\n\t\t\t\t...options,\n\t\t\t\towner: currentUser.userId,\n\t\t\t};\n\t\t}\n\t\tconst { data } = await this._axios.post<WebsiteData>(\"/websites\", options);\n\t\treturn new Website(this, this._axios, data);\n\t}\n\n\t/**\n\t * Gets all the user accounts (admin only)\n\t * @returns An array of all the user accounts\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/accounts/index.js#L15-L19 Relevant Umami source code}\n\t */\n\tpublic async getAccounts() {\n\t\tconst { data } = await this._axios.get<UserAccountData[]>(\"/accounts\");\n\t\treturn data.map((data) => new UserAccount(this._axios, data));\n\t}\n\n\t/**\n\t * Gets a user account (admin only)\n\t * @param userId The user ID\n\t * @returns The user account\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/accounts/[id]/index.js#L11-L19 Relevant Umami source code}\n\t */\n\tpublic async getAccount(userId: number) {\n\t\tconst { data } = await this._axios.get<UserAccountData>(`/accounts/${userId}`);\n\t\treturn new UserAccount(this._axios, data);\n\t}\n\n\t/**\n\t * Creates a user account (admin only)\n\t * @param options.username The username\n\t * @param options.password The password\n\t * @returns The user account\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/accounts/index.js#L21-L37 Relevant Umami source code}\n\t */\n\tpublic async createAccount(options: { username: string; password: string }) {\n\t\tconst { data } = await this._axios.post<UserAccountData>(\"/accounts\", options);\n\t\treturn new UserAccount(this._axios, data);\n\t}\n}\n","const {\n\tUMAMI_CLIENT_TIMEOUT_MS,\n\tUMAMI_CLIENT_USER_AGENT,\n\tUMAMI_CLIENT_TIME_PERIOD,\n\tUMAMI_CLIENT_TIME_UNIT,\n\tUMAMI_CLIENT_TIMEZONE,\n\tUMAMI_CLIENT_METRIC_TYPE,\n} = process.env;\n\nexport const DEFAULT_HTTP_CLIENT_TIMEOUT_MS = UMAMI_CLIENT_TIMEOUT_MS\n\t? parseInt(UMAMI_CLIENT_TIMEOUT_MS)\n\t: 2000;\nexport const DEFAULT_USER_AGENT =\n\tUMAMI_CLIENT_USER_AGENT ??\n\t\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Firefox/102.0\";\nexport const DEFAULT_TIME_PERIOD = UMAMI_CLIENT_TIME_PERIOD ?? \"24h\";\nexport const DEFAULT_TIME_UNIT = UMAMI_CLIENT_TIME_UNIT ?? \"hour\";\nexport const DEFAULT_TIMEZONE = UMAMI_CLIENT_TIMEZONE ?? \"America/Toronto\";\nexport const DEFAULT_METRIC_TYPE = UMAMI_CLIENT_METRIC_TYPE ?? \"url\";\n","import type { AxiosInstance } from \"axios\";\nimport type UmamiApiClient from \"../UmamiApiClient\";\nimport {\n\tDEFAULT_TIME_PERIOD,\n\tDEFAULT_TIME_UNIT,\n\tDEFAULT_TIMEZONE,\n\tDEFAULT_METRIC_TYPE,\n} from \"../defaults\";\n\ntype TimeUnit = \"year\" | \"month\" | \"day\" | \"hour\";\n\ntype MetricType =\n\t| \"url\"\n\t| \"referrer\"\n\t| \"browser\"\n\t| \"os\"\n\t| \"device\"\n\t| \"country\"\n\t| \"event\"\n\t| \"language\"\n\t| \"utm_source\"\n\t| \"utm_medium\"\n\t| \"utm_campaign\"\n\t| \"utm_content\"\n\t| \"utm_term\"\n\t| \"ref\";\n\ninterface Stats {\n\tpageviews: {\n\t\tvalue: number;\n\t\tchange: number;\n\t};\n\tuniques: {\n\t\tvalue: number;\n\t\tchange: number;\n\t};\n\tbounces: {\n\t\tvalue: number;\n\t\tchange: number;\n\t};\n\ttotaltime: {\n\t\tvalue: number;\n\t\tchange: number;\n\t};\n}\n\ninterface PageViews {\n\t/**\n\t * @param t The time period of the data\n\t * @param y The amount of page views in the time period\n\t */\n\tpageviews: {\n\t\tt: string;\n\t\ty: number;\n\t}[];\n\t/**\n\t * @param t The time period of the data\n\t * @param y The amount of sessions in the time period\n\t */\n\tsessions: {\n\t\tt: string;\n\t\ty: number;\n\t}[];\n}\n\n/**\n * @param x The name of the event\n * @param t The time period of the data\n * @param y The amount of events in the time period\n */\ninterface Event {\n\tx: string;\n\tt: string;\n\ty: number;\n}\n\n/**\n * @param x The metric's value\n * @param y The amount of this metric's value in the period of time\n */\ninterface Metric {\n\tx: string | null;\n\ty: number;\n}\n\ninterface ActiveVisitor {\n\tx: number;\n}\n\nconst HOUR_PERIODS = [\"1h\", \"1hour\", \"60min\", \"60minutes\"] as const;\ntype HourPeriod = (typeof HOUR_PERIODS)[number];\nconst DAY_PERIODS = [\"1d\", \"1day\", \"24h\", \"24hours\"] as const;\ntype DayPeriod = (typeof DAY_PERIODS)[number];\nconst WEEK_PERIODS = [\"7d\", \"7days\", \"1w\", \"1week\"] as const;\ntype WeekPeriod = (typeof WEEK_PERIODS)[number];\nconst MONTH_PERIODS = [\"31d\", \"31days\", \"1m\", \"1month\"] as const;\ntype MonthPeriod = (typeof MONTH_PERIODS)[number];\n\ntype TimePeriod = HourPeriod | DayPeriod | WeekPeriod | MonthPeriod;\n\nexport const convertPeriodToTime = (period: TimePeriod = \"24h\") => {\n\tlet delta: number;\n\tif (HOUR_PERIODS.includes(period as HourPeriod)) {\n\t\tdelta = 60 * 60 * 1000;\n\t} else if (DAY_PERIODS.includes(period as DayPeriod)) {\n\t\tdelta = 24 * 60 * 60 * 1000;\n\t} else if (WEEK_PERIODS.includes(period as WeekPeriod)) {\n\t\tdelta = 7 * 24 * 60 * 60 * 1000;\n\t} else if (MONTH_PERIODS.includes(period as MonthPeriod)) {\n\t\tdelta = 31 * 24 * 60 * 60 * 1000;\n\t} else {\n\t\tthrow `Unexpected period provided. Accepted values are : ${[\n\t\t\t...HOUR_PERIODS,\n\t\t\t...DAY_PERIODS,\n\t\t\t...WEEK_PERIODS,\n\t\t\t...MONTH_PERIODS,\n\t\t]}`;\n\t}\n\treturn {\n\t\tstart_at: Date.now() - delta,\n\t\tend_at: Date.now(),\n\t};\n};\n\nexport interface WebsiteData {\n\tid: number;\n\twebsiteUuid: string;\n\tuserId: number;\n\tname: string;\n\tdomain: string;\n\tshareId: string | null;\n\tcreatedAt: string;\n}\n\nexport class Website implements WebsiteData {\n\tprivate readonly _apiClient: UmamiApiClient;\n\tprivate readonly _axios: AxiosInstance;\n\tpublic readonly id: number;\n\tpublic readonly websiteUuid: string;\n\tpublic userId: number;\n\tpublic name: string;\n\tpublic domain: string;\n\tpublic shareId: string | null;\n\tpublic createdAt: string;\n\n\tconstructor(apiClient: UmamiApiClient, axios: AxiosInstance, data: WebsiteData) {\n\t\tthis._apiClient = apiClient;\n\t\tthis._axios = axios;\n\t\tthis.id = data.id;\n\t\tthis.websiteUuid = data.websiteUuid;\n\t\tthis.userId = data.userId;\n\t\tthis.name = data.name;\n\t\tthis.domain = data.domain;\n\t\tthis.shareId = data.shareId;\n\t\tthis.createdAt = data.createdAt;\n\t}\n\n\t/**\n\t * Updates the website.\n\t * @param options.domain The domain name of the website (e.g. umami.is)\n\t * @param options.name The name of the website (usually the same as the domain)\n\t * @param options.owner The website's owner's ID (by default, the logged-in's user's)\n\t * @param options.enableShareUrl Whether or not to enable public sharing.\n\t * @returns\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/websites/[id]/index.js#L23-L57 Relevant Umami source code}\n\t */\n\tpublic async update(options: {\n\t\tdomain: string;\n\t\tname: string;\n\t\towner?: number;\n\t\tenableShareUrl?: boolean;\n\t}) {\n\t\tif (!options.owner) {\n\t\t\tconst currentUser = await this._apiClient.getCurrentUser();\n\t\t\toptions = {\n\t\t\t\t...options,\n\t\t\t\towner: currentUser.userId,\n\t\t\t};\n\t\t}\n\n\t\tconst { data } = await this._axios.post<WebsiteData>(`/websites/${this.websiteUuid}`, options);\n\t\tObject.assign(this, data);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Deletes the website\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/websites/[id]/index.js#L59-L67 Relevant Umami source code}\n\t */\n\tpublic async delete() {\n\t\tawait this._axios.delete(`/websites/${this.websiteUuid}`);\n\t}\n\n\t/**\n\t * Gets the stats of the website from a specified time period\n\t * @param options.period The time period of stats to return\n\t * @param options.url Filter stats by URL\n\t * @param options.referrer Filter stats by referrer\n\t * @param options.os Filter stats by OS\n\t * @param options.browser Filter stats by browser\n\t * @param options.device Filter stats by device\n\t * @param options.country Filter stats by country\n\t * @returns The website's stats from the specified time period\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/websites/[id]/stats.js Relevant Umami source code}\n\t */\n\tpublic async getStats(options?: {\n\t\tperiod?: TimePeriod;\n\t\turl?: string;\n\t\treferrer?: string;\n\t\tos?: string;\n\t\tbrowser?: string;\n\t\tdevice?: string;\n\t\tcountry?: string;\n\t}) {\n\t\tconst { data } = await this._axios.get<Stats>(`/websites/${this.websiteUuid}/stats`, {\n\t\t\tparams: {\n\t\t\t\t...convertPeriodToTime(options?.period ?? DEFAULT_TIME_PERIOD),\n\t\t\t\turl: options?.url,\n\t\t\t\treferrer: options?.referrer,\n\t\t\t\tos: options?.os,\n\t\t\t\tbrowser: options?.browser,\n\t\t\t\tdevice: options?.device,\n\t\t\t\tcountry: options?.country,\n\t\t\t},\n\t\t});\n\t\treturn data;\n\t}\n\n\t/**\n\t * Resets the website's stats\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/websites/[id]/reset.js Relevant Umami source code}\n\t */\n\tpublic async resetStats() {\n\t\tawait this._axios.post(`/websites/${this.websiteUuid}/reset`);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Gets the pageviews of the website from a specified time period\n\t * @param options.period The time period of pageviews to return\n\t * @param options.unit The interval of time/precision of the returned pageviews\n\t * @param options.tz The timezone you're in (defaults to \"America/Toronto\")\n\t * @param options.url Filter pageviews by URL\n\t * @param options.referrer Filter pageviews by referrer\n\t * @param options.os Filter pageviews by OS\n\t * @param options.browser Filter pageviews by browser\n\t * @param options.device Filter pageviews by device\n\t * @param options.country Filter pageviews by country\n\t * @returns The website's pageviews from the specified time period\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/websites/[id]/pageviews.js Relevant Umami source code}\n\t */\n\tpublic async getPageviews(options?: {\n\t\tperiod?: TimePeriod;\n\t\tunit?: TimeUnit;\n\t\ttz?: string;\n\t\turl?: string;\n\t\treferrer?: string;\n\t\tos?: string;\n\t\tbrowser?: string;\n\t\tdevice?: string;\n\t\tcountry?: string;\n\t}) {\n\t\tconst { data } = await this._axios.get<PageViews>(`/websites/${this.websiteUuid}/pageviews`, {\n\t\t\tparams: {\n\t\t\t\t...convertPeriodToTime(options?.period ?? DEFAULT_TIME_PERIOD),\n\t\t\t\tunit: options?.unit ?? DEFAULT_TIME_UNIT,\n\t\t\t\ttz: options?.tz ?? DEFAULT_TIMEZONE,\n\t\t\t\turl: options?.url,\n\t\t\t\treferrer: options?.referrer,\n\t\t\t\tos: options?.os,\n\t\t\t\tbrowser: options?.browser,\n\t\t\t\tdevice: options?.device,\n\t\t\t\tcountry: options?.country,\n\t\t\t},\n\t\t});\n\t\treturn data;\n\t}\n\n\t/**\n\t * Gets the events of the website from a specified time period\n\t * @param options.period The time period of events to return\n\t * @param options.unit The interval of time/precision of the returned events\n\t * @param options.tz The timezone you're in (defaults to \"America/Toronto\")\n\t * @param options.url The url where the event happened.\n\t * @param options.event_type The type of event to request.\n\t * @returns An array of events from the specified time period\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/website/[id]/events.js Relevant Umami source code}\n\t */\n\tpublic async getEvents(options?: {\n\t\tperiod?: TimePeriod;\n\t\tunit?: TimeUnit;\n\t\ttz?: string;\n\t\turl?: string;\n\t\tevent_type?: string;\n\t}) {\n\t\tconst { data } = await this._axios.get<Event[]>(`/websites/${this.websiteUuid}/events`, {\n\t\t\tparams: {\n\t\t\t\t...convertPeriodToTime(options?.period ?? DEFAULT_TIME_PERIOD),\n\t\t\t\tunit: options?.unit ?? DEFAULT_TIME_UNIT,\n\t\t\t\ttz: options?.tz ?? DEFAULT_TIMEZONE,\n\t\t\t\turl: options?.url,\n\t\t\t\tevent_type: options?.event_type,\n\t\t\t},\n\t\t});\n\t\treturn data;\n\t}\n\n\t/**\n\t * Gets a type of metrics of the website from a specified time period\n\t * @param options.period The time period of events to return\n\t * @param options.type The type of metric to get. Defaults to url\n\t * @param options.url Filter metrics by URL\n\t * @param options.referrer Filter metrics by referrer\n\t * @param options.os Filter metrics by OS\n\t * @param options.browser Filter metrics by browser\n\t * @param options.device Filter metrics by device\n\t * @param options.country Filter metrics by country\n\t * @returns An array of metrics from the specified time period\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/websites/[id]/metrics.js Relevant Umami source code}\n\t */\n\tpublic async getMetrics(options?: {\n\t\tperiod?: TimePeriod;\n\t\ttype?: MetricType;\n\t\turl?: string;\n\t\treferrer?: string;\n\t\tos?: string;\n\t\tbrowser?: string;\n\t\tdevice?: string;\n\t\tcountry?: string;\n\t}) {\n\t\tconst { data } = await this._axios.get<Metric[]>(`/websites/${this.websiteUuid}/metrics`, {\n\t\t\tparams: {\n\t\t\t\t...convertPeriodToTime(options?.period ?? DEFAULT_TIME_PERIOD),\n\t\t\t\ttype: options?.type ?? DEFAULT_METRIC_TYPE,\n\t\t\t\turl: options?.url,\n\t\t\t\treferrer: options?.referrer,\n\t\t\t\tos: options?.os,\n\t\t\t\tbrowser: options?.browser,\n\t\t\t\tdevice: options?.device,\n\t\t\t\tcountry: options?.country,\n\t\t\t},\n\t\t});\n\t\treturn data;\n\t}\n\n\t/**\n\t * Gets the active visitors of a website\n\t * @returns\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/websites/[id]/active.js Relevant Umami source code}\n\t */\n\tpublic async getActiveVisitors() {\n\t\tconst { data } = await this._axios.get<ActiveVisitor[]>(`/websites/${this.websiteUuid}/active`);\n\t\treturn data;\n\t}\n}\n","import type { AxiosInstance } from \"axios\";\n\nexport interface UserAccountData {\n\tid: number;\n\tusername: string;\n\tisAdmin: boolean;\n\tcreatedAt: string;\n\tupdatedAt: string;\n\taccountUuid: string;\n}\n\nexport class UserAccount implements UserAccountData {\n\tprivate readonly _axios: AxiosInstance;\n\tpublic readonly id: number;\n\tpublic username: string;\n\tpublic isAdmin: boolean;\n\tpublic readonly createdAt: string;\n\tpublic updatedAt: string;\n\tpublic accountUuid: string;\n\n\tconstructor(axios: AxiosInstance, data: UserAccountData) {\n\t\tthis._axios = axios;\n\t\tthis.id = data.id;\n\t\tthis.username = data.username;\n\t\tthis.isAdmin = data.isAdmin;\n\t\tthis.createdAt = data.createdAt;\n\t\tthis.updatedAt = data.updatedAt;\n\t\tthis.accountUuid = data.accountUuid;\n\t}\n\n\t/**\n\t * Updates a user account\n\t * @param options.username New username (admin only)\n\t * @param options.password New password\n\t * @returns The user account\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/accounts/[id]/index.js#L21-L53 Relevant Umami source code}\n\t */\n\tpublic async update(options: { username: string; password: string }) {\n\t\tconst { data } = await this._axios.post(`/accounts/${this.id}`, options);\n\t\tObject.assign(this, data);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Updates a user account password\n\t * @param options.current_password Current password\n\t * @param options.new_password New password\n\t * @returns The user account\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/accounts/[id]/password.js Relevant Umami source code}\n\t */\n\tpublic async changePassword(options: { current_password: string; new_password: string }) {\n\t\tawait this._axios.post(`/accounts/${this.accountUuid}/password`, options);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Deletes the user account (admin only)\n\t * @see {@link https://github.com/umami-software/umami/blob/master/pages/api/accounts/[id]/index.js#L55-L63 Relevant Umami source code}\n\t */\n\tpublic async delete() {\n\t\tawait this._axios.delete(`/accounts/${this.id}`);\n\t}\n}\n"],"mappings":"0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GCAA,IAAAI,EAIO,sBCJP,GAAM,CACL,wBAAAC,EACA,wBAAAC,EACA,yBAAAC,EACA,uBAAAC,EACA,sBAAAC,EACA,yBAAAC,CACD,EAAI,QAAQ,IAECC,EAAiCN,EAC3C,SAASA,CAAuB,EAChC,IACUO,EACZN,GACA,uFACYO,EAAsBN,GAA4B,MAClDO,EAAoBN,GAA0B,OAC9CO,EAAmBN,GAAyB,kBAC5CO,EAAsBN,GAA4B,MCuE/D,IAAMO,EAAe,CAAC,KAAM,QAAS,QAAS,WAAW,EAEnDC,EAAc,CAAC,KAAM,OAAQ,MAAO,SAAS,EAE7CC,EAAe,CAAC,KAAM,QAAS,KAAM,OAAO,EAE5CC,EAAgB,CAAC,MAAO,SAAU,KAAM,QAAQ,EAKzCC,EAAsB,CAACC,EAAqB,QAAU,CAClE,IAAIC,EACJ,GAAIN,EAAa,SAASK,CAAoB,EAC7CC,EAAQ,GAAK,GAAK,YACRL,EAAY,SAASI,CAAmB,EAClDC,EAAQ,GAAK,GAAK,GAAK,YACbJ,EAAa,SAASG,CAAoB,EACpDC,EAAQ,EAAI,GAAK,GAAK,GAAK,YACjBH,EAAc,SAASE,CAAqB,EACtDC,EAAQ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AC3C,CAOA,MAAa,aAAc,CAC1B,GAAM,CAAE,KAAAA,CAAK,EAAI,MAAM,KAAK,OAAO,IAAuB,WAAW,EACrE,OAAOA,EAAK,IAAKA,GAAS,IAAIS,EAAY,KAAK,OAAQT,CAAI,CAAC,CAC7D,CAQA,MAAa,WAAWU,EAAgB,CACvC,GAAM,CAAE,KAAAV,CAAK,EAAI,MAAM,KAAK,OAAO,IAAqB,aAAaU,GAAQ,EAC7E,OAAO,IAAID,EAAY,KAAK,OAAQT,CAAI,CACzC,CASA,MAAa,cAAcC,EAAiD,CAC3E,GAAM,CAAE,KAAAD,CAAK,EAAI,MAAM,KAAK,OAAO,KAAsB,YAAaC,CAAO,EAC7E,OAAO,IAAIQ,EAAY,KAAK,OAAQT,CAAI,CACzC,CACD,ED/SA,IAAOW,EAAQC","names":["src_exports","__export","src_default","__toCommonJS","import_axios","UMAMI_CLIENT_TIMEOUT_MS","UMAMI_CLIENT_USER_AGENT","UMAMI_CLIENT_TIME_PERIOD","UMAMI_CLIENT_TIME_UNIT","UMAMI_CLIENT_TIMEZONE","UMAMI_CLIENT_METRIC_TYPE","DEFAULT_HTTP_CLIENT_TIMEOUT_MS","DEFAULT_USER_AGENT","DEFAULT_TIME_PERIOD","DEFAULT_TIME_UNIT","DEFAULT_TIMEZONE","DEFAULT_METRIC_TYPE","HOUR_PERIODS","DAY_PERIODS","WEEK_PERIODS","MONTH_PERIODS","convertPeriodToTime","period","delta","Website","apiClient","axios","data","options","currentUser","DEFAULT_TIME_PERIOD","DEFAULT_TIME_UNIT","DEFAULT_TIMEZONE","DEFAULT_METRIC_TYPE","UserAccount","axios","data","options","UmamiApiClient","server","username","password","axios","DEFAULT_HTTP_CLIENT_TIMEOUT_MS","config","auth","error","type","payload","userAgent","DEFAULT_USER_AGENT","data","options","Website","websiteUuid","websites","key","value","website","currentUser","UserAccount","userId","src_default","UmamiApiClient"]}