Skip to content

Commit b1dc734

Browse files
chore: refactor url logic
1 parent 4bb31f0 commit b1dc734

1 file changed

Lines changed: 59 additions & 27 deletions

File tree

src/swagger/client/api.ts

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -361,19 +361,24 @@ export class SwaggerAPI {
361361
}
362362

363363
/**
364-
* Publish a portal product and generate a published URL with environment-specific domain.
365-
* Returns `liveUrl` for live publishes and `previewUrl` for preview publishes.
364+
* Prepare publication metadata and URL for a portal product and page.
365+
* Fetches product, portal, and section details, resolves TOC item if provided,
366+
* and builds the publication URL.
366367
* @param productId - ID of the product to publish
367-
* @param preview - Whether to publish in preview mode (default: false)
368-
* @param tableOfContentsId - Optional table of contents UUID, or identifier in the format 'portal-subdomain:product-slug:section-slug:table-of-contents-slug'
369-
* @returns Complete publish response with product details and the resolved published URL
368+
* @param preview - Whether this is a preview publish
369+
* @param tableOfContentsId - Optional table of contents UUID or identifier
370+
* @returns Publication metadata including URL and narrowed entity details
370371
*/
371-
async publishPortalProduct(
372+
private async preparePublicationMetadata(
372373
productId: string,
373-
preview: boolean = false,
374-
tableOfContentsId: string | null = null,
375-
): Promise<PublishPortalProductResponse | FallbackResponse> {
376-
374+
preview: boolean,
375+
tableOfContentsId: string | null,
376+
): Promise<{
377+
publicationUrl: string;
378+
product: Pick<Product, "id" | "name" | "slug">;
379+
portal: Pick<Portal, "id" | "name" | "subdomain" | "customDomain">;
380+
tableOfContentsItem: Pick<TableOfContentsItem, "id" | "slug" | "title" | "order" | "parentId"> | null;
381+
}> {
377382
const [productDetails, sections] = await Promise.all([
378383
this.getPortalProduct(productId),
379384
this.getPortalProductSections(productId, {
@@ -404,24 +409,8 @@ export class SwaggerAPI {
404409
preview,
405410
);
406411

407-
const response = await fetch(
408-
`${this.config.portalBasePath}/products/${productId}/published-content?preview=${preview}`,
409-
{
410-
method: "PUT",
411-
headers: this.headers,
412-
},
413-
);
414-
415-
const result = await this.handleResponse<SuccessResponse>(response, {
416-
success: true,
417-
} as SuccessResponse);
418-
419412
return {
420-
...result,
421-
preview,
422-
...(preview
423-
? { previewUrl: publicationUrl }
424-
: { liveUrl: publicationUrl }),
413+
publicationUrl,
425414
product: {
426415
id: productDetails.id,
427416
name: productDetails.name,
@@ -440,6 +429,49 @@ export class SwaggerAPI {
440429
order: targetTocItem.order,
441430
parentId: targetTocItem.parentId,
442431
} : null,
432+
};
433+
}
434+
435+
/**
436+
* Publish a portal product and generate a published URL with environment-specific domain.
437+
* Returns `liveUrl` for live publishes and `previewUrl` for preview publishes.
438+
* @param productId - ID of the product to publish
439+
* @param preview - Whether to publish in preview mode (default: false)
440+
* @param tableOfContentsId - Optional table of contents UUID, or identifier in the format 'portal-subdomain:product-slug:section-slug:table-of-contents-slug'
441+
* @returns Complete publish response with product details and the resolved published URL
442+
*/
443+
async publishPortalProduct(
444+
productId: string,
445+
preview: boolean = false,
446+
tableOfContentsId: string | null = null,
447+
): Promise<PublishPortalProductResponse | FallbackResponse> {
448+
const metadata = await this.preparePublicationMetadata(
449+
productId,
450+
preview,
451+
tableOfContentsId,
452+
);
453+
454+
const response = await fetch(
455+
`${this.config.portalBasePath}/products/${productId}/published-content?preview=${preview}`,
456+
{
457+
method: "PUT",
458+
headers: this.headers,
459+
},
460+
);
461+
462+
const result = await this.handleResponse<SuccessResponse>(response, {
463+
success: true,
464+
} as SuccessResponse);
465+
466+
return {
467+
...result,
468+
preview,
469+
...(preview
470+
? { previewUrl: metadata.publicationUrl }
471+
: { liveUrl: metadata.publicationUrl }),
472+
product: metadata.product,
473+
portal: metadata.portal,
474+
tableOfContentsItem: metadata.tableOfContentsItem,
443475
} as PublishPortalProductResponse;
444476
}
445477

0 commit comments

Comments
 (0)