Skip to content

Commit

Permalink
use dub SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Oct 5, 2024
1 parent 0d25748 commit 4fca097
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 110 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
43 changes: 14 additions & 29 deletions packages/cli/src/api/links.ts
Original file line number Diff line number Diff line change
@@ -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();
}
23 changes: 2 additions & 21 deletions packages/cli/src/commands/shorten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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;
},
},
],
{
Expand All @@ -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");
Expand Down
60 changes: 0 additions & 60 deletions packages/cli/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4fca097

Please sign in to comment.