aliaslive-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # alias-sdk
2
+
3
+ Official JavaScript / TypeScript SDK for [alias.live](https://aliasurlshortener.com).
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install aliaslive-sdk
9
+ ```
10
+
11
+ ## Quick start
12
+
13
+ ```ts
14
+ import { AliasClient } from 'aliaslive-sdk';
15
+
16
+ const alias = new AliasClient({ apiKey: 'alias_xxxxxxxxxxxx' });
17
+
18
+ // Shorten a single URL
19
+ const { shortUrl } = await alias.shorten('https://example.com/very-long-path');
20
+ console.log(shortUrl); // https://alias.live/abc123
21
+
22
+ // Custom slug
23
+ const result = await alias.shorten('https://example.com', { slug: 'my-link' });
24
+
25
+ // With password and expiry
26
+ const result = await alias.shorten('https://example.com', {
27
+ slug: 'secret',
28
+ password: 'hunter2',
29
+ expiresAt: '2025-12-31T23:59:59Z',
30
+ });
31
+ ```
32
+
33
+ ## Bulk shortening
34
+
35
+ ```ts
36
+ const results = await alias.bulkShorten([
37
+ { destination: 'https://example.com/page-one' },
38
+ { destination: 'https://example.com/page-two', slug: 'page-two' },
39
+ ]);
40
+
41
+ for (const r of results) {
42
+ if (r.error) {
43
+ console.error(`Failed: ${r.destination} — ${r.error}`);
44
+ } else {
45
+ console.log(`${r.destination} → ${r.shortUrl}`);
46
+ }
47
+ }
48
+ ```
49
+
50
+ ## Link management
51
+
52
+ ```ts
53
+ // List links (paginated)
54
+ const { links, total } = await alias.links.list();
55
+ const page2 = await alias.links.list(2, 20);
56
+
57
+ // Delete
58
+ await alias.links.delete('my-link');
59
+
60
+ // Analytics
61
+ const stats = await alias.links.analytics('my-link', { days: 30 });
62
+ console.log(stats.total_clicks);
63
+ console.log(stats.countries);
64
+ ```
65
+
66
+ ## Error handling
67
+
68
+ ```ts
69
+ import { AliasClient, AliasHttpError } from 'alias-sdk';
70
+
71
+ try {
72
+ await alias.shorten('https://example.com', { slug: 'taken' });
73
+ } catch (err) {
74
+ if (err instanceof AliasHttpError) {
75
+ console.error(err.status, err.code, err.message);
76
+ // 409 CONFLICT Slug already exists
77
+ }
78
+ }
79
+ ```
80
+
81
+ ## API key
82
+
83
+ Generate one at [aliasurlshortener.com/account](https://aliasurlshortener.com/account) → API Keys.
84
+
85
+ ## CommonJS
86
+
87
+ ```js
88
+ const { AliasClient } = require('aliaslive-sdk');
89
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ AliasClient: () => AliasClient,
24
+ AliasHttpError: () => HttpError,
25
+ default: () => index_default
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+ var HttpError = class extends Error {
29
+ constructor(status, code, message) {
30
+ super(message);
31
+ this.status = status;
32
+ this.code = code;
33
+ this.name = "AliasHttpError";
34
+ }
35
+ };
36
+ async function request(baseUrl, apiKey, method, path, body, params) {
37
+ const url = new URL(`${baseUrl}${path}`);
38
+ if (params) {
39
+ Object.entries(params).forEach(([k, v]) => url.searchParams.set(k, String(v)));
40
+ }
41
+ const res = await fetch(url.toString(), {
42
+ method,
43
+ headers: {
44
+ "Content-Type": "application/json",
45
+ "X-Api-Key": apiKey
46
+ },
47
+ body: body !== void 0 ? JSON.stringify(body) : void 0
48
+ });
49
+ const json = await res.json();
50
+ if (!res.ok || !json.success) {
51
+ throw new HttpError(res.status, json.error?.code ?? "UNKNOWN", json.error?.message ?? `HTTP ${res.status}`);
52
+ }
53
+ return json.data;
54
+ }
55
+ var LinksNamespace = class {
56
+ constructor(_req) {
57
+ this._req = _req;
58
+ }
59
+ /** List links. Paginated — default 10 per page. */
60
+ async list(page = 1, limit = 10) {
61
+ const raw = await this._req("GET", "/links", void 0, { page, limit });
62
+ return {
63
+ links: raw.result,
64
+ total: raw.total,
65
+ totalClicks: raw.total_clicks,
66
+ page: raw.page,
67
+ limit: raw.limit
68
+ };
69
+ }
70
+ /** Delete a link by slug. */
71
+ delete(slug) {
72
+ return this._req("DELETE", `/links/${encodeURIComponent(slug)}`);
73
+ }
74
+ /** Get click analytics for a link. */
75
+ analytics(slug, options) {
76
+ return this._req("GET", `/links/${encodeURIComponent(slug)}/analytics`, void 0, {
77
+ days: options?.days ?? 30
78
+ });
79
+ }
80
+ };
81
+ var AliasClient = class {
82
+ constructor({ apiKey, baseUrl = "https://alias.live/api/v1" }) {
83
+ if (!apiKey) throw new Error("AliasClient: apiKey is required");
84
+ this._base = baseUrl.replace(/\/$/, "");
85
+ this._key = apiKey;
86
+ this.links = new LinksNamespace(this._req.bind(this));
87
+ }
88
+ _req(method, path, body, params) {
89
+ return request(this._base, this._key, method, path, body, params);
90
+ }
91
+ /**
92
+ * Shorten a single URL.
93
+ *
94
+ * @example
95
+ * const { shortUrl } = await alias.shorten('https://example.com');
96
+ * const { shortUrl } = await alias.shorten('https://example.com', { slug: 'my-link' });
97
+ */
98
+ async shorten(destination, options = {}) {
99
+ const { slug, password, expiresAt } = options;
100
+ const data = await this._req("POST", "/customShort", {
101
+ destination,
102
+ ...slug ? { slug } : {},
103
+ ...password ? { password } : {},
104
+ ...expiresAt ? { expires_at: expiresAt } : {}
105
+ });
106
+ return {
107
+ slug: data.slug,
108
+ shortUrl: `https://alias.live/${data.slug}`,
109
+ destination
110
+ };
111
+ }
112
+ /**
113
+ * Shorten multiple URLs in one request (max 50).
114
+ *
115
+ * @example
116
+ * const results = await alias.bulkShorten([
117
+ * { destination: 'https://a.com' },
118
+ * { destination: 'https://b.com', slug: 'b-link' },
119
+ * ]);
120
+ */
121
+ async bulkShorten(items) {
122
+ if (items.length === 0) return [];
123
+ if (items.length > 50) throw new Error("bulkShorten: max 50 items per call");
124
+ const data = await this._req(
125
+ "POST",
126
+ "/links/bulk",
127
+ { links: items }
128
+ );
129
+ return data.results.map((r) => ({
130
+ destination: r.destination,
131
+ slug: r.slug,
132
+ shortUrl: r.short_url,
133
+ error: r.error
134
+ }));
135
+ }
136
+ };
137
+ var index_default = AliasClient;
138
+ // Annotate the CommonJS export names for ESM import in node:
139
+ 0 && (module.exports = {
140
+ AliasClient,
141
+ AliasHttpError
142
+ });
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Alias URL Shortener — Official JavaScript / TypeScript SDK
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { AliasClient } from 'aliaslive-sdk';
7
+ *
8
+ * const alias = new AliasClient({ apiKey: 'alias_xxxx' });
9
+ *
10
+ * const { shortUrl } = await alias.shorten('https://example.com');
11
+ * const { shortUrl } = await alias.shorten('https://example.com', { slug: 'my-link' });
12
+ * const results = await alias.bulkShorten([{ destination: 'https://a.com' }]);
13
+ * const { links } = await alias.links.list();
14
+ * await alias.links.delete('my-link');
15
+ * const analytics = await alias.links.analytics('my-link');
16
+ * ```
17
+ */
18
+ interface AliasClientOptions {
19
+ /** API key from aliasurlshortener.com/account */
20
+ apiKey: string;
21
+ /** Override the API base URL (default: https://alias.live/api/v1) */
22
+ baseUrl?: string;
23
+ }
24
+ interface ShortenOptions {
25
+ /** Custom slug (letters, numbers, _ or - ; max 40 chars) */
26
+ slug?: string;
27
+ /** Password to protect the link */
28
+ password?: string;
29
+ /** Expiry as an ISO-8601 datetime string */
30
+ expiresAt?: string;
31
+ }
32
+ interface ShortenResult {
33
+ slug: string;
34
+ shortUrl: string;
35
+ destination: string;
36
+ }
37
+ interface BulkItem {
38
+ destination: string;
39
+ slug?: string;
40
+ }
41
+ interface BulkResult {
42
+ destination: string;
43
+ slug?: string;
44
+ shortUrl?: string;
45
+ error?: string;
46
+ }
47
+ interface Link {
48
+ slug: string;
49
+ destination: string;
50
+ view_count: number;
51
+ created_at: string;
52
+ expires_at?: string | null;
53
+ has_password: boolean;
54
+ created_via: string;
55
+ }
56
+ interface LinksPage {
57
+ links: Link[];
58
+ total: number;
59
+ totalClicks: number;
60
+ page: number;
61
+ limit: number;
62
+ }
63
+ interface Analytics {
64
+ total_clicks: number;
65
+ daily: {
66
+ date: string;
67
+ clicks: number;
68
+ }[];
69
+ countries: {
70
+ country: string;
71
+ count: number;
72
+ }[];
73
+ devices: {
74
+ device: string;
75
+ count: number;
76
+ }[];
77
+ browsers: {
78
+ browser: string;
79
+ count: number;
80
+ }[];
81
+ referrers: {
82
+ referrer: string;
83
+ count: number;
84
+ }[];
85
+ }
86
+ declare class HttpError extends Error {
87
+ readonly status: number;
88
+ readonly code: string;
89
+ constructor(status: number, code: string, message: string);
90
+ }
91
+ declare class LinksNamespace {
92
+ private readonly _req;
93
+ constructor(_req: <T>(m: string, p: string, b?: unknown, q?: Record<string, string | number>) => Promise<T>);
94
+ /** List links. Paginated — default 10 per page. */
95
+ list(page?: number, limit?: number): Promise<LinksPage>;
96
+ /** Delete a link by slug. */
97
+ delete(slug: string): Promise<void>;
98
+ /** Get click analytics for a link. */
99
+ analytics(slug: string, options?: {
100
+ days?: number;
101
+ }): Promise<Analytics>;
102
+ }
103
+ declare class AliasClient {
104
+ private readonly _base;
105
+ private readonly _key;
106
+ /** Namespaced link operations: list, delete, analytics. */
107
+ readonly links: LinksNamespace;
108
+ constructor({ apiKey, baseUrl }: AliasClientOptions);
109
+ private _req;
110
+ /**
111
+ * Shorten a single URL.
112
+ *
113
+ * @example
114
+ * const { shortUrl } = await alias.shorten('https://example.com');
115
+ * const { shortUrl } = await alias.shorten('https://example.com', { slug: 'my-link' });
116
+ */
117
+ shorten(destination: string, options?: ShortenOptions): Promise<ShortenResult>;
118
+ /**
119
+ * Shorten multiple URLs in one request (max 50).
120
+ *
121
+ * @example
122
+ * const results = await alias.bulkShorten([
123
+ * { destination: 'https://a.com' },
124
+ * { destination: 'https://b.com', slug: 'b-link' },
125
+ * ]);
126
+ */
127
+ bulkShorten(items: BulkItem[]): Promise<BulkResult[]>;
128
+ }
129
+
130
+ export { AliasClient, type AliasClientOptions, HttpError as AliasHttpError, type Analytics, type BulkItem, type BulkResult, type Link, type LinksPage, type ShortenOptions, type ShortenResult, AliasClient as default };
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Alias URL Shortener — Official JavaScript / TypeScript SDK
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { AliasClient } from 'aliaslive-sdk';
7
+ *
8
+ * const alias = new AliasClient({ apiKey: 'alias_xxxx' });
9
+ *
10
+ * const { shortUrl } = await alias.shorten('https://example.com');
11
+ * const { shortUrl } = await alias.shorten('https://example.com', { slug: 'my-link' });
12
+ * const results = await alias.bulkShorten([{ destination: 'https://a.com' }]);
13
+ * const { links } = await alias.links.list();
14
+ * await alias.links.delete('my-link');
15
+ * const analytics = await alias.links.analytics('my-link');
16
+ * ```
17
+ */
18
+ interface AliasClientOptions {
19
+ /** API key from aliasurlshortener.com/account */
20
+ apiKey: string;
21
+ /** Override the API base URL (default: https://alias.live/api/v1) */
22
+ baseUrl?: string;
23
+ }
24
+ interface ShortenOptions {
25
+ /** Custom slug (letters, numbers, _ or - ; max 40 chars) */
26
+ slug?: string;
27
+ /** Password to protect the link */
28
+ password?: string;
29
+ /** Expiry as an ISO-8601 datetime string */
30
+ expiresAt?: string;
31
+ }
32
+ interface ShortenResult {
33
+ slug: string;
34
+ shortUrl: string;
35
+ destination: string;
36
+ }
37
+ interface BulkItem {
38
+ destination: string;
39
+ slug?: string;
40
+ }
41
+ interface BulkResult {
42
+ destination: string;
43
+ slug?: string;
44
+ shortUrl?: string;
45
+ error?: string;
46
+ }
47
+ interface Link {
48
+ slug: string;
49
+ destination: string;
50
+ view_count: number;
51
+ created_at: string;
52
+ expires_at?: string | null;
53
+ has_password: boolean;
54
+ created_via: string;
55
+ }
56
+ interface LinksPage {
57
+ links: Link[];
58
+ total: number;
59
+ totalClicks: number;
60
+ page: number;
61
+ limit: number;
62
+ }
63
+ interface Analytics {
64
+ total_clicks: number;
65
+ daily: {
66
+ date: string;
67
+ clicks: number;
68
+ }[];
69
+ countries: {
70
+ country: string;
71
+ count: number;
72
+ }[];
73
+ devices: {
74
+ device: string;
75
+ count: number;
76
+ }[];
77
+ browsers: {
78
+ browser: string;
79
+ count: number;
80
+ }[];
81
+ referrers: {
82
+ referrer: string;
83
+ count: number;
84
+ }[];
85
+ }
86
+ declare class HttpError extends Error {
87
+ readonly status: number;
88
+ readonly code: string;
89
+ constructor(status: number, code: string, message: string);
90
+ }
91
+ declare class LinksNamespace {
92
+ private readonly _req;
93
+ constructor(_req: <T>(m: string, p: string, b?: unknown, q?: Record<string, string | number>) => Promise<T>);
94
+ /** List links. Paginated — default 10 per page. */
95
+ list(page?: number, limit?: number): Promise<LinksPage>;
96
+ /** Delete a link by slug. */
97
+ delete(slug: string): Promise<void>;
98
+ /** Get click analytics for a link. */
99
+ analytics(slug: string, options?: {
100
+ days?: number;
101
+ }): Promise<Analytics>;
102
+ }
103
+ declare class AliasClient {
104
+ private readonly _base;
105
+ private readonly _key;
106
+ /** Namespaced link operations: list, delete, analytics. */
107
+ readonly links: LinksNamespace;
108
+ constructor({ apiKey, baseUrl }: AliasClientOptions);
109
+ private _req;
110
+ /**
111
+ * Shorten a single URL.
112
+ *
113
+ * @example
114
+ * const { shortUrl } = await alias.shorten('https://example.com');
115
+ * const { shortUrl } = await alias.shorten('https://example.com', { slug: 'my-link' });
116
+ */
117
+ shorten(destination: string, options?: ShortenOptions): Promise<ShortenResult>;
118
+ /**
119
+ * Shorten multiple URLs in one request (max 50).
120
+ *
121
+ * @example
122
+ * const results = await alias.bulkShorten([
123
+ * { destination: 'https://a.com' },
124
+ * { destination: 'https://b.com', slug: 'b-link' },
125
+ * ]);
126
+ */
127
+ bulkShorten(items: BulkItem[]): Promise<BulkResult[]>;
128
+ }
129
+
130
+ export { AliasClient, type AliasClientOptions, HttpError as AliasHttpError, type Analytics, type BulkItem, type BulkResult, type Link, type LinksPage, type ShortenOptions, type ShortenResult, AliasClient as default };
package/dist/index.js ADDED
@@ -0,0 +1,116 @@
1
+ // src/index.ts
2
+ var HttpError = class extends Error {
3
+ constructor(status, code, message) {
4
+ super(message);
5
+ this.status = status;
6
+ this.code = code;
7
+ this.name = "AliasHttpError";
8
+ }
9
+ };
10
+ async function request(baseUrl, apiKey, method, path, body, params) {
11
+ const url = new URL(`${baseUrl}${path}`);
12
+ if (params) {
13
+ Object.entries(params).forEach(([k, v]) => url.searchParams.set(k, String(v)));
14
+ }
15
+ const res = await fetch(url.toString(), {
16
+ method,
17
+ headers: {
18
+ "Content-Type": "application/json",
19
+ "X-Api-Key": apiKey
20
+ },
21
+ body: body !== void 0 ? JSON.stringify(body) : void 0
22
+ });
23
+ const json = await res.json();
24
+ if (!res.ok || !json.success) {
25
+ throw new HttpError(res.status, json.error?.code ?? "UNKNOWN", json.error?.message ?? `HTTP ${res.status}`);
26
+ }
27
+ return json.data;
28
+ }
29
+ var LinksNamespace = class {
30
+ constructor(_req) {
31
+ this._req = _req;
32
+ }
33
+ /** List links. Paginated — default 10 per page. */
34
+ async list(page = 1, limit = 10) {
35
+ const raw = await this._req("GET", "/links", void 0, { page, limit });
36
+ return {
37
+ links: raw.result,
38
+ total: raw.total,
39
+ totalClicks: raw.total_clicks,
40
+ page: raw.page,
41
+ limit: raw.limit
42
+ };
43
+ }
44
+ /** Delete a link by slug. */
45
+ delete(slug) {
46
+ return this._req("DELETE", `/links/${encodeURIComponent(slug)}`);
47
+ }
48
+ /** Get click analytics for a link. */
49
+ analytics(slug, options) {
50
+ return this._req("GET", `/links/${encodeURIComponent(slug)}/analytics`, void 0, {
51
+ days: options?.days ?? 30
52
+ });
53
+ }
54
+ };
55
+ var AliasClient = class {
56
+ constructor({ apiKey, baseUrl = "https://alias.live/api/v1" }) {
57
+ if (!apiKey) throw new Error("AliasClient: apiKey is required");
58
+ this._base = baseUrl.replace(/\/$/, "");
59
+ this._key = apiKey;
60
+ this.links = new LinksNamespace(this._req.bind(this));
61
+ }
62
+ _req(method, path, body, params) {
63
+ return request(this._base, this._key, method, path, body, params);
64
+ }
65
+ /**
66
+ * Shorten a single URL.
67
+ *
68
+ * @example
69
+ * const { shortUrl } = await alias.shorten('https://example.com');
70
+ * const { shortUrl } = await alias.shorten('https://example.com', { slug: 'my-link' });
71
+ */
72
+ async shorten(destination, options = {}) {
73
+ const { slug, password, expiresAt } = options;
74
+ const data = await this._req("POST", "/customShort", {
75
+ destination,
76
+ ...slug ? { slug } : {},
77
+ ...password ? { password } : {},
78
+ ...expiresAt ? { expires_at: expiresAt } : {}
79
+ });
80
+ return {
81
+ slug: data.slug,
82
+ shortUrl: `https://alias.live/${data.slug}`,
83
+ destination
84
+ };
85
+ }
86
+ /**
87
+ * Shorten multiple URLs in one request (max 50).
88
+ *
89
+ * @example
90
+ * const results = await alias.bulkShorten([
91
+ * { destination: 'https://a.com' },
92
+ * { destination: 'https://b.com', slug: 'b-link' },
93
+ * ]);
94
+ */
95
+ async bulkShorten(items) {
96
+ if (items.length === 0) return [];
97
+ if (items.length > 50) throw new Error("bulkShorten: max 50 items per call");
98
+ const data = await this._req(
99
+ "POST",
100
+ "/links/bulk",
101
+ { links: items }
102
+ );
103
+ return data.results.map((r) => ({
104
+ destination: r.destination,
105
+ slug: r.slug,
106
+ shortUrl: r.short_url,
107
+ error: r.error
108
+ }));
109
+ }
110
+ };
111
+ var index_default = AliasClient;
112
+ export {
113
+ AliasClient,
114
+ HttpError as AliasHttpError,
115
+ index_default as default
116
+ };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "aliaslive-sdk",
3
+ "version": "1.0.0",
4
+ "description": "Official JavaScript / TypeScript SDK for alias.live URL shortener",
5
+ "keywords": ["url-shortener", "alias", "sdk", "short-links"],
6
+ "author": "Harsh Malkani",
7
+ "license": "MIT",
8
+ "homepage": "https://aliasurlshortener.com",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/harshmalkani/alias-sdk.git"
12
+ },
13
+ "type": "module",
14
+ "main": "./dist/index.cjs",
15
+ "module": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.js",
21
+ "require": "./dist/index.cjs"
22
+ }
23
+ },
24
+ "files": ["dist", "README.md"],
25
+ "scripts": {
26
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean",
27
+ "dev": "tsup src/index.ts --format esm,cjs --dts --watch"
28
+ },
29
+ "devDependencies": {
30
+ "tsup": "^8.0.0",
31
+ "typescript": "^5.0.0"
32
+ },
33
+ "engines": { "node": ">=18" }
34
+ }