Skip to content

Commit eef2772

Browse files
Refactor/type-validation (#951)
* chore(data): add @zk-kit/utils dependency * refactor(data): refactor parameter validation using @zk-kit/utils
1 parent b96467f commit eef2772

File tree

5 files changed

+31
-34
lines changed

5 files changed

+31
-34
lines changed

packages/data/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
},
3939
"dependencies": {
4040
"@semaphore-protocol/utils": "4.8.2",
41+
"@zk-kit/utils": "1.3.0",
4142
"axios": "1.6.6",
4243
"ethers": "6.13.4"
4344
}

packages/data/src/checkParameter.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/data/src/ethers.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import {
1717
Provider
1818
} from "ethers/providers"
1919
import { SemaphoreABI } from "@semaphore-protocol/utils/constants"
20+
import { requireString } from "@zk-kit/utils/error-handlers"
2021
import { EthersNetwork, EthersOptions, GroupResponse } from "./types"
21-
import checkParameter from "./checkParameter"
2222
import getEvents from "./getEvents"
2323

2424
/**
@@ -40,16 +40,16 @@ export default class SemaphoreEthers {
4040
* @param options Configuration options for the ethers provider and the Semaphore contract.
4141
*/
4242
constructor(networkOrEthereumURL: EthersNetwork | string = defaultNetwork, options: EthersOptions = {}) {
43-
checkParameter(networkOrEthereumURL, "networkOrEthereumURL", "string")
43+
requireString(networkOrEthereumURL, "networkOrEthereumURL")
4444

4545
if (options.provider) {
46-
checkParameter(options.provider, "provider", "string")
46+
requireString(options.provider, "provider")
4747
} else if (!networkOrEthereumURL.startsWith("http")) {
4848
options.provider = "infura"
4949
}
5050

5151
if (options.apiKey) {
52-
checkParameter(options.apiKey, "apiKey", "string")
52+
requireString(options.apiKey, "apiKey")
5353
}
5454

5555
if (isSupportedNetwork(networkOrEthereumURL)) {
@@ -140,7 +140,7 @@ export default class SemaphoreEthers {
140140
* @returns A promise that resolves to a GroupResponse object.
141141
*/
142142
async getGroup(groupId: string): Promise<GroupResponse> {
143-
checkParameter(groupId, "groupId", "string")
143+
requireString(groupId, "groupId")
144144

145145
const groupAdmin = await this._contract.getGroupAdmin(groupId)
146146

@@ -172,7 +172,7 @@ export default class SemaphoreEthers {
172172
* @returns A promise that resolves to an array of member identity commitments as strings.
173173
*/
174174
async getGroupMembers(groupId: string): Promise<string[]> {
175-
checkParameter(groupId, "groupId", "string")
175+
requireString(groupId, "groupId")
176176

177177
const groupAdmin = await this._contract.getGroupAdmin(groupId)
178178

@@ -257,7 +257,7 @@ export default class SemaphoreEthers {
257257
* @returns A promise that resolves to an array of validated proofs.
258258
*/
259259
async getGroupValidatedProofs(groupId: string): Promise<any> {
260-
checkParameter(groupId, "groupId", "string")
260+
requireString(groupId, "groupId")
261261

262262
const groupAdmin = await this._contract.getGroupAdmin(groupId)
263263

@@ -290,8 +290,8 @@ export default class SemaphoreEthers {
290290
* @returns A promise that resolves to true if the member is part of the group, otherwise false.
291291
*/
292292
async isGroupMember(groupId: string, member: string): Promise<boolean> {
293-
checkParameter(groupId, "groupId", "string")
294-
checkParameter(member, "member", "string")
293+
requireString(groupId, "groupId")
294+
requireString(member, "member")
295295

296296
return this._contract.hasMember(groupId, member)
297297
}

packages/data/src/subgraph.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defaultNetwork, SupportedNetwork } from "@semaphore-protocol/utils/networks"
22
import { AxiosRequestConfig } from "axios"
3-
import checkParameter from "./checkParameter"
3+
import { requireString, requireObject, requireBoolean } from "@zk-kit/utils/error-handlers"
44
import getURL from "./getURL"
55
import request from "./request"
66
import { GroupOptions, GroupResponse } from "./types"
@@ -24,7 +24,7 @@ export default class SemaphoreSubgraph {
2424
* @param networkOrSubgraphURL Either a supported network identifier or a direct URL to the subgraph.
2525
*/
2626
constructor(networkOrSubgraphURL: SupportedNetwork | string = defaultNetwork) {
27-
checkParameter(networkOrSubgraphURL, "networkOrSubgraphURL", "string")
27+
requireString(networkOrSubgraphURL, "networkOrSubgraphURL")
2828

2929
if (typeof networkOrSubgraphURL === "string" && networkOrSubgraphURL.startsWith("http")) {
3030
this._url = networkOrSubgraphURL
@@ -73,12 +73,12 @@ export default class SemaphoreSubgraph {
7373
* @returns A promise that resolves to an array of group details.
7474
*/
7575
async getGroups(options: GroupOptions = {}): Promise<GroupResponse[]> {
76-
checkParameter(options, "options", "object")
76+
requireObject(options, "options")
7777

7878
const { members = false, validatedProofs = false } = options
7979

80-
checkParameter(members, "members", "boolean")
81-
checkParameter(validatedProofs, "validatedProofs", "boolean")
80+
requireBoolean(members, "members")
81+
requireBoolean(validatedProofs, "validatedProofs")
8282

8383
let filtersQuery = ""
8484

@@ -164,13 +164,13 @@ export default class SemaphoreSubgraph {
164164
* @returns A promise that resolves to the details of the specified group.
165165
*/
166166
async getGroup(groupId: string, options: Omit<GroupOptions, "filters"> = {}): Promise<GroupResponse> {
167-
checkParameter(groupId, "groupId", "string")
168-
checkParameter(options, "options", "object")
167+
requireString(groupId, "groupId")
168+
requireObject(options, "options")
169169

170170
const { members = false, validatedProofs = false } = options
171171

172-
checkParameter(members, "members", "boolean")
173-
checkParameter(validatedProofs, "validatedProofs", "boolean")
172+
requireBoolean(members, "members")
173+
requireBoolean(validatedProofs, "validatedProofs")
174174

175175
const config: AxiosRequestConfig = {
176176
method: "post",
@@ -247,8 +247,8 @@ export default class SemaphoreSubgraph {
247247
* @returns A promise that resolves to true if the member is part of the group, otherwise false.
248248
*/
249249
async isGroupMember(groupId: string, member: string): Promise<boolean> {
250-
checkParameter(groupId, "groupId", "string")
251-
checkParameter(member, "member", "string")
250+
requireString(groupId, "groupId")
251+
requireString(member, "member")
252252

253253
const config: AxiosRequestConfig = {
254254
method: "post",

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7624,6 +7624,7 @@ __metadata:
76247624
"@rollup/plugin-json": "npm:^6.1.0"
76257625
"@rollup/plugin-typescript": "npm:^11.1.6"
76267626
"@semaphore-protocol/utils": "npm:4.8.2"
7627+
"@zk-kit/utils": "npm:1.3.0"
76277628
axios: "npm:1.6.6"
76287629
ethers: "npm:6.13.4"
76297630
rimraf: "npm:^5.0.5"
@@ -9778,6 +9779,15 @@ __metadata:
97789779
languageName: node
97799780
linkType: hard
97809781

9782+
"@zk-kit/utils@npm:1.3.0":
9783+
version: 1.3.0
9784+
resolution: "@zk-kit/utils@npm:1.3.0"
9785+
dependencies:
9786+
buffer: "npm:^6.0.3"
9787+
checksum: 10/cfeb48687486034250f2878db115a54b02d3dca8a10d372e861bc6825e23dc1add5497e512adf4ed39706c62f4841384e138db1fbec3320af87deeabaf428063
9788+
languageName: node
9789+
linkType: hard
9790+
97819791
"JSONStream@npm:^1.3.5":
97829792
version: 1.3.5
97839793
resolution: "JSONStream@npm:1.3.5"

0 commit comments

Comments
 (0)