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.

update cli domain access

Suraj7085a440bd3f
diff --git a/packages/cli/src/commands/domain.ts b/packages/cli/src/commands/domain.ts
deleted file mode 100644
index 3c867489ee..0000000000
--- a/packages/cli/src/commands/domain.ts
+++ /dev/null
@@ -1,328 +0,0 @@
-import {
- createDomain,
- deleteDomain,
- getDomains,
- updateDomain,
-} from "@/utils/get-domains";
-import { handleError } from "@/utils/handle-error";
-import { logger } from "@/utils/logger";
-import chalk from "chalk";
-import { Command } from "commander";
-import Configstore from "configstore";
-import ora from "ora";
-import prompts from "prompts";
-import { z } from "zod";
-
-const domainOptionsSchema = z.object({
- slug: z.string().min(3, {
- message: "Please provide a valid slug",
- }),
-});
-
-export const domain = new Command()
- .name("domain")
- .description("Configure your workspace domain")
- .action(async () => {
- const spinner = ora("Fetching domains").start();
-
- try {
- const showDomains = async () => {
- const domains = await getDomains();
- if (domains) spinner.stop();
-
- return [
- ...domains.map((domain) => ({
- title: domain.slug,
- value: domain.slug,
- })),
- {
- title: "amzn.id",
- value: "amzn.id",
- },
- {
- title: "chatg.pt",
- value: "chatg.pt",
- },
- {
- title: "dub.sh",
- value: "dub.sh",
- },
- {
- title: "fig.page",
- value: "fig.page",
- },
- {
- title: "ggl.link",
- value: "ggl.link",
- },
- {
- title: "git.new",
- value: "git.new",
- },
- {
- title: "spti.fi",
- value: "spti.fi",
- },
- ];
- };
-
- const options = await prompts(
- [
- {
- type: "select",
- name: "domain",
- message: "Select a domain",
- choices: await showDomains(),
- validate: (value) => {
- const result = domainOptionsSchema.shape.slug.safeParse(value);
- return result.success || result.error.errors[0].message;
- },
- },
- ],
- {
- onCancel: () => {
- logger.info("");
- logger.warn("You cancelled the prompt.");
- logger.info("");
- process.exit(0);
- },
- },
- );
-
- const getconfig = new Configstore("dubcli");
- getconfig.set("domain", options.domain);
-
- spinner.succeed("Done");
- logger.info("");
- logger.info(`${chalk.green("Success!")} Configuration updated.`);
- logger.info("");
- } catch (error) {
- spinner.stop();
- handleError(error);
- }
- });
-
-domain
- .command("create")
- .description("Create a new domain")
- .argument("[slug]", "Name of the domain")
- .action(async (slug) => {
- try {
- let newDomain = { slug };
-
- if (!newDomain.slug) {
- newDomain = await prompts(
- [
- {
- type: "text",
- name: "slug",
- message: "Enter new name of the domain:",
- validate: (value) => {
- const result = domainOptionsSchema.shape.slug.safeParse(value);
- return result.success || result.error.errors[0].message;
- },
- },
- ],
- {
- onCancel: () => {
- logger.info("");
- logger.warn("You cancelled the prompt.");
- logger.info("");
- process.exit(0);
- },
- },
- );
- }
-
- const validatedData = domainOptionsSchema.parse(newDomain);
- const spinner = ora("Creating new domain").start();
-
- try {
- await createDomain(validatedData.slug);
- spinner.succeed("Domain created successfully");
-
- logger.info("");
- logger.info(
- `${chalk.green("Success!")} New Domain '${validatedData.slug}' has been created.`,
- );
- logger.info("");
- } catch (error) {
- spinner.fail("Failed to create new domain");
- spinner.stop();
- logger.info("");
- throw error;
- }
- } catch (error) {
- handleError(error);
- }
- });
-
-domain
- .command("update")
- .description("Update an existing domain")
- .argument("[slug]", "Name of the domain")
- .action(async (slug) => {
- const fetchingSpinner = ora("Fetching domains").start();
-
- try {
- const domains = await getDomains();
- if (domains) fetchingSpinner.stop();
-
- const { domain: oldDomain } = await prompts(
- [
- {
- type: "select",
- name: "domain",
- message: "Select a domain to update:",
- choices: domains.map((d) => ({ title: d.slug, value: d.slug })),
- validate: (value) => {
- const result = domainOptionsSchema.shape.slug.safeParse(value);
- return result.success || result.error.errors[0].message;
- },
- },
- ],
- {
- onCancel: () => {
- logger.info("");
- logger.warn("You cancelled the prompt.");
- logger.info("");
- process.exit(0);
- },
- },
- );
-
- let newDomain = { slug };
-
- if (!newDomain.slug) {
- newDomain = await prompts(
- [
- {
- type: "text",
- name: "slug",
- message: "Enter new name of the domain:",
- },
- ],
- {
- onCancel: () => {
- logger.info("");
- logger.warn("You cancelled the prompt.");
- logger.info("");
- process.exit(0);
- },
- },
- );
- }
-
- const validatedData = domainOptionsSchema.parse(newDomain);
- const spinner = ora("Updating domain").start();
-
- try {
- await updateDomain({
- newSlug: validatedData.slug,
- oldSlug: oldDomain,
- });
-
- spinner.succeed("Domain updated successfully");
-
- logger.info("");
- logger.info(
- `${chalk.green(
- "Success!",
- )} Domain '${oldDomain}' has been updated to '${validatedData.slug}'.`,
- );
- logger.info("");
- } catch (error) {
- spinner.fail("Failed to update domain");
- spinner.stop();
- logger.info("");
- throw error;
- }
- } catch (error) {
- handleError(error);
- }
- });
-
-domain
- .command("delete")
- .description("delete a domain")
- .argument("[slug]", "name of the domain")
- .action(async (slug) => {
- try {
- let selectDomain = { slug };
-
- if (!selectDomain.slug) {
- const fetchingSpinner = ora("Fetching domains").start();
-
- const domains = await getDomains();
- if (domains) fetchingSpinner.stop();
-
- selectDomain = await prompts(
- [
- {
- type: "select",
- name: "slug",
- message: "Select a domain to delete:",
- choices: domains.map((d) => ({ title: d.slug, value: d.slug })),
- validate: (value) => {
- const result = domainOptionsSchema.shape.slug.safeParse(value);
- return result.success || result.error.errors[0].message;
- },
- },
- ],
- {
- onCancel: () => {
- logger.info("");
- logger.warn("You cancelled the prompt.");
- logger.info("");
- process.exit(0);
- },
- },
- );
- }
-
- const { confirm } = await prompts(
- [
- {
- type: "confirm",
- name: "confirm",
- message: `Are you sure you want to delete the domain '${selectDomain.slug}'?`,
- initial: false,
- },
- ],
- {
- onCancel: () => {
- logger.info("");
- logger.warn("You cancelled the prompt.");
- logger.info("");
- process.exit(0);
- },
- },
- );
-
- if (!confirm) {
- logger.info("Deletion cancelled.");
- }
-
- const validatedData = domainOptionsSchema.parse(selectDomain);
- const spinner = ora("Deleting domain").start();
-
- try {
- await deleteDomain(validatedData.slug);
-
- spinner.succeed("Domain deleted successfully");
-
- logger.info("");
- logger.info(
- `${chalk.green("Success!")} Domain '${validatedData.slug}' has been deleted.`,
- );
- logger.info("");
- } catch (error) {
- spinner.fail("Failed to update domain");
- spinner.stop();
- logger.info("");
- throw error;
- }
- } catch (error) {
- handleError(error);
- }
- });
diff --git a/packages/cli/src/commands/domains.ts b/packages/cli/src/commands/domains.ts
new file mode 100644
index 0000000000..8266f15c55
--- /dev/null
+++ b/packages/cli/src/commands/domains.ts
@@ -0,0 +1,70 @@
+import { getDomains } from "@/utils/get-domains";
+import { handleError } from "@/utils/handle-error";
+import { logger } from "@/utils/logger";
+import chalk from "chalk";
+import { Command } from "commander";
+import Configstore from "configstore";
+import ora from "ora";
+import prompts from "prompts";
+import { z } from "zod";
+
+const domainOptionsSchema = z.object({
+ slug: z.string().min(3, {
+ message: "Please provide a valid slug",
+ }),
+});
+
+export const domains = new Command()
+ .name("domains")
+ .description("Configure your workspace domain")
+ .action(async () => {
+ const spinner = ora("Fetching domains").start();
+
+ try {
+ const showDomains = async () => {
+ const slugs = await getDomains();
+ if (slugs) spinner.stop();
+
+ return [
+ ...slugs.map((slug) => ({
+ title: slug,
+ value: slug,
+ })),
+ ];
+ };
+
+ const options = await prompts(
+ [
+ {
+ type: "select",
+ name: "domain",
+ message: "Select a domain",
+ choices: await showDomains(),
+ validate: (value) => {
+ const result = domainOptionsSchema.shape.slug.safeParse(value);
+ return result.success || result.error.errors[0].message;
+ },
+ },
+ ],
+ {
+ onCancel: () => {
+ logger.info("");
+ logger.warn("You cancelled the prompt.");
+ logger.info("");
+ process.exit(0);
+ },
+ },
+ );
+
+ const getconfig = new Configstore("dubcli");
+ getconfig.set("domain", options.domain);
+
+ spinner.succeed("Done");
+ logger.info("");
+ logger.info(`${chalk.green("Success!")} Configuration updated.`);
+ logger.info("");
+ } catch (error) {
+ spinner.stop();
+ handleError(error);
+ }
+ });
diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts
index 809c3ad35f..8cd0435b24 100644
--- a/packages/cli/src/index.ts
+++ b/packages/cli/src/index.ts
@@ -1,7 +1,7 @@
#!/usr/bin/env node
import { config } from "@/commands/config";
-import { domain } from "@/commands/domain";
+import { domains } from "@/commands/domains";
import { link } from "@/commands/link";
import { login } from "@/commands/login";
import { getPackageInfo } from "@/utils/get-package-info";
@@ -25,7 +25,7 @@ async function main() {
program
.addCommand(login)
.addCommand(config)
- .addCommand(domain)
+ .addCommand(domains)
.addCommand(link);
program.parse();
diff --git a/packages/cli/src/utils/get-domains.ts b/packages/cli/src/utils/get-domains.ts
index 9447744928..3cfe4894c2 100644
--- a/packages/cli/src/utils/get-domains.ts
+++ b/packages/cli/src/utils/get-domains.ts
@@ -14,8 +14,19 @@ export async function getDomains() {
},
};
- const response = await fetch("https://api.dub.co/domains", options);
- return await parseApiResponse<GetDomain[]>(response);
+ const [domainsResponse, defaultDomainsResponse] = await Promise.all([
+ fetch("https://api.dub.co/domains", options),
+ fetch("https://api.dub.co/domains/default", options),
+ ]);
+
+ const [domains, defaultDomains] = await Promise.all([
+ parseApiResponse<GetDomain[]>(domainsResponse),
+ parseApiResponse<string[]>(defaultDomainsResponse),
+ ]);
+
+ const allSlugs = [...domains.map((domain) => domain.slug), ...defaultDomains];
+
+ return Array.from(new Set(allSlugs));
}
export async function createDomain(slug: string) {