-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2024-02-22: Supernatural: I’ll interrogate the code
- start code examples
- Loading branch information
Showing
7 changed files
with
117 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const customer: { dob?: Record<string, any> } = {}; | ||
|
||
// 1. Object property check | ||
{ | ||
// before | ||
if (typeof customer.dob !== 'undefined') { | ||
customer.dob.constructed = new Date().toLocaleDateString(); | ||
} | ||
|
||
// after | ||
if (customer.dob !== undefined) { | ||
customer.dob.constructed = new Date().toLocaleDateString(); | ||
} | ||
/* | ||
It used to be that `undefined` was a global variable that could be reassigned. | ||
Nowadays, it's impossible. Don't be superstitious, you shouldn't use bulky constructions with `typeof` checks | ||
*/ | ||
} | ||
|
||
// 2. Global variable check | ||
{ | ||
// before | ||
if (typeof customerDoB !== 'undefined') { | ||
customerDoB = new Date().toLocaleDateString() | ||
} | ||
// after | ||
// there is no "after" for this construction ¯\_(ツ)_/¯, thanks to TypeScript | ||
|
||
/* | ||
There was a case when we check the value of global variable, that might not be declared, and that causes an error. | ||
If you are not sure if the global variable exists, it's better check it explicitly (using `if ('varname' in global)`). | ||
Don't be superstitious, TypeScript highlights cases like this and protects you from accessing non-declared variables. | ||
*/ | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
declare function saveQuotes(quotes: any): void; | ||
|
||
// before | ||
{ | ||
const doQuotes = async () => | ||
fetch('http://localhost:3000') | ||
.then((quotes) => { | ||
saveQuotes(quotes) | ||
}) | ||
.catch((err) => { | ||
throw new Error(err); | ||
}); | ||
|
||
// When I see code like this, I imagine a developer who thinks "I bet I can throw the error much further than `fetch` does" | ||
} | ||
|
||
// after | ||
{ | ||
const doQuotes = async () => | ||
fetch('http://localhost:3000') | ||
.then((quotes) => { | ||
saveQuotes(quotes) | ||
}) | ||
|
||
/* | ||
Don't be superstitious, if you are not going to process the error somehow (log, enrich or trace it), don't catch-throw error | ||
*/ | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
// before | ||
class HTTPError extends Error { | ||
status: number; | ||
constructor(status: number, message: string | undefined = undefined) { | ||
super(message); | ||
this.status = status; | ||
} | ||
} | ||
|
||
/* | ||
Most likely, the developer who wrote this had just started using TypeScript and default assignment feature. | ||
That's why it's really important to know you job well :) | ||
*/ | ||
} | ||
|
||
{ | ||
// after | ||
class HTTPError extends Error { | ||
status: number; | ||
constructor(status: number, message?: string) { | ||
super(message); | ||
this.status = status; | ||
} | ||
} | ||
|
||
/* | ||
In TypeScript, there is much more elegant way to make the variable optional, using question mark before type assignment. | ||
And don't be superstitious, any variable in JavaScript (superset of which TypeScript is) is `undefined` by default. | ||
*/ | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
type MiddlewareFunction = (req: Record<string, any>, res: unknown, next: (a: any) => void) => void; | ||
declare function isTokenValid(token: any): Promise<boolean> | ||
export const authenticate : MiddlewareFunction = (req, res, next) : void => { | ||
try { | ||
const isValid = await isTokenValid(req.headers.Authorization); | ||
!isValid && next(new Error(401)); | ||
} catch (err) { | ||
next(new UnauthorizedError(err.message)); | ||
} | ||
}; |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const data = [0, 1, 2, 3]; | ||
|
||
function getAsyncData(key): Promise<{}> { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(data[key]); | ||
}, 1000) | ||
}) | ||
} | ||
|
||
await getAsyncData(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