Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use correct relative path to bundled client when imported from nested module #1123

Merged
merged 1 commit into from
Oct 5, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/gentle-ladybugs-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: use correct relative path to bundled client when imported from nested module
18 changes: 15 additions & 3 deletions packages/openapi-ts/src/generate/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@
import path from 'node:path';

import { getConfig, isStandaloneClient } from '../utils/config';
import { ensureDirSync } from './utils';
import { ensureDirSync, relativeModulePath } from './utils';

export const clientModulePath = () => {
export const clientModulePath = ({
sourceOutput,
}: {
sourceOutput: string;
}) => {
const config = getConfig();
return config.client.bundle ? './client' : config.client.name;

if (config.client.bundle) {
return relativeModulePath({
moduleOutput: 'client',
sourceOutput,
});
}

Check warning on line 19 in packages/openapi-ts/src/generate/client.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/generate/client.ts#L15-L19

Added lines #L15 - L19 were not covered by tests

return config.client.name;
};

export const clientOptionsTypeName = () => 'Options';
Expand Down
1 change: 0 additions & 1 deletion packages/openapi-ts/src/generate/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const generatePlugins = async ({
plugin.handler({
client,
files,
outputParts,
plugin,
});
}
Expand Down
12 changes: 7 additions & 5 deletions packages/openapi-ts/src/generate/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,24 +761,26 @@ export const generateServices = async ({

const isStandalone = isStandaloneClient(config);

const servicesOutput = 'services';

files.services = new TypeScriptFile({
dir: config.output.path,
name: 'services.ts',
name: `${servicesOutput}.ts`,
});

// Import required packages and core files.
if (isStandalone) {
files.services.import({
module: clientModulePath(),
module: clientModulePath({ sourceOutput: servicesOutput }),
name: 'createClient',
});
files.services.import({
module: clientModulePath(),
module: clientModulePath({ sourceOutput: servicesOutput }),
name: 'createConfig',
});
files.services.import({
asType: true,
module: clientModulePath(),
module: clientModulePath({ sourceOutput: servicesOutput }),
name: clientOptionsTypeName(),
});
} else {
Expand Down Expand Up @@ -857,7 +859,7 @@ export const generateServices = async ({
client,
onClientImport: (imported) => {
files.services.import({
module: clientModulePath(),
module: clientModulePath({ sourceOutput: servicesOutput }),
name: imported,
});
},
Expand Down
27 changes: 27 additions & 0 deletions packages/openapi-ts/src/generate/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,30 @@
mkdirSync(path, { recursive: true });
}
};

/**
* Construct a relative import path to modules. This is used for example
* in plugins to import types or services module.
*/
export const relativeModulePath = ({
moduleOutput,
sourceOutput,
}: {
/**
* Output path to the imported module.
* @example
* 'types'
*/
moduleOutput: string;
/**
* Output path to the source module.
* @example
* '@tanstack/react-query'
*/
sourceOutput: string;
}): string => {
const outputParts = sourceOutput.split('/');
const relativePath =
new Array(outputParts.length).fill('').join('../') || './';
return `${relativePath}${moduleOutput}`;
};

Check warning on line 34 in packages/openapi-ts/src/generate/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/generate/utils.ts#L14-L34

Added lines #L14 - L34 were not covered by tests
16 changes: 10 additions & 6 deletions packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
operationResponseTypeName,
toOperationName,
} from '../../../generate/services';
import { relativeModulePath } from '../../../generate/utils';
import { isOperationParameterRequired } from '../../../openApi';
import { getOperationKey } from '../../../openApi/common/parser/operation';
import type { Client } from '../../../types/client';
Expand Down Expand Up @@ -643,7 +644,6 @@
export const handler: PluginDefinition['handler'] = ({
client,
files,
outputParts,
plugin,
}) => {
if (
Expand All @@ -662,13 +662,14 @@

file.import({
asType: true,
module: clientModulePath(),
module: clientModulePath({ sourceOutput: plugin.output }),

Check warning on line 665 in packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts#L665

Added line #L665 was not covered by tests
name: clientOptionsTypeName(),
});

const relativePath =
new Array(outputParts.length).fill('').join('../') || './';
const typesModulePath = relativePath + files.types.getName(false);
const typesModulePath = relativeModulePath({
moduleOutput: files.types.getName(false),
sourceOutput: plugin.output,
});

Check warning on line 672 in packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts#L669-L672

Added lines #L669 - L672 were not covered by tests

const mutationsType =
plugin.name === '@tanstack/svelte-query' ||
Expand Down Expand Up @@ -1211,7 +1212,10 @@
file.add(statement);
}

const servicesModulePath = relativePath + files.services.getName(false);
const servicesModulePath = relativeModulePath({
moduleOutput: files.services.getName(false),
sourceOutput: plugin.output,
});

Check warning on line 1218 in packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/plugins/@tanstack/query-core/plugin.ts#L1215-L1218

Added lines #L1215 - L1218 were not covered by tests

if (hasQueries || hasInfiniteQueries) {
file.import({
Expand Down
1 change: 0 additions & 1 deletion packages/openapi-ts/src/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export interface PluginDefinition {
handler: (args: {
client: Client;
files: Files;
outputParts: string[];
plugin: Config['plugins'][number];
}) => void;
name: string;
Expand Down
24 changes: 12 additions & 12 deletions packages/openapi-ts/test/sample.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ const main = async () => {
/** @type {import('../src').UserConfig} */
const config = {
client: {
// bundle: true,
bundle: true,
// name: '@hey-api/client-axios',
name: '@hey-api/client-fetch',
},
// debug: true,
// experimental_parser: true,
// input: './test/spec/v3-transforms.json',
// input: './test/spec/v3.json',
input: './test/spec/3.1.0/required-all-of-ref.json',
input: './test/spec/v3.json',
// input: './test/spec/3.1.0/required-all-of-ref.json',
// input: './test/spec/v2.json',
// input: 'https://mongodb-mms-prod-build-server.s3.amazonaws.com/openapi/2caffd88277a4e27c95dcefc7e3b6a63a3b03297-v2-2023-11-15.json',
// name: 'foo',
output: {
path: './test/generated/sample/',
},
// plugins: [
// {
// // infiniteQueryOptions: false,
// // mutationOptions: false,
// name: '@tanstack/react-query',
// // queryOptions: false,
// },
// // '@hey-api/services',
// ],
plugins: [
{
// infiniteQueryOptions: false,
// mutationOptions: false,
name: '@tanstack/react-query',
// queryOptions: false,
},
// '@hey-api/services',
],
schemas: {
export: false,
},
Expand Down