npm.io
0.0.2 • Published 1 year ago

@clnk/sdk

Licence
MIT
Version
0.0.2
Deps
1
Size
37 kB
Vulns
0
Weekly
0

Clnk URL Shortener SDK

A TypeScript SDK for interacting with the Clnk URL shortener GraphQL API.

Features

  • Create and manage short URLs
  • Generate QR codes for short URLs
  • Authentication (login, register, token refresh)
  • API key management
  • TypeScript support with full type definitions

Installation

npm install @clnk/sdk
# or
yarn add @clnk/sdk

Quick Start

import { ClnkSDK } from '@clnk/sdk';

// Initialize the SDK
const sdk = new ClnkSDK({
  apiKey: 'your-api-key',  // Required
  accessToken: 'your-access-token' // Optional, can be set later with sdk.setAccessToken()
});

// Login
const auth = await sdk.login({
  email: 'user@example.com',
  password: 'password123'
});

// Create a short URL
const shortUrl = await sdk.createUrl({
  url: 'https://example.com/very-long-url',
  shorten: true // Let the API auto-generate a short code
});

// Generate a QR code URL
const qrCodeUrl = sdk.generateQRCodeUrl(shortUrl.shortUrl);

Authentication

Login
const authData = await sdk.login({
  email: 'user@example.com',
  password: 'password123'
});

// The SDK automatically saves the access token for future requests
console.log(authData.accessToken);
console.log(authData.refreshToken);
console.log(authData.user);
Register
const registerData = await sdk.register({
  name: 'John Doe',
  email: 'user@example.com',
  password: 'password123'
});
Refresh Token
const refreshData = await sdk.refreshToken('your-refresh-token');
// The SDK automatically saves the new access token
Set Access Token Manually
sdk.setAccessToken('your-access-token');

URL Management

Create URL
// Auto-generated short code
const url1 = await sdk.createUrl({
  url: 'https://example.com/long-url',
  shorten: true
});

// Custom short code
const url2 = await sdk.createUrl({
  url: 'https://example.com/long-url',
  code: 'custom-code'
});

// With an image (for visual links)
const url3 = await sdk.createUrl({
  url: 'https://example.com/long-url',
  shorten: true,
  image: 'https://example.com/image.jpg'
});
Update URL
const updatedUrl = await sdk.updateUrl({
  id: 'url-id',
  url: 'https://example.com/updated-url',
  shortUrl: 'new-custom-code'
});
Delete URL
const deleted = await sdk.deleteUrl('url-id');
Get URL
// By ID
const url1 = await sdk.getUrl({ id: 'url-id' });

// By code
const url2 = await sdk.getUrl({ code: 'abc123' });
Get URLs
// Get user's URLs
const urlData = await sdk.getUrls({
  pagination: { page: 1, limit: 10 }
});

// With filtering
const filteredUrls = await sdk.getUrls({
  filter: {
    url: 'example.com'
  },
  pagination: { page: 1, limit: 20 }
});
Get All URLs (Admin)
const allUrls = await sdk.getAllUrls({
  pagination: { page: 1, limit: 50 }
});

QR Code Generation

// Default size (300x300)
const qrUrl1 = sdk.generateQRCodeUrl('https://clnk.to/abc123');

// Custom size
const qrUrl2 = sdk.generateQRCodeUrl('https://clnk.to/abc123', 500);

API Key Management

// Generate new API key
const apiKey = await sdk.generateApiKey();

User Management

// Get current user info
const currentUser = await sdk.getCurrentUser();

Important Notes

  1. API Key is required when initializing the SDK
  2. Access token can be provided during initialization or set later with setAccessToken()
  3. The SDK automatically manages the access token after login or token refresh

License

MIT