Skip to content

Commit 5c1ab93

Browse files
committed
refactor(nx-plugin): handle configs from builtin client packages
1 parent 1156b91 commit 5c1ab93

File tree

7 files changed

+443
-139
lines changed

7 files changed

+443
-139
lines changed

packages/nx-plugin/package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,22 @@
4848
"executors": "./executors.json",
4949
"generators": "./generators.json",
5050
"dependencies": {
51-
"@hey-api/json-schema-ref-parser": "1.0.6",
51+
"@hey-api/json-schema-ref-parser": "^1.0.6",
5252
"@hey-api/openapi-ts": "workspace:*",
53-
"@nx/devkit": "21.0.3",
53+
"@nx/devkit": "^21.3.5",
5454
"api-smart-diff": "^1.0.6",
55-
"latest-version": "9.0.0",
56-
"nx": "21.0.3",
55+
"latest-version": "^9.0.0",
56+
"nx": "^21.3.5",
57+
"prettier": "^3.6.2",
5758
"swagger2openapi": "^7.0.8",
58-
"tslib": "2.8.1",
59-
"xcurl": "2.1.2"
59+
"tslib": "^2.8.1",
60+
"xcurl": "^2.1.2"
6061
},
6162
"devDependencies": {
6263
"@config/vite-base": "workspace:*",
6364
"@types/swagger2openapi": "^7.0.4",
64-
"typescript": "5.8.3",
65-
"vitest": "3.1.1"
65+
"typescript": "^5.8.3",
66+
"vitest": "^3.2.4"
6667
},
6768
"files": [
6869
"executors.json",

packages/nx-plugin/src/executors/update-api/updateApi.spec.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ vi.mock('@hey-api/openapi-ts/internal', async (importOriginal) => {
2020
return {
2121
...actual,
2222
initConfigs: vi.fn((config: Parameters<typeof initConfigs>[0]) =>
23-
Promise.resolve([
24-
{
25-
input: config?.input ?? 'default-input',
26-
output: config?.output ?? 'default-output',
27-
plugins: config?.plugins ?? [],
28-
},
29-
]),
23+
Promise.resolve({
24+
dependencies: [],
25+
results: [
26+
{
27+
config: {
28+
input: config?.input ?? 'default-input',
29+
output: config?.output ?? 'default-output',
30+
plugins: config?.plugins ?? [],
31+
},
32+
errors: [],
33+
},
34+
],
35+
}),
3036
),
3137
};
3238
});

packages/nx-plugin/src/generators/openapi-client/openapiClient.spec.ts

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,19 @@ vi.mock('@hey-api/openapi-ts/internal', async (importOriginal) => {
3030
return {
3131
...actual,
3232
initConfigs: vi.fn((config: Parameters<typeof initConfigs>[0]) =>
33-
Promise.resolve([
34-
{
35-
input: config?.input ?? 'default-input',
36-
output: config?.output ?? 'default-output',
37-
plugins: config?.plugins ?? [],
38-
},
39-
]),
33+
Promise.resolve({
34+
dependencies: [],
35+
results: [
36+
{
37+
config: {
38+
input: config?.input ?? 'default-input',
39+
output: config?.output ?? 'default-output',
40+
plugins: config?.plugins ?? [],
41+
},
42+
errors: [],
43+
},
44+
],
45+
}),
4046
),
4147
};
4248
});
@@ -215,7 +221,7 @@ describe('openapi-client generator', () => {
215221
});
216222

217223
describe('updatePackageJson', () => {
218-
it('should update package.json with correct dependencies', async () => {
224+
it('should not add the fetch client to the package.json', async () => {
219225
const { options, tree } = await getGeneratorOptions({
220226
name: `test-api-${randomUUID()}`,
221227
tempDirectory,
@@ -252,7 +258,49 @@ describe('openapi-client generator', () => {
252258
});
253259

254260
const packageJson = readJson(tree, `${projectRoot}/package.json`);
255-
expect(packageJson.dependencies['@hey-api/client-fetch']).toBeDefined();
261+
expect(
262+
packageJson.dependencies['@hey-api/client-fetch'],
263+
).not.toBeDefined();
264+
});
265+
266+
it('should add the random client to the package.json', async () => {
267+
const { options, tree } = await getGeneratorOptions({
268+
name: `test-api-${randomUUID()}`,
269+
tempDirectory,
270+
});
271+
const normalizedOptions = normalizeOptions(options);
272+
const { projectName, projectRoot, projectScope } = normalizedOptions;
273+
274+
// Create initial package.json
275+
tree.write(
276+
`${projectRoot}/package.json`,
277+
JSON.stringify({
278+
dependencies: {},
279+
devDependencies: {},
280+
name: `${projectScope}/${projectName}`,
281+
}),
282+
);
283+
284+
// Create tsconfig.base.json
285+
tree.write(
286+
'tsconfig.base.json',
287+
JSON.stringify({
288+
compilerOptions: {
289+
paths: {},
290+
},
291+
}),
292+
);
293+
294+
await updatePackageJson({
295+
clientType: 'MyRandomClient',
296+
isPrivate: true,
297+
plugins: [],
298+
projectRoot,
299+
tree,
300+
});
301+
302+
const packageJson = readJson(tree, `${projectRoot}/package.json`);
303+
expect(packageJson.dependencies['MyRandomClient']).toBeDefined();
256304
});
257305

258306
it('should update tsconfig with correct dependencies', async () => {

packages/nx-plugin/src/generators/openapi-client/openapiClient.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -897,8 +897,19 @@ export async function updatePackageJson({
897897
}) {
898898
const { default: latestVersion } = await import('latest-version');
899899

900+
const nonPackageClients = [
901+
'@hey-api/client-fetch',
902+
'@hey-api/client-axios',
903+
'@hey-api/client-nuxt',
904+
'@hey-api/client-next',
905+
];
900906
// add the client as a dependency
901-
const clientDetails = getPackageDetails(clientType);
907+
const clientDetails = nonPackageClients.includes(clientType)
908+
? {
909+
packageName: clientType,
910+
packageVersion: undefined,
911+
}
912+
: getPackageDetails(clientType);
902913
// add the openapi-ts as a dependency
903914
const openApiTsDetails = getPackageDetails('@hey-api/openapi-ts');
904915

@@ -923,13 +934,16 @@ export async function updatePackageJson({
923934
]);
924935

925936
// Update package.json to add dependencies and scripts
926-
const deps = results.reduce(
927-
(acc, result) => {
928-
acc[result.packageName] = result.packageVersion;
929-
return acc;
930-
},
931-
{} as Record<string, string>,
932-
);
937+
const deps = results
938+
// filter out the packages that have no version
939+
.filter((v) => v.packageVersion !== undefined)
940+
.reduce(
941+
(acc, result) => {
942+
acc[result.packageName] = result.packageVersion;
943+
return acc;
944+
},
945+
{} as Record<string, string>,
946+
);
933947

934948
if ((await clientDetails).packageName === '@hey-api/client-axios') {
935949
const axiosVersion = await latestVersion('axios');

packages/nx-plugin/src/utils.spec.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,19 @@ vi.mock('@hey-api/openapi-ts/internal', async (importOriginal) => {
4646
}),
4747
),
4848
initConfigs: vi.fn((config: Parameters<typeof initConfigs>[0]) =>
49-
Promise.resolve([
50-
{
51-
input: config?.input ?? 'default-input',
52-
output: config?.output ?? 'default-output',
53-
plugins: config?.plugins ?? [],
54-
},
55-
]),
49+
Promise.resolve({
50+
dependencies: [],
51+
results: [
52+
{
53+
config: {
54+
input: config?.input ?? 'default-input',
55+
output: config?.output ?? 'default-output',
56+
plugins: config?.plugins ?? [],
57+
},
58+
errors: [],
59+
},
60+
],
61+
}),
5662
),
5763
parseOpenApiSpec: vi.fn(() => ({
5864
spec: {

packages/nx-plugin/src/utils.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,29 @@ export async function bundleAndDereferenceSpecFile({
136136
plugins: [client, ...plugins] as ClientConfig['plugins'],
137137
});
138138
// getting the first config
139-
const config = configs[0];
139+
const dependencies = configs.dependencies;
140+
const firstResult = configs.results[0];
141+
if (!firstResult) {
142+
logger.error('Failed to load config.');
143+
throw new Error('Failed to load config.');
144+
}
145+
// check if the config is valid
146+
const { config, errors } = firstResult;
147+
const firstError = errors[0];
148+
if (firstError && !config) {
149+
logger.error(`Failed to load config: ${firstError.message}`);
150+
throw new Error(`Failed to load config: ${firstError.message}`, {
151+
cause: firstError,
152+
});
153+
}
140154
if (!config) {
141155
logger.error('Failed to load config.');
142156
throw new Error('Failed to load config.');
143157
}
144158
logger.debug(`Parsing spec...`);
145159
const context = parseOpenApiSpec({
146160
config,
161+
dependencies,
147162
spec,
148163
});
149164
if (!context) {

0 commit comments

Comments
 (0)