Skip to content

Commit 6646754

Browse files
committed
refactor: enhance type safety for upstream utility function parameters and add non-null assertions in E2E tests.
1 parent 8d73919 commit 6646754

File tree

5 files changed

+39
-26
lines changed

5 files changed

+39
-26
lines changed

e2e/tests/protos.crud-all-fields.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ test.describe('CRUD proto with all fields', () => {
7777
expect(createdProto).toBeDefined();
7878
expect(createdProto?.value.id).toBeDefined();
7979

80-
createdProtoId = createdProto?.value.id || '';
80+
createdProtoId = createdProto!.value.id!;
8181

8282
// Verify content matches
8383
expect(createdProto?.value.content).toBe(protoContent);

e2e/tests/protos.crud-required-fields.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ test.describe('CRUD proto with required fields only', () => {
7676
expect(createdProto).toBeDefined();
7777
expect(createdProto?.value.id).toBeDefined();
7878

79-
createdProtoId = createdProto?.value.id || '';
79+
createdProtoId = createdProto!.value.id!;
8080

8181
// Verify content matches
8282
expect(createdProto?.value.content).toBe(protoContent);

src/components/form-slice/FormPartUpstream/util.ts

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,42 @@ const isAllUndefined = (obj: Record<string, unknown>) =>
3737
(v) => v === undefined || v === null || v === '' || Number.isNaN(v)
3838
);
3939

40-
export const produceRmEmptyUpstreamFields = produce((draft: Record<string, any>) => {
41-
if (draft.timeout && isAllUndefined(draft.timeout)) {
42-
delete draft.timeout;
43-
}
44-
if (draft.keepalive_pool && isAllUndefined(draft.keepalive_pool)) {
45-
delete draft.keepalive_pool;
46-
}
47-
if (draft.tls && isAllUndefined(draft.tls)) {
48-
delete draft.tls;
49-
}
50-
51-
if (draft.upstream) {
52-
if (draft.upstream.timeout && isAllUndefined(draft.upstream.timeout)) {
53-
delete draft.upstream.timeout;
40+
export const produceRmEmptyUpstreamFields = produce(
41+
(
42+
draft: {
43+
timeout?: Record<string, unknown>;
44+
keepalive_pool?: Record<string, unknown>;
45+
tls?: Record<string, unknown>;
46+
upstream?: {
47+
timeout?: Record<string, unknown>;
48+
keepalive_pool?: Record<string, unknown>;
49+
tls?: Record<string, unknown>;
50+
};
51+
} & Record<string, unknown>
52+
) => {
53+
if (draft.timeout && isAllUndefined(draft.timeout)) {
54+
delete draft.timeout;
55+
}
56+
if (draft.keepalive_pool && isAllUndefined(draft.keepalive_pool)) {
57+
delete draft.keepalive_pool;
5458
}
55-
if (
56-
draft.upstream.keepalive_pool &&
57-
isAllUndefined(draft.upstream.keepalive_pool)
58-
) {
59-
delete draft.upstream.keepalive_pool;
59+
if (draft.tls && isAllUndefined(draft.tls)) {
60+
delete draft.tls;
6061
}
61-
if (draft.upstream.tls && isAllUndefined(draft.upstream.tls)) {
62-
delete draft.upstream.tls;
62+
63+
if (draft.upstream) {
64+
if (draft.upstream.timeout && isAllUndefined(draft.upstream.timeout)) {
65+
delete draft.upstream.timeout;
66+
}
67+
if (
68+
draft.upstream.keepalive_pool &&
69+
isAllUndefined(draft.upstream.keepalive_pool)
70+
) {
71+
delete draft.upstream.keepalive_pool;
72+
}
73+
if (draft.upstream.tls && isAllUndefined(draft.upstream.tls)) {
74+
delete draft.upstream.tls;
75+
}
6376
}
6477
}
65-
});
78+
);

src/routes/services/add.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import { postServiceReq, type ServicePostType } from '@/apis/services';
2525
import { FormSubmitBtn } from '@/components/form/Btn';
2626
import { FormPartService } from '@/components/form-slice/FormPartService';
2727
import { ServicePostSchema } from '@/components/form-slice/FormPartService/schema';
28+
import { produceRmEmptyUpstreamFields } from '@/components/form-slice/FormPartUpstream/util';
2829
import { FormTOCBox } from '@/components/form-slice/FormSection';
2930
import PageHeader from '@/components/page/PageHeader';
3031
import { req } from '@/config/req';
31-
import { produceRmEmptyUpstreamFields } from '@/components/form-slice/FormPartUpstream/util';
3232
import { produceRmUpstreamWhenHas } from '@/utils/form-producer';
3333
import { pipeProduce } from '@/utils/producer';
3434

src/routes/services/detail.$id/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { getServiceQueryOptions } from '@/apis/hooks';
3232
import { putServiceReq } from '@/apis/services';
3333
import { FormSubmitBtn } from '@/components/form/Btn';
3434
import { FormPartService } from '@/components/form-slice/FormPartService';
35+
import { produceRmEmptyUpstreamFields } from '@/components/form-slice/FormPartUpstream/util';
3536
import { FormTOCBox } from '@/components/form-slice/FormSection';
3637
import { FormSectionGeneral } from '@/components/form-slice/FormSectionGeneral';
3738
import { DeleteResourceBtn } from '@/components/page/DeleteResourceBtn';
@@ -41,7 +42,6 @@ import { req } from '@/config/req';
4142
import { APISIX, type APISIXType } from '@/types/schema/apisix';
4243
import { produceRmUpstreamWhenHas } from '@/utils/form-producer';
4344
import { pipeProduce } from '@/utils/producer';
44-
import { produceRmEmptyUpstreamFields } from '@/components/form-slice/FormPartUpstream/util';
4545

4646
type Props = {
4747
readOnly: boolean;

0 commit comments

Comments
 (0)