From 2e0223d7a21abdc9919eeff7c8f22f078cad77a3 Mon Sep 17 00:00:00 2001 From: Jeroen Branje Date: Mon, 8 Jan 2024 13:42:56 +0100 Subject: [PATCH 1/3] feat(envited.ascs.digital): Abstract out return messages Signed-off-by: Jeroen Branje --- apps/envited.ascs.digital/.env.dev | 17 +++++++++ .../app/api/hello/route.ts | 1 + .../common/constants/index.ts | 1 + .../common/constants/response.ts | 6 ++++ .../common/utils/index.ts | 1 + .../common/utils/utils.test.ts | 36 +++++++++++++++++++ .../common/utils/utils.ts | 13 +++++++ 7 files changed, 75 insertions(+) create mode 100644 apps/envited.ascs.digital/.env.dev create mode 100644 apps/envited.ascs.digital/common/constants/response.ts create mode 100644 apps/envited.ascs.digital/common/utils/index.ts create mode 100644 apps/envited.ascs.digital/common/utils/utils.test.ts create mode 100644 apps/envited.ascs.digital/common/utils/utils.ts diff --git a/apps/envited.ascs.digital/.env.dev b/apps/envited.ascs.digital/.env.dev new file mode 100644 index 00000000..8a34b6cf --- /dev/null +++ b/apps/envited.ascs.digital/.env.dev @@ -0,0 +1,17 @@ +ENV='dev' +PLATFORM='node' +POSTGRES_URL='postgres://admin:123456@localhost:5436/envited' +POSTGRES_HOST='localhost' +POSTGRES_PORT='5436' +POSTGRES_DATABASE_NAME='envited' +POSTGRES_DATABASE_USER='admin' +POSTGRES_DATABASE_PASSWORD='123456' + +NEXT_PUBLIC_APP_URL='https://localhost:4200' +APP_URL='https://localhost:4200' + +ROLE_TO_ASSUME='' +AWS_ACCESS_KEY_ID='' +AWS_SECRET_ACCESS_KEY='' +AURORA_SECRET_ARN='' +AURORA_RESOURCE_ARN='' diff --git a/apps/envited.ascs.digital/app/api/hello/route.ts b/apps/envited.ascs.digital/app/api/hello/route.ts index 10d1578a..58bc3a90 100644 --- a/apps/envited.ascs.digital/app/api/hello/route.ts +++ b/apps/envited.ascs.digital/app/api/hello/route.ts @@ -4,6 +4,7 @@ export async function GET(request: Request) { try { const connection = await db() const tables = await connection.fetchTables() + return Response.json(tables) } catch (error) { console.log('error', error) diff --git a/apps/envited.ascs.digital/common/constants/index.ts b/apps/envited.ascs.digital/common/constants/index.ts index e9559e2c..72d91333 100644 --- a/apps/envited.ascs.digital/common/constants/index.ts +++ b/apps/envited.ascs.digital/common/constants/index.ts @@ -1 +1,2 @@ export { ERRORS } from './errors' +export { RESPONSES } from './response' diff --git a/apps/envited.ascs.digital/common/constants/response.ts b/apps/envited.ascs.digital/common/constants/response.ts new file mode 100644 index 00000000..2dba1525 --- /dev/null +++ b/apps/envited.ascs.digital/common/constants/response.ts @@ -0,0 +1,6 @@ +export const RESPONSES = { + BAD_REQUEST: 'Bad request', + INTERNAL_SERVER_ERROR: 'Internal server error', + NO_CONTENT: 'No content', + NOT_FOUND: 'Not found', +} diff --git a/apps/envited.ascs.digital/common/utils/index.ts b/apps/envited.ascs.digital/common/utils/index.ts new file mode 100644 index 00000000..7039fc21 --- /dev/null +++ b/apps/envited.ascs.digital/common/utils/index.ts @@ -0,0 +1 @@ +export { ok, badRequest, noContent, notFound } from './utils' diff --git a/apps/envited.ascs.digital/common/utils/utils.test.ts b/apps/envited.ascs.digital/common/utils/utils.test.ts new file mode 100644 index 00000000..ae041a6b --- /dev/null +++ b/apps/envited.ascs.digital/common/utils/utils.test.ts @@ -0,0 +1,36 @@ +import { NextResponse } from 'next/server' + +import * as SUT from './utils' + +jest.mock('next/server', () => ({ + NextResponse: { + json: (x: string) => x, + }, +})) + +describe('common/utils', () => { + describe('ok', () => { + it.each([ + ['Message', 'Message'], + [[], []], + ])('should return as expected', (message, expected) => { + // given ... we have a ok response + // when ... we want to get the result + const result = SUT.ok(NextResponse as any)(message) + + // then ... it returns the result as expected + expect(result).toEqual(expected) + }) + }) + + describe('badRequest', () => { + it('should return as expected', () => { + // given ... we have a ok response + // when ... we want to get the result + const result = SUT.badRequest(NextResponse as any) + + // then ... it returns the result as expected + expect(result).toEqual({ status: 403, statusText: 'Bad request' }) + }) + }) +}) diff --git a/apps/envited.ascs.digital/common/utils/utils.ts b/apps/envited.ascs.digital/common/utils/utils.ts new file mode 100644 index 00000000..0ff25e35 --- /dev/null +++ b/apps/envited.ascs.digital/common/utils/utils.ts @@ -0,0 +1,13 @@ +import { NextResponse } from 'next/server' + +import { RESPONSES } from '../constants' + +export const ok = (response: NextResponse) => result => response.json(result) + +export const badRequest = (response: NextResponse) => response.json({ status: 403, statusText: RESPONSES.BAD_REQUEST }) + +export const internalServerError = () => Response.json({ status: 500, statusText: RESPONSES.INTERNAL_SERVER_ERROR }) + +export const notFound = () => Response.json({ status: 404, statusText: RESPONSES.NOT_FOUND }) + +export const noContent = () => Response.json({ status: 205, statusText: RESPONSES.NO_CONTENT }) From 19ff2bf2261395a164ba8f4d0543caeb96c04de6 Mon Sep 17 00:00:00 2001 From: Roy Scheeren Date: Mon, 8 Jan 2024 17:43:02 +0100 Subject: [PATCH 2/3] feat(envited.ascs.digital): refactor Signed-off-by: Roy Scheeren --- .../app/api/hello/route.ts | 3 +- .../common/constants/response.ts | 20 ++++-- .../common/utils/index.ts | 2 +- .../common/utils/utils.test.ts | 68 ++++++++++++++----- .../common/utils/utils.ts | 12 ++-- package-lock.json | 9 ++- package.json | 3 +- 7 files changed, 85 insertions(+), 32 deletions(-) diff --git a/apps/envited.ascs.digital/app/api/hello/route.ts b/apps/envited.ascs.digital/app/api/hello/route.ts index 58bc3a90..52c7cefd 100644 --- a/apps/envited.ascs.digital/app/api/hello/route.ts +++ b/apps/envited.ascs.digital/app/api/hello/route.ts @@ -1,3 +1,4 @@ +import { ok } from '../../../common/utils' import { db } from '../../../common/database/queries' export async function GET(request: Request) { @@ -5,7 +6,7 @@ export async function GET(request: Request) { 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) diff --git a/apps/envited.ascs.digital/common/constants/response.ts b/apps/envited.ascs.digital/common/constants/response.ts index 2dba1525..ffa23364 100644 --- a/apps/envited.ascs.digital/common/constants/response.ts +++ b/apps/envited.ascs.digital/common/constants/response.ts @@ -1,6 +1,18 @@ export const RESPONSES = { - BAD_REQUEST: 'Bad request', - INTERNAL_SERVER_ERROR: 'Internal server error', - NO_CONTENT: 'No content', - NOT_FOUND: 'Not found', + 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', + }, } diff --git a/apps/envited.ascs.digital/common/utils/index.ts b/apps/envited.ascs.digital/common/utils/index.ts index 7039fc21..2e4055dc 100644 --- a/apps/envited.ascs.digital/common/utils/index.ts +++ b/apps/envited.ascs.digital/common/utils/index.ts @@ -1 +1 @@ -export { ok, badRequest, noContent, notFound } from './utils' +export { ok, badRequest, noContent, notFound, internalServerError } from './utils' diff --git a/apps/envited.ascs.digital/common/utils/utils.test.ts b/apps/envited.ascs.digital/common/utils/utils.test.ts index ae041a6b..d5775ce9 100644 --- a/apps/envited.ascs.digital/common/utils/utils.test.ts +++ b/apps/envited.ascs.digital/common/utils/utils.test.ts @@ -1,36 +1,70 @@ -import { NextResponse } from 'next/server' +/** + * @jest-environment node + */ +import { RESPONSES } from '../constants'; import * as SUT from './utils' -jest.mock('next/server', () => ({ - NextResponse: { - json: (x: string) => x, - }, -})) - 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', (message, expected) => { - // given ... we have a ok response + ])('should return as expected', async (message, expected) => { // when ... we want to get the result - const result = SUT.ok(NextResponse as any)(message) - // 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 as expected', () => { - // given ... we have a ok response - // when ... we want to get the result - const result = SUT.badRequest(NextResponse as any) + 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() - // then ... it returns the result as expected - expect(result).toEqual({ status: 403, statusText: 'Bad request' }) + 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) }) }) }) diff --git a/apps/envited.ascs.digital/common/utils/utils.ts b/apps/envited.ascs.digital/common/utils/utils.ts index 0ff25e35..a5bafee0 100644 --- a/apps/envited.ascs.digital/common/utils/utils.ts +++ b/apps/envited.ascs.digital/common/utils/utils.ts @@ -1,13 +1,11 @@ -import { NextResponse } from 'next/server' - import { RESPONSES } from '../constants' -export const ok = (response: NextResponse) => result => response.json(result) +export const ok = (data: any) => Response.json(data) -export const badRequest = (response: NextResponse) => response.json({ status: 403, statusText: RESPONSES.BAD_REQUEST }) +export const badRequest = () => Response.json(null, { status: RESPONSES.badRequest.status, statusText: RESPONSES.badRequest.statusText }) -export const internalServerError = () => Response.json({ status: 500, statusText: RESPONSES.INTERNAL_SERVER_ERROR }) +export const internalServerError = () => Response.json(null, { status: RESPONSES.internalServerError.status, statusText: RESPONSES.internalServerError.statusText }) -export const notFound = () => Response.json({ status: 404, statusText: RESPONSES.NOT_FOUND }) +export const notFound = () => Response.json(null, { status: RESPONSES.notFound.status, statusText: RESPONSES.notFound.statusText}) -export const noContent = () => Response.json({ status: 205, statusText: RESPONSES.NO_CONTENT }) +export const noContent = () => new Response(null, { status: RESPONSES.noContent.status, statusText: RESPONSES.noContent.statusText }) diff --git a/package-lock.json b/package-lock.json index f7473996..dfcfab1c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -82,7 +82,8 @@ "ts-jest": "^29.1.0", "ts-node": "10.9.1", "typescript": "~5.2.2", - "url-loader": "^4.1.1" + "url-loader": "^4.1.1", + "whatwg-fetch": "^3.6.20" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -36706,6 +36707,12 @@ "node": ">=12" } }, + "node_modules/whatwg-fetch": { + "version": "3.6.20", + "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.6.20.tgz", + "integrity": "sha512-EqhiFU6daOA8kpjOWTL0olhVOF3i7OrFzSYiGsEMB8GcXS+RrzauAERX65xMeNWVqxA6HXH2m69Z9LaKKdisfg==", + "dev": true + }, "node_modules/whatwg-mimetype": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-3.0.0.tgz", diff --git a/package.json b/package.json index 0a1b685e..d7cfbfc3 100644 --- a/package.json +++ b/package.json @@ -78,6 +78,7 @@ "ts-jest": "^29.1.0", "ts-node": "10.9.1", "typescript": "~5.2.2", - "url-loader": "^4.1.1" + "url-loader": "^4.1.1", + "whatwg-fetch": "^3.6.20" } } From 5018f518136e9e385d33224c4e35f067a823d87c Mon Sep 17 00:00:00 2001 From: Roy Scheeren Date: Mon, 8 Jan 2024 17:47:24 +0100 Subject: [PATCH 3/3] feat(envited.ascs.digital): remove unused Signed-off-by: Roy Scheeren --- apps/envited.ascs.digital/.env.dev | 17 ----------------- package-lock.json | 10 +--------- package.json | 4 +--- 3 files changed, 2 insertions(+), 29 deletions(-) delete mode 100644 apps/envited.ascs.digital/.env.dev diff --git a/apps/envited.ascs.digital/.env.dev b/apps/envited.ascs.digital/.env.dev deleted file mode 100644 index 8a34b6cf..00000000 --- a/apps/envited.ascs.digital/.env.dev +++ /dev/null @@ -1,17 +0,0 @@ -ENV='dev' -PLATFORM='node' -POSTGRES_URL='postgres://admin:123456@localhost:5436/envited' -POSTGRES_HOST='localhost' -POSTGRES_PORT='5436' -POSTGRES_DATABASE_NAME='envited' -POSTGRES_DATABASE_USER='admin' -POSTGRES_DATABASE_PASSWORD='123456' - -NEXT_PUBLIC_APP_URL='https://localhost:4200' -APP_URL='https://localhost:4200' - -ROLE_TO_ASSUME='' -AWS_ACCESS_KEY_ID='' -AWS_SECRET_ACCESS_KEY='' -AURORA_SECRET_ARN='' -AURORA_RESOURCE_ARN='' diff --git a/package-lock.json b/package-lock.json index dfcfab1c..66e24f52 100644 --- a/package-lock.json +++ b/package-lock.json @@ -81,9 +81,7 @@ "tailwindcss": "^3.3.6", "ts-jest": "^29.1.0", "ts-node": "10.9.1", - "typescript": "~5.2.2", - "url-loader": "^4.1.1", - "whatwg-fetch": "^3.6.20" + "typescript": "~5.2.2" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -36707,12 +36705,6 @@ "node": ">=12" } }, - "node_modules/whatwg-fetch": { - "version": "3.6.20", - "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.6.20.tgz", - "integrity": "sha512-EqhiFU6daOA8kpjOWTL0olhVOF3i7OrFzSYiGsEMB8GcXS+RrzauAERX65xMeNWVqxA6HXH2m69Z9LaKKdisfg==", - "dev": true - }, "node_modules/whatwg-mimetype": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-3.0.0.tgz", diff --git a/package.json b/package.json index d7cfbfc3..ba26cf78 100644 --- a/package.json +++ b/package.json @@ -77,8 +77,6 @@ "tailwindcss": "^3.3.6", "ts-jest": "^29.1.0", "ts-node": "10.9.1", - "typescript": "~5.2.2", - "url-loader": "^4.1.1", - "whatwg-fetch": "^3.6.20" + "typescript": "~5.2.2" } }