Skip to content

feat: update worma example config and fix axios generic type - #177

Merged
czhlin merged 2 commits into
mainfrom
pr/update-worma-example-and-fix-axios-type
Jul 26, 2026
Merged

feat: update worma example config and fix axios generic type#177
czhlin merged 2 commits into
mainfrom
pr/update-worma-example-and-fix-axios-type

Conversation

@czhlin

@czhlin czhlin commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR refactors the worma TypeScript example configuration to showcase all supported HTTP client presets, and fixes a type inference bug in the axios service template where the response type was incorrectly mapped.


Changes

1. Refactor Example Config (examples/typescript/worma.config.ts)

Before After
Only 1 generator (alovaGlobals with absolute Windows path) 5 generators demonstrating all HTTP client presets

Each preset now has its own section with English comments explaining its purpose:

// ① alova function template
//   Generates standalone API functions, each exported separately
{
  output: 'src/api/alova',
  serverName: 'Alova Functional',
  plugins: [swagger('petstore.json'), alova()],
},

// ② alovaGlobals global template
//   Registers all APIs on a global object, usable without import
{
  output: 'src/api/alova-globals',
  serverName: 'Alova Globals',
  plugins: [swagger('petstore.json'), alovaGlobals({ global: 'MyApis' })],
},

// ③ axios template
//   Based on axios instance, automatically injects axios interceptors
{
  output: 'src/api/axios',
  serverName: 'Axios',
  plugins: [swagger('petstore.json'), axios()],
},

// ④ fetch template
//   Zero dependencies, based on native fetch, suitable for lightweight projects
{
  output: 'src/api/fetch',
  serverName: 'Fetch',
  plugins: [swagger('petstore.json'), fetch()],
},

// ⑤ ky template
//   Based on ky request library, auto JSON parsing and error handling
{
  output: 'src/api/ky',
  serverName: 'Ky',
  plugins: [swagger('petstore.json'), ky()],
},

Additional fixes:

  • Replaced hardcoded Windows absolute path (C:/Users/Administrator/Desktop/api-docs.json) with portable relative path (petstore.json)
  • Removed unused aiDoc import and plugin usage from the old configuration
  • Moved from Chinese to English comments for consistency

2. Fix Axios Generic Type (packages/worma/src/template/presets/axios/typescript/services/{tag}.ts.handlebars)

Problem

Generated axios service functions produced incorrect type inference. For example:

// Generated code had this type signature
const data = await getPets({ params: { limit: 10 } });
// data type was incorrectly inferred as AxiosRequestConfig instead of Pet[]

Root Cause

Axios v1 instance methods use a dual-generic signature:

axiosInstance<T = unknown, R = T, D = T>(config: AxiosRequestConfig<D>): Promise<R>;
  • T → request body type (first generic)
  • R → response data type (second generic)
  • If only one generic is provided, it maps to T (request body), not R (response)

The template was passing only one generic argument, causing the response type to be inferred as the request body type instead:

// Before — only one generic, incorrectly mapped
return axiosInstance<GetPetsResponse>({ ... });

Fix

- return axiosInstance<{{{name}}}Response>({
+ return axiosInstance<unknown, {{{name}}}Response>({

By explicitly passing unknown as the first generic (request body), the second generic correctly maps to the response type, restoring proper type inference:

// After — two generics, correctly mapped
return axiosInstance<unknown, GetPetsResponse>({ ... });

Impact

Aspect Detail
User-facing Regenerated axios services now have correctly typed responses
Breaking change No — purely a type-level fix
Affected files Only users who regenerate their axios-based API with this template
Compatibility Compatible with all axios versions using the dual-generic signature (v1.x)

Files Changed

 .changeset/fix-axios-generic-type.md                                               | 45 +++++++++++
 examples/typescript/worma.config.ts                                                | 46 ++++++++---
 .../presets/axios/typescript/services/{tag}.ts.handlebars                          |  2 +-
 3 files changed, 82 insertions(+), 12 deletions(-)

Testing

To verify the fix, regenerate an axios-based API service and confirm the response type is correctly inferred:

cd examples/typescript
npx worma  # or pnpm worma
# Check src/api/axios/services/*.ts — response types should now be correct

czhlin added 2 commits July 26, 2026 10:15
…nd fix axios generic type

- refactor examples/typescript/worma.config.ts to include alova, alovaGlobals, axios, fetch, and ky generator examples with English documentation

- fix axios service template generic type from axiosInstance<T> to axiosInstance<unknown, T> for proper response typing
@changeset-bot

changeset-bot Bot commented Jul 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 923cf21

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wormajs Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added the worma label Jul 26, 2026
@czhlin
czhlin merged commit b98a407 into main Jul 26, 2026
6 checks passed
@JOU-amjs JOU-amjs mentioned this pull request Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant