Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

some updates

Kiran K5ae38a769f6a
diff --git a/packages/cli/src/utils/get-domains.ts b/packages/cli/src/api/domains.ts
similarity index 84%
rename from packages/cli/src/utils/get-domains.ts
rename to packages/cli/src/api/domains.ts
index 3cfe4894c2..e24feac503 100644
--- a/packages/cli/src/utils/get-domains.ts
+++ b/packages/cli/src/api/domains.ts
@@ -1,8 +1,13 @@
-import type { GetDomain } from "@/types";
-import { getConfig } from "@/utils/get-config";
+import type { Domain } from "@/types";
+import { getConfig } from "@/utils/config";
import { parseApiResponse } from "@/utils/parser";
import fetch from "node-fetch";
+interface UpdateDomainProps {
+ oldSlug: string;
+ newSlug: string;
+}
+
export async function getDomains() {
const config = await getConfig();
@@ -20,7 +25,7 @@ export async function getDomains() {
]);
const [domains, defaultDomains] = await Promise.all([
- parseApiResponse<GetDomain[]>(domainsResponse),
+ parseApiResponse<Domain[]>(domainsResponse),
parseApiResponse<string[]>(defaultDomainsResponse),
]);
@@ -44,12 +49,8 @@ export async function createDomain(slug: string) {
};
const response = await fetch("https://api.dub.co/domains", options);
- return await parseApiResponse<GetDomain[]>(response);
-}
-interface UpdateDomainProps {
- oldSlug: string;
- newSlug: string;
+ return await parseApiResponse<Domain[]>(response);
}
export async function updateDomain({ newSlug, oldSlug }: UpdateDomainProps) {
@@ -71,9 +72,7 @@ export async function updateDomain({ newSlug, oldSlug }: UpdateDomainProps) {
options,
);
- const parsedResponse = await parseApiResponse<GetDomain[]>(response);
-
- return parsedResponse;
+ return await parseApiResponse<Domain[]>(response);
}
export async function deleteDomain(slug: string) {
@@ -87,5 +86,6 @@ export async function deleteDomain(slug: string) {
};
const response = await fetch(`https://api.dub.co/domains/${slug}`, options);
- return await parseApiResponse<GetDomain["id"]>(response);
+
+ return await parseApiResponse<Domain["id"]>(response);
}
diff --git a/packages/cli/src/utils/get-links.ts b/packages/cli/src/api/links.ts
similarity index 69%
rename from packages/cli/src/utils/get-links.ts
rename to packages/cli/src/api/links.ts
index 6774692000..cf7db76bba 100644
--- a/packages/cli/src/utils/get-links.ts
+++ b/packages/cli/src/api/links.ts
@@ -1,5 +1,5 @@
-import type { CreateLinkProps, GetLink } from "@/types";
-import { getConfig } from "@/utils/get-config";
+import type { CreateLinkProps, Link } from "@/types";
+import { getConfig } from "@/utils/config";
import { parseApiResponse } from "@/utils/parser";
import fetch from "node-fetch";
@@ -20,7 +20,8 @@ export async function createLink({ url, key }: CreateLinkProps) {
};
const response = await fetch("https://api.dub.co/links", options);
- return await parseApiResponse<GetLink>(response);
+
+ return await parseApiResponse<Link>(response);
}
export async function getLinks() {
@@ -35,12 +36,6 @@ export async function getLinks() {
};
const response = await fetch("https://api.dub.co/links", options);
- const parsedData = await parseApiResponse<GetLink[]>(response);
-
- const links = parsedData.map((link) => ({
- url: `https://${link.domain}/${link.key}`,
- clicks: link.clicks,
- }));
- return links;
+ return await parseApiResponse<Link[]>(response);
}
diff --git a/packages/cli/src/commands/config.ts b/packages/cli/src/commands/config.ts
index 38003f3a59..3d4fbda72a 100644
--- a/packages/cli/src/commands/config.ts
+++ b/packages/cli/src/commands/config.ts
@@ -1,4 +1,4 @@
-import { getConfig } from "@/utils/get-config";
+import { getConfig } from "@/utils/config";
import { handleError } from "@/utils/handle-error";
import { logger } from "@/utils/logger";
import { Command } from "commander";
@@ -7,7 +7,7 @@ import ora from "ora";
export const config = new Command()
.name("config")
- .description("see your configured workspace credentails")
+ .description("see your configured workspace credentials")
.action(async () => {
const spinner = ora("Getting config file").start();
diff --git a/packages/cli/src/commands/domains.ts b/packages/cli/src/commands/domains.ts
index 8266f15c55..2477cf1a9e 100644
--- a/packages/cli/src/commands/domains.ts
+++ b/packages/cli/src/commands/domains.ts
@@ -1,4 +1,4 @@
-import { getDomains } from "@/utils/get-domains";
+import { getDomains } from "@/api/domains";
import { handleError } from "@/utils/handle-error";
import { logger } from "@/utils/logger";
import chalk from "chalk";
diff --git a/packages/cli/src/commands/link.ts b/packages/cli/src/commands/link.ts
index 7ab3bc53a7..f34670adf6 100644
--- a/packages/cli/src/commands/link.ts
+++ b/packages/cli/src/commands/link.ts
@@ -1,5 +1,5 @@
-import { getConfig } from "@/utils/get-config";
-import { createLink, getLinks } from "@/utils/get-links";
+import { createLink, getLinks } from "@/api/links";
+import { getConfig } from "@/utils/config";
import { getNanoid } from "@/utils/get-nanoid";
import { handleError } from "@/utils/handle-error";
import { logger } from "@/utils/logger";
@@ -16,7 +16,7 @@ const addOptionsSchema = z.object({
export const link = new Command()
.name("link")
- .description("Configure domain for your workspace")
+ .description("Create a short link")
.argument("[url]", "Destination URL")
.argument("[key]", "Short key", getNanoid())
.action(async (url, key) => {
@@ -96,7 +96,7 @@ link
const links = await getLinks();
spinner.stop();
- console.table(links, ["url", "clicks"]);
+ console.table(links, ["shortLink", "clicks"]);
} catch (error) {
handleError(error);
}
diff --git a/packages/cli/src/commands/login.ts b/packages/cli/src/commands/login.ts
index 2b78b6353b..4555980992 100644
--- a/packages/cli/src/commands/login.ts
+++ b/packages/cli/src/commands/login.ts
@@ -24,7 +24,7 @@ export const login = new Command()
credentials = await prompts(
[
{
- type: "text",
+ type: "password",
name: "key",
message: "Enter your workspace API key:",
validate: (value) => {
diff --git a/packages/cli/src/types/index.ts b/packages/cli/src/types/index.ts
index fb48466ade..d8b67c953c 100644
--- a/packages/cli/src/types/index.ts
+++ b/packages/cli/src/types/index.ts
@@ -3,7 +3,7 @@ export interface DubConfig {
domain?: string;
}
-export type GetDomain = {
+export type Domain = {
id: string;
slug: string;
verified: boolean;
@@ -35,7 +35,7 @@ type Geo<T = string> = {
[key: string]: T;
};
-export type GetLink = {
+export type Link = {
id: string;
domain: string;
key: string;
diff --git a/packages/cli/src/utils/config.ts b/packages/cli/src/utils/config.ts
new file mode 100644
index 0000000000..7b405dab0a
--- /dev/null
+++ b/packages/cli/src/utils/config.ts
@@ -0,0 +1,14 @@
+import type { DubConfig } from "@/types";
+import Configstore from "configstore";
+
+export async function getConfig(): Promise<DubConfig> {
+ const getConfig = new Configstore("dubcli");
+
+ if (!getConfig.size) {
+ throw new Error(
+ "Workspace API key not found. Run `dub login` to configure your API key.",
+ );
+ }
+
+ return await getConfig.all;
+}
diff --git a/packages/cli/src/utils/get-config.ts b/packages/cli/src/utils/get-config.ts
deleted file mode 100644
index ffdf694a07..0000000000
--- a/packages/cli/src/utils/get-config.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import type { DubConfig } from "@/types";
-import Configstore from "configstore";
-
-export async function getConfig() {
- const getConfig = new Configstore("dubcli");
-
- if (!getConfig.size) {
- throw new Error("Missing configuration. Please try to login again.");
- }
-
- const config = (await getConfig.all) as DubConfig;
-
- return config;
-}
diff --git a/packages/cli/src/utils/parser.ts b/packages/cli/src/utils/parser.ts
index 6a4930f19b..f8fe2b6289 100644
--- a/packages/cli/src/utils/parser.ts
+++ b/packages/cli/src/utils/parser.ts
@@ -12,8 +12,11 @@ export async function parseApiResponse<T>(response: AnyResponse): Promise<T> {
if ("error" in parsedData) {
throw new Error((parsedData as APIError).error.message);
}
+
return parsedData as T;
}
+
const textData = await response.text();
+
throw new Error(textData);
}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 38db97c021..a154b52915 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -928,7 +928,7 @@ packages:
engines: {node: '>=6.0.0'}
dependencies:
'@jridgewell/gen-mapping': 0.3.3
- '@jridgewell/trace-mapping': 0.3.22
+ '@jridgewell/trace-mapping': 0.3.25
/@aws-crypto/[email protected]:
resolution: {integrity: sha512-341lBBkiY1DfDNKai/wXM3aujNBkXR7tq1URPQDL9wi3AUbI80NR74uF1TXHMm7po1AcnFk8iu2S2IeU/+/A+Q==}
@@ -17511,6 +17511,11 @@ packages:
minimatch: 3.1.2
dev: false
+ resolution: {integrity: sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==}
+ engines: {node: '>=10.5.0'}
+ dev: false
+
resolution: {integrity: sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==}
engines: {node: 4.x || >=6.0.0}
@@ -21959,6 +21964,11 @@ packages:
defaults: 1.0.4
dev: false
+ resolution: {integrity: sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==}
+ engines: {node: '>= 8'}
+ dev: false
+
resolution: {integrity: sha512-nYfoOqb4EmElljyXU2qdeE76KsvoHdftQKY4DzA9Aw8DervCg2bG634pHLrJ/d6+B4mE3nWTSJv8Mo7B2mbZkw==}
dev: false