Skip to content

Commit

Permalink
Let's be explicit with types for response bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
tmclaugh committed Oct 22, 2024
1 parent bf67e4c commit 277b30a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
31 changes: 19 additions & 12 deletions src/handlers/CreateEntity/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
import {
Entity
} from '@backstage/catalog-model'
import { SuccessResponseType } from '../../lib/SuccessResponseType.js'
import { ErrorResponseType } from '../../lib/ErrorResponseType.js'

const LOGGER = new Logger()

Expand Down Expand Up @@ -78,7 +80,8 @@ export async function handler_create (event: APIGatewayProxyEvent, context: Cont
try {
await putEntity(entity, false)
statusCode = 201
body = JSON.stringify({'request_id': event_id})
const response: SuccessResponseType = { "request_id": event_id }
body = JSON.stringify(response)
} catch (error) {
LOGGER.error("Operation failed", { event })
const fault = (<DynamoDBServiceException>error).$fault
Expand All @@ -90,10 +93,11 @@ export async function handler_create (event: APIGatewayProxyEvent, context: Cont
statusCode = 500
break;
}
body = JSON.stringify({
name: (<Error>error).name,
const errorResponse: ErrorResponseType = {
error: (<Error>error).name,
message: (<Error>error).message
})
}
body = JSON.stringify(errorResponse)
}

return {
Expand All @@ -118,12 +122,13 @@ export async function handler_upsert (event: APIGatewayProxyEvent, context: Cont
kind !== event.pathParameters?.kind ||
name !== event.pathParameters?.name
) {
const response: ErrorResponseType = {
error: 'BadRequest',
message: 'Entity metadata does not match request path'
}
return {
statusCode: 400,
body: JSON.stringify({
name: 'BadRequest',
message: 'Entity metadata does not match request path'
})
body: JSON.stringify(response)
}
}

Expand All @@ -132,7 +137,8 @@ export async function handler_upsert (event: APIGatewayProxyEvent, context: Cont
try {
await putEntity(entity, true)
statusCode = 200
body = JSON.stringify({'request_id': event_id})
const response: SuccessResponseType = { "request_id": event_id }
body = JSON.stringify(response)
} catch (error) {
LOGGER.error("Operation failed", { event })
const fault = (<DynamoDBServiceException>error).$fault
Expand All @@ -144,10 +150,11 @@ export async function handler_upsert (event: APIGatewayProxyEvent, context: Cont
statusCode = 500
break;
}
body = JSON.stringify({
name: (<Error>error).name,
const errorResponse: ErrorResponseType = {
error: (<Error>error).name,
message: (<Error>error).message
})
}
body = JSON.stringify(errorResponse)
}

return {
Expand Down
12 changes: 8 additions & 4 deletions src/handlers/DeleteEntity/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
DeleteItemCommandInput,
DynamoDBServiceException
} from '@aws-sdk/client-dynamodb'
import { SuccessResponseType } from '../../lib/SuccessResponseType.js'
import { ErrorResponseType } from '../../lib/ErrorResponseType.js'

const LOGGER = new Logger()

Expand Down Expand Up @@ -64,7 +66,8 @@ export async function handler (event: APIGatewayProxyEvent, context: Context): P
name
)
statusCode = 200
body = JSON.stringify({"request_id": event_id})
const response: SuccessResponseType = { "request_id": event_id }
body = JSON.stringify(response)
} catch (error) {
LOGGER.error("Operation failed", { event })
const fault = (<DynamoDBServiceException>error).$fault
Expand All @@ -76,10 +79,11 @@ export async function handler (event: APIGatewayProxyEvent, context: Context): P
statusCode = 500
break;
}
body = JSON.stringify({
name: (<Error>error).name,
const errorResponse: ErrorResponseType = {
error: (<Error>error).name,
message: (<Error>error).message
})
}
body = JSON.stringify(errorResponse)
}

return {
Expand Down
10 changes: 6 additions & 4 deletions src/handlers/GetEntity/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import {
Entity
} from '@backstage/catalog-model'
import { ErrorResponseType } from '../../lib/ErrorResponseType.js'

const LOGGER = new Logger()

Expand Down Expand Up @@ -74,7 +75,7 @@ export async function handler (event: APIGatewayProxyEvent, _: Context): Promise
let statusCode: number
let body: string
try {
const entity = await getEntity(
const entity: Entity = await getEntity(
namespace,
kind,
name
Expand All @@ -92,10 +93,11 @@ export async function handler (event: APIGatewayProxyEvent, _: Context): Promise
statusCode = 500
break;
}
body = JSON.stringify({
name: (<Error>error).name,
const errorResponse: ErrorResponseType = {
error: (<Error>error).name,
message: (<Error>error).message
})
}
body = JSON.stringify(errorResponse)
}

return {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/ErrorResponseType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface ErrorResponseType {
error: string
message: string
}
3 changes: 3 additions & 0 deletions src/lib/SuccessResponseType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface SuccessResponseType {
request_id: string
}

0 comments on commit 277b30a

Please sign in to comment.