Skip to content

Commit a985f93

Browse files
authored
Merge pull request #4509 from aryaemami59/tsup-codegen
2 parents 6bf212c + 327556b commit a985f93

File tree

11 files changed

+459
-2467
lines changed

11 files changed

+459
-2467
lines changed

.github/workflows/test-codegen.yml

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- '.github/workflows/test-codegen.yml'
2727
2828
build:
29-
needs: changes
29+
needs: [changes]
3030
if: ${{ needs.changes.outputs.codegen == 'true' }}
3131

3232
defaults:
@@ -35,7 +35,7 @@ jobs:
3535

3636
runs-on: ${{ matrix.os }}
3737

38-
name: Build artifact for ${{ matrix.os }} with Node ${{ matrix.node-version }}
38+
name: 'Build artifact: ${{ matrix.os }} + Node ${{ matrix.node-version }}'
3939

4040
strategy:
4141
matrix:
@@ -74,7 +74,7 @@ jobs:
7474
defaults:
7575
run:
7676
working-directory: ./packages/rtk-query-codegen-openapi
77-
name: Test final build artifact with Node.js ${{ matrix.node-version }} on ${{ matrix.os }}
77+
name: 'Test build artifact: ${{ matrix.os }} + Node ${{ matrix.node-version }}'
7878
runs-on: ${{ matrix.os }}
7979
strategy:
8080
fail-fast: false
@@ -110,7 +110,41 @@ jobs:
110110

111111
- name: Run tests
112112
run: yarn test
113+
env:
114+
TEST_DIST: true
113115

114116
- name: Did we fail?
115117
if: failure()
116118
run: ls -R
119+
120+
are-the-types-wrong:
121+
name: Check package definition with are-the-types-wrong
122+
123+
needs: [build]
124+
runs-on: ubuntu-latest
125+
strategy:
126+
fail-fast: false
127+
matrix:
128+
node-version: [20.x]
129+
steps:
130+
- name: Checkout repo
131+
uses: actions/checkout@v4
132+
133+
- name: Use node ${{ matrix.node-version }}
134+
uses: actions/setup-node@v4
135+
with:
136+
node-version: ${{ matrix.node-version }}
137+
cache: 'yarn'
138+
139+
- name: Install deps
140+
run: yarn install
141+
142+
- name: Download artifact
143+
id: download-artifact
144+
uses: actions/download-artifact@v4
145+
with:
146+
path: ./packages/rtk-query-codegen-openapi
147+
name: package
148+
149+
- name: Run are-the-types-wrong
150+
run: yarn dlx @arethetypeswrong/cli@latest ./package.tgz --format table

docs/rtk-query/usage/code-generation.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export type EndpointMatcherFunction = (
122122
#### Filtering endpoints
123123
124124
If you only want to include a few endpoints, you can use the `filterEndpoints` config option to filter your endpoints.
125-
Note that endpoints are transformed to camel case. For example, `login_user` will become `loginUser`.
125+
Note that endpoints are transformed to camel case. For example, `login_user` will become `loginUser`.
126126
`filterEndpoints` will be checked against this camel case version of the endpoint.
127127
128128
```ts no-transpile title="openapi-config.ts"
@@ -171,7 +171,7 @@ const withOverride: ConfigFile = {
171171
endpointOverrides: [
172172
{
173173
pattern: /.*/,
174-
parameterFilter: (_name, parameter) => parameter.in !== "header",
174+
parameterFilter: (_name, parameter) => parameter.in !== 'header',
175175
},
176176
],
177177
}

docs/tutorials/quick-start.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,4 @@ Here's the complete counter application as a running CodeSandbox:
303303

304304
## What's Next?
305305

306-
We recommend going through [**the "Redux Essentials" and "Redux Fundamentals" tutorials in the Redux core docs**](https://redux.js.org/tutorials/index), which will give you a complete understanding of how Redux works, what Redux Toolkit does, and how to use it correctly.
306+
We recommend going through [**the "Redux Essentials" and "Redux Fundamentals" tutorials in the Redux core docs**](https://redux.js.org/tutorials/index), which will give you a complete understanding of how Redux works, what Redux Toolkit does, and how to use it correctly.

packages/rtk-query-codegen-openapi/package.json

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,23 @@
33
"version": "1.2.0",
44
"main": "lib/index.js",
55
"types": "lib/index.d.ts",
6+
"module": "lib/index.mjs",
67
"author": "Lenz Weber",
78
"license": "MIT",
9+
"sideEffects": false,
10+
"exports": {
11+
"./package.json": "./package.json",
12+
".": {
13+
"import": {
14+
"types": "./lib/index.d.mts",
15+
"default": "./lib/index.mjs"
16+
},
17+
"default": {
18+
"types": "./lib/index.d.ts",
19+
"default": "./lib/index.js"
20+
}
21+
}
22+
},
823
"repository": {
924
"type": "git",
1025
"url": "git+https://github.com/reduxjs/redux-toolkit.git"
@@ -14,15 +29,15 @@
1429
},
1530
"homepage": "https://redux-toolkit.js.org",
1631
"bin": {
17-
"rtk-query-codegen-openapi": "lib/bin/cli.js"
32+
"rtk-query-codegen-openapi": "lib/bin/cli.mjs"
1833
},
1934
"scripts": {
20-
"build": "yarn clean && tsc -p tsconfig.build.json && chmod +x lib/bin/cli.js",
35+
"build": "yarn clean && tsup && chmod +x lib/bin/cli.mjs",
2136
"clean": "rimraf lib",
22-
"prepack": "yarn build",
37+
"prepack": "yarn build && chmod +x lib/bin/cli.mjs",
2338
"format": "prettier --write \"src/**/*.ts\"",
2439
"test:update": "vitest --run --update",
25-
"test:update:enum": "lib/bin/cli.js test/config.example.enum.ts",
40+
"test:update:enum": "lib/bin/cli.mjs test/config.example.enum.ts",
2641
"test": "vitest --run --typecheck",
2742
"test:watch": "vitest --watch",
2843
"cli": "esr src/bin/cli.ts"
@@ -39,7 +54,7 @@
3954
"@reduxjs/toolkit": "^1.6.0",
4055
"@types/commander": "^2.12.2",
4156
"@types/glob-to-regexp": "^0.4.0",
42-
"@types/lodash": "^4.14.165",
57+
"@types/lodash.camelcase": "^4.3.9",
4358
"@types/node": "^20.11.10",
4459
"@types/semver": "^7.3.9",
4560
"chalk": "^4.1.0",
@@ -53,19 +68,20 @@
5368
"pretty-quick": "^4.0.0",
5469
"rimraf": "^5.0.5",
5570
"ts-node": "^10.9.2",
71+
"tsup": "^8.2.4",
5672
"vite-tsconfig-paths": "^5.0.1",
5773
"vitest": "^2.0.5",
5874
"yalc": "^1.0.0-pre.47"
5975
},
6076
"dependencies": {
6177
"@apidevtools/swagger-parser": "^10.0.2",
6278
"commander": "^6.2.0",
63-
"lodash": "^4.17.21",
79+
"lodash.camelcase": "^4.3.0",
6480
"oazapfts": "^6.1.0",
6581
"prettier": "^3.2.5",
6682
"semver": "^7.3.5",
6783
"swagger2openapi": "^7.0.4",
68-
"typescript": "^5.3.3"
84+
"typescript": "^5.5.4"
6985
},
7086
"husky": {
7187
"hooks": {

packages/rtk-query-codegen-openapi/src/bin/cli.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#!/usr/bin/env node
22

3+
import { generateEndpoints, parseConfig } from '@rtk-query/codegen-openapi';
34
import program from 'commander';
5+
import { createRequire } from 'node:module';
46
import { dirname, resolve } from 'node:path';
5-
import { generateEndpoints, parseConfig } from '../';
7+
8+
const require = createRequire(__filename);
69

710
let ts = false;
811
try {

packages/rtk-query-codegen-openapi/src/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { camelCase } from 'lodash';
1+
import camelCase from 'lodash.camelcase';
22
import path from 'node:path';
33
import ApiGenerator, {
44
getOperationName as _getOperationName,

packages/rtk-query-codegen-openapi/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import fs from 'node:fs';
2+
import { createRequire } from 'node:module';
23
import path from 'node:path';
4+
import { generateApi } from './generate';
35
import type { CommonOptions, ConfigFile, GenerationOptions, OutputFileOptions } from './types';
46
import { isValidUrl, prettify } from './utils';
57
export type { ConfigFile } from './types';
68

9+
const require = createRequire(__filename);
10+
711
export async function generateEndpoints(options: GenerationOptions): Promise<string | void> {
812
const schemaLocation = options.schemaFile;
913

@@ -12,7 +16,6 @@ export async function generateEndpoints(options: GenerationOptions): Promise<str
1216
: path.resolve(process.cwd(), schemaLocation);
1317

1418
const sourceCode = await enforceOazapftsTsVersion(async () => {
15-
const { generateApi } = await import('./generate');
1619
return generateApi(schemaAbsPath, options);
1720
});
1821
const { outputFile, prettierConfigFile } = options;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { Options } from 'tsup';
2+
import { defineConfig } from 'tsup';
3+
4+
const tsconfig = 'tsconfig.build.json' satisfies Options['tsconfig'];
5+
6+
export default defineConfig((options): Options[] => {
7+
const commonOptions: Options = {
8+
entry: { index: 'src/index.ts' },
9+
sourcemap: true,
10+
tsconfig,
11+
clean: true,
12+
target: ['esnext'],
13+
outDir: 'lib',
14+
splitting: false,
15+
removeNodeProtocol: false,
16+
shims: true,
17+
...options,
18+
};
19+
20+
return [
21+
{ ...commonOptions, name: 'Modern ESM', format: ['esm'], entry: { index: 'src/index.ts' }, dts: true },
22+
{ ...commonOptions, name: 'CJS Development', format: ['cjs'], entry: { index: 'src/index.ts' }, dts: true },
23+
{
24+
...commonOptions,
25+
format: ['esm'],
26+
name: 'CLI Development',
27+
external: ['@rtk-query/codegen-openapi'],
28+
minify: true,
29+
entry: { 'bin/cli': 'src/bin/cli.ts' },
30+
},
31+
];
32+
});

packages/rtk-query-codegen-openapi/vitest.config.mts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ const __dirname = path.dirname(__filename);
1010
export default defineConfig({
1111
plugins: [tsconfigPaths({ projects: ['./tsconfig.json'] })],
1212
test: {
13+
alias: process.env.TEST_DIST
14+
? {
15+
'@rtk-query/codegen-openapi': path.join(__dirname, '../..', 'node_modules/@rtk-query/codegen-openapi'),
16+
}
17+
: undefined,
1318
testTimeout: 10_000,
1419
pool: 'forks',
1520
globals: true,

packages/toolkit/src/query/tests/buildHooks.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -770,8 +770,8 @@ describe('hooks tests', () => {
770770
endpoints: (builder) => ({
771771
getTest: builder.query<string, number>({
772772
async queryFn() {
773-
await new Promise((resolve) => setTimeout(resolve, 1000));
774-
return { data: "data!" };
773+
await new Promise((resolve) => setTimeout(resolve, 1000))
774+
return { data: 'data!' }
775775
},
776776
keepUnusedDataFor: 0,
777777
}),
@@ -783,13 +783,13 @@ describe('hooks tests', () => {
783783
})
784784

785785
const checkNumQueries = (count: number) => {
786-
const cacheEntries = Object.keys((storeRef.store.getState()).api.queries)
786+
const cacheEntries = Object.keys(storeRef.store.getState().api.queries)
787787
const queries = cacheEntries.length
788788

789789
expect(queries).toBe(count)
790790
}
791791

792-
let i = 0;
792+
let i = 0
793793

794794
function User() {
795795
const [fetchTest, { isFetching, isUninitialized }] =
@@ -817,7 +817,7 @@ describe('hooks tests', () => {
817817
})
818818

819819
// There should only be one stored query once they have had time to resolve
820-
checkNumQueries( 1)
820+
checkNumQueries(1)
821821
})
822822

823823
// See https://github.com/reduxjs/redux-toolkit/issues/3182

0 commit comments

Comments
 (0)