OpenAPITools / OpenAPITools/openapi-generator

[REQ] Function-based Typescript client for better tree-shaking

Open
#12,283 3 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

The current Typescript generators are all class-based. There's a base class, and route-based classes that inherit from it and contain action-based functions. This is fine when working with bigger, more monolithic applications, since all the functions will surely be used somewhere.

It doesn't lend itself well to an architecture where components are smaller, and perhaps lazy-loaded. In such a setup, the project would benefit from a more granular approach, where singular functions can be imported.

Those functions would of course need some shared functionality — currently present in the base class — but that can be solved by importing that setup function

Suggest a fix/enhancement

I did try to implement a template that does it for NSwag studio, with... none to moderate success, so I can't provide a ready-made solution. However, the way I envision the end result to be, would be something along the lines of

import * as setup from '../setup';
import type { UserDto, UserProfile, UserCard } from '../models/user'

export const getUserProfile = async (id: number): Promise<UserProfile> => {
    const _client = setup();
    // client...
    return result;
}

export const getUserCard = async (id: number): Promise<UserCard> => {
    const _client = setup();
    // client...
    return result;
}

export const createUser = async (user: UserDto): Promise<boolean> => {
    const _client = setup();
    // client...
    return result;
}

so that in my component (or wherever else) I can just do

import { UserProfile } from './client/models/user';
import { getUserProfile } from '/clients/clients/user';

const profile = await getUserProfile(id);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file, test, or generator entry point is named. First locate the current TypeScript generators, their base-class and route/action templates, and the attempted NSwag Studio template; compare their generated output with the function-based example. Done means a supported function-based client can expose independently importable operations while retaining shared setup functionality and working generated types.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.