it-at-m / refarch-templates

All templates for the Reference Architecture of it@M
MIT License
8 stars 4 forks source link

[Feature] Extend openapi client generation #620

Open langehm opened 11 hours ago

langehm commented 11 hours ago

Relevant template

No response

Problem description (optional)

After closing this issue the api will be generated automatically. But using it in the client still leaves some room for improvement. Futhermore there exists not generator for vue.

Desired solution

Generate some api function with vue functionality (reactivity - see example below). This example exposes reactive variables to the component which simplifies the implementation a lot in VueJS. The below code can be used to manually create those function for each api call - this is probably doable for a few request but not if this needs to be done for the whole specification.

Example for some generic vue implementation for calling the api:

export function useApiCall<TRequest, TResponse = void>(
  apiMethod: (params: TRequest) => Promise<TResponse>
) {
  const loadingInternal = ref(false);
  const errorInternal = ref(false);
  const dataInternal = ref<TResponse | null>(null);

  const loading = readonly(loadingInternal);
  const error = readonly(errorInternal);
  const data = readonly(dataInternal);

  const call = (params: TRequest): Promise<TResponse | boolean> => {
    loadingInternal.value = true;
    errorInternal.value = false;

    return apiMethod(params)
      .then((data) => (dataInternal.value = data))
      .catch(() => (errorInternal.value = true))
      .finally(() => (loadingInternal.value = false));
  };

  return {
    loading,
    error,
    data,
    call,
  };
}

Considered alternatives (optional)

Alternative soultion would be to customize the openapi generator. This can be done with the provided .mustache files (example for the DefaultApi). Now there exist two ways to do this:

  1. use the templating mechanism to inject a custom template upon generating the files
  2. donate a contribution to the openapi-generator project with implementing a generator type for typescript-vue

Additional context (optional)

No response

No duplicate

Code of Conduct

langehm commented 11 hours ago

This issue depends on the previous openapi issue https://github.com/it-at-m/refarch-templates/issues/619