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
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 }; | |
}) |
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
const message = | |
i % 3 === 0 && i % 5 === 0 ? ( | |
"fizzbuzz" | |
) : i % 3 === 0 ? ( | |
"fizz" | |
) : i % 5 === 0 ? ( | |
"buzz" | |
) : i % 7 === 0 ? ( | |
"foo" | |
) : ( |
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
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; |
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
import React from 'react'; | |
import { compose } from "...somewhere..."; | |
export const diContext = React.createContext({ | |
container: {}, | |
run: () => {}, | |
)); | |
export const { Consumer } = diContext; |
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
const getFrom = container => prop => container[prop]; | |
const createInstance = (factory, deps) => | |
deps.some(isPromise) | |
? Promise.all(deps).then(resolvedDeps => factory(...resolvedDeps)) | |
: factory(...deps); | |
export const asFactory = (factory, inject = factory.inject) => container => | |
Array.isArray(inject) | |
? createInstance(factory, inject.map(getFrom(container))) |
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
const getFrom = container => prop => container[prop]; | |
const createInstance = (factory, deps) => | |
deps.some(isPromise) | |
? Promise.all(deps).then(resolvedDeps => factory(...resolvedDeps)) | |
: factory(...deps); | |
export const asFactory = factory => container => | |
Array.isArray(factory.inject) | |
? createInstance(factory, factory.inject.map(getFrom(container))) |
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
import { multiple } from "true-di"; | |
export const asValue = <T>(value: T) => <C>(dependencies: C) => value; | |
export const asFactory = <D, T>(factory: (dependencies: D) => T) => factory; | |
asFactory.multi = multiple; | |
type ClassConstructor<D extends {}, T> = { | |
new (dependencies: D): T |
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
type ComparatorFn<T> = (a: T, b: T) => number; | |
type CasterFn<T> = (value: T) => number; | |
export const reverse = <T>(compare: ComparatorFn<T>): ComparatorFn<T> => (a: T, b: T) => -compare(a, b); | |
const combineComparators = <T>(compareA: ComparatorFn<T>, compareB: ComparatorFn<T>): ComparatorFn<T> => | |
(a: T, b: T) => compareA(a, b) || compareB(a, b); | |
const voidComparator = <T>(a: T, b: T): number => 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
import Express from 'express'; | |
import diContainer from 'true-di'; | |
import createDataAccessService from '../services/data-access-service'; | |
import createAuthorizationService from '../services/authorization-service'; | |
import { IApolloContext } from '../interfaces'; | |
const createContext = (dbConnection: Promise<Connection>) => ({ req }: { req: Express.Context }) => | |
dbConnection.then( | |
conn => diContainer<IApolloContext>({ | |
dataAccessService: () => createDataAccessService(conn), |
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
/** @type {(ms: number) => Promise<void>} */ | |
const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); | |
export default delay; |