UNPKG

tiny1z

Version:

The Tiny1z API Client is a convenient and lightweight npm package that allows developers to seamlessly integrate the Tiny1z URL Shortener API into their applications. Simplify URL shortening, retrieval, and management with just a few lines of code.

1 lines 4.05 kB
{"version":3,"sources":["../index.ts"],"names":[],"mappings":";AAkCA,IAAqB,eAArB,MAAkC;AAAA,EAIhC,YAAY,SAAuC;AACjD,QAAI,OAAO,YAAY,UAAU;AAC/B,WAAK,SAAS;AACd,WAAK,UAAU;AAAA,IACjB,OAAO;AACL,WAAK,SAAS,QAAQ;AACtB,WAAK,UAAU,QAAQ,WAAW;AAAA,IACpC;AAAA,EACF;AAAA,EAEA,MAAc,QAAW,UAAkB,QAAgB,MAAwB;AACjF,QAAI;AACF,YAAM,MAAM,MAAM,MAAM,GAAG,KAAK,OAAO,GAAG,QAAQ,IAAI;AAAA,QACpD;AAAA,QACA,SAAS;AAAA,UACP,gBAAgB;AAAA,UAChB,eAAe,KAAK;AAAA,QACtB;AAAA,QACA,GAAI,OAAO,EAAE,MAAM,KAAK,UAAU,IAAI,EAAE,IAAI,CAAC;AAAA,MAC/C,CAAC;AACD,aAAO,MAAM,IAAI,KAAK;AAAA,IACxB,SAAS,OAAO;AACd,cAAQ,MAAM,qBAAqB,KAAK;AACxC,aAAO,EAAE,OAAO,MAAM,KAAK,SAAS,wBAAwB;AAAA,IAC9D;AAAA,EACF;AAAA;AAAA,EAGA,MAAM,gBAAgB,MAAqB;AACzC,WAAO,KAAK,QAAQ,kBAAkB,QAAQ,IAAI;AAAA,EACpD;AAAA;AAAA,EAGA,MAAM,cAAc,MAAmB;AACrC,WAAO,KAAK,QAAQ,uBAAuB,QAAQ,IAAI;AAAA,EACzD;AAAA;AAAA,EAGA,MAAM,YAAY,WAAmB;AACnC,WAAO,KAAK,QAAQ,0BAA0B,mBAAmB,SAAS,CAAC,IAAI,KAAK;AAAA,EACtF;AAAA;AAAA,EAGA,MAAM,cAAc,MAAc;AAChC,WAAO,KAAK,QAAQ,qBAAqB,mBAAmB,IAAI,CAAC,IAAI,KAAK;AAAA,EAC5E;AAAA;AAAA,EAGA,MAAM,WAAW,SAAS,GAAG,QAAQ,IAAI,OAAkB,QAAQ;AACjE,WAAO,KAAK;AAAA,MACV,2BAA2B,MAAM,UAAU,KAAK,SAAS,IAAI;AAAA,MAC7D;AAAA,IACF;AAAA,EACF;AACF","sourcesContent":["// Tiny1z API Client - Modern, class-based, and typed\r\n\r\nexport interface SingleUrlData {\r\n original_url: string;\r\n alias?: string;\r\n password?: string;\r\n expiry_duration?: number;\r\n expiry_unit?: 'hours' | 'days' | 'months';\r\n user_reference?: string;\r\n}\r\n\r\nexport interface BulkUrlItem {\r\n url: string;\r\n alias?: string;\r\n password?: string;\r\n expiry_duration?: number;\r\n expiry_unit?: 'hours' | 'days' | 'months';\r\n}\r\n\r\nexport interface BulkUrlData {\r\n original_urls: BulkUrlItem[];\r\n global_password?: string | null;\r\n global_expiry_duration?: number | null;\r\n global_expiry_unit?: 'hours' | 'days' | 'months' | null;\r\n user_reference?: string;\r\n}\r\n\r\nexport type SortOrder = 'asc' | 'desc';\r\n\r\nexport interface Tiny1zClientOptions {\r\n apiKey: string;\r\n baseUrl?: string;\r\n}\r\n\r\nexport default class Tiny1zClient {\r\n private apiKey: string;\r\n private baseUrl: string;\r\n\r\n constructor(options: Tiny1zClientOptions | string) {\r\n if (typeof options === 'string') {\r\n this.apiKey = options;\r\n this.baseUrl = 'https://tiny1z.com';\r\n } else {\r\n this.apiKey = options.apiKey;\r\n this.baseUrl = options.baseUrl || 'https://tiny1z.com';\r\n }\r\n }\r\n\r\n private async request<T>(endpoint: string, method: string, body?: any): Promise<T> {\r\n try {\r\n const res = await fetch(`${this.baseUrl}${endpoint}`, {\r\n method,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n Authorization: this.apiKey,\r\n },\r\n ...(body ? { body: JSON.stringify(body) } : {}),\r\n });\r\n return await res.json();\r\n } catch (error) {\r\n console.error('Tiny1z API error:', error);\r\n return { error: true, msg: error || 'Failed to fetch data.' } as any;\r\n }\r\n }\r\n\r\n /** Shorten a single URL */\r\n async createSingleUrl(data: SingleUrlData) {\r\n return this.request('/api/v1/create', 'POST', data);\r\n }\r\n\r\n /** Shorten multiple URLs in bulk */\r\n async createBulkUrl(data: BulkUrlData) {\r\n return this.request('/api/v1/create/bulk', 'POST', data);\r\n }\r\n\r\n /** Retrieve URLs created by a specific user reference */\r\n async getUserUrls(reference: string) {\r\n return this.request(`/api/v1/user?reference=${encodeURIComponent(reference)}`, 'GET');\r\n }\r\n\r\n /** Get detailed information about a specific short URL */\r\n async singleUrlInfo(slug: string) {\r\n return this.request(`/api/v1/user?slug=${encodeURIComponent(slug)}`, 'GET');\r\n }\r\n\r\n /** Fetch all URLs created by the authenticated user */\r\n async getAllUrls(offset = 0, limit = 10, sort: SortOrder = 'desc') {\r\n return this.request(\r\n `/api/v1/user/all?offset=${offset}&limit=${limit}&sort=${sort}`,\r\n 'GET'\r\n );\r\n }\r\n}"]}