TODO
TODO
This file contains hidden or 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
| // Companion code for "The Error Is a Value" (part 2 of the exceptions series). | |
| // `npm run build` typechecks this file (prebuild runs `tsc --noEmit`), so the | |
| // code printed in the post cannot drift from code that compiles. | |
| // | |
| // Trade-off vs the repo's coding standards, kept deliberately: this file | |
| // mirrors the published post 1:1 for series continuity. Boolean `ok` | |
| // discriminator (the exact type part 1 printed), plain tagged-object errors | |
| // rather than `Error` subclasses, the `never` exhaustiveness idiom rather than | |
| // `casesHandled`, and a hand-rolled `pipe` with `any` in its variadic runtime. | |
| // No JSDoc: the post is the documentation. |
This file contains hidden or 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
| type Address = { | |
| street: string; | |
| city: string; | |
| country: string; | |
| }; | |
| export type Order = { | |
| address: Address; | |
| }; |
This file contains hidden or 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
| try { | |
| const doc = await s3.getObject(); | |
| } catch (err) { | |
| // error is any or unknown | |
| if (err instanceof ObjectNotFoundError) { | |
| // TODO: handle error | |
| } | |
| } |
This file contains hidden or 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
| import { reduceMap } from './reduce-map'; | |
| describe('reduceMap', () => { | |
| const sumValueAndSubtractKey = (result: number, value: number, key: number) => result + value - key; | |
| it('should return initial value for empty map', () => { | |
| expect( | |
| reduceMap( | |
| new Map(), | |
| sumValueAndSubtractKey, |
This file contains hidden or 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
| import { Controller, ControllerOptions } from '@nestjs/common'; | |
| import { join } from 'path'; | |
| import { AuthenticationGuard, AccessControlGuard } from 'app/guards'; | |
| expose function AdminController(options: ControllerOptions) { | |
| return function (target: Function) { | |
| Controller(join('/admin', options.path))(target); | |
| AuthenticationGuard()(target); | |
| AccessControlGuard({ role: 'admin' })(target); | |
| }; |
This file contains hidden or 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
| @AdminController(options) { | |
| @Controller({ path: '/admin' + options.path }) | |
| @AuthenticationGuard() | |
| @AccessControlGuard({ role: 'admin' }) | |
| } |
This file contains hidden or 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
| import { Controller, Get } from '@nestjs/common'; | |
| import { AdminController, AccessControlGuard } from 'app/decorators'; | |
| @AdminController({ path: '/dashboard' }) | |
| export class AdminDashboardController { | |
| @Get() | |
| index(): string { | |
| return 'dashboard data for admin'; | |
| } | |
| } |
NewerOlder