with-defaults.js 893 B

12345678910111213141516171819202122
  1. import fetchWrapper from "./fetch-wrapper";
  2. export default function withDefaults(oldEndpoint, newDefaults) {
  3. const endpoint = oldEndpoint.defaults(newDefaults);
  4. const newApi = function (route, parameters) {
  5. const endpointOptions = endpoint.merge(route, parameters);
  6. if (!endpointOptions.request || !endpointOptions.request.hook) {
  7. return fetchWrapper(endpoint.parse(endpointOptions));
  8. }
  9. const request = (route, parameters) => {
  10. return fetchWrapper(endpoint.parse(endpoint.merge(route, parameters)));
  11. };
  12. Object.assign(request, {
  13. endpoint,
  14. defaults: withDefaults.bind(null, endpoint),
  15. });
  16. return endpointOptions.request.hook(request, endpointOptions);
  17. };
  18. return Object.assign(newApi, {
  19. endpoint,
  20. defaults: withDefaults.bind(null, endpoint),
  21. });
  22. }