Skip to content

Instantly share code, notes, and snippets.

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

Dmytro Shchehlov DScheglov

🏠
Working from home
View GitHub Profile
@DScheglov
DScheglov / props-resolver.ts
Created March 3, 2023 18:51
propsResolver
type AnyResolvers = {
[prop in PropertyKey]: (params: any) => any
}
type Resolved<R extends AnyResolvers> = {
[prop in keyof R]: Awaited<ReturnType<R[prop]>>
}
type UnionToIntersection<U> = (U extends any ? (a: U) => any : never) extends ((b: infer A) => any) ? A : never;
const startsWith = (letter: string) =>
(word: string) =>
word[0] === letter;
const isAny = () =>
true;
const lastLetterOf = (word: string): string =>
word[word.length - 1];
const isThenable = value => typeof value?.then === "function";
const wrapHandler = (handler, resolve, reject) => resultOrError => {
try {
const handledResult = handler(resultOrError);
if (isThenable(handledResult)) {
handledResult.then(resolve, reject);
} else {
resolve(handledResult);
@DScheglov
DScheglov / CodingService.java
Last active January 20, 2021 08:05
factory-method
public abstract class CodingService {
abstract Developer createDeveloper();
public void writeCode() {
Developer dev = createDeveloper();
String code = dev.writeCode();
System.out.println(code);
}
}
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Observable } from 'rxjs';
import { flatMap } from 'rxjs/operators';
import { fromFunctionCall } from '../common/utils';
@Injectable()
export class LoginInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const req = context.switchToHttp().getRequest();
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Observable, Observer } from 'rxjs';
import { flatMap } from 'rxjs/operators';
const nextAndComplete = <T>(observer: Observer<T>, value?: T): void => {
observer.next(result);
observer.complete();
};
@Injectable()
import { createParamDecorator, ExecutionContext, SetMetadata } from '@nestjs/common';
export const Passport = createParamDecorator((data: unknown, ctx: ExecutionContext) => {
const req = ctx.switchToHttp().getRequest();
const logIn = <U>(user: U): Promise<U> => new Promise(
(resolve, reject) => req.logIn(user, err => err ? reject(err) : resolve(user))
);
return { logIn };
})
const message =
i % 3 === 0 && i % 5 === 0 ? (
"fizzbuzz"
) : i % 3 === 0 ? (
"fizz"
) : i % 5 === 0 ? (
"buzz"
) : i % 7 === 0 ? (
"foo"
) : (
interface IServices {
logger: ILogger,
dataAccessService: IDataAccessService,
ecommerceService: IECommerceService,
}
class ServiceLocator implements IServices {
private _logger: ILogger | null = null;
private _dataAccessService: IDataAccessService | null = null;
private _ecommerceService: IECommerceService | null = null;
@DScheglov
DScheglov / di.js
Last active October 21, 2020 07:00
import React from 'react';
import { compose } from "...somewhere...";
export const diContext = React.createContext({
container: {},
run: () => {},
));
export const { Consumer } = diContext;