@tinyurl-ca/sdk 1.0.1

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,208 @@
1
+ # TinyURL.ca JavaScript/TypeScript SDK
2
+
3
+ Official JavaScript/TypeScript SDK for the TinyURL.ca API.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @tinyurl-ca/sdk
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import { TinyURLClient } from '@tinyurl-ca/sdk';
15
+
16
+ // Initialize client
17
+ const client = new TinyURLClient({
18
+ apiKey: 'your_api_key_here'
19
+ });
20
+
21
+ // Shorten a URL
22
+ const link = await client.createLink({
23
+ url: 'https://example.com'
24
+ });
25
+
26
+ console.log(link.shortUrl); // https://tinyurl.ca/abc123
27
+ ```
28
+
29
+ ## API Reference
30
+
31
+ ### Initialize Client
32
+
33
+ ```typescript
34
+ import { TinyURLClient } from '@tinyurl-ca/sdk';
35
+
36
+ const client = new TinyURLClient({
37
+ apiKey: 'your_api_key_here',
38
+ baseURL: 'https://tinyurl.ca/api/v1' // optional
39
+ });
40
+ ```
41
+
42
+ ### Create a Link
43
+
44
+ ```typescript
45
+ const link = await client.createLink({
46
+ url: 'https://example.com',
47
+ customCode: 'my-link', // optional
48
+ title: 'My Website', // optional
49
+ expiresIn: 86400 // optional, seconds
50
+ });
51
+
52
+ // Response
53
+ {
54
+ id: 'abc123',
55
+ shortCode: 'my-link',
56
+ shortUrl: 'https://tinyurl.ca/my-link',
57
+ originalUrl: 'https://example.com',
58
+ title: 'My Website',
59
+ clicks: 0,
60
+ createdAt: '2026-04-04T00:00:00Z',
61
+ expiresAt: '2026-04-05T00:00:00Z'
62
+ }
63
+ ```
64
+
65
+ ### Get Link Details
66
+
67
+ ```typescript
68
+ const link = await client.getLink('link_id');
69
+ ```
70
+
71
+ ### List Links
72
+
73
+ ```typescript
74
+ const links = await client.listLinks({
75
+ limit: 50, // optional, default 50
76
+ offset: 0 // optional, default 0
77
+ });
78
+ ```
79
+
80
+ ### Delete Link
81
+
82
+ ```typescript
83
+ await client.deleteLink('link_id');
84
+ ```
85
+
86
+ ### Shorten URL (convenience method)
87
+
88
+ ```typescript
89
+ const shortUrl = await client.shorten('https://example.com');
90
+ // Returns: 'https://tinyurl.ca/abc123'
91
+
92
+ // With custom code
93
+ const customUrl = await client.shorten('https://example.com', 'promo');
94
+ // Returns: 'https://tinyurl.ca/promo'
95
+ ```
96
+
97
+ ## TypeScript Support
98
+
99
+ Full TypeScript support with type definitions included.
100
+
101
+ ```typescript
102
+ import { TinyURLClient, Link, CreateLinkOptions } from '@tinyurl-ca/sdk';
103
+
104
+ const options: CreateLinkOptions = {
105
+ url: 'https://example.com',
106
+ customCode: 'my-link'
107
+ };
108
+
109
+ const link: Link = await client.createLink(options);
110
+ ```
111
+
112
+ ## Error Handling
113
+
114
+ ```typescript
115
+ try {
116
+ const link = await client.createLink({
117
+ url: 'https://example.com',
118
+ customCode: 'taken'
119
+ });
120
+ } catch (error) {
121
+ if (error.response?.status === 409) {
122
+ console.log('Custom code already in use');
123
+ } else {
124
+ console.error('Error:', error.message);
125
+ }
126
+ }
127
+ ```
128
+
129
+ ## Examples
130
+
131
+ ### Node.js
132
+
133
+ ```javascript
134
+ const { TinyURLClient } = require('@tinyurl-ca/sdk');
135
+
136
+ const client = new TinyURLClient({
137
+ apiKey: process.env.TINYURL_API_KEY
138
+ });
139
+
140
+ async function main() {
141
+ // Create link
142
+ const link = await client.createLink({
143
+ url: 'https://mywebsite.com',
144
+ title: 'My Website'
145
+ });
146
+
147
+ console.log('Short URL:', link.shortUrl);
148
+
149
+ // List all links
150
+ const links = await client.listLinks();
151
+ console.log('Total links:', links.length);
152
+ }
153
+
154
+ main();
155
+ ```
156
+
157
+ ### Next.js API Route
158
+
159
+ ```typescript
160
+ import { TinyURLClient } from '@tinyurl-ca/sdk';
161
+ import { NextRequest, NextResponse } from 'next/server';
162
+
163
+ const client = new TinyURLClient({
164
+ apiKey: process.env.TINYURL_API_KEY!
165
+ });
166
+
167
+ export async function POST(request: NextRequest) {
168
+ const { url } = await request.json();
169
+
170
+ const link = await client.createLink({ url });
171
+
172
+ return NextResponse.json({ shortUrl: link.shortUrl });
173
+ }
174
+ ```
175
+
176
+ ### Express.js
177
+
178
+ ```javascript
179
+ const express = require('express');
180
+ const { TinyURLClient } = require('@tinyurl-ca/sdk');
181
+
182
+ const app = express();
183
+ const client = new TinyURLClient({
184
+ apiKey: process.env.TINYURL_API_KEY
185
+ });
186
+
187
+ app.post('/shorten', async (req, res) => {
188
+ try {
189
+ const { url } = req.body;
190
+ const link = await client.createLink({ url });
191
+ res.json({ shortUrl: link.shortUrl });
192
+ } catch (error) {
193
+ res.status(500).json({ error: error.message });
194
+ }
195
+ });
196
+
197
+ app.listen(3000);
198
+ ```
199
+
200
+ ## License
201
+
202
+ MIT
203
+
204
+ ## Support
205
+
206
+ - Documentation: https://tinyurl.ca/docs
207
+ - Issues: https://github.com/raj-iwt/tinyurl-sdk/issues
208
+ - Email: support@tinyurl.ca
@@ -0,0 +1,70 @@
1
+ interface TinyURLConfig {
2
+ apiKey: string;
3
+ baseURL?: string;
4
+ }
5
+ interface CreateLinkOptions {
6
+ url: string;
7
+ customCode?: string;
8
+ title?: string;
9
+ expiresIn?: number;
10
+ }
11
+ interface Link {
12
+ id: string;
13
+ shortCode: string;
14
+ shortUrl: string;
15
+ originalUrl: string;
16
+ title?: string;
17
+ clicks: number;
18
+ createdAt: string;
19
+ expiresAt?: string;
20
+ }
21
+ interface ListLinksOptions {
22
+ limit?: number;
23
+ offset?: number;
24
+ }
25
+ interface APIResponse<T> {
26
+ success: boolean;
27
+ data: T;
28
+ pagination?: {
29
+ limit: number;
30
+ offset: number;
31
+ total: number;
32
+ };
33
+ }
34
+ declare class TinyURLClient {
35
+ private api;
36
+ constructor(config: TinyURLConfig);
37
+ /**
38
+ * Create a new short link
39
+ * @param options - Link creation options
40
+ * @returns Created link details
41
+ */
42
+ createLink(options: CreateLinkOptions): Promise<Link>;
43
+ /**
44
+ * Get a specific link by ID
45
+ * @param id - Link ID
46
+ * @returns Link details
47
+ */
48
+ getLink(id: string): Promise<Link>;
49
+ /**
50
+ * List all links
51
+ * @param options - List options
52
+ * @returns Array of links
53
+ */
54
+ listLinks(options?: ListLinksOptions): Promise<Link[]>;
55
+ /**
56
+ * Delete a link
57
+ * @param id - Link ID to delete
58
+ */
59
+ deleteLink(id: string): Promise<void>;
60
+ /**
61
+ * Shorten a URL (convenience method)
62
+ * @param url - URL to shorten
63
+ * @param customCode - Optional custom short code
64
+ * @returns Short URL
65
+ */
66
+ shorten(url: string, customCode?: string): Promise<string>;
67
+ }
68
+ declare function createClient(config: TinyURLConfig): TinyURLClient;
69
+
70
+ export { type APIResponse, type CreateLinkOptions, type Link, type ListLinksOptions, TinyURLClient, type TinyURLConfig, createClient, TinyURLClient as default };
@@ -0,0 +1,70 @@
1
+ interface TinyURLConfig {
2
+ apiKey: string;
3
+ baseURL?: string;
4
+ }
5
+ interface CreateLinkOptions {
6
+ url: string;
7
+ customCode?: string;
8
+ title?: string;
9
+ expiresIn?: number;
10
+ }
11
+ interface Link {
12
+ id: string;
13
+ shortCode: string;
14
+ shortUrl: string;
15
+ originalUrl: string;
16
+ title?: string;
17
+ clicks: number;
18
+ createdAt: string;
19
+ expiresAt?: string;
20
+ }
21
+ interface ListLinksOptions {
22
+ limit?: number;
23
+ offset?: number;
24
+ }
25
+ interface APIResponse<T> {
26
+ success: boolean;
27
+ data: T;
28
+ pagination?: {
29
+ limit: number;
30
+ offset: number;
31
+ total: number;
32
+ };
33
+ }
34
+ declare class TinyURLClient {
35
+ private api;
36
+ constructor(config: TinyURLConfig);
37
+ /**
38
+ * Create a new short link
39
+ * @param options - Link creation options
40
+ * @returns Created link details
41
+ */
42
+ createLink(options: CreateLinkOptions): Promise<Link>;
43
+ /**
44
+ * Get a specific link by ID
45
+ * @param id - Link ID
46
+ * @returns Link details
47
+ */
48
+ getLink(id: string): Promise<Link>;
49
+ /**
50
+ * List all links
51
+ * @param options - List options
52
+ * @returns Array of links
53
+ */
54
+ listLinks(options?: ListLinksOptions): Promise<Link[]>;
55
+ /**
56
+ * Delete a link
57
+ * @param id - Link ID to delete
58
+ */
59
+ deleteLink(id: string): Promise<void>;
60
+ /**
61
+ * Shorten a URL (convenience method)
62
+ * @param url - URL to shorten
63
+ * @param customCode - Optional custom short code
64
+ * @returns Short URL
65
+ */
66
+ shorten(url: string, customCode?: string): Promise<string>;
67
+ }
68
+ declare function createClient(config: TinyURLConfig): TinyURLClient;
69
+
70
+ export { type APIResponse, type CreateLinkOptions, type Link, type ListLinksOptions, TinyURLClient, type TinyURLConfig, createClient, TinyURLClient as default };
package/dist/index.js ADDED
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ TinyURLClient: () => TinyURLClient,
34
+ createClient: () => createClient,
35
+ default: () => index_default
36
+ });
37
+ module.exports = __toCommonJS(index_exports);
38
+ var import_axios = __toESM(require("axios"));
39
+ var TinyURLClient = class {
40
+ constructor(config) {
41
+ this.api = import_axios.default.create({
42
+ baseURL: config.baseURL || "https://tinyurl.ca/api/v1",
43
+ headers: {
44
+ "X-API-Key": config.apiKey,
45
+ "Content-Type": "application/json"
46
+ }
47
+ });
48
+ }
49
+ /**
50
+ * Create a new short link
51
+ * @param options - Link creation options
52
+ * @returns Created link details
53
+ */
54
+ async createLink(options) {
55
+ const response = await this.api.post("/links", options);
56
+ return response.data.data;
57
+ }
58
+ /**
59
+ * Get a specific link by ID
60
+ * @param id - Link ID
61
+ * @returns Link details
62
+ */
63
+ async getLink(id) {
64
+ const response = await this.api.get(`/links/${id}`);
65
+ return response.data.data;
66
+ }
67
+ /**
68
+ * List all links
69
+ * @param options - List options
70
+ * @returns Array of links
71
+ */
72
+ async listLinks(options) {
73
+ const response = await this.api.get("/links", {
74
+ params: options
75
+ });
76
+ return response.data.data;
77
+ }
78
+ /**
79
+ * Delete a link
80
+ * @param id - Link ID to delete
81
+ */
82
+ async deleteLink(id) {
83
+ await this.api.delete(`/links/${id}`);
84
+ }
85
+ /**
86
+ * Shorten a URL (convenience method)
87
+ * @param url - URL to shorten
88
+ * @param customCode - Optional custom short code
89
+ * @returns Short URL
90
+ */
91
+ async shorten(url, customCode) {
92
+ const link = await this.createLink({ url, customCode });
93
+ return link.shortUrl;
94
+ }
95
+ };
96
+ function createClient(config) {
97
+ return new TinyURLClient(config);
98
+ }
99
+ var index_default = TinyURLClient;
100
+ // Annotate the CommonJS export names for ESM import in node:
101
+ 0 && (module.exports = {
102
+ TinyURLClient,
103
+ createClient
104
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,68 @@
1
+ // src/index.ts
2
+ import axios from "axios";
3
+ var TinyURLClient = class {
4
+ constructor(config) {
5
+ this.api = axios.create({
6
+ baseURL: config.baseURL || "https://tinyurl.ca/api/v1",
7
+ headers: {
8
+ "X-API-Key": config.apiKey,
9
+ "Content-Type": "application/json"
10
+ }
11
+ });
12
+ }
13
+ /**
14
+ * Create a new short link
15
+ * @param options - Link creation options
16
+ * @returns Created link details
17
+ */
18
+ async createLink(options) {
19
+ const response = await this.api.post("/links", options);
20
+ return response.data.data;
21
+ }
22
+ /**
23
+ * Get a specific link by ID
24
+ * @param id - Link ID
25
+ * @returns Link details
26
+ */
27
+ async getLink(id) {
28
+ const response = await this.api.get(`/links/${id}`);
29
+ return response.data.data;
30
+ }
31
+ /**
32
+ * List all links
33
+ * @param options - List options
34
+ * @returns Array of links
35
+ */
36
+ async listLinks(options) {
37
+ const response = await this.api.get("/links", {
38
+ params: options
39
+ });
40
+ return response.data.data;
41
+ }
42
+ /**
43
+ * Delete a link
44
+ * @param id - Link ID to delete
45
+ */
46
+ async deleteLink(id) {
47
+ await this.api.delete(`/links/${id}`);
48
+ }
49
+ /**
50
+ * Shorten a URL (convenience method)
51
+ * @param url - URL to shorten
52
+ * @param customCode - Optional custom short code
53
+ * @returns Short URL
54
+ */
55
+ async shorten(url, customCode) {
56
+ const link = await this.createLink({ url, customCode });
57
+ return link.shortUrl;
58
+ }
59
+ };
60
+ function createClient(config) {
61
+ return new TinyURLClient(config);
62
+ }
63
+ var index_default = TinyURLClient;
64
+ export {
65
+ TinyURLClient,
66
+ createClient,
67
+ index_default as default
68
+ };
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@tinyurl-ca/sdk",
3
+ "version": "1.0.1",
4
+ "description": "Official JavaScript/TypeScript SDK for TinyURL.ca API",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "scripts": {
9
+ "build": "tsup src/index.ts --format cjs,esm --dts",
10
+ "test": "jest",
11
+ "prepublishOnly": "npm run build"
12
+ },
13
+ "keywords": [
14
+ "url-shortener",
15
+ "tinyurl",
16
+ "sdk",
17
+ "api-client",
18
+ "typescript"
19
+ ],
20
+ "author": "TinyURL.ca",
21
+ "license": "MIT",
22
+ "dependencies": {
23
+ "axios": "^1.6.0"
24
+ },
25
+ "devDependencies": {
26
+ "@types/node": "^20.10.0",
27
+ "tsup": "^8.0.0",
28
+ "typescript": "^5.3.0",
29
+ "jest": "^29.7.0",
30
+ "@types/jest": "^29.5.0"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/raj-iwt/linksnap.git",
35
+ "directory": "sdk"
36
+ },
37
+ "homepage": "https://tinyurl.ca/docs"
38
+ }