Skip to content

Commit

Permalink
using base url param
Browse files Browse the repository at this point in the history
  • Loading branch information
aumkar committed Sep 25, 2023
1 parent a447653 commit 56431ad
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
32 changes: 20 additions & 12 deletions packages/cli/src/cli/commands/documents.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { exit } from "process";
import { validateComponentSchema } from "@pantheon-systems/pcc-sdk-core";
import axios from "axios";
import chalk from "chalk";
import dayjs from "dayjs";
import ora from "ora";
import AddOnApiHelper from "../../lib/addonApiHelper";
import { printTable } from "../../lib/cliDisplay";
import config from "../../lib/config";
import { filterUndefinedProperties, parameterize } from "../../lib/utils";
import { errorHandler } from "../exceptions";
Expand All @@ -14,21 +8,32 @@ type GeneratePreviewParam = {
documentId: string;
baseUrl: string;
};
function generateBaseAPIPath(siteData: Site) {
function generateBaseAPIPath(siteData: Site, baseUrl?: string) {
if (!siteData) return "#";

const isPlayground = siteData.__isPlayground;

let _baseUrl: string;
if (baseUrl) _baseUrl = baseUrl;
else if (isPlayground) _baseUrl = config.playgroundUrl;
else _baseUrl = siteData.url;

return isPlayground
? `${config.playgroundUrl}/api/${siteData.id}/pantheoncloud`
: `${siteData.url}/api/pantheoncloud`;
? `${_baseUrl}/api/${siteData.id}/pantheoncloud`
: `${_baseUrl}/api/pantheoncloud`;
}

async function generateDocumentPath(
site: Site,
docId: string,
isPreview: boolean,
queryParams?: Record<string, string>,
{
baseUrl,
queryParams,
}: {
baseUrl?: string;
queryParams?: Record<string, string>;
},
) {
const augmentedQueryParams = { ...queryParams };

Expand All @@ -41,7 +46,7 @@ async function generateDocumentPath(
? {}
: filterUndefinedProperties(augmentedQueryParams);

return `${generateBaseAPIPath(site)}/document/${docId}${
return `${generateBaseAPIPath(site, baseUrl)}/document/${docId}${
Object.values(params).length > 0 ? `/?${parameterize(params)}` : ""
}`;
}
Expand Down Expand Up @@ -69,7 +74,10 @@ export const generatePreviewLink = errorHandler<GeneratePreviewParam>(
}

const buildLink = `${await generateDocumentPath(site, documentId, true, {
publishingLevel: "REALTIME",
queryParams: {
publishingLevel: "REALTIME",
},
baseUrl,
})}`;

console.log("build link: ", buildLink);
Expand Down
25 changes: 13 additions & 12 deletions packages/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ yargs(hideBin(process.argv))
"Revokes token for a given id.",
(yargs) => {
yargs.positional("<id>", {
describe: "ID of the token which you want to revoke",
describe: "ID of the token which you want to revoke.",
demandOption: true,
type: "string",
});
Expand Down Expand Up @@ -178,13 +178,13 @@ yargs(hideBin(process.argv))
"Shows component schema of the site.",
(yargs) => {
yargs.option("url", {
describe: "Site url",
describe: "Site url.",
type: "string",
demandOption: true,
});

yargs.option("apiPath", {
describe: "API path such as /api/pantheoncloud/component_schema",
describe: "API path such as /api/pantheoncloud/component_schema.",
type: "string",
demandOption: false,
});
Expand All @@ -205,12 +205,12 @@ yargs(hideBin(process.argv))
)
.command(
"configure <id> [options]",
"Configure properties for a given site",
"Configure properties for a given site.",
(yargs) => {
yargs
.strictCommands()
.positional("<id>", {
describe: "ID of the site which you want to configure",
describe: "ID of the site which you want to configure.",
demandOption: true,
type: "string",
})
Expand Down Expand Up @@ -246,24 +246,25 @@ yargs(hideBin(process.argv))
)
.command(
"webhooks <cmd> [options]",
"Manage webhooks for a given site",
"Manage webhooks for a given site.",
(yargs) => {
yargs
.strictCommands()
.demandCommand()
.command(
"history <id>",
"View webhook event delivery logs for a given site",
"View webhook event delivery logs for a given site.",
(yargs) => {
yargs
.strictCommands()
.positional("<id>", {
describe: "ID of the site for which you want to see logs",
describe:
"ID of the site for which you want to see logs.",
demandOption: true,
type: "string",
})
.option("limit", {
describe: "Number of logs to fetch at a time",
describe: "Number of logs to fetch at a time.",
type: "number",
default: 100,
demandOption: false,
Expand All @@ -284,7 +285,7 @@ yargs(hideBin(process.argv))
)
.command(
"document <cmd> [options]",
"Enables you to manage documents for a PCC Project",
"Enables you to manage documents for a PCC Project.",
(yargs) => {
yargs
.strictCommands()
Expand All @@ -296,12 +297,12 @@ yargs(hideBin(process.argv))
yargs
.strictCommands()
.positional("<id>", {
describe: "ID of the document",
describe: "ID of the document.",
demandOption: true,
type: "string",
})
.option("baseUrl", {
describe: "Base URL for the generated preview link",
describe: "Base URL for the generated preview link.",
type: "string",
demandOption: false,
});
Expand Down

0 comments on commit 56431ad

Please sign in to comment.