Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/headless/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ export function toUpdateConfigContent(

/**
* Helper to convert InstallationConfigContent to ConfigContent
* Throws error if required fields are missing
* Returns result object with either data or error
*/
export function toCreateConfigContent(
config: InstallationConfigContent,
): ConfigContent {
export function toCreateConfigContent(config: InstallationConfigContent): {
data?: ConfigContent;
error?: Error;
} {
if (!isValidCreateConfig(config)) {
throw new Error("Config must have a provider field for creation");
return {
error: new Error("Config must have a provider field for creation"),
};
}
return config;
return { data: config };
}

export type { InstallationConfigContent };
17 changes: 15 additions & 2 deletions src/headless/installation/useCreateInstallation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export function useCreateInstallation() {
onSettled?: () => void;
}) => {
if (installation) {
const error = new Error("Installation already created. Try updating instead.");
const error = new Error(
"Installation already created. Try updating instead.",
);
onError?.(error);
onSettled?.();
return;
Expand All @@ -61,6 +63,17 @@ export function useCreateInstallation() {
onSettled?.();
return;
}

// Validate config before creating installation
const configResult = toCreateConfigContent(config);
if (configResult.error || !configResult.data) {
onError?.(
configResult.error || new Error("Invalid configuration data"),
);
onSettled?.();
return;
}

// assemble create installation requests from providers
const createInstallationRequest: CreateInstallationOperationRequest = {
projectIdOrName,
Expand All @@ -69,7 +82,7 @@ export function useCreateInstallation() {
groupRef,
connectionId: connection?.id,
config: {
content: toCreateConfigContent(config),
content: configResult.data,
},
},
};
Expand Down
8 changes: 6 additions & 2 deletions src/hooks/mutation/useUpdateOauthConnectMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ export const useUpdateOauthConnectMutation = () => {
return useMutation({
mutationKey: ["updateOauthConnection"],
mutationFn: async (request: OauthUpdateRequest) => {
const api = await getAPI();
// Validate required fields before making API call
if (!request.projectIdOrName || !request.connectionId) {
throw new Error("Project ID and connection ID are required");
// Return rejected promise instead of throw to be more explicit
return Promise.reject(
new Error("Project ID and connection ID are required"),
);
}
const api = await getAPI();
Comment thread
dionlow marked this conversation as resolved.
return api.oAuthApi.oauthUpdate(request);
},
onSuccess: () => {
Expand Down