Skip to content

Commit ea51130

Browse files
committed
refactor: move out legacy parser functions
1 parent 4ff2429 commit ea51130

File tree

42 files changed

+354
-306
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+354
-306
lines changed

packages/openapi-ts/src/generate/__tests__/class.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { writeFileSync } from 'node:fs';
33
import { describe, expect, it, vi } from 'vitest';
44

55
import { setConfig } from '../../utils/config';
6-
import { generateClientClass } from '../class';
6+
import { generateLegacyClientClass } from '../class';
77
import { mockTemplates, openApi } from './mocks';
88

99
vi.mock('node:fs');
1010

11-
describe('generateClientClass', () => {
11+
describe('generateLegacyClientClass', () => {
1212
it('writes to filesystem', async () => {
1313
setConfig({
1414
client: {
@@ -33,15 +33,15 @@ describe('generateClientClass', () => {
3333
useOptions: true,
3434
});
3535

36-
const client: Parameters<typeof generateClientClass>[2] = {
36+
const client: Parameters<typeof generateLegacyClientClass>[2] = {
3737
models: [],
3838
server: 'http://localhost:8080',
3939
services: [],
4040
types: {},
4141
version: 'v1',
4242
};
4343

44-
await generateClientClass(openApi, './dist', client, mockTemplates);
44+
await generateLegacyClientClass(openApi, './dist', client, mockTemplates);
4545

4646
expect(writeFileSync).toHaveBeenCalled();
4747
});

packages/openapi-ts/src/generate/__tests__/core.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import path from 'node:path';
44
import { beforeEach, describe, expect, it, vi } from 'vitest';
55

66
import { setConfig } from '../../utils/config';
7-
import { generateCore } from '../core';
7+
import { generateLegacyCore } from '../core';
88
import { mockTemplates } from './mocks';
99

1010
vi.mock('node:fs');
1111

12-
describe('generateCore', () => {
13-
let templates: Parameters<typeof generateCore>[2];
12+
describe('generateLegacyCore', () => {
13+
let templates: Parameters<typeof generateLegacyCore>[2];
1414
beforeEach(() => {
1515
templates = mockTemplates;
1616
});
1717

1818
it('writes to filesystem', async () => {
19-
const client: Parameters<typeof generateCore>[1] = {
19+
const client: Parameters<typeof generateLegacyCore>[1] = {
2020
models: [],
2121
server: 'http://localhost:8080',
2222
services: [],
@@ -47,7 +47,7 @@ describe('generateCore', () => {
4747
useOptions: true,
4848
});
4949

50-
await generateCore('/', client, templates);
50+
await generateLegacyCore('/', client, templates);
5151

5252
expect(writeFileSync).toHaveBeenCalledWith(
5353
path.resolve('/', '/OpenAPI.ts'),
@@ -76,7 +76,7 @@ describe('generateCore', () => {
7676
});
7777

7878
it('uses client server value for base', async () => {
79-
const client: Parameters<typeof generateCore>[1] = {
79+
const client: Parameters<typeof generateLegacyCore>[1] = {
8080
models: [],
8181
server: 'http://localhost:8080',
8282
services: [],
@@ -107,7 +107,7 @@ describe('generateCore', () => {
107107
useOptions: true,
108108
});
109109

110-
await generateCore('/', client, templates);
110+
await generateLegacyCore('/', client, templates);
111111

112112
expect(templates.core.settings).toHaveBeenCalledWith({
113113
$config: config,
@@ -118,7 +118,7 @@ describe('generateCore', () => {
118118
});
119119

120120
it('uses custom value for base', async () => {
121-
const client: Parameters<typeof generateCore>[1] = {
121+
const client: Parameters<typeof generateLegacyCore>[1] = {
122122
models: [],
123123
server: 'http://localhost:8080',
124124
services: [],
@@ -150,7 +150,7 @@ describe('generateCore', () => {
150150
useOptions: true,
151151
});
152152

153-
await generateCore('/', client, templates);
153+
await generateLegacyCore('/', client, templates);
154154

155155
expect(templates.core.settings).toHaveBeenCalledWith({
156156
$config: config,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('generateIndexFile', () => {
4848
}),
4949
};
5050

51-
await generateIndexFile({ files });
51+
generateIndexFile({ files });
5252

5353
files.index.write();
5454

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { describe, expect, it, vi } from 'vitest';
44

55
import type { Client } from '../../types/client';
66
import { setConfig } from '../../utils/config';
7-
import { generateOutput } from '../output';
7+
import { generateLegacyOutput } from '../output';
88
import { mockTemplates, openApi } from './mocks';
99

1010
vi.mock('node:fs');
1111

12-
describe('generateOutput', () => {
12+
describe('generateLegacyOutput', () => {
1313
it('writes to filesystem', async () => {
1414
setConfig({
1515
client: {
@@ -42,9 +42,8 @@ describe('generateOutput', () => {
4242
version: 'v1',
4343
};
4444

45-
await generateOutput({
45+
await generateLegacyOutput({
4646
client,
47-
context: undefined,
4847
openApi,
4948
templates: mockTemplates,
5049
});

packages/openapi-ts/src/generate/__tests__/schemas.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import { describe, expect, it, vi } from 'vitest';
66
import type { OpenApiV3Schema } from '../../openApi';
77
import type { Files } from '../../types/utils';
88
import { setConfig } from '../../utils/config';
9-
import { generateSchemas } from '../schemas';
9+
import { generateLegacySchemas } from '../schemas';
1010
import { openApi } from './mocks';
1111

1212
vi.mock('node:fs');
1313

14-
describe('generateSchemas', () => {
14+
describe('generateLegacySchemas', () => {
1515
it('writes to filesystem', async () => {
1616
setConfig({
1717
client: {
@@ -50,7 +50,7 @@ describe('generateSchemas', () => {
5050

5151
const files: Files = {};
5252

53-
await generateSchemas({ files, openApi });
53+
await generateLegacySchemas({ files, openApi });
5454

5555
files.schemas.write();
5656

@@ -103,7 +103,7 @@ describe('generateSchemas', () => {
103103

104104
const files: Files = {};
105105

106-
await generateSchemas({ files, openApi });
106+
await generateLegacySchemas({ files, openApi });
107107

108108
files.schemas.write();
109109

packages/openapi-ts/src/generate/__tests__/services.spec.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import type { Operation } from '../../types/client';
77
import type { Files } from '../../types/utils';
88
import { setConfig } from '../../utils/config';
99
import { TypeScriptFile } from '../files';
10-
import { generateServices } from '../services';
10+
import { generateLegacyServices } from '../services';
1111

1212
vi.mock('node:fs');
1313

14-
describe('generateServices', () => {
14+
describe('generateLegacyServices', () => {
1515
it('writes to filesystem', async () => {
1616
setConfig({
1717
client: {
@@ -36,7 +36,7 @@ describe('generateServices', () => {
3636
useOptions: false,
3737
});
3838

39-
const client: Parameters<typeof generateServices>[0]['client'] = {
39+
const client: Parameters<typeof generateLegacyServices>[0]['client'] = {
4040
models: [],
4141
server: 'http://localhost:8080',
4242
services: [
@@ -80,9 +80,8 @@ describe('generateServices', () => {
8080
name: 'types.ts',
8181
});
8282

83-
await generateServices({
83+
await generateLegacyServices({
8484
client,
85-
context: undefined,
8685
files,
8786
});
8887

@@ -120,7 +119,7 @@ describe('methodNameBuilder', () => {
120119
summary: null,
121120
};
122121

123-
const client: Parameters<typeof generateServices>[0]['client'] = {
122+
const client: Parameters<typeof generateLegacyServices>[0]['client'] = {
124123
models: [],
125124
server: 'http://localhost:8080',
126125
services: [
@@ -166,9 +165,8 @@ describe('methodNameBuilder', () => {
166165
name: 'types.ts',
167166
});
168167

169-
await generateServices({
168+
await generateLegacyServices({
170169
client,
171-
context: undefined,
172170
files,
173171
});
174172

@@ -214,9 +212,8 @@ describe('methodNameBuilder', () => {
214212
name: 'types.ts',
215213
});
216214

217-
await generateServices({
215+
await generateLegacyServices({
218216
client,
219-
context: undefined,
220217
files,
221218
});
222219

@@ -264,9 +261,8 @@ describe('methodNameBuilder', () => {
264261
name: 'types.ts',
265262
});
266263

267-
await generateServices({
264+
await generateLegacyServices({
268265
client,
269-
context: undefined,
270266
files,
271267
});
272268

packages/openapi-ts/src/generate/__tests__/types.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { describe, expect, it, vi } from 'vitest';
55

66
import { setConfig } from '../../utils/config';
77
import { TypeScriptFile } from '../files';
8-
import { generateTypes } from '../types';
8+
import { generateLegacyTypes } from '../types';
99

1010
vi.mock('node:fs');
1111

12-
describe('generateTypes', () => {
12+
describe('generateLegacyTypes', () => {
1313
it('writes to filesystem', async () => {
1414
setConfig({
1515
client: {
@@ -34,7 +34,7 @@ describe('generateTypes', () => {
3434
useOptions: true,
3535
});
3636

37-
const client: Parameters<typeof generateTypes>[0]['client'] = {
37+
const client: Parameters<typeof generateLegacyTypes>[0]['client'] = {
3838
models: [
3939
{
4040
$refs: [],
@@ -73,9 +73,8 @@ describe('generateTypes', () => {
7373
}),
7474
};
7575

76-
await generateTypes({
76+
await generateLegacyTypes({
7777
client,
78-
context: undefined,
7978
files,
8079
});
8180

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { ensureDirSync } from './utils';
1818
* @param client Client containing models, schemas, and services
1919
* @param templates The loaded handlebar templates
2020
*/
21-
export const generateClientClass = async (
21+
export const generateLegacyClientClass = async (
2222
openApi: OpenApi,
2323
outputPath: string,
2424
client: Client,

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import { copyFileSync } from 'node:fs';
22
import path from 'node:path';
33

4-
import { getConfig, isLegacyClient } from '../utils/config';
4+
import type { Config } from '../types/config';
55
import { ensureDirSync, relativeModulePath } from './utils';
66

7+
/**
8+
* Returns path to the client module. When using client packages, this will be
9+
* simply the name of the package. When bundling a client, this will be a
10+
* relative path to the bundled client folder.
11+
*/
712
export const clientModulePath = ({
13+
config,
814
sourceOutput,
915
}: {
16+
config: Config;
1017
sourceOutput: string;
11-
}) => {
12-
const config = getConfig();
13-
18+
}): string => {
1419
if (config.client.bundle) {
1520
return relativeModulePath({
1621
moduleOutput: 'client',
@@ -24,26 +29,20 @@ export const clientModulePath = ({
2429
export const clientOptionsTypeName = () => 'Options';
2530

2631
/**
27-
* (optional) Creates a `client.ts` file containing the same exports as the
28-
* client package. Creates a `client` directory containing the modules from
29-
* the client package. These files are generated only when `client.bundle` is
30-
* set to true.
32+
* Creates a `client` directory containing the same modules as the client package.
3133
*/
32-
export const generateClient = async (
33-
outputPath: string,
34-
moduleName: string,
35-
) => {
36-
const config = getConfig();
37-
38-
if (isLegacyClient(config) || !config.client.bundle) {
39-
return;
40-
}
41-
34+
export const generateClientBundle = ({
35+
name,
36+
outputPath,
37+
}: {
38+
name: string;
39+
outputPath: string;
40+
}): void => {
4241
// create directory for client modules
4342
const dirPath = path.resolve(outputPath, 'client');
4443
ensureDirSync(dirPath);
4544

46-
const clientModulePath = path.normalize(require.resolve(moduleName));
45+
const clientModulePath = path.normalize(require.resolve(name));
4746
const clientModulePathComponents = clientModulePath.split(path.sep);
4847
const clientSrcPath = [
4948
...clientModulePathComponents.slice(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type { Templates } from '../utils/handlebars';
1818
* @param client Client containing models, schemas, and services
1919
* @param templates The loaded handlebar templates
2020
*/
21-
export const generateCore = async (
21+
export const generateLegacyCore = async (
2222
outputPath: string,
2323
client: Client,
2424
templates: Templates,

0 commit comments

Comments
 (0)