Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/request / src / adapters / mock.ts
Size: Mime:
import { isFunction, fromMapToObj } from 'exotic'
import { OneRequestStore } from '../typings'

// from config?
const mockUrl = process.env.BASE_URL || 'http://localhost:4000'

export function toUrl(url: string = '') {
  // for now, autofix it to include the baseUrl
  if (!url.includes('http')) {
    return mockUrl + url
  } else {
    return url
  }
}

export function adaptRequest(store: OneRequestStore) {
  const {
    params = '',
    method,
    tapParams,
    tapResponse,
    ...remaining
  } = fromMapToObj(store)
  const url = toUrl(remaining.url)
  const nock = require('nock')

  const handleGet = (urlRequested: string) => {
    // `api/${url}`
    const doesRequestIncludeUrl =
      urlRequested.includes(url) || url.includes(urlRequested)
    // console.log('[oneRequest] asMock - handleGet', { url, urlRequested, doesRequestIncludeUrl })
    return doesRequestIncludeUrl
  }

  const data = isFunction(tapResponse)
    ? tapResponse()
    : { '@@EMPTY_RESPONSE': true }

  if (method === 'POST') {
    return nock(mockUrl)
      .post(url)
      .query(params)
      .reply(201, data)
  } else {
    return nock(mockUrl)
      .get(handleGet)
      .query(params)
      .reply(200, data)
  }
}