Skip to content

Commit 8c4e726

Browse files
committed
chore: handle all relative paths the same way
1 parent 2f8f1dd commit 8c4e726

2 files changed

Lines changed: 75 additions & 4 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import path from 'node:path';
2+
3+
import { resolveRuntimeConfigPath } from '../../client-core/client';
4+
5+
describe('resolveRuntimeConfigPath', () => {
6+
it('preserves path mapping and module specifiers', () => {
7+
const outputPath = path.join(process.cwd(), 'src/client');
8+
9+
expect(
10+
resolveRuntimeConfigPath({
11+
outputPath,
12+
runtimeConfigPath: '~/foo.ts',
13+
}),
14+
).toBe('~/foo.ts');
15+
16+
expect(
17+
resolveRuntimeConfigPath({
18+
outputPath,
19+
runtimeConfigPath: '@/foo.ts',
20+
}),
21+
).toBe('@/foo.ts');
22+
23+
expect(
24+
resolveRuntimeConfigPath({
25+
outputPath,
26+
runtimeConfigPath: '@scope/runtime-config',
27+
}),
28+
).toBe('@scope/runtime-config');
29+
});
30+
31+
it('resolves relative filesystem paths from cwd to output-relative import', () => {
32+
const outputPath = path.join(process.cwd(), 'src/client');
33+
34+
expect(
35+
resolveRuntimeConfigPath({
36+
outputPath,
37+
runtimeConfigPath: './src/hey-api.ts',
38+
}),
39+
).toBe('../hey-api.ts');
40+
});
41+
42+
it('resolves absolute filesystem paths to output-relative import', () => {
43+
const cwd = process.cwd();
44+
const outputPath = path.join(cwd, 'src/client');
45+
46+
expect(
47+
resolveRuntimeConfigPath({
48+
outputPath,
49+
runtimeConfigPath: path.join(cwd, 'src/runtime/hey-api.ts'),
50+
}),
51+
).toBe('../runtime/hey-api.ts');
52+
});
53+
54+
it('adds ./ prefix for same-directory files', () => {
55+
const cwd = process.cwd();
56+
const outputPath = path.join(cwd, 'src/client');
57+
58+
expect(
59+
resolveRuntimeConfigPath({
60+
outputPath,
61+
runtimeConfigPath: path.join(cwd, 'src/client/hey-api.ts'),
62+
}),
63+
).toBe('./hey-api.ts');
64+
});
65+
});

packages/openapi-ts/src/plugins/@hey-api/client-core/client.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@ import { $ } from '../../../ts-dsl';
88
import type { PluginHandler } from './types';
99
import { getClientBaseUrlKey } from './utils';
1010

11-
function resolveRuntimeConfigPath({
11+
export function resolveRuntimeConfigPath({
1212
outputPath,
1313
runtimeConfigPath,
1414
}: {
1515
outputPath: string;
1616
runtimeConfigPath: string;
1717
}): string {
18-
// if (!path.isAbsolute(runtimeConfigPath) && !runtimeConfigPath.startsWith('./')) {
19-
// return runtimeConfigPath;
20-
// }
18+
const isFileSystemPath =
19+
path.isAbsolute(runtimeConfigPath) ||
20+
runtimeConfigPath.startsWith('./') ||
21+
runtimeConfigPath.startsWith('../');
22+
23+
if (!isFileSystemPath) {
24+
return runtimeConfigPath;
25+
}
26+
2127
const absoluteInputPath = path.isAbsolute(runtimeConfigPath)
2228
? runtimeConfigPath
2329
: path.resolve(process.cwd(), runtimeConfigPath);

0 commit comments

Comments
 (0)