Skip to content

Commit 0af9539

Browse files
feat: new index strategy to open more bundling strategies
1 parent 52cef38 commit 0af9539

File tree

8 files changed

+210
-24
lines changed

8 files changed

+210
-24
lines changed

src/templates/index.hbs renamed to src/templates/indexes/index.hbs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ export { {{{clientName}}} } from './{{{clientName}}}';
55
{{#if @root.exportHooks}}
66
export { use{{{pascalCase clientName}}}, {{{pascalCase clientName}}}Provider } from './use{{{pascalCase clientName}}}';
77
{{/if}}
8-
98
{{/if}}
9+
1010
{{#if @root.exportCore}}
1111
export { ApiError } from './core/ApiError';
1212
{{#if @root.exportClient}}
@@ -16,35 +16,23 @@ export { CancelablePromise, CancelError } from './core/CancelablePromise';
1616
export { OpenAPI } from './core/OpenAPI';
1717
export type { OpenAPIConfig } from './core/OpenAPI';
1818
{{/if}}
19+
1920
{{#if @root.exportModels}}
2021
{{#if models}}
21-
22-
{{#each models}}
23-
{{#if @root.useUnionTypes}}
24-
export type { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './models/{{{name}}}';
25-
{{else if enum}}
26-
export { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './models/{{{name}}}';
27-
{{else if enums}}
28-
export { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './models/{{{name}}}';
29-
{{else}}
30-
export type { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './models/{{{name}}}';
31-
{{/if}}
32-
{{/each}}
22+
export * from './models';
3323
{{/if}}
3424
{{/if}}
25+
3526
{{#if @root.exportSchemas}}
3627
{{#if models}}
37-
3828
{{#each models}}
3929
export { ${{{name}}} } from './schemas/${{{name}}}';
4030
{{/each}}
4131
{{/if}}
4232
{{/if}}
33+
4334
{{#if @root.exportServices}}
4435
{{#if services}}
45-
46-
{{#each services}}
47-
export { {{{name}}}{{{@root.postfixServices}}} } from './services/{{{name}}}{{{@root.postfixServices}}}';
48-
{{/each}}
36+
export * from './services';
4937
{{/if}}
5038
{{/if}}

src/templates/indexes/indexHooks.hbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{{>header}}
2+
3+
{{#if services}}
4+
{{#each services}}
5+
export * from './use{{{pascalCase name}}}';
6+
{{/each}}
7+
{{/if}}

src/templates/indexes/indexModels.hbs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{>header}}
2+
3+
{{#if @root.exportModels}}
4+
{{#if models}}
5+
{{#each models}}
6+
{{#if @root.useUnionTypes}}
7+
export type { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './{{{name}}}';
8+
{{else if enum}}
9+
export { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './{{{name}}}';
10+
{{else if enums}}
11+
export { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './{{{name}}}';
12+
{{else}}
13+
export type { {{{name}}}{{#if @root.postfixModels}} as {{{name}}}{{{@root.postfixModels}}}{{/if}} } from './{{{name}}}';
14+
{{/if}}
15+
{{/each}}
16+
{{/if}}
17+
{{/if}}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{{>header}}
2+
3+
{{#if services}}
4+
{{#each services}}
5+
export * from './{{{name}}}{{{@root.postfixServices}}}';
6+
{{/each}}
7+
{{/if}}

src/utils/__mocks__/templates.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { Templates } from '../registerHandlebarTemplates';
22

33
export const templates: Templates = {
4-
index: () => 'index',
4+
indexes: {
5+
index: () => 'index',
6+
indexModels: () => 'indexModels',
7+
indexServices: () => 'indexServices',
8+
indexHooks: () => 'indexHooks',
9+
},
510
client: () => 'client',
611
useClient: () => 'useClient',
712
exports: {

src/utils/registerHandlebarTemplates.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ import templateExportModel from '../templates/exportModel.hbs';
5252
import templateExportSchema from '../templates/exportSchema.hbs';
5353
import templateExportService from '../templates/exportService.hbs';
5454
import templateExportHook from '../templates/exportHook.hbs';
55-
import templateIndex from '../templates/index.hbs';
55+
import templateIndex from '../templates/indexes/index.hbs';
56+
import templateIndexModels from '../templates/indexes/indexModels.hbs';
57+
import templateIndexServices from '../templates/indexes/indexServices.hbs';
58+
import templateIndexHooks from '../templates/indexes/indexHooks.hbs';
5659
import partialBase from '../templates/partials/base.hbs';
5760
import partialExportComposition from '../templates/partials/exportComposition.hbs';
5861
import partialExportEnum from '../templates/partials/exportEnum.hbs';
@@ -84,7 +87,12 @@ import partialTypeUnion from '../templates/partials/typeUnion.hbs';
8487
import { registerHandlebarHelpers } from './registerHandlebarHelpers';
8588

8689
export interface Templates {
87-
index: Handlebars.TemplateDelegate;
90+
indexes: {
91+
index: Handlebars.TemplateDelegate;
92+
indexModels: Handlebars.TemplateDelegate;
93+
indexServices: Handlebars.TemplateDelegate;
94+
indexHooks: Handlebars.TemplateDelegate;
95+
};
8896
client: Handlebars.TemplateDelegate;
8997
useClient: Handlebars.TemplateDelegate;
9098
exports: {
@@ -119,7 +127,12 @@ export const registerHandlebarTemplates = (root: {
119127

120128
// Main templates (entry points for the files we write to disk)
121129
const templates: Templates = {
122-
index: Handlebars.template(templateIndex),
130+
indexes: {
131+
index: Handlebars.template(templateIndex),
132+
indexModels: Handlebars.template(templateIndexModels),
133+
indexServices: Handlebars.template(templateIndexServices),
134+
indexHooks: Handlebars.template(templateIndexHooks),
135+
},
123136
client: Handlebars.template(templateClient),
124137
useClient: Handlebars.template(templateUseClient),
125138
exports: {

src/utils/writeClient.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import type { Templates } from './registerHandlebarTemplates';
1010
import { writeClientClass } from './writeClientClass';
1111
import { writeClientCore } from './writeClientCore';
1212
import { writeClientHooks } from './writeClientHooks';
13-
import { writeClientIndex } from './writeClientIndex';
13+
import {
14+
writeClientIndex,
15+
writeClientIndexHooks,
16+
writeClientIndexModels,
17+
writeClientIndexServices,
18+
} from './writeClientIndex';
1419
import { writeClientModels } from './writeClientModels';
1520
import { writeClientSchemas } from './writeClientSchemas';
1621
import { writeClientServices } from './writeClientServices';
@@ -85,6 +90,20 @@ export const writeClient = async (
8590
postfixServices,
8691
clientName
8792
);
93+
await writeClientIndexServices(
94+
client,
95+
templates,
96+
outputPathServices,
97+
useUnionTypes,
98+
exportCore,
99+
exportServices,
100+
exportModels,
101+
exportHooks,
102+
exportSchemas,
103+
postfixServices,
104+
postfixModels,
105+
clientName
106+
);
88107
}
89108

90109
if (exportHooks) {
@@ -101,6 +120,20 @@ export const writeClient = async (
101120
postfixServices,
102121
clientName
103122
);
123+
await writeClientIndexHooks(
124+
client,
125+
templates,
126+
outputPathHooks,
127+
useUnionTypes,
128+
exportCore,
129+
exportServices,
130+
exportModels,
131+
exportHooks,
132+
exportSchemas,
133+
postfixServices,
134+
postfixModels,
135+
clientName
136+
);
104137
}
105138

106139
if (exportSchemas) {
@@ -113,6 +146,20 @@ export const writeClient = async (
113146
await rmdir(outputPathModels);
114147
await mkdir(outputPathModels);
115148
await writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes, indent);
149+
await writeClientIndexModels(
150+
client,
151+
templates,
152+
outputPathModels,
153+
useUnionTypes,
154+
exportCore,
155+
exportServices,
156+
exportModels,
157+
exportHooks,
158+
exportSchemas,
159+
postfixServices,
160+
postfixModels,
161+
clientName
162+
);
116163
}
117164

118165
if (isDefined(clientName)) {

src/utils/writeClientIndex.ts

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,109 @@ export const writeClientIndex = async (
3838
postfixModels: string,
3939
clientName?: string
4040
): Promise<void> => {
41-
const templateResult = templates.index({
41+
const templateResult = templates.indexes.index({
42+
exportCore,
43+
exportServices,
44+
exportModels,
45+
exportHooks,
46+
exportSchemas,
47+
useUnionTypes,
48+
postfixServices,
49+
postfixModels,
50+
clientName,
51+
server: client.server,
52+
version: client.version,
53+
models: sortModelsByName(client.models),
54+
services: sortServicesByName(client.services),
55+
exportClient: isDefined(clientName),
56+
});
57+
58+
await writeFile(resolve(outputPath, 'index.ts'), templateResult);
59+
};
60+
61+
export const writeClientIndexModels = async (
62+
client: Client,
63+
templates: Templates,
64+
outputPath: string,
65+
useUnionTypes: boolean,
66+
exportCore: boolean,
67+
exportServices: boolean,
68+
exportModels: boolean,
69+
exportHooks: boolean,
70+
exportSchemas: boolean,
71+
postfixServices: string,
72+
postfixModels: string,
73+
clientName?: string
74+
): Promise<void> => {
75+
const templateResult = templates.indexes.indexModels({
76+
exportCore,
77+
exportServices,
78+
exportModels,
79+
exportHooks,
80+
exportSchemas,
81+
useUnionTypes,
82+
postfixServices,
83+
postfixModels,
84+
clientName,
85+
server: client.server,
86+
version: client.version,
87+
models: sortModelsByName(client.models),
88+
services: sortServicesByName(client.services),
89+
exportClient: isDefined(clientName),
90+
});
91+
92+
await writeFile(resolve(outputPath, 'index.ts'), templateResult);
93+
};
94+
95+
export const writeClientIndexServices = async (
96+
client: Client,
97+
templates: Templates,
98+
outputPath: string,
99+
useUnionTypes: boolean,
100+
exportCore: boolean,
101+
exportServices: boolean,
102+
exportModels: boolean,
103+
exportHooks: boolean,
104+
exportSchemas: boolean,
105+
postfixServices: string,
106+
postfixModels: string,
107+
clientName?: string
108+
): Promise<void> => {
109+
const templateResult = templates.indexes.indexServices({
110+
exportCore,
111+
exportServices,
112+
exportModels,
113+
exportHooks,
114+
exportSchemas,
115+
useUnionTypes,
116+
postfixServices,
117+
postfixModels,
118+
clientName,
119+
server: client.server,
120+
version: client.version,
121+
models: sortModelsByName(client.models),
122+
services: sortServicesByName(client.services),
123+
exportClient: isDefined(clientName),
124+
});
125+
126+
await writeFile(resolve(outputPath, 'index.ts'), templateResult);
127+
};
128+
129+
export const writeClientIndexHooks = async (
130+
client: Client,
131+
templates: Templates,
132+
outputPath: string,
133+
useUnionTypes: boolean,
134+
exportCore: boolean,
135+
exportServices: boolean,
136+
exportModels: boolean,
137+
exportHooks: boolean,
138+
exportSchemas: boolean,
139+
postfixServices: string,
140+
postfixModels: string,
141+
clientName?: string
142+
): Promise<void> => {
143+
const templateResult = templates.indexes.indexHooks({
42144
exportCore,
43145
exportServices,
44146
exportModels,

0 commit comments

Comments
 (0)