Skip to content

Commit 4f6beba

Browse files
committed
fix(metadata.test.ts): metadata validation handling and responses
Restores the metadata validation endpoint testing. Additionally the full metadata controller is using try catches and we updated the response types. The response types have been made more simple and flexible, for one to support the cases of validation where the processing can be successful but the data is invalid. Lastly, responses where data is set as null were cleaned so simply not return data.
1 parent 5c46b42 commit 4f6beba

File tree

10 files changed

+340
-265
lines changed

10 files changed

+340
-265
lines changed
Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import { jsonToBlob } from "../utils/jsonToBlob.js";
2-
import { Body, Controller, Post, Response, Route, SuccessResponse, Tags } from "tsoa";
2+
import {
3+
Body,
4+
Controller,
5+
Post,
6+
Response,
7+
Route,
8+
SuccessResponse,
9+
Tags,
10+
} from "tsoa";
311
import { StorageService } from "../services/StorageService.js";
412
import { parseAndValidateMerkleTree } from "../utils/parseAndValidateMerkleTreeDump.js";
513
import type {
6-
ApiResponse,
714
StorageResponse,
815
StoreAllowListRequest,
916
ValidateAllowListRequest,
10-
ValidationResponse
17+
ValidationResponse,
1118
} from "../types/api.js";
1219

1320
@Route("v1/allowlists")
1421
@Tags("Allowlists")
1522
export class AllowListController extends Controller {
16-
1723
/**
1824
* Submits a new allowlist for validation and storage on IPFS. While we maintain a database of allowlists, the allowlist itself is stored on IPFS.
1925
* Try to keep a backup of the allowlist for recovery purposes.
@@ -22,32 +28,45 @@ export class AllowListController extends Controller {
2228
*/
2329
@Post()
2430
@SuccessResponse(201, "Data uploaded successfully", "application/json")
25-
@Response<ApiResponse>(422, "Unprocessable content", {
31+
@Response<StorageResponse>(422, "Unprocessable content", {
2632
success: false,
2733
message: "Errors while validating allow list",
28-
errors: { allowList: "Invalid allowList. Length is 0" }
34+
errors: { allowList: "Invalid allowList. Length is 0" },
2935
})
30-
public async storeAllowList(@Body() requestBody: StoreAllowListRequest): Promise<StorageResponse> {
36+
public async storeAllowList(
37+
@Body() requestBody: StoreAllowListRequest,
38+
): Promise<StorageResponse> {
3139
const storage = await StorageService.init();
3240

33-
const result = parseAndValidateMerkleTree(requestBody);
41+
try {
42+
const result = parseAndValidateMerkleTree(requestBody);
43+
44+
if (!result.valid || !result.data) {
45+
this.setStatus(422);
46+
return {
47+
success: false,
48+
message: "Errors while validating allow list",
49+
errors: result.errors,
50+
};
51+
}
52+
53+
const cid = await storage.uploadFile({
54+
file: jsonToBlob(requestBody.allowList),
55+
});
56+
this.setStatus(201);
3457

35-
if (!result.valid || !result.data) {
36-
this.setStatus(422);
58+
return {
59+
success: true,
60+
data: cid,
61+
};
62+
} catch (error) {
63+
this.setStatus(500);
3764
return {
3865
success: false,
39-
message: "Errors while validating allow list",
40-
errors: result.errors
66+
message: "Error uploading data",
67+
errors: { allowList: "Error uploading data" },
4168
};
4269
}
43-
44-
const cid = await storage.uploadFile({ file: jsonToBlob(requestBody.allowList) });
45-
this.setStatus(201);
46-
47-
return {
48-
success: true,
49-
data: cid
50-
};
5170
}
5271

5372
/**
@@ -57,26 +76,41 @@ export class AllowListController extends Controller {
5776
*/
5877
@Post("/validate")
5978
@SuccessResponse(200, "Valid allowlist object", "application/json")
60-
@Response<ApiResponse>(422, "Unprocessable content", {
79+
@Response<ValidationResponse>(422, "Unprocessable content", {
6180
success: false,
81+
valid: false,
6282
message: "Metadata validation failed",
63-
errors: { allowList: "Invalid allowList. Length is 0" }
83+
errors: { allowList: "Invalid allowList. Length is 0" },
6484
})
65-
public async validateAllowList(@Body() requestBody: ValidateAllowListRequest): Promise<ValidationResponse> {
66-
const result = parseAndValidateMerkleTree(requestBody);
85+
public async validateAllowList(
86+
@Body() requestBody: ValidateAllowListRequest,
87+
): Promise<ValidationResponse> {
88+
try {
89+
const result = parseAndValidateMerkleTree(requestBody);
90+
91+
if (!result.valid || !result.data) {
92+
this.setStatus(422);
93+
return {
94+
success: true,
95+
valid: false,
96+
message: "Errors while validating allow list",
97+
errors: result.errors,
98+
};
99+
}
67100

68-
if (!result.valid || !result.data) {
69-
this.setStatus(422);
101+
this.setStatus(201);
102+
return {
103+
success: true,
104+
valid: true,
105+
};
106+
} catch (error) {
107+
this.setStatus(500);
70108
return {
71109
success: false,
72-
message: "Errors while validating allow list",
73-
errors: result.errors
110+
valid: false,
111+
message: "Error uploading data",
112+
errors: { allowList: "Error uploading data" },
74113
};
75114
}
76-
77-
this.setStatus(201);
78-
return {
79-
success: true
80-
};
81115
}
82-
}
116+
}

src/controllers/BlueprintController.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
Tags,
1111
} from "tsoa";
1212
import type {
13-
AddOrCreateBlueprintResponse,
13+
BlueprintResponse,
1414
ApiResponse,
1515
BlueprintCreateRequest,
1616
BlueprintDeleteRequest,
@@ -36,7 +36,7 @@ export class BlueprintController extends Controller {
3636
})
3737
public async createBlueprint(
3838
@Body() requestBody: BlueprintCreateRequest,
39-
): Promise<AddOrCreateBlueprintResponse> {
39+
): Promise<BlueprintResponse> {
4040
const inputSchema = z.object({
4141
form_values: z.object({
4242
title: z
@@ -131,7 +131,6 @@ export class BlueprintController extends Controller {
131131
return {
132132
success: false,
133133
message: "Invalid input",
134-
data: null,
135134
errors: JSON.parse(parsedBody.error.toString()),
136135
};
137136
}
@@ -316,7 +315,7 @@ export class BlueprintController extends Controller {
316315
public async mintBlueprint(
317316
@Path() blueprintId: number,
318317
@Body() requestBody: BlueprintQueueMintRequest,
319-
): Promise<AddOrCreateBlueprintResponse> {
318+
): Promise<BlueprintResponse> {
320319
const inputSchema = z.object({
321320
signature: z.string(),
322321
chain_id: z.number(),
@@ -331,7 +330,6 @@ export class BlueprintController extends Controller {
331330
return {
332331
success: false,
333332
message: "Invalid input",
334-
data: null,
335333
errors: JSON.parse(parsedBody.error.toString()),
336334
};
337335
}

src/controllers/HyperboardController.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import type {
1515
ApiResponse,
1616
HyperboardCreateRequest,
17-
HyperboardCreateResponse,
17+
HyperboardResponse,
1818
HyperboardUpdateRequest,
1919
} from "../types/api.js";
2020
import { z } from "zod";
@@ -43,7 +43,7 @@ export class HyperboardController extends Controller {
4343
})
4444
public async createHyperboard(
4545
@Body() requestBody: HyperboardCreateRequest,
46-
): Promise<HyperboardCreateResponse> {
46+
): Promise<HyperboardResponse> {
4747
const inputSchema = z
4848
.object({
4949
chainIds: z
@@ -175,7 +175,6 @@ export class HyperboardController extends Controller {
175175
return {
176176
success: false,
177177
message: "Invalid input",
178-
data: null,
179178
errors: JSON.parse(parsedBody.error.toString()),
180179
};
181180
}
@@ -228,7 +227,6 @@ export class HyperboardController extends Controller {
228227
return {
229228
success: false,
230229
message: "Invalid signature",
231-
data: null,
232230
};
233231
}
234232

@@ -262,7 +260,6 @@ export class HyperboardController extends Controller {
262260
return {
263261
success: false,
264262
message: "Error creating hyperboard",
265-
data: null,
266263
};
267264
}
268265

@@ -355,7 +352,6 @@ export class HyperboardController extends Controller {
355352
return {
356353
success: false,
357354
message: "Error updating collection",
358-
data: null,
359355
};
360356
}
361357
}
@@ -430,7 +426,6 @@ export class HyperboardController extends Controller {
430426
return {
431427
success: false,
432428
message: "Error creating collection",
433-
data: null,
434429
};
435430
}
436431
}
@@ -453,7 +448,7 @@ export class HyperboardController extends Controller {
453448
public async updateHyperboard(
454449
@Path() hyperboardId: string,
455450
@Body() requestBody: HyperboardUpdateRequest,
456-
): Promise<ApiResponse<{ id: string } | null>> {
451+
): Promise<HyperboardResponse> {
457452
const inputSchema = z
458453
.object({
459454
id: z.string().uuid(),
@@ -590,7 +585,6 @@ export class HyperboardController extends Controller {
590585
return {
591586
success: false,
592587
message: "Invalid input",
593-
data: null,
594588
errors: JSON.parse(parsedBody.error.toString()),
595589
};
596590
}
@@ -603,7 +597,6 @@ export class HyperboardController extends Controller {
603597
return {
604598
success: false,
605599
message: "Hyperboard not found",
606-
data: null,
607600
};
608601
}
609602

@@ -657,7 +650,6 @@ export class HyperboardController extends Controller {
657650
return {
658651
success: false,
659652
message: "Invalid signature",
660-
data: null,
661653
};
662654
}
663655

@@ -671,7 +663,6 @@ export class HyperboardController extends Controller {
671663
return {
672664
success: false,
673665
message: "Not authorized to update hyperboard",
674-
data: null,
675666
};
676667
}
677668

@@ -692,7 +683,6 @@ export class HyperboardController extends Controller {
692683
return {
693684
success: false,
694685
message: "Error updating hyperboard",
695-
data: null,
696686
};
697687
}
698688

@@ -794,7 +784,6 @@ export class HyperboardController extends Controller {
794784
return {
795785
success: false,
796786
message: "Error updating collection",
797-
data: null,
798787
};
799788
}
800789
}
@@ -867,7 +856,6 @@ export class HyperboardController extends Controller {
867856
return {
868857
success: false,
869858
message: "Error creating collection",
870-
data: null,
871859
};
872860
}
873861
}

0 commit comments

Comments
 (0)