@mcma/api
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -63,5 +63,11 @@ import { ContextVariableProvider, Resource, ResourceType } from "@mcma/core"; | ||
isBadRequestDueToMissingBody<T extends Resource>(): T | undefined; | ||
resourceCreated<T extends Resource>(resource: T): void; | ||
resourceIfFound<T extends Resource>(resource: T, setBody?: boolean): boolean; | ||
hasRequestBody(): boolean; | ||
getRequestBody<T extends Resource>(): T; | ||
setResponseCode(statusCode: number | HttpStatusCode, statusMessage?: string): void; | ||
setResponseBody<T extends Resource>(resource: T): void; | ||
setResponseResourceCreated<T extends Resource>(resource: T): void; | ||
setResponseBadRequestDueToMissingBody():void; | ||
setResponseResourceNotFound(): void; | ||
} | ||
@@ -112,3 +118,3 @@ | ||
overrideHandler(handler: McmaApiRouteHandler): void; | ||
onStarted(handleOnStarted: ((requestContext: McmaApiRequestContext) => Promise<void>)): void; | ||
onStarted(handleOnStarted: ((requestContext: McmaApiRequestContext) => Promise<void>)): boolean | void; | ||
onCompleted(handleOnCompleted: ((requestContext: McmaApiRequestContext) => Promise<T>)): void; | ||
@@ -115,0 +121,0 @@ build(): McmaApiRoute; |
@@ -25,2 +25,3 @@ const HttpStatusCode = { | ||
UNSUPPORTED_TYPE: 415, | ||
UNPROCESSABLE_ENTITY: 422, | ||
INTERNAL_ERROR: 500, | ||
@@ -82,2 +83,4 @@ NOT_IMPLEMENTED: 501, | ||
return "Unsupported Media Type"; | ||
case HttpStatusCode.UNPROCESSABLE_ENTITY: | ||
return "Unprocessable Entity"; | ||
case HttpStatusCode.INTERNAL_ERROR: | ||
@@ -84,0 +87,0 @@ return "Internal Server Error"; |
@@ -32,29 +32,31 @@ const { ContextVariableProvider } = require("@mcma/core"); | ||
isBadRequestDueToMissingBody() { | ||
let resource = this.request && this.request.body; | ||
if (!resource) { | ||
this.response.statusCode = HttpStatusCode.BAD_REQUEST; | ||
this.response.statusMessage = "Missing request body."; | ||
} | ||
return resource; | ||
hasRequestBody() { | ||
return !!this.request.body; | ||
} | ||
getRequestBody() { | ||
return this.request.body; | ||
} | ||
setResponseStatusCode(statusCode, statusMessage) { | ||
this.response.statusCode = statusCode; | ||
this.response.statusMessage = statusMessage; | ||
} | ||
resourceCreated(resource) { | ||
this.response.statusCode = HttpStatusCode.CREATED; | ||
setResponseBody(body) { | ||
this.response.body = body; | ||
} | ||
setResponseBadRequestDueToMissingBody() { | ||
this.setResponseStatusCode(HttpStatusCode.BAD_REQUEST, "Missing request body."); | ||
} | ||
setResponseResourceCreated(resource) { | ||
this.response.headers["Location"] = resource.id; | ||
this.response.body = resource; | ||
this.setResponseStatusCode(HttpStatusCode.CREATED); | ||
this.setResponseBody(resource); | ||
} | ||
resourceIfFound(resource, setBody = true) { | ||
if (!resource) { | ||
this.response.statusCode = HttpStatusCode.NOT_FOUND; | ||
this.response.statusMessage = "No resource found on path '" + this.request.path + "'."; | ||
return false; | ||
} | ||
if (setBody) { | ||
this.response.body = resource; | ||
} | ||
return true; | ||
setResponseResourceNotFound() { | ||
this.setResponseStatusCode(HttpStatusCode.NOT_FOUND, "No resource found on path '" + this.request.path + "'."); | ||
} | ||
@@ -61,0 +63,0 @@ } |
@@ -127,3 +127,6 @@ const uuidv4 = require("uuid/v4"); | ||
if (onStarted) { | ||
await onStarted(requestContext); | ||
const continueRequest = await onStarted(requestContext); | ||
if (continueRequest !== undefined && !continueRequest) { | ||
return; | ||
} | ||
} | ||
@@ -158,7 +161,11 @@ | ||
if (onStarted) { | ||
await onStarted(requestContext); | ||
const continueRequest = await onStarted(requestContext); | ||
if (continueRequest !== undefined && !continueRequest) { | ||
return; | ||
} | ||
} | ||
const resource = requestContext.isBadRequestDueToMissingBody(); | ||
const resource = requestContext.getRequestBody(); | ||
if (!resource) { | ||
requestContext.setResponseBadRequestDueToMissingBody(); | ||
return; | ||
@@ -175,3 +182,3 @@ } | ||
requestContext.resourceCreated(resource); | ||
requestContext.setResponseResourceCreated(resource); | ||
} | ||
@@ -191,3 +198,6 @@ } | ||
if (onStarted) { | ||
await onStarted(requestContext); | ||
const continueRequest = await onStarted(requestContext); | ||
if (continueRequest !== undefined && !continueRequest) { | ||
return; | ||
} | ||
} | ||
@@ -202,3 +212,7 @@ | ||
requestContext.resourceIfFound(resource); | ||
if (resource) { | ||
requestContext.setResponseBody(resource); | ||
} else { | ||
requestContext.setResponseResourceNotFound(); | ||
} | ||
} | ||
@@ -218,7 +232,11 @@ } | ||
if (onStarted) { | ||
await onStarted(requestContext); | ||
const continueRequest = await onStarted(requestContext); | ||
if (continueRequest !== undefined && !continueRequest) { | ||
return; | ||
} | ||
} | ||
const resource = requestContext.isBadRequestDueToMissingBody(); | ||
const resource = requestContext.getRequestBody(); | ||
if (!resource) { | ||
requestContext.setResponseBadRequestDueToMissingBody(); | ||
return; | ||
@@ -235,3 +253,3 @@ } | ||
requestContext.response.body = resource; | ||
requestContext.setResponseBody(resource); | ||
} | ||
@@ -251,3 +269,6 @@ } | ||
if (onStarted) { | ||
await onStarted(requestContext); | ||
const continueRequest = await onStarted(requestContext); | ||
if (continueRequest !== undefined && !continueRequest) { | ||
return; | ||
} | ||
} | ||
@@ -261,3 +282,4 @@ | ||
if (!requestContext.resourceIfFound(resource, false)) { | ||
if (!resource) { | ||
requestContext.setResponseResourceNotFound(); | ||
return; | ||
@@ -264,0 +286,0 @@ } |
{ | ||
"name": "@mcma/api", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "Node module for building APIs based on the EBU MCMA framework", | ||
@@ -34,5 +34,5 @@ "engines": { | ||
"peerDependencies": { | ||
"@mcma/client": "0.5.1", | ||
"@mcma/core": "0.5.1", | ||
"@mcma/data": "0.5.1" | ||
"@mcma/client": "0.5.2", | ||
"@mcma/core": "0.5.2", | ||
"@mcma/data": "0.5.2" | ||
}, | ||
@@ -39,0 +39,0 @@ "devDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32559
783