Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better infer zod response type #38

Merged
merged 6 commits into from
Aug 6, 2024
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
5 changes: 5 additions & 0 deletions src/types/icrc-responses.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ describe('icrc-responses', () => {
...validResponse,
result: {
supportedStandards: [
// @ts-expect-error: we are testing this on purpose
{
name: 'ICRC-25'
}
Expand Down Expand Up @@ -242,6 +243,7 @@ describe('icrc-responses', () => {
...validResponse,
result: {
supportedStandards: [
// @ts-expect-error: we are testing this on purpose
{
url: 'https://github.com/dfinity/ICRC/blob/main/ICRCs/ICRC-25/ICRC-25.md'
}
Expand All @@ -266,6 +268,7 @@ describe('icrc-responses', () => {

const response: IcrcSupportedStandardsResponseType = {
...rest,
// @ts-expect-error: we are testing this on purpose
result: {}
};

Expand Down Expand Up @@ -311,6 +314,7 @@ describe('icrc-responses', () => {
it('should throw if response has no valid result string', () => {
const invalidResponse: IcrcReadyResponseType = {
...validResponse,
// @ts-expect-error: we are testing this on purpose
result: 'test'
};
expect(() => IcrcReadyResponse.parse(invalidResponse)).toThrow();
Expand All @@ -319,6 +323,7 @@ describe('icrc-responses', () => {
it('should throw if response has no valid result type', () => {
const invalidResponse: IcrcReadyResponseType = {
...validResponse,
// @ts-expect-error: we are testing this on purpose
result: {
hello: 'world'
}
Expand Down
42 changes: 34 additions & 8 deletions src/types/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,50 @@ const RpcResponse = Rpc.extend({

export type RpcResponseType = z.infer<typeof RpcResponse>;

const RpcResponseContent = z
.object({
result: z.any(),
error: RpcResponseError
})
.partial();
const RpcResponseContent = <T extends z.ZodTypeAny>(
result: T
): z.ZodObject<{
result: z.ZodOptional<T>;
error: z.ZodOptional<typeof RpcResponseError>;
}> =>
z
.object({
result,
error: RpcResponseError
})
.partial();

type RpcResponseContentType = z.infer<typeof RpcResponseContent>;
// TODO: Maybe we can use this type in inferRpcResponse?
type _RpcResponseContentType<T extends z.ZodTypeAny> = z.infer<
ReturnType<typeof RpcResponseContent<T>>
>;

const RpcResponseWithError = RpcResponse.extend({
error: RpcResponseError
});

export type RpcResponseWithErrorType = z.infer<typeof RpcResponseWithError>;

// TODO: Simplify the return type to avoid redundancy with other types. Consider using a more concise or existing type definition.
export const inferRpcResponse = <T extends z.ZodTypeAny>(
result: T
): z.ZodType<RpcResponseType & RpcResponseContentType> =>
): z.ZodEffects<
z.ZodObject<
{
jsonrpc: z.ZodLiteral<'2.0'>;
id: z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodNull]>;
result: z.ZodOptional<T>;
error: z.ZodOptional<
z.ZodObject<{
code: z.ZodUnion<[z.ZodNumber, z.ZodNativeEnum<typeof RpcErrorCode>]>;
message: z.ZodString;
data: z.ZodOptional<z.ZodNever>;
}>
>;
},
'strict'
>
> =>
RpcResponseWithError.omit({error: true})
.merge(
z
Expand Down