UNPKG

@tribute-tg/sdk

Version:

Unofficial Tribute SDK for Node.js and browsers

1 lines 5.37 kB
{"version":3,"file":"index.mjs","names":["init: RequestInit"],"sources":["../src/cache.ts","../src/index.ts"],"sourcesContent":["const getCache = async (): Promise<Cache | undefined> => {\n const hasCaches = 'caches' in globalThis;\n if (!hasCaches) return undefined;\n if ('default' in caches) return caches.default as Cache;\n return (await caches.open('default')) as unknown as Cache;\n};\n\nexport const fetchWithCache = async (\n request: RequestInfo | URL,\n { maxAge }: { maxAge: number } = { maxAge: 300 } // 5 minutes in seconds\n): Promise<Response> => {\n const cache = await getCache();\n if (!cache) return await fetch(request);\n const cached = await cache?.match(request);\n if (cached) {\n return new Response(cached.body, cached);\n } else {\n const response = await fetch(request);\n const cacheable = new Response(response.body, response);\n try {\n cacheable.headers.append('Cache-Control', `s-maxage=${maxAge}`);\n } catch (error) {\n console.error(error);\n }\n await cache?.put(request, cacheable.clone());\n return cacheable;\n }\n};\n","import type { ChannelResponse } from './types/channel-response';\nimport type { Dashboard } from './types/dashboard';\nimport type { Subscription, SubscriptionPayload, SubscriptionResponse } from './types/subscription';\nimport type { SubscriptionMemberResponse } from './types/subscription-member-response';\nimport type { SubscriptionPeriod, SubscriptionPeriodPayload } from './types/subscription-period';\nimport { fetchWithCache } from './cache';\n\nexport class Tribute {\n private baseUrl = 'https://tribute.tg/api';\n private version = 'v4';\n private cacheMaxAge: number;\n public token: string;\n public apiKey: string;\n\n constructor({ token, apiKey, cacheMaxAge }: { token: string; apiKey: string; cacheMaxAge?: number }) {\n this.token = token;\n this.apiKey = apiKey;\n this.cacheMaxAge = cacheMaxAge ?? 300; // 5 minutes in seconds\n }\n\n private async request<T>(route: string, body?: any, method?: string): Promise<T> {\n const url = `${this.baseUrl}/${this.version}${route}`;\n const init: RequestInit = {\n headers: { Authorization: `TgAuth ${this.token}`, 'Api-Key': this.apiKey },\n method: method ?? (body ? 'POST' : 'GET'),\n body: body ? JSON.stringify(body) : undefined,\n };\n const request = new Request(url, init);\n const response = this.cacheMaxAge\n ? await fetchWithCache(request, { maxAge: this.cacheMaxAge })\n : await fetch(request);\n return response.json();\n }\n\n getDashboard(): Promise<Dashboard> {\n return this.request(`/dashboard`);\n }\n\n getChannel(id: number): Promise<ChannelResponse> {\n return this.request(`/channel/${id}`);\n }\n\n createSubscription(payload: SubscriptionPayload): Promise<Subscription> {\n return this.request(`/subscription`, payload);\n }\n\n getSubscription(id: number): Promise<SubscriptionResponse> {\n return this.request(`/subscription/${id}`);\n }\n\n updateSubscription(payload: Pick<SubscriptionPayload, 'name' | 'description' | 'buttonText'>): Promise<Subscription> {\n return this.request(`/subscription`, payload, 'PATCH');\n }\n\n createSubscriptionPeriod(subscriptionId: number, payload: SubscriptionPeriodPayload): Promise<SubscriptionPeriod> {\n return this.request(`/subscription/${subscriptionId}/periods`, payload);\n }\n\n updateSubscriptionPeriod(\n subscriptionId: number,\n periodId: number,\n payload: SubscriptionPeriodPayload\n ): Promise<SubscriptionPeriod> {\n return this.request(`/subscription/${subscriptionId}/periods/${periodId}`, payload, 'PATCH');\n }\n\n getSubscriptionMember(id: number): Promise<SubscriptionMemberResponse> {\n return this.request(`/subscription_member/${id}`);\n }\n}\n\nexport * from './types';\n"],"mappings":";AAAA,MAAM,WAAW,YAAwC;AAEvD,KAAI,EADc,YAAY,YACd,QAAO;AACvB,KAAI,aAAa,OAAQ,QAAO,OAAO;AACvC,QAAQ,MAAM,OAAO,KAAK,UAAU;;AAGtC,MAAa,iBAAiB,OAC5B,SACA,EAAE,WAA+B,EAAE,QAAQ,KAAK,KAC1B;CACtB,MAAM,QAAQ,MAAM,UAAU;AAC9B,KAAI,CAAC,MAAO,QAAO,MAAM,MAAM,QAAQ;CACvC,MAAM,SAAS,MAAM,OAAO,MAAM,QAAQ;AAC1C,KAAI,OACF,QAAO,IAAI,SAAS,OAAO,MAAM,OAAO;MACnC;EACL,MAAM,WAAW,MAAM,MAAM,QAAQ;EACrC,MAAM,YAAY,IAAI,SAAS,SAAS,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