Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/rtk-query-codegen-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ export type { ConfigFile } from './types';

const require = createRequire(__filename);

export async function generateEndpoints(
options: Omit<GenerationOptions, 'outputFile'> & { outputFile?: never }
): Promise<string>;

export async function generateEndpoints(
options: Omit<GenerationOptions, 'outputFile'> & Required<Pick<GenerationOptions, 'outputFile'>>
): Promise<void>;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { generateEndpoints } from '@rtk-query/codegen-openapi';
import * as path from 'node:path';

const withoutOutputFile = {
apiFile: './fixtures/emptyApi.ts',
schemaFile: path.join(__dirname, 'fixtures', 'petstore.json'),
};

const withOutputFile = {
apiFile: './fixtures/emptyApi.ts',
outputFile: './test/tmp/out.ts',
schemaFile: path.join(__dirname, 'fixtures', 'petstore.json'),
};

describe('generateEndpoints return type narrowing', () => {
test('narrows to Promise<string> when outputFile is omitted', () => {
expectTypeOf(generateEndpoints(withoutOutputFile)).toEqualTypeOf<Promise<string>>();

expectTypeOf(generateEndpoints(withoutOutputFile)).resolves.toBeString();

expectTypeOf(generateEndpoints).toBeCallableWith(withoutOutputFile).returns.resolves.toBeString();
});

test('narrows to Promise<void> when outputFile is provided', () => {
expectTypeOf(generateEndpoints(withOutputFile)).toEqualTypeOf<Promise<void>>();

expectTypeOf(generateEndpoints(withOutputFile)).resolves.toBeVoid();

expectTypeOf(generateEndpoints).toBeCallableWith(withOutputFile).returns.resolves.toBeVoid();
});
});
23 changes: 23 additions & 0 deletions packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,3 +933,26 @@ describe('esmExtensions option', () => {
expect(content).toContain("import { api } from '../../fixtures/emptyApi'");
});
});

describe('generateEndpoints return type narrowing', () => {
const schemaFile = resolve(__dirname, 'fixtures', 'petstore.json');

test('returns a string when outputFile is omitted', async () => {
const result = await generateEndpoints({
apiFile: './fixtures/emptyApi.ts',
schemaFile,
});

expect(result).toBeTypeOf('string');
});

test('returns void when outputFile is provided', async () => {
const result = await generateEndpoints({
apiFile: './fixtures/emptyApi.ts',
outputFile: './test/tmp/out.ts',
schemaFile,
});

expect(result).toBeUndefined();
});
});
35 changes: 27 additions & 8 deletions packages/rtk-query-codegen-openapi/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import * as path from 'node:path';
import tsconfigPaths from 'vite-tsconfig-paths';
import { defineConfig } from 'vitest/config';

// No __dirname under Node ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import packageJson from './package.json' with { type: 'json' };

export default defineConfig({
plugins: [tsconfigPaths({ projects: ['./tsconfig.json'] })],
plugins: [
tsconfigPaths({
configNames: ['tsconfig.json'],
projects: ['./tsconfig.json'],
root: import.meta.dirname,
}),
],

root: import.meta.dirname,

test: {
name: {
label: packageJson.name,
},

alias: process.env.TEST_DIST
? {
'@rtk-query/codegen-openapi': path.join(__dirname, '../..', 'node_modules/@rtk-query/codegen-openapi'),
'@rtk-query/codegen-openapi': path.join(import.meta.dirname, '..', '..', 'node_modules', packageJson.name),
}
: undefined,

dir: path.join(import.meta.dirname, 'test'),
root: import.meta.dirname,
testTimeout: 10_000,

typecheck: {
enabled: true,
tsconfig: path.join(import.meta.dirname, 'tsconfig.json'),
},

pool: 'forks',
globals: true,
setupFiles: ['./test/vitest.setup.ts'],
watch: false,
},
});
Loading
Loading