Skip to content

Commit

Permalink
refactor(dto): make port a number in the api contract
Browse files Browse the repository at this point in the history
  • Loading branch information
nicotsx committed Dec 27, 2024
1 parent f0e3f39 commit a73a0e4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from 'zod';

export const appFormSchema = z
.object({
port: z.coerce.number().optional(),
port: z.coerce.number().min(1024).max(65535).optional(),
exposed: z.boolean().optional(),
exposedLocal: z.boolean().optional(),
openPort: z.boolean().optional().default(true),
Expand All @@ -13,16 +13,7 @@ export const appFormSchema = z
.extend({})
.catchall(z.unknown());

export class AppFormBody extends createZodDto(
z.object({
port: z.string().optional(),
exposed: z.boolean().optional(),
exposedLocal: z.boolean().optional(),
openPort: z.boolean().optional(),
domain: z.string().optional(),
isVisibleOnGuestDashboard: z.boolean().optional(),
}),
) {}
export class AppFormBody extends createZodDto(appFormSchema) {}

export class UninstallAppBody extends createZodDto(z.object({ removeBackups: z.boolean() })) {}

Expand Down
9 changes: 7 additions & 2 deletions packages/backend/src/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2994,7 +2994,11 @@
"type": "object",
"properties": {
"port": {
"type": "string"
"type": "number",
"minimum": 1024,
"exclusiveMinimum": false,
"maximum": 65535,
"exclusiveMaximum": false
},
"exposed": {
"type": "boolean"
Expand All @@ -3003,7 +3007,8 @@
"type": "boolean"
},
"openPort": {
"type": "boolean"
"type": "boolean",
"default": true
},
"domain": {
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/api-client/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export type AppContextDto = {
};

export type AppFormBody = {
port?: string;
port?: number;
exposed?: boolean;
exposedLocal?: boolean;
openPort?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useId } from 'react';
import toast from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
import { InstallFormButtons } from '../../install-form-buttons/install-form-buttons';
import { InstallForm } from '../../install-form/install-form';
import { type FormValues, InstallForm } from '../../install-form/install-form';

interface IProps {
info: AppInfo;
Expand All @@ -34,6 +34,13 @@ export const InstallDialog: React.FC<IProps> = ({ info, isOpen, onClose }) => {
},
});

const normalizeFormValues = (values: FormValues) => {
return {
...values,
port: values.port ? Number(values.port) : undefined,
};
};

return (
<Dialog open={isOpen} onOpenChange={onClose}>
<DialogContent>
Expand All @@ -43,7 +50,7 @@ export const InstallDialog: React.FC<IProps> = ({ info, isOpen, onClose }) => {
<ScrollArea maxheight={500}>
<DialogDescription>
<InstallForm
onSubmit={(data) => installMutation.mutate({ path: { urn: info.urn }, body: data })}
onSubmit={(data) => installMutation.mutate({ path: { urn: info.urn }, body: normalizeFormValues(data) })}
formFields={info.form_fields}
info={info}
formId={formId}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export const UpdateSettingsDialog: React.FC<IProps> = ({ info, config, isOpen, o
},
});

const normalizeFormValues = (values: FormValues) => {
return {
...values,
port: values.port ? Number(values.port) : undefined,
};
};

return (
<Dialog open={isOpen} onOpenChange={onClose}>
<DialogContent>
Expand All @@ -46,7 +53,7 @@ export const UpdateSettingsDialog: React.FC<IProps> = ({ info, config, isOpen, o
<ScrollArea maxheight={500}>
<DialogDescription>
<InstallForm
onSubmit={(values: FormValues) => updateConfig.mutate({ path: { urn: info.urn }, body: values })}
onSubmit={(values: FormValues) => updateConfig.mutate({ path: { urn: info.urn }, body: normalizeFormValues(values) })}
formFields={info.form_fields}
info={info}
initialValues={{ ...config }}
Expand Down

0 comments on commit a73a0e4

Please sign in to comment.