Skip to content

Commit 6d0e069

Browse files
authored
Merge pull request #1987 from hey-api/fix/export-from-index-nested
fix: correct path to nested plugin files when using exportFromIndex
2 parents 830739e + 71e2fd0 commit 6d0e069

File tree

12 files changed

+42
-11
lines changed

12 files changed

+42
-11
lines changed

.changeset/wet-cycles-confess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hey-api/openapi-ts': patch
3+
---
4+
5+
fix: correct path to nested plugin files when using exportFromIndex

packages/openapi-ts-tests/test/openapi-ts.config.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ export default defineConfig(() => {
3636
// openapi: '3.1.0',
3737
// paths: {},
3838
// },
39-
path: path.resolve(
40-
__dirname,
41-
'spec',
42-
'3.1.x',
43-
'discriminator-mapped-many.yaml',
44-
),
39+
path: path.resolve(__dirname, 'spec', '3.1.x', 'full.json'),
4540
// path: 'http://localhost:4000/',
4641
// path: 'https://get.heyapi.dev/',
4742
// path: 'https://get.heyapi.dev/hey-api/backend?branch=main&version=1.0.0',
@@ -76,7 +71,7 @@ export default defineConfig(() => {
7671
// bundle: true,
7772
// bundleSource_EXPERIMENTAL: true,
7873
// exportFromIndex: true,
79-
// name: '@hey-api/client-fetch',
74+
name: '@hey-api/client-fetch',
8075
// strictBaseUrl: true,
8176
},
8277
{
@@ -108,7 +103,7 @@ export default defineConfig(() => {
108103
// enumsCase: 'camelCase',
109104
// exportInlineEnums: true,
110105
// identifierCase: 'preserve',
111-
name: '@hey-api/typescript',
106+
// name: '@hey-api/typescript',
112107
// readOnlyWriteOnlyBehavior: 'off',
113108
// readableNameBuilder: 'Readable{{name}}',
114109
// writableNameBuilder: 'Writable{{name}}',
@@ -118,11 +113,12 @@ export default defineConfig(() => {
118113
// name: 'fastify',
119114
},
120115
{
121-
// name: '@tanstack/react-query',
116+
exportFromIndex: true,
117+
name: '@tanstack/react-query',
122118
},
123119
{
124120
// exportFromIndex: true,
125-
name: 'zod',
121+
// name: 'zod',
126122
},
127123
],
128124
// useOptions: false,

packages/openapi-ts/src/generate/__tests__/index.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,17 @@ describe('generateIndexFile', () => {
6868
const files: Parameters<typeof generateIndexFile>[0]['files'] = {
6969
schemas: new TypeScriptFile({
7070
dir: '/',
71+
id: 'schemas',
7172
name: 'schemas.ts',
7273
}),
7374
sdk: new TypeScriptFile({
7475
dir: '/',
76+
id: 'sdk',
7577
name: 'sdk.ts',
7678
}),
7779
types: new TypeScriptFile({
7880
dir: '/',
81+
id: 'types',
7982
name: 'types.ts',
8083
}),
8184
};

packages/openapi-ts/src/generate/files.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class TypeScriptFile {
5959
*/
6060
private _exportFromIndex: boolean;
6161
private _headers: Array<string> = [];
62+
private _id: string;
6263
private _identifierCase: StringCase | undefined;
6364
private _imports = new Map<string, Map<string, ImportExportItemObject>>();
6465
private _items: Array<ts.Node | string> = [];
@@ -80,6 +81,7 @@ export class TypeScriptFile {
8081
dir,
8182
exportFromIndex = false,
8283
header = true,
84+
id,
8385
identifierCase,
8486
name,
8587
}: {
@@ -89,10 +91,17 @@ export class TypeScriptFile {
8991
*/
9092
exportFromIndex?: boolean;
9193
header?: boolean;
94+
/**
95+
* Unique file ID. Used to generate correct relative paths to the file.
96+
* This should be refactored later as it's basically the file name unless
97+
* nested inside another folder.
98+
*/
99+
id: string;
92100
identifierCase?: StringCase;
93101
name: string;
94102
}) {
95103
this._exportFromIndex = exportFromIndex;
104+
this._id = id;
96105
this._identifierCase = identifierCase;
97106
this._name = this._setName(name);
98107
this._path = path.resolve(dir, this._name);
@@ -138,6 +147,10 @@ export class TypeScriptFile {
138147
return this._exportFromIndex;
139148
}
140149

150+
public get id(): string {
151+
return this._id;
152+
}
153+
141154
public identifier({
142155
namespace,
143156
...args

packages/openapi-ts/src/generate/indexFile.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const generateIndexFile = ({ files }: { files: Files }): void => {
99

1010
files.index = new TypeScriptFile({
1111
dir: config.output.path,
12+
id: 'index',
1213
name: 'index.ts',
1314
});
1415

packages/openapi-ts/src/generate/output.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export const generateLegacyOutput = async ({
9090
);
9191
files[plugin.name] = new TypeScriptFile({
9292
dir: outputDir,
93+
id: `legacy-unused-${plugin.name}`,
9394
name: `${outputParts[outputParts.length - 1]}.ts`,
9495
});
9596
plugin._handlerLegacy({
@@ -162,7 +163,10 @@ export const generateOutput = async ({ context }: { context: IR.Context }) => {
162163
// what's exported so we can support named exports
163164
indexFile.add(
164165
compiler.exportAllDeclaration({
165-
module: `./${fileName}`,
166+
module: indexFile.relativePathToFile({
167+
context,
168+
id: file.id,
169+
}),
166170
}),
167171
);
168172
}

packages/openapi-ts/src/ir/context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export class IRContext<Spec extends Record<string, any> = any> {
136136
const createdFile = new TypeScriptFile({
137137
dir: outputDir,
138138
exportFromIndex: file.exportFromIndex,
139+
id: file.id,
139140
identifierCase: file.identifierCase,
140141
name: `${outputParts[outputParts.length - 1]}.ts`,
141142
});

packages/openapi-ts/src/plugins/@hey-api/schemas/plugin-legacy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export const handlerLegacy: Plugin.LegacyHandler<Config> = ({
7777

7878
files.schemas = new TypeScriptFile({
7979
dir: config.output.path,
80+
id: 'schemas',
8081
name: 'schemas.ts',
8182
});
8283

packages/openapi-ts/src/plugins/@hey-api/sdk/__tests__/plugin.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ describe('handlerLegacy', () => {
111111

112112
files.types = new TypeScriptFile({
113113
dir: '/',
114+
id: 'types',
114115
name: 'types.ts',
115116
});
116117

@@ -234,6 +235,7 @@ describe('methodNameBuilder', () => {
234235

235236
files.types = new TypeScriptFile({
236237
dir: '/',
238+
id: 'types',
237239
name: 'types.ts',
238240
});
239241

@@ -318,6 +320,7 @@ describe('methodNameBuilder', () => {
318320

319321
files.types = new TypeScriptFile({
320322
dir: '/',
323+
id: 'types',
321324
name: 'types.ts',
322325
});
323326

@@ -404,6 +407,7 @@ describe('methodNameBuilder', () => {
404407

405408
files.types = new TypeScriptFile({
406409
dir: '/',
410+
id: 'types',
407411
name: 'types.ts',
408412
});
409413

packages/openapi-ts/src/plugins/@hey-api/sdk/plugin-legacy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ export const handlerLegacy: Plugin.LegacyHandler<Config> = ({
802802

803803
files.sdk = new TypeScriptFile({
804804
dir: config.output.path,
805+
id: 'sdk',
805806
name: `${sdkOutput}.ts`,
806807
});
807808

0 commit comments

Comments
 (0)