-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathparams.schema.ts
More file actions
151 lines (136 loc) · 4.13 KB
/
params.schema.ts
File metadata and controls
151 lines (136 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { z } from "@hono/zod-openapi";
import {
DEFAULT_EVM_CHAIN_ID,
isNormalizedName,
isSelectionEmpty,
type Name,
type ResolverRecordsSelection,
} from "@ensnode/ensnode-sdk";
import {
makeCoinTypeStringSchema,
makeDefaultableChainIdStringSchema,
makeLowercaseAddressSchema,
} from "@ensnode/ensnode-sdk/internal";
const excludingDefaultChainId = z
.number()
.refine(
(val) => val !== DEFAULT_EVM_CHAIN_ID,
`Must not be the 'default' EVM chain id (${DEFAULT_EVM_CHAIN_ID}).`,
);
const boolstring = z
.string()
.pipe(z.enum(["true", "false"]))
.transform((val) => val === "true")
.openapi({ type: "boolean" });
const stringarray = z
.string()
.transform((val) => val.split(","))
.pipe(z.array(z.string().min(1)).min(1))
.refine((values) => new Set(values).size === values.length, {
message: "Must be a set of unique entries.",
});
const name = z
.string()
.refine(isNormalizedName, "Must be normalized, see https://docs.ens.domains/resolution/names/")
.transform((val) => val as Name)
.describe("ENS name to resolve (e.g. 'vitalik.eth'). Must be normalized per ENSIP-15.");
const trace = z
.optional(boolstring)
.default(false)
.describe("Include detailed resolution trace information in the response.")
.openapi({ default: false });
const accelerate = z
.optional(boolstring)
.default(false)
.describe("Attempt accelerated CCIP-Read resolution using L1 data.")
.openapi({
default: false,
});
const address = makeLowercaseAddressSchema().describe(
"EVM wallet address (e.g. '0xd8da6bf26964af9d7eed9e03e53415d37aa96045').",
);
const defaultableChainId = makeDefaultableChainIdStringSchema().describe(
"Chain ID as a string (e.g. '1' for Ethereum mainnet). Use '0' for the default EVM chain.",
);
const coinType = makeCoinTypeStringSchema();
const chainIdsWithoutDefaultChainId = z
.optional(stringarray.pipe(z.array(defaultableChainId.pipe(excludingDefaultChainId))))
.describe(
"Comma-separated list of chain IDs to resolve primary names for (e.g. '1,10,8453'). The default EVM chain ID (0) is not allowed.",
);
const rawSelectionParams = z.object({
nameRecord: z
.string()
.optional()
.describe("Whether to include the ENS name record in the response.")
.openapi({
enum: ["true", "false"],
}),
addresses: z
.string()
.optional()
.describe(
"Comma-separated list of coin types to resolve addresses for (e.g. '60' for ETH, '2147483658' for OP).",
),
texts: z
.string()
.optional()
.describe(
"Comma-separated list of text record keys to resolve (e.g. 'avatar,description,url').",
),
});
const selection = z
.object({
nameRecord: z.optional(boolstring),
addresses: z.optional(stringarray.pipe(z.array(coinType))),
texts: z.optional(stringarray),
})
.transform((value, ctx) => {
const selection: ResolverRecordsSelection = {
...(value.nameRecord && { name: true }),
...(value.addresses && { addresses: value.addresses }),
...(value.texts && { texts: value.texts }),
};
if (isSelectionEmpty(selection)) {
ctx.issues.push({
code: "custom",
message: "Selection cannot be empty.",
input: selection,
});
return z.NEVER;
}
return selection;
});
/**
* Query Param Schema
*
* Allows treating a query param with no value as if the query param
* value was 'undefined'.
*
* Note: This overrides a default behavior when the default value for
* a query param is an empty string. Empty string causes an edge case
* for query param value coercion into numbers:
* ```ts
* // empty string coercion into Number type
* Number('') === 0;
* ```
*
* The `preprocess` method replaces an empty string with `undefined` value,
* and the output type is set to `unknown` to allow easy composition with
* other specialized Zod schemas.
*/
const queryParam = z.preprocess((v) => (v === "" ? undefined : v), z.unknown());
export const params = {
boolstring,
stringarray,
name,
trace,
accelerate,
address,
defaultableChainId,
coinType,
selectionParams: rawSelectionParams,
selection,
chainIdsWithoutDefaultChainId,
queryParam,
};