import * as _ from "lodash" ; import { responseCreatorFactory } from "../generator" ; import { CreateResponse , IFaker , IListener , ISerializedRequest , ISerializedResponse , IUnmockOptions , ServiceStoreType , } from "../interfaces" ; import { IRandomNumberGenerator , randomNumberGenerator , } from "../random-number-generator" ; import { Service } from "../service" ; import { addFromNock , NockAPI , ServiceStore } from "../service/serviceStore" ; export interface IFakerConstructorArguments { listeners ? : IListener [ ] ; serviceStore : ServiceStore ; randomNumberGenerator ? : IRandomNumberGenerator ; optionalsProbability ? : 1.0 ; minItems ? : 0 ; } export interface IFakerOptions { randomNumberGenerator : IRandomNumberGenerator ; minItems : number ; optionalsProbability : number ; } const DEFAULT_OPTIONS = { isWhitelisted : ( __ ) => false , randomize : ( ) => true , log : ( __ ) => { } , } ; export default class UnmockFaker implements IFaker { public createResponse : CreateResponse ; /**
     * Add a new service to the faker using `nock` syntax.
     */ public readonly mock : NockAPI ; /**
     * Equivalent to `mock`, use that instead.
     */ public readonly nock : NockAPI ; public minItems : number ; public optionalsProbability : number ; public readonly randomNumberGenerator : IRandomNumberGenerator ; private readonly serviceStore : ServiceStore ; private readonly listeners : IListener [ ] ; param options public constructor ( { listeners , randomNumberGenerator : rng , serviceStore , optionalsProbability , minItems , } ) { this . listeners = listeners ? listeners : [ ] ; this . randomNumberGenerator = rng || randomNumberGenerator ( { } ) ; this . optionalsProbability = optionalsProbability || 1.0 ; this . minItems = minItems || 0 ; this . serviceStore = serviceStore ; this . createResponse = this . createResponseCreator ( ) ; this . nock = addFromNock ( this . serviceStore ) ; this . mock = addFromNock ( this . serviceStore ) ; } param options public setOptions ( options ) { this . createResponse = this . createResponseCreator ( options ) ; } param request throws returns public generate ( request ) { return this . createResponse ( request ) ; } /**
     * Services dictionary mapping service name to `Service` object.
     */ public get services ( ) : ServiceStoreType { return ( this . serviceStore && this . serviceStore . services ) || { } ; } param service public add ( service ) { this . serviceStore . add ( service ) ; } param service public update ( service ) { this . serviceStore . updateOrAddService ( service ) ; } /**
     * Reset the states of all services.
     */ public reset ( ) { this . serviceStore . resetServices ( ) ; } /**
     * Remove all services.
     */ public purge ( ) { this . serviceStore . removeAll ( ) ; } private createResponseCreator ( options ? ) { return responseCreatorFactory ( { listeners : this . listeners , options : options || DEFAULT_OPTIONS , fakerOptions : { randomNumberGenerator : this . randomNumberGenerator , minItems : this . minItems , optionalsProbability : this . optionalsProbability , } , store : this . serviceStore , } ) ; } }