util.js 884 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. module.exports = {
  2. loadFixture,
  3. fixtureToInstace,
  4. getInstance
  5. }
  6. const fetch = require('node-fetch')
  7. const merge = require('lodash/merge')
  8. const GitHub = require('../')
  9. function loadFixture (scenario) {
  10. return fetch('http://localhost:3000/fixtures', {
  11. method: 'post',
  12. headers: {'content-type': 'application/json'},
  13. body: JSON.stringify({scenario})
  14. })
  15. .then(response => response.json())
  16. .catch(error => {
  17. if (error.code === 'ECONNREFUSED') {
  18. throw new Error('Fixtures server could not be reached. Make sure to start it with "npm run start-fixtures-server"')
  19. }
  20. throw error
  21. })
  22. }
  23. function fixtureToInstace ({url}, options) {
  24. return new GitHub(merge(options, {
  25. baseUrl: url
  26. }))
  27. }
  28. function getInstance (scenario, options) {
  29. return loadFixture(scenario)
  30. .then(fixture => fixtureToInstace(fixture, options))
  31. }