@@ -28,16 +28,6 @@ export const acmCoreOrganization = z
2828 examples : [ "ACM" , "Infrastructure Committee" ] ,
2929 } ) ;
3030
31- export function withTags < T extends FastifyZodOpenApiSchema > (
32- tags : string [ ] ,
33- schema : T ,
34- ) {
35- return {
36- tags,
37- ...schema ,
38- } ;
39- }
40-
4131export type RoleSchema = {
4232 "x-required-roles" : AppRoles [ ] ;
4333 "x-disable-api-key-auth" : boolean ;
@@ -48,6 +38,128 @@ type RolesConfig = {
4838 disableApiKeyAuth : boolean ;
4939} ;
5040
41+ export function getCorrectJsonSchema < T , U > ( {
42+ schema,
43+ example,
44+ description,
45+ } : {
46+ schema : T ;
47+ example : U ;
48+ description : string ;
49+ } ) {
50+ return {
51+ description,
52+ content : {
53+ "application/json" : {
54+ example,
55+ schema,
56+ } ,
57+ } ,
58+ } ;
59+ }
60+
61+ export const notAuthenticatedError = getCorrectJsonSchema ( {
62+ schema : z
63+ . object ( {
64+ name : z . literal ( "UnauthenticatedError" ) ,
65+ id : z . literal ( 102 ) ,
66+ message : z . string ( ) . min ( 1 ) ,
67+ } )
68+ . meta ( {
69+ id : "notAuthenticatedError" ,
70+ } ) ,
71+ description : "The request could not be authenticated." ,
72+ example : {
73+ name : "UnauthenticatedError" ,
74+ id : 102 ,
75+ message : "Token not found." ,
76+ } ,
77+ } ) ;
78+
79+ export const notFoundError = getCorrectJsonSchema ( {
80+ schema : z
81+ . object ( {
82+ name : z . literal ( "NotFoundError" ) ,
83+ id : z . literal ( 103 ) ,
84+ message : z . string ( ) . min ( 1 ) ,
85+ } )
86+ . meta ( {
87+ id : "notFoundError" ,
88+ } ) ,
89+ description : "The resource could not be found." ,
90+ example : {
91+ name : "NotFoundError" ,
92+ id : 103 ,
93+ message : "{url} is not a valid URL." ,
94+ } ,
95+ } ) ;
96+
97+ export const notAuthorizedError = getCorrectJsonSchema ( {
98+ schema : z
99+ . object ( {
100+ name : z . literal ( "UnauthorizedError" ) ,
101+ id : z . literal ( 101 ) ,
102+ message : z . string ( ) . min ( 1 ) ,
103+ } )
104+ . meta ( {
105+ id : "notAuthorizedError" ,
106+ } ) ,
107+ description :
108+ "The caller does not have the appropriate permissions for this task." ,
109+ example : {
110+ name : "UnauthorizedError" ,
111+ id : 101 ,
112+ message : "User does not have the privileges for this task." ,
113+ } ,
114+ } ) ;
115+
116+ export const internalServerError = getCorrectJsonSchema ( {
117+ schema : {
118+ content : {
119+ "application/json" : {
120+ schema : z
121+ . object ( {
122+ name : z . literal ( "InternalServerError" ) ,
123+ id : z . literal ( 100 ) ,
124+ message : z . string ( ) . min ( 1 ) ,
125+ } )
126+ . meta ( {
127+ id : "internalServerError" ,
128+ description :
129+ "The server encountered an error processing the request." ,
130+ } ) ,
131+ } ,
132+ } ,
133+ } ,
134+ description : "The server encountered an error." ,
135+ example : {
136+ name : "InternalServerError" ,
137+ id : 100 ,
138+ message :
139+ "An internal server error occurred. Please try again or contact support." ,
140+ } ,
141+ } ) ;
142+
143+ export const rateLimitExceededError = getCorrectJsonSchema ( {
144+ schema : z
145+ . object ( {
146+ name : z . literal ( "RateLimitExceededError" ) ,
147+ id : z . literal ( 409 ) ,
148+ message : z . literal ( "Rate limit exceeded." ) ,
149+ } )
150+ . meta ( {
151+ id : "RateLimitExceededError" ,
152+ description :
153+ "You have sent too many requests. Check the response headers and try again." ,
154+ } ) ,
155+ description : "The request exceeeds the rate limit." ,
156+ example : {
157+ name : "RateLimitExceededError" ,
158+ id : 409 ,
159+ message : "Rate limit exceeded." ,
160+ } ,
161+ } ) ;
162+
51163export function withRoles < T extends FastifyZodOpenApiSchema > (
52164 roles : AppRoles [ ] ,
53165 schema : T ,
@@ -57,6 +169,11 @@ export function withRoles<T extends FastifyZodOpenApiSchema>(
57169 if ( ! disableApiKeyAuth ) {
58170 security . push ( { apiKeyAuth : [ ] } ) ;
59171 }
172+ const responses = {
173+ 401 : notAuthorizedError ,
174+ 403 : notAuthenticatedError ,
175+ ...schema . response ,
176+ } ;
60177 return {
61178 security,
62179 "x-required-roles" : roles ,
@@ -66,5 +183,22 @@ export function withRoles<T extends FastifyZodOpenApiSchema>(
66183 ? `${ disableApiKeyAuth ? "API key authentication is not permitted for this route.\n\n" : "" } Requires one of the following roles: ${ roles . join ( ", " ) } .${ schema . description ? `\n\n${ schema . description } ` : "" } `
67184 : "Requires valid authentication but no specific role." ,
68185 ...schema ,
186+ response : responses ,
187+ } ;
188+ }
189+
190+ export function withTags < T extends FastifyZodOpenApiSchema > (
191+ tags : string [ ] ,
192+ schema : T ,
193+ ) {
194+ const responses = {
195+ 500 : internalServerError ,
196+ 429 : rateLimitExceededError ,
197+ ...schema . response ,
198+ } ;
199+ return {
200+ tags,
201+ ...schema ,
202+ response : responses ,
69203 } ;
70204}
0 commit comments