This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed linting, acceptance tests, added user types
- Loading branch information
Frederic Charette
authored and
Frederic Charette
committed
Sep 23, 2021
1 parent
143e5d6
commit c82ed36
Showing
17 changed files
with
129 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { randomUUID } from 'crypto'; | ||
|
||
export default function context(req, res, next) { | ||
const requestId = req.headers['x-request-id'] || randomUUID(); | ||
req.id = requestId; | ||
res.setHeader('x-request-id', requestId); | ||
const requestId = req.headers['x-request-id'] || randomUUID(); | ||
req.id = requestId; | ||
res.setHeader('x-request-id', requestId); | ||
|
||
next(); | ||
next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
export default function security(req, res, next) { | ||
res.removeHeader('X-Powered-By'); | ||
if (decodeURIComponent(req.url).includes('<script>')) { | ||
return res.status(406).end('Illegal component in URI'); | ||
} | ||
next(); | ||
res.removeHeader('X-Powered-By'); | ||
|
||
if (decodeURIComponent(req.url).includes('<script>')) { | ||
return res.status(406).end('Illegal component in URI'); | ||
} | ||
|
||
next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { query } from '@nc/utils/db'; | ||
|
||
export function readUser(userId) { | ||
return query(`SELECT * FROM users WHERE id = $1`, [userId]) | ||
.then(response => response.rows?.[0]); | ||
return query('SELECT * FROM users WHERE id = $1', [userId]) | ||
.then((response) => response.rows?.[0]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
import { User } from './types'; | ||
|
||
const publicFields = ['first_name', 'last_name', 'company_name']; | ||
|
||
export function capitalize(word) { | ||
const str = `${word}`; | ||
return str[0].toUpperCase() + str.slice(1); | ||
const str = `${word}`; | ||
return str[0].toUpperCase() + str.slice(1); | ||
} | ||
|
||
export function secureTrim(user) { | ||
return JSON.stringify(user, publicFields); | ||
export function secureTrim(user: User): string { | ||
return JSON.stringify(user, publicFields); | ||
} | ||
|
||
export function format(rawUser) { | ||
return { | ||
id: rawUser.id, | ||
first_name: capitalize(rawUser.first_name), | ||
last_name: capitalize(rawUser.last_name), | ||
company_name: rawUser.company_name, | ||
ssn: rawUser.ssn, | ||
}; | ||
export function format(rawUser): User { | ||
return { | ||
id: rawUser.id, | ||
first_name: capitalize(rawUser.first_name), | ||
last_name: capitalize(rawUser.last_name), | ||
company_name: rawUser.company_name, | ||
ssn: rawUser.ssn, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
import { BadRequest, InternalError, NotFound } from '@nc/utils/errors'; | ||
import { format } from './formatter'; | ||
import { readUser } from './data/db-user'; | ||
import { to } from '@nc/utils/async'; | ||
import { format } from './formatter'; | ||
import { User } from './types'; | ||
import { BadRequest, InternalError, NotFound } from '@nc/utils/errors'; | ||
|
||
export async function getUserDetails(userId) { | ||
if (!userId) { | ||
throw BadRequest('userId property is missing.'); | ||
} | ||
export async function getUserDetails(userId): Promise<User> { | ||
if (!userId) { | ||
throw BadRequest('userId property is missing.'); | ||
} | ||
|
||
const [dbError, rawUser] = await to(readUser(userId)); | ||
const [dbError, rawUser] = await to(readUser(userId)); | ||
|
||
if (dbError) { | ||
throw InternalError(`Error fetching data from the DB: ${dbError.message}`); | ||
} | ||
if (dbError) { | ||
throw InternalError(`Error fetching data from the DB: ${dbError.message}`); | ||
} | ||
|
||
if (!rawUser) { | ||
throw NotFound(`Could not find user with id ${userId}`); | ||
} | ||
if (!rawUser) { | ||
throw NotFound(`Could not find user with id ${userId}`); | ||
} | ||
|
||
return format(rawUser); | ||
return format(rawUser); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import config from 'config'; | ||
import { Client } from 'pg'; | ||
import config from 'config'; | ||
|
||
let db; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.