@@ -9,6 +9,8 @@ import * as mp from "./missing-parts";
9
9
import * as epValidation from "./endpoint-validation" ;
10
10
import * as protocol from "./protocol" ;
11
11
12
+ /* eslint-disable sonarjs/no-duplicate-string */
13
+
12
14
test ( "Verify that modifying additional endpoint spec data type works" , async ( c ) => {
13
15
c . plan ( 14 ) ;
14
16
@@ -112,6 +114,108 @@ test("Verify that modifying additional endpoint spec data type works", async (c)
112
114
c . deepEqual ( permissionChecks , [ ] ) ;
113
115
} ) ;
114
116
117
+ test ( "Verify that modifying additional endpoint spec data type works also for inline endpoint functions" , async ( c ) => {
118
+ c . plan ( 14 ) ;
119
+
120
+ // Create app and tracking array
121
+ const { app, permissionChecks } = newBuilder ( ) ;
122
+ type StateSpecBase = spec . StateSpecBaseOfAppBuilder < typeof app > ;
123
+
124
+ // Create endpoints
125
+ const url = app . url `/${ mp . urlParameter ( "urlParam" , protocol . urlParam ) } ` ( { } ) ;
126
+ const authState = {
127
+ userId : true ,
128
+ } as const satisfies StateSpecBase ;
129
+ const unauthState = {
130
+ userId : false ,
131
+ } as const satisfies StateSpecBase ;
132
+
133
+ const seenArgs : Array < unknown > = [ ] ;
134
+
135
+ const endpointInfos = [
136
+ url . endpoint < Endpoint1 > ( { } ) (
137
+ {
138
+ method : "GET" ,
139
+ state : authState ,
140
+ responseBody : mp . responseBody ( "simpleResponseBody" ) ,
141
+ permissions : "permissions" ,
142
+ } ,
143
+ ( args ) => {
144
+ seenArgs . push ( args ) ;
145
+ return "simpleResponseBody" as const ;
146
+ } ,
147
+ ) ,
148
+
149
+ url . endpoint < Endpoint2 > ( { } ) (
150
+ {
151
+ method : "POST" ,
152
+ state : unauthState ,
153
+ responseBody : mp . responseBody ( "simpleResponseBody" ) ,
154
+ } ,
155
+ ( args ) => {
156
+ seenArgs . push ( args ) ;
157
+ return "simpleResponseBody" as const ;
158
+ } ,
159
+ ) ,
160
+ ] ;
161
+
162
+ const { endpoints } = app . createEndpoints ( { } , endpointInfos ) ;
163
+
164
+ // Initial asserts
165
+ c . deepEqual ( endpoints . length , 1 ) ;
166
+ c . deepEqual ( permissionChecks , [ ] ) ;
167
+
168
+ // Check endpoint with permissions
169
+ await epValidation . validateEndpoint (
170
+ c ,
171
+ endpoints [ 0 ] ,
172
+ ( ) => seenArgs ,
173
+ // No additional prefix is needed
174
+ "" ,
175
+ // Remove body, query, and state from inputs as they are unused by this endpoint
176
+ ( info ) => {
177
+ const result = info . splice ( 3 ) ;
178
+ result . unshift ( [ "stateInformation.stateInfo.0" , "userId" ] ) ;
179
+ return result ;
180
+ } ,
181
+ // Expected output is also different
182
+ {
183
+ contentType : "text/plain" ,
184
+ output : '"simpleResponseBody"' ,
185
+ } ,
186
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any
187
+ ( args ) => data . omit ( args , "body" , "query" ) as any ,
188
+ ) ;
189
+ c . deepEqual ( permissionChecks , [ "permissions" ] ) ;
190
+
191
+ // Check endpoint without permissions
192
+ permissionChecks . length = 0 ;
193
+ seenArgs . length = 0 ;
194
+ await epValidation . validateEndpoint (
195
+ c ,
196
+ endpoints [ 0 ] ,
197
+ ( ) => seenArgs ,
198
+ // No additional prefix is needed
199
+ "" ,
200
+ // Remove body, query, and state from inputs as they are unused by this endpoint
201
+ ( info ) => {
202
+ const result = info . splice ( 3 ) ;
203
+ result . unshift ( [ "stateInformation.stateInfo.0" , "userId" ] ) ;
204
+ return result ;
205
+ } ,
206
+ // Expected output is also different
207
+ {
208
+ contentType : "text/plain" ,
209
+ output : '"simpleResponseBody"' ,
210
+ } ,
211
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any
212
+ ( args ) => data . omit ( args , "body" , "query" ) as any ,
213
+ // Now invoke the unauthenticatedEndpoint() method by using POST
214
+ "POST" ,
215
+ ) ;
216
+ c . deepEqual ( permissionChecks , [ ] ) ;
217
+ } ) ;
218
+
115
219
/**
116
220
* This [higher-kinded types (HKT)](https://www.matechs.com/blog/encoding-hkts-in-typescript-once-again) interface simulates real-life situation, where endpoints which require authentication (their state spec contains "userId" property), also must specify the permissions that the operation requires.
117
221
* In this test setup, the permission type is simply "string", but in reality, it can be something more complex and domain-specific.
0 commit comments