The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
use dub SDK
Steven Tey4fca097178ce
diff --git a/packages/cli/package.json b/packages/cli/package.jsonindex 79ecfb3237..d9239235c3 100644--- a/packages/cli/package.json+++ b/packages/cli/package.json@@ -44,6 +44,7 @@"chalk": "^5.3.0","commander": "^11.1.0","configstore": "^6.0.0",+ "dub": "^0.43.0","fs-extra": "^11.2.0","json-colorizer": "^2.2.2","nanoid": "^5.0.7",diff --git a/packages/cli/src/api/links.ts b/packages/cli/src/api/links.tsindex cf7db76bba..b7d8349603 100644--- a/packages/cli/src/api/links.ts+++ b/packages/cli/src/api/links.ts@@ -1,41 +1,26 @@-import type { CreateLinkProps, Link } from "@/types";import { getConfig } from "@/utils/config";-import { parseApiResponse } from "@/utils/parser";-import fetch from "node-fetch";+import { Dub } from "dub";-export async function createLink({ url, key }: CreateLinkProps) {+export async function createLink({ url, key }: { url: string; key?: string }) {const config = await getConfig();- const options = {- method: "POST",- headers: {- Authorization: `Bearer ${config.key}`,- "Content-Type": "application/json",- },- body: JSON.stringify({- domain: config.domain,- url: url,- key: key,- }),- };+ const dub = new Dub({+ token: config.key,+ });- const response = await fetch("https://api.dub.co/links", options);-- return await parseApiResponse<Link>(response);+ return await dub.links.create({+ domain: config.domain,+ url: url,+ key: key,+ });}export async function getLinks() {const config = await getConfig();- const options = {- method: "GET",- headers: {- Authorization: `Bearer ${config.key}`,- "Content-Type": "application/json",- },- };-- const response = await fetch("https://api.dub.co/links", options);+ const dub = new Dub({+ token: config.key,+ });- return await parseApiResponse<Link[]>(response);+ return await dub.links.list();}diff --git a/packages/cli/src/commands/shorten.ts b/packages/cli/src/commands/shorten.tsindex 4cf60200e5..3d72fefd82 100644--- a/packages/cli/src/commands/shorten.ts+++ b/packages/cli/src/commands/shorten.ts@@ -7,12 +7,6 @@ import chalk from "chalk";import { Command } from "commander";import ora from "ora";import prompts from "prompts";-import { z } from "zod";--const addOptionsSchema = z.object({- url: z.string().url("Please enter a valid URL"),- key: z.string().min(4, "Key must be at least 4 characters long"),-});export const shorten = new Command().name("shorten")@@ -32,20 +26,12 @@ export const shorten = new Command()type: "text",name: "url",message: "Enter your Destination URL:",- validate: (value) => {- const result = addOptionsSchema.shape.url.safeParse(value);- return result.success || result.error.errors[0].message;- },},{type: "text",name: "key",message: "Enter your Short link:",initial: getNanoid(),- validate: (value) => {- const result = addOptionsSchema.shape.key.safeParse(value);- return result.success || result.error.errors[0].message;- },},],{@@ -59,20 +45,15 @@ export const shorten = new Command());}- const validatedData = addOptionsSchema.parse(linkData);const spinner = ora("Creating new short link").start();try {- const generatedShortLink = await createLink(validatedData);+ const generatedShortLink = await createLink(linkData);spinner.succeed("New short link created!");logger.info("");- logger.info(- chalk.green(- `https://${generatedShortLink.domain}/${generatedShortLink.key}`,- ),- );+ logger.info(chalk.green(generatedShortLink.shortLink));logger.info("");} catch (error) {spinner.fail("Failed to create link");diff --git a/packages/cli/src/types/index.ts b/packages/cli/src/types/index.tsindex d8b67c953c..7931476b92 100644--- a/packages/cli/src/types/index.ts+++ b/packages/cli/src/types/index.ts@@ -19,66 +19,6 @@ export type Domain = {expiresAt: string;};};--export interface CreateLinkProps {- url: string;- key?: string;-}--type tag = {- id: string;- name: string;- color: string;-};--type Geo<T = string> = {- [key: string]: T;-};--export type Link = {- id: string;- domain: string;- key: string;- externalId: string;- url: string;- trackConversion: boolean;- archived: boolean;- expiresAt: string;- expiredUrl: string;- password: string;- proxy: boolean;- title: string;- description: string;- image: string;- video: string;- rewrite: boolean;- doIndex: boolean;- ios: string;- android: string;- geo: Geo;- publicStats: boolean;- tagId: string;- tags: tag[];- comments: string;- shortLink: string;- qrCode: string;- utm_source: string;- utm_medium: string;- utm_campaign: string;- utm_term: string;- utm_content: string;- userId: string;- workspaceId: string;- clicks: number;- lastClicked: string;- leads: number;- sales: number;- saleAmount: number;- createdAt: string;- updatedAt: string;- projectId: string;-};-export interface APIError {error: {code: string;diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 5b9246e96c..d7406b2da9 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -490,6 +490,9 @@ importers:configstore:specifier: ^6.0.0version: 6.0.0+ dub:+ specifier: ^0.43.0+ version: 0.43.0([email protected])fs-extra:specifier: ^11.2.0version: 11.2.0@@ -12443,6 +12446,14 @@ packages:zod: 3.22.4dev: false+ resolution: {integrity: sha512-PxS0165fQqeMNDYdSbAAxc69WphtonAyVWh3IVbM0isdGfOeTNIX9gqqZ8ZZORr5/L4CVnipnip3+PRIlVu5Rg==}+ peerDependencies:+ zod: '>= 3'+ dependencies:+ zod: 3.23.8+ dev: false+resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==}