@@ -136,7 +136,7 @@ describe("emitter-express", () => {
136
136
assert . match ( contents , / e x p o r t t y p e l i s t P e t s B o d y = u n d e f i n e d ; / ) ;
137
137
assert . match (
138
138
contents ,
139
- / e x p o r t t y p e l i s t P e t s R e s p o n s e B o d y = { p e t s : P e t S t o r e . P e t \[ \] } ; /
139
+ / e x p o r t t y p e l i s t P e t s R e s p o n s e B o d y = { p e t s : P e t \[ \] } ; /
140
140
) ;
141
141
} ) ;
142
142
@@ -150,14 +150,14 @@ describe("emitter-express", () => {
150
150
it ( "emits the route callback type" , ( ) => {
151
151
assert . match (
152
152
contents ,
153
- / e x p o r t i n t e r f a c e P e t s H a n d l e r s \{ ( \n | .) * l i s t P e t s : \( \. \. \. h a n d l e r s : A r r a y < P e t s . l i s t P e t s H a n d l e r > \) = > v o i d ; ( \n | .) * \} /
153
+ / e x p o r t i n t e r f a c e H a n d l e r s \{ ( \n | .) * l i s t P e t s : \( \. \. \. h a n d l e r s : A r r a y < l i s t P e t s H a n d l e r > \) = > v o i d ; ( \n | .) * \} /
154
154
) ;
155
155
} ) ;
156
156
157
157
it ( "emits the route callback implementation" , ( ) => {
158
158
assert . match (
159
159
contents ,
160
- / c o n s t l i s t P e t s : P e t s H a n d l e r s \[ " l i s t P e t s " \] = \( \. \. \. h a n d l e r s \) = > \{ [ \n \s ] * r o u t e r .g e t \( " \/ p e t s " , \. \. \. h a n d l e r s \) ; ( \n | .) * \} ; /
160
+ / c o n s t l i s t P e t s : P e t S t o r e . P e t s . H a n d l e r s \[ " l i s t P e t s " \] = \( \. \. \. h a n d l e r s \) = > \{ [ \n \s ] * r o u t e r .g e t \( " \/ p e t s " , \. \. \. h a n d l e r s \) ; ( \n | .) * \} ; /
161
161
) ;
162
162
assert . match ( contents , / r e t u r n \{ ( \n | .) * l i s t P e t s , ( \n | .) * \} ; / ) ;
163
163
} ) ;
@@ -170,7 +170,7 @@ describe("emitter-express", () => {
170
170
assert . match ( contents , / e x p o r t t y p e g e t P e t B o d y = u n d e f i n e d ; / ) ;
171
171
assert . match (
172
172
contents ,
173
- / e x p o r t t y p e g e t P e t R e s p o n s e B o d y = ( \n | . ) * \| \{ p e t : P e t S t o r e . P e t \} ( \n | . ) * \| \{ e r r o r : P e t S t o r e . N o t F o u n d E r r o r \} ; /
173
+ / e x p o r t t y p e g e t P e t R e s p o n s e B o d y = \{ p e t : P e t \} \| \{ e r r o r : N o t F o u n d E r r o r \} ; /
174
174
) ;
175
175
} ) ;
176
176
@@ -184,14 +184,14 @@ describe("emitter-express", () => {
184
184
it ( "emits the route callback type" , ( ) => {
185
185
assert . match (
186
186
contents ,
187
- / e x p o r t i n t e r f a c e P e t s H a n d l e r s \{ ( \n | .) * g e t P e t : \( \. \. \. h a n d l e r s : A r r a y < P e t s . g e t P e t H a n d l e r > \) = > v o i d ; ( \n | .) * \} /
187
+ / e x p o r t i n t e r f a c e H a n d l e r s \{ ( \n | .) * g e t P e t : \( \. \. \. h a n d l e r s : A r r a y < g e t P e t H a n d l e r > \) = > v o i d ; ( \n | .) * \} /
188
188
) ;
189
189
} ) ;
190
190
191
191
it ( "emits the route callback implementation" , ( ) => {
192
192
assert . match (
193
193
contents ,
194
- / c o n s t g e t P e t : P e t s H a n d l e r s \[ " g e t P e t " \] = \( \. \. \. h a n d l e r s \) = > \{ [ \n \s ] * r o u t e r .g e t \( " \/ p e t s \/ : p e t I d " , \. \. \. h a n d l e r s \) ; ( \n | .) * \} ; /
194
+ / c o n s t g e t P e t : P e t S t o r e . P e t s . H a n d l e r s \[ " g e t P e t " \] = \( \. \. \. h a n d l e r s \) = > \{ [ \n \s ] * r o u t e r .g e t \( " \/ p e t s \/ : p e t I d " , \. \. \. h a n d l e r s \) ; ( \n | .) * \} ; /
195
195
) ;
196
196
assert . match ( contents , / r e t u r n \{ ( \n | .) * g e t P e t , ( \n | .) * \} ; / ) ;
197
197
} ) ;
@@ -201,10 +201,10 @@ describe("emitter-express", () => {
201
201
it ( "emits the function types" , ( ) => {
202
202
assert . match ( contents , / e x p o r t t y p e c r e a t e P e t P a r a m s = \{ \} ; / ) ;
203
203
assert . match ( contents , / e x p o r t t y p e c r e a t e P e t Q u e r y = \{ \} ; / ) ;
204
- assert . match ( contents , / e x p o r t t y p e c r e a t e P e t B o d y = P e t S t o r e . P e t ; / ) ;
204
+ assert . match ( contents , / e x p o r t t y p e c r e a t e P e t B o d y = P e t ; / ) ;
205
205
assert . match (
206
206
contents ,
207
- / e x p o r t t y p e c r e a t e P e t R e s p o n s e B o d y = \{ p e t : P e t S t o r e . P e t \} ; /
207
+ / e x p o r t t y p e c r e a t e P e t R e s p o n s e B o d y = \{ p e t : P e t \} ; /
208
208
) ;
209
209
} ) ;
210
210
@@ -218,14 +218,14 @@ describe("emitter-express", () => {
218
218
it ( "emits the route callback type" , ( ) => {
219
219
assert . match (
220
220
contents ,
221
- / e x p o r t i n t e r f a c e P e t s H a n d l e r s \{ ( \n | .) * c r e a t e P e t : \( \. \. \. h a n d l e r s : A r r a y < P e t s . c r e a t e P e t H a n d l e r > \) = > v o i d ; ( \n | .) * \} /
221
+ / e x p o r t i n t e r f a c e H a n d l e r s \{ ( \n | .) * c r e a t e P e t : \( \. \. \. h a n d l e r s : A r r a y < c r e a t e P e t H a n d l e r > \) = > v o i d ; ( \n | .) * \} /
222
222
) ;
223
223
} ) ;
224
224
225
225
it ( "emits the route callback implementation" , ( ) => {
226
226
assert . match (
227
227
contents ,
228
- / c o n s t c r e a t e P e t : P e t s H a n d l e r s \[ " c r e a t e P e t " \] = \( \. \. \. h a n d l e r s \) = > \{ [ \n \s ] * r o u t e r .p o s t \( " \/ p e t s " , \. \. \. h a n d l e r s \) ; ( \n | .) * \} ; /
228
+ / c o n s t c r e a t e P e t : P e t S t o r e . P e t s . H a n d l e r s \[ " c r e a t e P e t " \] = \( \. \. \. h a n d l e r s \) = > \{ [ \n \s ] * r o u t e r .p o s t \( " \/ p e t s " , \. \. \. h a n d l e r s \) ; ( \n | .) * \} ; /
229
229
) ;
230
230
assert . match ( contents , / r e t u r n \{ ( \n | .) * c r e a t e P e t , ( \n | .) * \} ; / ) ;
231
231
} ) ;
@@ -244,7 +244,7 @@ describe("emitter-express", () => {
244
244
) ;
245
245
assert . match (
246
246
contents ,
247
- / e x p o r t t y p e u p d a t e P e t R e s p o n s e B o d y = ( \n | . ) * \| \{ p e t : P e t S t o r e . P e t \} ( \n | . ) * \| \{ e r r o r : P e t S t o r e . N o t F o u n d E r r o r \} ; /
247
+ / e x p o r t t y p e u p d a t e P e t R e s p o n s e B o d y = \{ p e t : P e t \} \| \{ e r r o r : N o t F o u n d E r r o r \} ; /
248
248
) ;
249
249
} ) ;
250
250
@@ -258,14 +258,14 @@ describe("emitter-express", () => {
258
258
it ( "emits the route callback type" , ( ) => {
259
259
assert . match (
260
260
contents ,
261
- / e x p o r t i n t e r f a c e P e t s H a n d l e r s \{ ( \n | .) * u p d a t e P e t : \( \. \. \. h a n d l e r s : A r r a y < P e t s . u p d a t e P e t H a n d l e r > \) = > v o i d ; ( \n | .) * \} /
261
+ / e x p o r t i n t e r f a c e H a n d l e r s \{ ( \n | .) * u p d a t e P e t : \( \. \. \. h a n d l e r s : A r r a y < u p d a t e P e t H a n d l e r > \) = > v o i d ; ( \n | .) * \} /
262
262
) ;
263
263
} ) ;
264
264
265
265
it ( "emits the route callback implementation" , ( ) => {
266
266
assert . match (
267
267
contents ,
268
- / c o n s t u p d a t e P e t : P e t s H a n d l e r s \[ " u p d a t e P e t " \] = \( \. \. \. h a n d l e r s \) = > \{ [ \n \s ] * r o u t e r .p u t \( " \/ p e t s \/ : p e t I d " , \. \. \. h a n d l e r s \) ; ( \n | .) * \} ; /
268
+ / c o n s t u p d a t e P e t : P e t S t o r e . P e t s . H a n d l e r s \[ " u p d a t e P e t " \] = \( \. \. \. h a n d l e r s \) = > \{ [ \n \s ] * r o u t e r .p u t \( " \/ p e t s \/ : p e t I d " , \. \. \. h a n d l e r s \) ; ( \n | .) * \} ; /
269
269
) ;
270
270
assert . match ( contents , / r e t u r n \{ ( \n | .) * u p d a t e P e t , ( \n | .) * \} ; / ) ;
271
271
} ) ;
@@ -287,7 +287,7 @@ describe("emitter-express", () => {
287
287
) ;
288
288
assert . match (
289
289
contents ,
290
- / e x p o r t n a m e s p a c e A n i m a l s \{ ( \n | .) * e x p o r t t y p e l i s t P e t s R e s p o n s e B o d y = { p e t s : P e t S t o r e . P e t \[ \] } ; ( \n | .) * \} /
290
+ / e x p o r t n a m e s p a c e A n i m a l s \{ ( \n | .) * e x p o r t t y p e l i s t P e t s R e s p o n s e B o d y = { p e t s : P e t \[ \] } ; ( \n | .) * \} /
291
291
) ;
292
292
} ) ;
293
293
@@ -301,22 +301,22 @@ describe("emitter-express", () => {
301
301
it ( "emits the route callback type" , ( ) => {
302
302
assert . match (
303
303
contents ,
304
- / e x p o r t i n t e r f a c e A n i m a l s H a n d l e r s \{ ( \n | .) * l i s t P e t s : \( \. \. \. h a n d l e r s : A r r a y < A n i m a l s . l i s t P e t s H a n d l e r > \) = > v o i d ; ( \n | .) * \} /
304
+ / e x p o r t i n t e r f a c e H a n d l e r s \{ ( \n | .) * l i s t P e t s : \( \. \. \. h a n d l e r s : A r r a y < l i s t P e t s H a n d l e r > \) = > v o i d ; ( \n | .) * \} /
305
305
) ;
306
306
} ) ;
307
307
308
308
it ( "emits the route callback implementation" , ( ) => {
309
309
assert . match (
310
310
contents ,
311
- / c o n s t l i s t P e t s : A n i m a l s H a n d l e r s \[ " l i s t P e t s " \] = \( \. \. \. h a n d l e r s \) = > \{ [ \n \s ] * r o u t e r .g e t \( " \/ a n i m a l s " , \. \. \. h a n d l e r s \) ; ( \n | .) * \} ; /
311
+ / c o n s t l i s t P e t s : P e t S t o r e . A n i m a l s . H a n d l e r s \[ " l i s t P e t s " \] = \( \. \. \. h a n d l e r s \) = > \{ [ \n \s ] * r o u t e r .g e t \( " \/ a n i m a l s " , \. \. \. h a n d l e r s \) ; ( \n | .) * \} ; /
312
312
) ;
313
313
assert . match ( contents , / r e t u r n \{ ( \n | .) * l i s t P e t s , ( \n | .) * \} ; / ) ;
314
314
} ) ;
315
315
316
316
it ( 'emits the "Animals" namespace in the TypedRouter' , ( ) => {
317
317
assert . match (
318
318
contents ,
319
- / e x p o r t i n t e r f a c e T y p e d R o u t e r \{ ( \n | .) * A n i m a l s : A n i m a l s H a n d l e r s ; ( \n | .) * \} /
319
+ / e x p o r t i n t e r f a c e T y p e d R o u t e r \{ ( \n | .) * P e t S t o r e A n i m a l s : P e t S t o r e . A n i m a l s . H a n d l e r s ; ( \n | .) * \} /
320
320
) ;
321
321
} ) ;
322
322
} ) ;
@@ -341,6 +341,57 @@ describe("emitter-express", () => {
341
341
assert . match ( contents , / e x p o r t f u n c t i o n c r e a t e T y p e d R o u t e r / ) ;
342
342
} ) ;
343
343
344
+ describe ( "namespaces" , async ( ) => {
345
+ beforeAll ( async ( ) => {
346
+ contents = await emitTypeSpecToTs ( `
347
+ import "@typespec/http";
348
+ using TypeSpec.Http;
349
+
350
+
351
+ @server("https://example.com", "Single server endpoint")
352
+ namespace PetStore;
353
+
354
+ model Pet {
355
+ id: int32;
356
+ name: string;
357
+ age: int32;
358
+ kind: string;
359
+ }
360
+
361
+ @route("/pets")
362
+ namespace Pets {
363
+ @get
364
+ op listPets(@query type?: string): {
365
+ @body pets: Pet[];
366
+ };
367
+
368
+ @route("/{type}")
369
+ namespace ByType {
370
+ @get
371
+ op listPets(@path type: string): {
372
+ @body pets: Pet[];
373
+ };
374
+
375
+ @route("/{age}")
376
+ namespace ByAge {
377
+ @get
378
+ op listPets(@path type: string, @path age: int32): {
379
+ @body pets: Pet[];
380
+ };
381
+ }
382
+ }
383
+ }
384
+ ` ) ;
385
+ } ) ;
386
+
387
+ it ( "emits the hierarchy of namespace types" , ( ) => {
388
+ assert . match (
389
+ contents ,
390
+ / e x p o r t n a m e s p a c e P e t S t o r e \{ ( \n | .) * e x p o r t n a m e s p a c e P e t s \{ ( \n | .) * e x p o r t n a m e s p a c e B y T y p e \{ ( \n | .) * e x p o r t n a m e s p a c e B y A g e \{ ( \n | .) * \} ( \n | .) * \} ( \n | .) * \} ( \n | .) * \} /
391
+ ) ;
392
+ } ) ;
393
+ } ) ;
394
+
344
395
// it("emits to multiple files", async () => {
345
396
// const host = await getHostForTypeSpecFile(testCode);
346
397
0 commit comments