Skip to content

Commit

Permalink
Merge pull request #4509 from aryaemami59/tsup-codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored Aug 30, 2024
2 parents 6bf212c + 327556b commit a985f93
Show file tree
Hide file tree
Showing 11 changed files with 459 additions and 2,467 deletions.
40 changes: 37 additions & 3 deletions .github/workflows/test-codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- '.github/workflows/test-codegen.yml'
build:
needs: changes
needs: [changes]
if: ${{ needs.changes.outputs.codegen == 'true' }}

defaults:
Expand All @@ -35,7 +35,7 @@ jobs:

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

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

strategy:
matrix:
Expand Down Expand Up @@ -74,7 +74,7 @@ jobs:
defaults:
run:
working-directory: ./packages/rtk-query-codegen-openapi
name: Test final build artifact with Node.js ${{ matrix.node-version }} on ${{ matrix.os }}
name: 'Test build artifact: ${{ matrix.os }} + Node ${{ matrix.node-version }}'
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand Down Expand Up @@ -110,7 +110,41 @@ jobs:

- name: Run tests
run: yarn test
env:
TEST_DIST: true

- name: Did we fail?
if: failure()
run: ls -R

are-the-types-wrong:
name: Check package definition with are-the-types-wrong

needs: [build]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version: [20.x]
steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Use node ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'

- name: Install deps
run: yarn install

- name: Download artifact
id: download-artifact
uses: actions/download-artifact@v4
with:
path: ./packages/rtk-query-codegen-openapi
name: package

- name: Run are-the-types-wrong
run: yarn dlx @arethetypeswrong/cli@latest ./package.tgz --format table
4 changes: 2 additions & 2 deletions docs/rtk-query/usage/code-generation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export type EndpointMatcherFunction = (
#### Filtering endpoints
If you only want to include a few endpoints, you can use the `filterEndpoints` config option to filter your endpoints.
Note that endpoints are transformed to camel case. For example, `login_user` will become `loginUser`.
Note that endpoints are transformed to camel case. For example, `login_user` will become `loginUser`.
`filterEndpoints` will be checked against this camel case version of the endpoint.
```ts no-transpile title="openapi-config.ts"
Expand Down Expand Up @@ -171,7 +171,7 @@ const withOverride: ConfigFile = {
endpointOverrides: [
{
pattern: /.*/,
parameterFilter: (_name, parameter) => parameter.in !== "header",
parameterFilter: (_name, parameter) => parameter.in !== 'header',
},
],
}
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/quick-start.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,4 @@ Here's the complete counter application as a running CodeSandbox:

## What's Next?

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.
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.
30 changes: 23 additions & 7 deletions packages/rtk-query-codegen-openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,23 @@
"version": "1.2.0",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"module": "lib/index.mjs",
"author": "Lenz Weber",
"license": "MIT",
"sideEffects": false,
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./lib/index.d.mts",
"default": "./lib/index.mjs"
},
"default": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
}
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/reduxjs/redux-toolkit.git"
Expand All @@ -14,15 +29,15 @@
},
"homepage": "https://redux-toolkit.js.org",
"bin": {
"rtk-query-codegen-openapi": "lib/bin/cli.js"
"rtk-query-codegen-openapi": "lib/bin/cli.mjs"
},
"scripts": {
"build": "yarn clean && tsc -p tsconfig.build.json && chmod +x lib/bin/cli.js",
"build": "yarn clean && tsup && chmod +x lib/bin/cli.mjs",
"clean": "rimraf lib",
"prepack": "yarn build",
"prepack": "yarn build && chmod +x lib/bin/cli.mjs",
"format": "prettier --write \"src/**/*.ts\"",
"test:update": "vitest --run --update",
"test:update:enum": "lib/bin/cli.js test/config.example.enum.ts",
"test:update:enum": "lib/bin/cli.mjs test/config.example.enum.ts",
"test": "vitest --run --typecheck",
"test:watch": "vitest --watch",
"cli": "esr src/bin/cli.ts"
Expand All @@ -39,7 +54,7 @@
"@reduxjs/toolkit": "^1.6.0",
"@types/commander": "^2.12.2",
"@types/glob-to-regexp": "^0.4.0",
"@types/lodash": "^4.14.165",
"@types/lodash.camelcase": "^4.3.9",
"@types/node": "^20.11.10",
"@types/semver": "^7.3.9",
"chalk": "^4.1.0",
Expand All @@ -53,19 +68,20 @@
"pretty-quick": "^4.0.0",
"rimraf": "^5.0.5",
"ts-node": "^10.9.2",
"tsup": "^8.2.4",
"vite-tsconfig-paths": "^5.0.1",
"vitest": "^2.0.5",
"yalc": "^1.0.0-pre.47"
},
"dependencies": {
"@apidevtools/swagger-parser": "^10.0.2",
"commander": "^6.2.0",
"lodash": "^4.17.21",
"lodash.camelcase": "^4.3.0",
"oazapfts": "^6.1.0",
"prettier": "^3.2.5",
"semver": "^7.3.5",
"swagger2openapi": "^7.0.4",
"typescript": "^5.3.3"
"typescript": "^5.5.4"
},
"husky": {
"hooks": {
Expand Down
5 changes: 4 additions & 1 deletion packages/rtk-query-codegen-openapi/src/bin/cli.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!/usr/bin/env node

import { generateEndpoints, parseConfig } from '@rtk-query/codegen-openapi';
import program from 'commander';
import { createRequire } from 'node:module';
import { dirname, resolve } from 'node:path';
import { generateEndpoints, parseConfig } from '../';

const require = createRequire(__filename);

let ts = false;
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/rtk-query-codegen-openapi/src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { camelCase } from 'lodash';
import camelCase from 'lodash.camelcase';
import path from 'node:path';
import ApiGenerator, {
getOperationName as _getOperationName,
Expand Down
5 changes: 4 additions & 1 deletion packages/rtk-query-codegen-openapi/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import fs from 'node:fs';
import { createRequire } from 'node:module';
import path from 'node:path';
import { generateApi } from './generate';
import type { CommonOptions, ConfigFile, GenerationOptions, OutputFileOptions } from './types';
import { isValidUrl, prettify } from './utils';
export type { ConfigFile } from './types';

const require = createRequire(__filename);

export async function generateEndpoints(options: GenerationOptions): Promise<string | void> {
const schemaLocation = options.schemaFile;

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

const sourceCode = await enforceOazapftsTsVersion(async () => {
const { generateApi } = await import('./generate');
return generateApi(schemaAbsPath, options);
});
const { outputFile, prettierConfigFile } = options;
Expand Down
32 changes: 32 additions & 0 deletions packages/rtk-query-codegen-openapi/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Options } from 'tsup';
import { defineConfig } from 'tsup';

const tsconfig = 'tsconfig.build.json' satisfies Options['tsconfig'];

export default defineConfig((options): Options[] => {
const commonOptions: Options = {
entry: { index: 'src/index.ts' },
sourcemap: true,
tsconfig,
clean: true,
target: ['esnext'],
outDir: 'lib',
splitting: false,
removeNodeProtocol: false,
shims: true,
...options,
};

return [
{ ...commonOptions, name: 'Modern ESM', format: ['esm'], entry: { index: 'src/index.ts' }, dts: true },
{ ...commonOptions, name: 'CJS Development', format: ['cjs'], entry: { index: 'src/index.ts' }, dts: true },
{
...commonOptions,
format: ['esm'],
name: 'CLI Development',
external: ['@rtk-query/codegen-openapi'],
minify: true,
entry: { 'bin/cli': 'src/bin/cli.ts' },
},
];
});
5 changes: 5 additions & 0 deletions packages/rtk-query-codegen-openapi/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const __dirname = path.dirname(__filename);
export default defineConfig({
plugins: [tsconfigPaths({ projects: ['./tsconfig.json'] })],
test: {
alias: process.env.TEST_DIST
? {
'@rtk-query/codegen-openapi': path.join(__dirname, '../..', 'node_modules/@rtk-query/codegen-openapi'),
}
: undefined,
testTimeout: 10_000,
pool: 'forks',
globals: true,
Expand Down
10 changes: 5 additions & 5 deletions packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,8 @@ describe('hooks tests', () => {
endpoints: (builder) => ({
getTest: builder.query<string, number>({
async queryFn() {
await new Promise((resolve) => setTimeout(resolve, 1000));
return { data: "data!" };
await new Promise((resolve) => setTimeout(resolve, 1000))
return { data: 'data!' }
},
keepUnusedDataFor: 0,
}),
Expand All @@ -783,13 +783,13 @@ describe('hooks tests', () => {
})

const checkNumQueries = (count: number) => {
const cacheEntries = Object.keys((storeRef.store.getState()).api.queries)
const cacheEntries = Object.keys(storeRef.store.getState().api.queries)
const queries = cacheEntries.length

expect(queries).toBe(count)
}

let i = 0;
let i = 0

function User() {
const [fetchTest, { isFetching, isUninitialized }] =
Expand Down Expand Up @@ -817,7 +817,7 @@ describe('hooks tests', () => {
})

// There should only be one stored query once they have had time to resolve
checkNumQueries( 1)
checkNumQueries(1)
})

// See https://github.com/reduxjs/redux-toolkit/issues/3182
Expand Down
Loading

0 comments on commit a985f93

Please sign in to comment.