Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View vahidvdn's full-sized avatar
🏠
Working from home

Vahid Najafi vahidvdn

🏠
Working from home
  • Berlin, Germany
View GitHub Profile
export interface Type<T = any> {
new (...args: any[]): T;
}
class Container {
public dependencies = [];
public init(deps: any[]) {
deps.map((target) => {
const isInjectable = Reflect.getMetadata('injectable', target);
@vahidvdn
vahidvdn / dependency-injection.ts
Created February 20, 2024 15:50
Implement simple dependency injection with Reflect
const reflectObj = {};
function Injectable() {
return function (target: any) {
console.log('Injectable');
Reflect.defineMetadata('injectable', true, target);
reflectObj[target] = true;
};
}
@vahidvdn
vahidvdn / public-api.decorator.ts
Last active February 21, 2024 15:54
Custom Public Decorator instead of nestjs
import { SetMetadata } from '@nestjs/common';
export const IS_PUBLIC_KEY = 'isPublic';
export const Public0 = () => SetMetadata(IS_PUBLIC_KEY, true); // nestjs way
export const reflectObj = {};
// custome way
export function Public() {
return function (
@Module({
providers: [
{
provide: 'MY_TIMEOUT',
useFactory: () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true)
}, 10000);
})
import { ICommand } from './command.interface';
export interface ICommandHandler<TCommand extends ICommand = any, TResult = any> {
execute(command: TCommand): Promise<TResult>;
}
@CommandHandler(AddProductToCartCommand)
export class AddProductToCartCommandHandler implements ICommandHandler<AddProductToCartCommand> {
constructor(private eventBus: EventBus) {}
async execute(command: AddProductToCartCommand): Promise<void> {
// ...
}
}
import { delay } from 'redux-saga';
import { takeEvery, put } from 'redux-saga/effects';
function* ageUpAsync() {
yield delay(4000);
yield put({ type: 'AGE_UP_ASYNC', value: 1 })
}
export function* watchAgeUp() {
yield takeEvery('AGE_UP', ageUpAsync);
// with thunk, instead of returning an object, we can return a function (for async tasks)
const callHttp = () => {
return function (dispatch) {
dispatch(beforeCall())
axios.get()
.then((res) => {
dispatch(afterSuccess(res.data))
})
.catch((error) => {
dispatch(afterFail(error.message))
const mapDispatchToProps = (dispatch) => {
return {
deletePost: (id) => dispatch({type: 'DELETE_POST', id: id}),
addPost: (post) => dispatch({type: 'ADD_POST', post: post})
}
}
import { CashifyOptionsFactory } from 'nestjs-cashify';
@Injectable()
export class CashifyConfigService implements CashifyOptionsFactory {
constructor(private configService: ConfigService) {}
createCashifyOptions() {
const rates = {
GBP: 0.92,
EUR: 1.00,