Skip to content

Commit

Permalink
feat(envited.ascs.digital): Abstract out return messages (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenbranje authored Jan 9, 2024
2 parents bed5f80 + 5018f51 commit b835733
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 5 deletions.
4 changes: 3 additions & 1 deletion apps/envited.ascs.digital/app/api/hello/route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { ok } from '../../../common/utils'
import { db } from '../../../common/database/queries'

export async function GET(request: Request) {
try {
const connection = await db()
const tables = await connection.fetchTables()
return Response.json(tables)

return ok(tables)
} catch (error) {
console.log('error', error)
return Response.json(error)
Expand Down
1 change: 1 addition & 0 deletions apps/envited.ascs.digital/common/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { ERRORS } from './errors'
export { RESPONSES } from './response'
18 changes: 18 additions & 0 deletions apps/envited.ascs.digital/common/constants/response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const RESPONSES = {
badRequest: {
status: 403,
statusText: 'Bad request',
},
internalServerError: {
status: 500,
statusText: 'Internal server error',
},
notFound: {
status: 404,
statusText: 'Not found',
},
noContent: {
status: 204,
statusText: 'No content',
},
}
1 change: 1 addition & 0 deletions apps/envited.ascs.digital/common/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ok, badRequest, noContent, notFound, internalServerError } from './utils'
70 changes: 70 additions & 0 deletions apps/envited.ascs.digital/common/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @jest-environment node
*/

import { RESPONSES } from '../constants';
import * as SUT from './utils'

describe('common/utils', () => {
const mockFetch = Promise.resolve({ json: () => Promise.resolve('') });
global.fetch = jest.fn().mockImplementation(() => mockFetch);

describe('ok', () => {
it.each([
['Message', 'Message'],
[[], []],
])('should return as expected', async (message, expected) => {
// when ... we want to get the result
// then ... it returns the result as expected
const response = SUT.ok(message)
const result = await response.json()

expect(result).toEqual(expected)
expect(response.status).toEqual(200)
})
})

describe('badRequest', () => {
it('should return a bad request reponse as expected', async () => {
// when ... we submit a bad request
// then ... it returns a bad request as expected
const response = SUT.badRequest()

expect(response.status).toEqual(403)
expect(response.statusText).toEqual(RESPONSES.badRequest.statusText)
})
})

describe('internalServerError', () => {
it('should return an internal server error as expected', async () => {
// when ... we encounter a irrecoverable error during the request
// then ... it returns an internal server error response as expected
const response = SUT.internalServerError()

expect(response.status).toEqual(500)
expect(response.statusText).toEqual(RESPONSES.internalServerError.statusText)
})
})

describe('notFound', () => {
it('should return a not found as expected', async () => {
// when ... we cannot find the requested resource
// then ... it returns a not found response as expected
const response = SUT.notFound()

expect(response.status).toEqual(404)
expect(response.statusText).toEqual(RESPONSES.notFound.statusText)
})
})

describe('noContent', () => {
it('should return a no content as expected', async () => {
// when ... we request a resource that returns an empty response
// then ... it returns a no content response as expected
const response = SUT.noContent()

expect(response.status).toEqual(204)
expect(response.statusText).toEqual(RESPONSES.noContent.statusText)
})
})
})
11 changes: 11 additions & 0 deletions apps/envited.ascs.digital/common/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { RESPONSES } from '../constants'

export const ok = (data: any) => Response.json(data)

export const badRequest = () => Response.json(null, { status: RESPONSES.badRequest.status, statusText: RESPONSES.badRequest.statusText })

export const internalServerError = () => Response.json(null, { status: RESPONSES.internalServerError.status, statusText: RESPONSES.internalServerError.statusText })

export const notFound = () => Response.json(null, { status: RESPONSES.notFound.status, statusText: RESPONSES.notFound.statusText})

export const noContent = () => new Response(null, { status: RESPONSES.noContent.status, statusText: RESPONSES.noContent.statusText })
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
"tailwindcss": "^3.3.6",
"ts-jest": "^29.1.0",
"ts-node": "10.9.1",
"typescript": "~5.2.2",
"url-loader": "^4.1.1"
"typescript": "~5.2.2"
}
}

0 comments on commit b835733

Please sign in to comment.