Intro
Motivation
Simple and straightforward mock/replacement/modification of your functions.
Your app is house and your code is bricks.
Simply register some bricks in Brickyard
and have optional possibility do this dynamically
when and where you want.
Problem example:
- Suppose you have function
hi()
- Suppose it make api call but you don't need it for now, want to test etc.
- And of course you don't want this only once, sometimes, like exception, not in prod
- But yes, you don't want make code modifications in code every time you want to skip original functionality
Solution with Brickyard
:
one of possible examples
- in
brickyard.enroll.ts
register youhi()
function:
import { hi } from './hi'
import { Brickyard } from '@nik-kita/brickyard'
export const bricks = Brickyard.init().enroll({ hi });
- in your
main.ts
instead of import directlyhi()
function => importbricks
:
import { bricks } from './brickyard.enroll.ts'
briks.hi()
-
For now your code is working without any changes!
bricks.hi()
exactly the same ashi()
! -
But create
brickyard.interceptor.ts
:
import { Brickyard } from '@nik-kita/brickyard'
export const { complete } = Brickyard.pre_init().intecept('hi', { fn: () => console.log('hi!, i was intercepted!') });
- And make it to be imprted before
brickyard.enroll.ts
:
import { hi } from './hi'
import { Brickyard } from '@nik-kita/brickyard'
import { complete } from './brickyard.interceptor'
export const bricks = Brickyard.init(complete()).enroll({ hi });
-
Now your
hi()
is intecepted! -
How to make such manipulation silent?
- honestly speaking this part is need some beautification... but:
- add
brickyard.interceptor.ts
to your.gitignore
file - yes, you should create it manually or will have error with imports... that is not really beauty
// brickyard.interceptor.ts
import { Brickyard } from '@nik-kita/brickyard'
// little strange... but it is example of aka empty interceptor
export const { complete } = Brickyard
.pre_init('_no_such_function_existed', { fn: () => {} }); - however you can then make modifications in it but for git your code still be the same!