From d11f7ed2505f622fe95985a575453d0cccd81619 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Wed, 27 Jan 2021 15:49:47 -0800 Subject: [PATCH] Update rest and openapi libraries to support verb decorator subpaths --- packages/adl/lib/rest.ts | 52 +++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/packages/adl/lib/rest.ts b/packages/adl/lib/rest.ts index dc424ae0b..da5a99a3e 100644 --- a/packages/adl/lib/rest.ts +++ b/packages/adl/lib/rest.ts @@ -67,12 +67,17 @@ export function isBody(entity: Type) { export type HttpVerb = "get" | "put" | "post" | "patch" | "delete"; -const operationVerbs = new Map(); +interface OperationRoute { + verb: HttpVerb; + subPath?: string; +} -function setOperationVerb(entity: Type, verb: HttpVerb) { +const operationRoutes = new Map(); + +function setOperationRoute(entity: Type, verb: OperationRoute) { if (entity.kind === "InterfaceProperty") { - if (!operationVerbs.has(entity)) { - operationVerbs.set(entity, verb); + if (!operationRoutes.has(entity)) { + operationRoutes.set(entity, verb); } else { throw new Error(`HTTP verb already applied to ${entity.name}`); } @@ -81,27 +86,42 @@ function setOperationVerb(entity: Type, verb: HttpVerb) { } } -export function getOperationVerb(entity: Type): HttpVerb | undefined { - return operationVerbs.get(entity); +export function getOperationRoute(entity: Type): OperationRoute | undefined { + return operationRoutes.get(entity); } -export function get(program: Program, entity: Type) { - setOperationVerb(entity, "get"); +export function get(program: Program, entity: Type, subPath?: string) { + setOperationRoute(entity, { + verb: "get", + subPath + }); } -export function put(program: Program, entity: Type) { - setOperationVerb(entity, "put"); +export function put(program: Program, entity: Type, subPath?: string) { + setOperationRoute(entity, { + verb: "put", + subPath + }); } -export function post(program: Program, entity: Type) { - setOperationVerb(entity, "post"); +export function post(program: Program, entity: Type, subPath?: string) { + setOperationRoute(entity, { + verb: "post", + subPath + }); } -export function patch(program: Program, entity: Type) { - setOperationVerb(entity, "patch"); +export function patch(program: Program, entity: Type, subPath?: string) { + setOperationRoute(entity, { + verb: "patch", + subPath + }); } // BUG #243: How do we deal with reserved words? -export function _delete(program: Program, entity: Type) { - setOperationVerb(entity, "delete"); +export function _delete(program: Program, entity: Type, subPath?: string) { + setOperationRoute(entity, { + verb: "delete", + subPath + }); }