|
9 | 9 | Operation,
|
10 | 10 | Program,
|
11 | 11 | StringLiteral,
|
| 12 | + SyntaxKind, |
12 | 13 | Tuple,
|
13 | 14 | Type,
|
14 | 15 | Union,
|
@@ -362,53 +363,48 @@ function rangeDescription(start: number, end: number) {
|
362 | 363 | return undefined;
|
363 | 364 | }
|
364 | 365 |
|
365 |
| -function setOperationVerb(program: Program, entity: Type, verb: HttpVerb): void { |
366 |
| - if (entity.kind === "Operation") { |
367 |
| - if (!program.stateMap(HttpStateKeys.verbs).has(entity)) { |
368 |
| - program.stateMap(HttpStateKeys.verbs).set(entity, verb); |
369 |
| - } else { |
370 |
| - reportDiagnostic(program, { |
371 |
| - code: "http-verb-duplicate", |
372 |
| - format: { entityName: entity.name }, |
373 |
| - target: entity, |
374 |
| - }); |
375 |
| - } |
376 |
| - } else { |
377 |
| - reportDiagnostic(program, { |
378 |
| - code: "http-verb-wrong-type", |
379 |
| - format: { verb, entityKind: entity.kind }, |
380 |
| - target: entity, |
| 366 | +function setOperationVerb(context: DecoratorContext, entity: Operation, verb: HttpVerb): void { |
| 367 | + validateVerbUniqueOnNode(context, entity); |
| 368 | + context.program.stateMap(HttpStateKeys.verbs).set(entity, verb); |
| 369 | +} |
| 370 | + |
| 371 | +function validateVerbUniqueOnNode(context: DecoratorContext, type: Operation) { |
| 372 | + const verbDecorators = type.decorators.filter( |
| 373 | + (x) => |
| 374 | + VERB_DECORATORS.includes(x.decorator) && |
| 375 | + x.node?.kind === SyntaxKind.DecoratorExpression && |
| 376 | + x.node?.parent === type.node |
| 377 | + ); |
| 378 | + |
| 379 | + if (verbDecorators.length > 1) { |
| 380 | + reportDiagnostic(context.program, { |
| 381 | + code: "http-verb-duplicate", |
| 382 | + format: { entityName: type.name }, |
| 383 | + target: context.decoratorTarget, |
381 | 384 | });
|
| 385 | + return false; |
382 | 386 | }
|
| 387 | + return true; |
383 | 388 | }
|
384 | 389 |
|
385 | 390 | export function getOperationVerb(program: Program, entity: Type): HttpVerb | undefined {
|
386 | 391 | return program.stateMap(HttpStateKeys.verbs).get(entity);
|
387 | 392 | }
|
388 | 393 |
|
389 |
| -export const $get: GetDecorator = (context: DecoratorContext, entity: Operation) => { |
390 |
| - setOperationVerb(context.program, entity, "get"); |
391 |
| -}; |
392 |
| - |
393 |
| -export const $put: PutDecorator = (context: DecoratorContext, entity: Operation) => { |
394 |
| - setOperationVerb(context.program, entity, "put"); |
395 |
| -}; |
396 |
| - |
397 |
| -export const $post: PostDecorator = (context: DecoratorContext, entity: Operation) => { |
398 |
| - setOperationVerb(context.program, entity, "post"); |
399 |
| -}; |
400 |
| - |
401 |
| -export const $patch: PatchDecorator = (context: DecoratorContext, entity: Operation) => { |
402 |
| - setOperationVerb(context.program, entity, "patch"); |
403 |
| -}; |
| 394 | +function createVerbDecorator(verb: HttpVerb) { |
| 395 | + return (context: DecoratorContext, entity: Operation) => { |
| 396 | + setOperationVerb(context, entity, verb); |
| 397 | + }; |
| 398 | +} |
404 | 399 |
|
405 |
| -export const $delete: DeleteDecorator = (context: DecoratorContext, entity: Operation) => { |
406 |
| - setOperationVerb(context.program, entity, "delete"); |
407 |
| -}; |
| 400 | +export const $get: GetDecorator = createVerbDecorator("get"); |
| 401 | +export const $put: PutDecorator = createVerbDecorator("put"); |
| 402 | +export const $post: PostDecorator = createVerbDecorator("post"); |
| 403 | +export const $patch: PatchDecorator = createVerbDecorator("patch"); |
| 404 | +export const $delete: DeleteDecorator = createVerbDecorator("delete"); |
| 405 | +export const $head: HeadDecorator = createVerbDecorator("head"); |
408 | 406 |
|
409 |
| -export const $head: HeadDecorator = (context: DecoratorContext, entity: Operation) => { |
410 |
| - setOperationVerb(context.program, entity, "head"); |
411 |
| -}; |
| 407 | +const VERB_DECORATORS = [$get, $head, $post, $put, $patch, $delete]; |
412 | 408 |
|
413 | 409 | export interface HttpServer {
|
414 | 410 | url: string;
|
|
0 commit comments