Repository URL to install this package:
|
Version:
7.0.2 ▾
|
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)
}
}