
package cz.habarta.typescript.generator.ext;

import cz.habarta.typescript.generator.Settings;
import cz.habarta.typescript.generator.emitter.Emitter;
import cz.habarta.typescript.generator.emitter.EmitterExtension;
import cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures;
import cz.habarta.typescript.generator.emitter.TsBeanModel;
import cz.habarta.typescript.generator.emitter.TsModel;
import cz.habarta.typescript.generator.util.Utils;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


public class AxiosClientExtension extends EmitterExtension {

    @Override
    public EmitterExtensionFeatures getFeatures() {
        final EmitterExtensionFeatures features = new EmitterExtensionFeatures();
        features.generatesRuntimeCode = true;
        features.generatesModuleCode = true;
        features.worksWithPackagesMappedToNamespaces = true;
        features.generatesJaxrsApplicationClient = true;
        features.restResponseType = "Promise<Axios.GenericAxiosResponse<R>>";
        features.restOptionsType = "<O>";
        features.npmPackageDependencies = Collections.singletonMap("axios", "0.19.0");
        return features;
    }

    @Override
    public void emitElements(Writer writer, Settings settings, boolean exportKeyword, TsModel model) {
        emitSharedPart(writer, settings);
        for (TsBeanModel bean : model.getBeans()) {
            if (bean.isJaxrsApplicationClientBean()) {
                final String clientName = bean.getName().getSimpleName();
                emitClient(writer, settings, exportKeyword, clientName);
            }
        }
    }

    private void emitSharedPart(Writer writer, Settings settings) {
        final List<String> template = Utils.readLines(getClass().getResourceAsStream("AxiosClientExtension-shared.template.ts"));
        Emitter.writeTemplate(writer, settings, template, null);
    }

    private void emitClient(Writer writer, Settings settings, boolean exportKeyword, String clientName) {
        final List<String> template = Utils.readLines(getClass().getResourceAsStream("AxiosClientExtension-client.template.ts"));
        final Map<String, String> replacements = new LinkedHashMap<>();
        replacements.put("\"", settings.quotes);
        replacements.put("/*export*/ ", exportKeyword ? "export " : "");
        replacements.put("$$RestApplicationClient$$", clientName);
        replacements.put("$$AxiosRestApplicationClient$$", "Axios" + clientName);
        Emitter.writeTemplate(writer, settings, template, replacements);
    }

}

@@ -40,16 +40,66 @@
 r.ext;%0A%0A
+import cz.habarta.typescript.generator.Extension;%0A
 import c
@@ -199,73 +199,8 @@
 er;%0A
-import cz.habarta.typescript.generator.emitter.EmitterExtension;%0A
 impo
@@ -543,17 +543,16 @@
 l.Map;%0A%0A
-%0A
 public c
@@ -586,23 +586,16 @@
 xtends E
-mitterE
 xtension
@@ -596,16 +596,384 @@
 ension %7B
+%0A%0A    public static final String CFG_AXIOS_VERSION = %22axiosVersion%22;%0A%0A    private String axiosVersion = %220.19.2%22;%0A%0A    @Override%0A    public void setConfiguration(Map%3CString, String%3E configuration) throws RuntimeException %7B%0A        if (configuration.containsKey(CFG_AXIOS_VERSION)) %7B%0A            this.axiosVersion = configuration.get(CFG_AXIOS_VERSION);%0A        %7D%0A    %7D
 %0A%0A    @O
@@ -1523,16 +1523,20 @@
 s%22, 
-%220.19.0%22
+axiosVersion
 );%0A 

Update axios to 0.19.2, make version configurable (#479)