Skip to content

Commit 49d5223

Browse files
committed
feat: add index.ts to models and service directory
1 parent 578371a commit 49d5223

13 files changed

+208
-2
lines changed

src/templates/modelsIndex.hbs

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

src/templates/servicesIndex.hbs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{{>header}}
2+
3+
{{#if @root.exportServices}}
4+
{{#if services}}
5+
{{#each services}}
6+
{{#equals @root.httpClient 'saddleback'}}
7+
export * as fetch{{{name}}}{{{@root.postfix}}} from './{{camelCase name}}{{{@root.postfix}}}{{#if ../additionalServiceFileExtension}}.service{{/if}}';
8+
{{else}}
9+
export { {{{name}}}{{{@root.postfix}}} } from './{{camelCase name}}{{{@root.postfix}}}{{#if ../additionalServiceFileExtension}}.service{{/if}}';
10+
{{/equals}}
11+
{{/each}}
12+
{{/if}}
13+
{{/if}}

src/utils/registerHandlebarTemplates.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import templateExportSaddlebackService from '../templates/exportSaddlebackServic
5757
import templateExportSchema from '../templates/exportSchema.hbs';
5858
import templateExportService from '../templates/exportService.hbs';
5959
import templateIndex from '../templates/index.hbs';
60+
import templateModelsIndex from '../templates/modelsIndex.hbs';
6061
import partialBase from '../templates/partials/base.hbs';
6162
import partialExportComposition from '../templates/partials/exportComposition.hbs';
6263
import partialExportEnum from '../templates/partials/exportEnum.hbs';
@@ -85,10 +86,13 @@ import partialTypeInterface from '../templates/partials/typeInterface.hbs';
8586
import partialTypeIntersection from '../templates/partials/typeIntersection.hbs';
8687
import partialTypeReference from '../templates/partials/typeReference.hbs';
8788
import partialTypeUnion from '../templates/partials/typeUnion.hbs';
89+
import templateServiceIndex from '../templates/servicesIndex.hbs';
8890
import { registerHandlebarHelpers } from './registerHandlebarHelpers';
8991

9092
export interface Templates {
9193
index: Handlebars.TemplateDelegate;
94+
serviceIndex: Handlebars.TemplateDelegate;
95+
modelsIndex: Handlebars.TemplateDelegate;
9296
client: Handlebars.TemplateDelegate;
9397
exports: {
9498
model: Handlebars.TemplateDelegate;
@@ -122,6 +126,8 @@ export const registerHandlebarTemplates = (root: {
122126
// Main templates (entry points for the files we write to disk)
123127
const templates: Templates = {
124128
index: Handlebars.template(templateIndex),
129+
serviceIndex: Handlebars.template(templateServiceIndex),
130+
modelsIndex: Handlebars.template(templateModelsIndex),
125131
client: Handlebars.template(templateClient),
126132
exports: {
127133
model: Handlebars.template(templateExportModel),

src/utils/writeClient.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ describe('writeClient', () => {
1818

1919
const templates: Templates = {
2020
index: () => 'index',
21+
modelsIndex: () => 'modelsIndex',
22+
serviceIndex: () => 'serviceIndex',
2123
client: () => 'client',
2224
exports: {
2325
model: () => 'model',

src/utils/writeClient.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { writeClientModels } from './writeClientModels';
1414
import { writeClientSchemas } from './writeClientSchemas';
1515
import { writeClientServices } from './writeClientServices';
1616
import { writeSaddlebackClientServices } from './writeSaddlebackClientServices';
17+
import { writeSaddlebackModelsIndex } from './writeSaddlebackModelsIndex';
18+
import { writeSaddlebackServiceIndex } from './writeSaddlebackServiceIndex';
1719

1820
/**
1921
* Write our OpenAPI client, using the given templates at the given output
@@ -128,6 +130,38 @@ export const writeClient = async (
128130
await mkdir(outputPath);
129131
await writeClientClass(client, templates, outputPath, httpClient, clientName, indent, postfix);
130132
}
133+
// write service index
134+
if (exportCore || exportServices || exportSchemas || exportModels) {
135+
await mkdir(outputPath);
136+
await writeSaddlebackServiceIndex(
137+
client,
138+
templates,
139+
outputPathServices,
140+
useUnionTypes,
141+
exportServices,
142+
postfix,
143+
httpClient,
144+
additionalModelFileExtension,
145+
additionalServiceFileExtension,
146+
clientName
147+
);
148+
}
149+
// write models index
150+
if (exportCore || exportServices || exportSchemas || exportModels) {
151+
await mkdir(outputPath);
152+
await writeSaddlebackModelsIndex(
153+
client,
154+
templates,
155+
outputPathModels,
156+
useUnionTypes,
157+
exportModels,
158+
postfix,
159+
httpClient,
160+
additionalModelFileExtension,
161+
additionalServiceFileExtension,
162+
clientName
163+
);
164+
}
131165

132166
if (exportCore || exportServices || exportSchemas || exportModels) {
133167
await mkdir(outputPath);

src/utils/writeClientClass.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ describe('writeClientClass', () => {
1818

1919
const templates: Templates = {
2020
index: () => 'index',
21+
modelsIndex: () => 'modelsIndex',
22+
serviceIndex: () => 'serviceIndex',
2123
client: () => 'client',
2224
exports: {
2325
model: () => 'model',

src/utils/writeClientCore.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ describe('writeClientCore', () => {
2020

2121
const templates: Templates = {
2222
index: () => 'index',
23+
modelsIndex: () => 'modelsIndex',
24+
serviceIndex: () => 'serviceIndex',
2325
client: () => 'client',
2426
exports: {
2527
model: () => 'model',

src/utils/writeClientIndex.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Client } from '../client/interfaces/Client';
2+
import { HttpClient } from '../HttpClient';
23
import { writeFile } from './fileSystem';
34
import type { Templates } from './registerHandlebarTemplates';
45
import { writeClientIndex } from './writeClientIndex';
5-
import { HttpClient } from '../HttpClient';
66

77
jest.mock('./fileSystem');
88

@@ -17,6 +17,8 @@ describe('writeClientIndex', () => {
1717

1818
const templates: Templates = {
1919
index: () => 'index',
20+
modelsIndex: () => 'modelsIndex',
21+
serviceIndex: () => 'serviceIndex',
2022
client: () => 'client',
2123
exports: {
2224
model: () => 'model',
@@ -36,7 +38,20 @@ describe('writeClientIndex', () => {
3638
},
3739
};
3840

39-
await writeClientIndex(client, templates, '/', true, true, true, true, true, 'Service', HttpClient.AXIOS, false, false);
41+
await writeClientIndex(
42+
client,
43+
templates,
44+
'/',
45+
true,
46+
true,
47+
true,
48+
true,
49+
true,
50+
'Service',
51+
HttpClient.AXIOS,
52+
false,
53+
false
54+
);
4055

4156
expect(writeFile).toBeCalledWith('/index.ts', 'index');
4257
});

src/utils/writeClientModels.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ describe('writeClientModels', () => {
3333

3434
const templates: Templates = {
3535
index: () => 'index',
36+
modelsIndex: () => 'modelsIndex',
37+
serviceIndex: () => 'serviceIndex',
3638
client: () => 'client',
3739
exports: {
3840
model: () => 'model',

src/utils/writeClientSchemas.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ describe('writeClientSchemas', () => {
3333

3434
const templates: Templates = {
3535
index: () => 'index',
36+
modelsIndex: () => 'modelsIndex',
37+
serviceIndex: () => 'serviceIndex',
3638
client: () => 'client',
3739
exports: {
3840
model: () => 'model',

0 commit comments

Comments
 (0)