Skip to content

Commit 177b020

Browse files
refactor: shema parse in the anticapture-client
1 parent c7fdee0 commit 177b020

5 files changed

Lines changed: 24 additions & 36 deletions

File tree

packages/anticapture-client/dist/anticapture-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class AnticaptureClient {
2424
// Handle empty or undefined responses
2525
if (!response || !response.data) {
2626
console.warn('No data received from GraphQL endpoint, returning empty response');
27-
return schema.parse(null);
27+
return schema.parse({});
2828
}
2929
if (response.data.errors) {
3030
throw new Error(`GraphQL errors: ${JSON.stringify(response.data.errors)}`);

packages/anticapture-client/dist/schemas.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from 'zod';
2-
export declare const SafeDaosResponseSchema: z.ZodCatch<z.ZodEffects<z.ZodObject<{
2+
export declare const SafeDaosResponseSchema: z.ZodEffects<z.ZodObject<{
33
daos: z.ZodNullable<z.ZodObject<{
44
items: z.ZodArray<z.ZodObject<{
55
id: z.ZodString;
@@ -41,8 +41,8 @@ export declare const SafeDaosResponseSchema: z.ZodCatch<z.ZodEffects<z.ZodObject
4141
id: string;
4242
}[];
4343
} | null;
44-
}>>;
45-
export declare const SafeProposalsResponseSchema: z.ZodCatch<z.ZodEffects<z.ZodObject<{
44+
}>;
45+
export declare const SafeProposalsResponseSchema: z.ZodEffects<z.ZodObject<{
4646
proposalsOnchains: z.ZodNullable<z.ZodObject<{
4747
items: z.ZodArray<z.ZodAny, "many">;
4848
}, "strip", z.ZodTypeAny, {
@@ -66,14 +66,14 @@ export declare const SafeProposalsResponseSchema: z.ZodCatch<z.ZodEffects<z.ZodO
6666
proposalsOnchains: {
6767
items: any[];
6868
} | null;
69-
}>>;
70-
export declare const SafeProposalByIdResponseSchema: z.ZodCatch<z.ZodObject<{
69+
}>;
70+
export declare const SafeProposalByIdResponseSchema: z.ZodObject<{
7171
proposalsOnchain: z.ZodNullable<z.ZodAny>;
7272
}, "strip", z.ZodTypeAny, {
7373
proposalsOnchain?: any;
7474
}, {
7575
proposalsOnchain?: any;
76-
}>>;
76+
}>;
7777
declare const VotingPowerHistoryItemSchema: z.ZodObject<{
7878
accountId: z.ZodString;
7979
timestamp: z.ZodString;
@@ -137,7 +137,7 @@ declare const VotingPowerHistoryItemSchema: z.ZodObject<{
137137
} | null | undefined;
138138
daoId?: string | null | undefined;
139139
}>;
140-
export declare const SafeVotingPowerHistoryResponseSchema: z.ZodCatch<z.ZodEffects<z.ZodObject<{
140+
export declare const SafeVotingPowerHistoryResponseSchema: z.ZodEffects<z.ZodObject<{
141141
votingPowerHistorys: z.ZodNullable<z.ZodObject<{
142142
items: z.ZodArray<z.ZodObject<{
143143
accountId: z.ZodString;
@@ -319,7 +319,7 @@ export declare const SafeVotingPowerHistoryResponseSchema: z.ZodCatch<z.ZodEffec
319319
daoId?: string | null | undefined;
320320
}[];
321321
} | null;
322-
}>>;
322+
}>;
323323
type SafeProposalsResponse = z.infer<typeof SafeProposalsResponseSchema>;
324324
type SafeVotingPowerHistoryResponse = z.infer<typeof SafeVotingPowerHistoryResponseSchema>;
325325
export type ProcessedVotingPowerHistory = z.infer<typeof VotingPowerHistoryItemSchema> & {

packages/anticapture-client/dist/schemas.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ exports.SafeDaosResponseSchema = zod_1.z.object({
1515
return { daos: { items: [] } };
1616
}
1717
return { daos: { items: data.daos.items } };
18-
}).catch(() => {
19-
console.warn('DaosResponse validation failed completely');
20-
return { daos: { items: [] } };
2118
});
19+
// .catch(() => {
20+
// console.warn('DaosResponse validation failed completely');
21+
// return { daos: { items: [] } };
22+
// });
2223
exports.SafeProposalsResponseSchema = zod_1.z.object({
2324
proposalsOnchains: zod_1.z.object({
2425
items: zod_1.z.array(zod_1.z.any())
@@ -29,16 +30,18 @@ exports.SafeProposalsResponseSchema = zod_1.z.object({
2930
return { proposalsOnchains: { items: [] } };
3031
}
3132
return { proposalsOnchains: { items: data.proposalsOnchains.items } };
32-
}).catch(() => {
33-
console.warn('ProposalsResponse validation failed completely');
34-
return { proposalsOnchains: { items: [] } };
3533
});
34+
// .catch(() => {
35+
// console.warn('ProposalsResponse validation failed completely');
36+
// return { proposalsOnchains: { items: [] } };
37+
// });
3638
exports.SafeProposalByIdResponseSchema = zod_1.z.object({
3739
proposalsOnchain: zod_1.z.any().nullable()
38-
}).catch(() => {
39-
console.warn('ProposalByIdResponse validation failed completely');
40-
return { proposalsOnchain: null };
4140
});
41+
// .catch(() => {
42+
// console.warn('ProposalByIdResponse validation failed completely');
43+
// return { proposalsOnchain: null };
44+
// });
4245
// Define schema for voting power history item (based on actual API response)
4346
// Handle real-world scenarios where API might return null values or missing fields
4447
const VotingPowerHistoryItemSchema = zod_1.z.object({
@@ -67,9 +70,6 @@ exports.SafeVotingPowerHistoryResponseSchema = zod_1.z.object({
6770
return {
6871
votingPowerHistorys: data.votingPowerHistorys || { items: [] }
6972
};
70-
}).catch(() => {
71-
console.warn('VotingPowerHistoryResponse validation failed, returning empty data');
72-
return { votingPowerHistorys: { items: [] } };
7373
});
7474
// Internal helper function to process validated proposals
7575
function processProposals(validated, daoId) {

packages/anticapture-client/src/anticapture-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class AnticaptureClient {
4545
// Handle empty or undefined responses
4646
if (!response || !response.data) {
4747
console.warn('No data received from GraphQL endpoint, returning empty response');
48-
return schema.parse(null);
48+
return schema.parse({});
4949
}
5050

5151
if (response.data.errors) {

packages/anticapture-client/src/schemas.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,29 @@ export const SafeDaosResponseSchema = z.object({
55
daos: z.object({
66
items: z.array(z.object({ id: z.string() }))
77
}).nullable()
8-
}).transform((data, ctx) => {
8+
}).transform((data) => {
99
if (!data.daos || !data.daos.items) {
1010
console.warn('DaosResponse has null daos or items:', data);
1111
return { daos: { items: [] } };
1212
}
1313
return { daos: { items: data.daos.items } };
14-
}).catch(() => {
15-
console.warn('DaosResponse validation failed completely');
16-
return { daos: { items: [] } };
1714
});
1815

1916

2017
export const SafeProposalsResponseSchema = z.object({
2118
proposalsOnchains: z.object({
2219
items: z.array(z.any())
2320
}).nullable()
24-
}).transform((data, ctx) => {
21+
}).transform((data) => {
2522
if (!data.proposalsOnchains || !data.proposalsOnchains.items) {
2623
console.warn('ProposalsResponse has null proposalsOnchains or items:', data);
2724
return { proposalsOnchains: { items: [] } };
2825
}
2926
return { proposalsOnchains: { items: data.proposalsOnchains.items } };
30-
}).catch(() => {
31-
console.warn('ProposalsResponse validation failed completely');
32-
return { proposalsOnchains: { items: [] } };
3327
});
3428

3529
export const SafeProposalByIdResponseSchema = z.object({
3630
proposalsOnchain: z.any().nullable()
37-
}).catch(() => {
38-
console.warn('ProposalByIdResponse validation failed completely');
39-
return { proposalsOnchain: null };
4031
});
4132

4233
// Define schema for voting power history item (based on actual API response)
@@ -68,9 +59,6 @@ export const SafeVotingPowerHistoryResponseSchema = z.object({
6859
return {
6960
votingPowerHistorys: data.votingPowerHistorys || { items: [] }
7061
};
71-
}).catch(() => {
72-
console.warn('VotingPowerHistoryResponse validation failed, returning empty data');
73-
return { votingPowerHistorys: { items: [] } };
7462
});
7563

7664

0 commit comments

Comments
 (0)