Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

56
Views
How to abstract environmental JS class implementations

I have an abstraction of an adapter. Through an environmeht variable I control which implementation is used. I am not satisfied with this approach, since the mock variant is imported in production code. I would like to have something like in Symfony, where implementation of abstractions can be defined in environmental service configurations.

Currently I have following abstraction:

import HttpERPAdapter from "./HttpERPAdapter";
import MockERPAdapter from "./MockERPAdapter"; // todo, find an abstraction solution to not deploy this in production
import httpClient from "common/HttpClient";

const create = () => {
    if (process.env.REACT_APP_ERP_API_URL === 'mock') {
        return new MockERPAdapter();
    }
    return new HttpERPAdapter(httpClient);
}

const erpAdapter = create();
export default erpAdapter;
  • The mock variant: Mainly used when developing GUI when I have not to rely on real api data, for faster development.
  • The productive variant: For for developing with real api data, and for the productive build

I am sure that a compilation in a production build will contain the mock class, this is also what I want to prevent.
In general I want that the productive code knows nothing about mocks/fake-services.

Any ideas on how to separate the mock implementation from the productive code?

7 months ago ยท Juan Pablo Isaza
Answer question
Find remote jobs