feature.ts•2.83 kB
import { z } from 'zod';
import * as azureDevOpsClient from '../../../clients/azure-devops';
import { handleRequestError } from '../../../shared/errors/handle-request-error';
import { CreateWikiPageSchema } from './schema';
import { defaultOrg, defaultProject } from '../../../utils/environment';
/**
* Creates a new wiki page in Azure DevOps.
* If a page already exists at the specified path, it will be updated.
*
* @param {z.infer<typeof CreateWikiPageSchema>} params - The parameters for creating the wiki page.
* @returns {Promise<any>} A promise that resolves with the API response.
*/
export const createWikiPage = async (
params: z.infer<typeof CreateWikiPageSchema>,
client?: {
defaults?: { organizationId?: string; projectId?: string };
put: (
url: string,
data: Record<string, unknown>,
) => Promise<{ data: unknown }>;
}, // For testing purposes only
) => {
try {
const { organizationId, projectId, wikiId, pagePath, content, comment } =
params;
// For testing mode, use the client's defaults
if (client && client.defaults) {
const org = organizationId ?? client.defaults.organizationId;
const project = projectId ?? client.defaults.projectId;
if (!org) {
throw new Error(
'Organization ID is not defined. Please provide it or set a default.',
);
}
// This branch is for testing only
const apiUrl = `${org}/${
project ? `${project}/` : ''
}_apis/wiki/wikis/${wikiId}/pages?path=${encodeURIComponent(
pagePath ?? '/',
)}&api-version=7.1-preview.1`;
// Prepare the request body
const requestBody: Record<string, unknown> = { content };
if (comment) {
requestBody.comment = comment;
}
// Make the API request
const response = await client.put(apiUrl, requestBody);
return response.data;
} else {
// Use default organization and project if not provided
const org = organizationId ?? defaultOrg;
const project = projectId ?? defaultProject;
if (!org) {
throw new Error(
'Organization ID is not defined. Please provide it or set a default.',
);
}
// Create the client
const wikiClient = await azureDevOpsClient.getWikiClient({
organizationId: org,
});
// Prepare the wiki page content
const wikiPageContent = {
content,
};
// This is the real implementation
return await wikiClient.updatePage(
wikiPageContent,
project,
wikiId,
pagePath ?? '/',
{
comment: comment ?? undefined,
},
);
}
} catch (error: unknown) {
throw await handleRequestError(
error,
'Failed to create or update wiki page',
);
}
};