Skip to content

Commit 9d8a30d

Browse files
committed
add functionality to create samples alongside client
1 parent 26319f9 commit 9d8a30d

12 files changed

+1069
-0
lines changed

bin/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const params = program
1717
.option('--useOptions', 'Use options instead of arguments')
1818
.option('--useUnionTypes', 'Use union types instead of enums')
1919
.option('--exportCore <value>', 'Write core files to disk', true)
20+
.option('--exportSamples <value>', 'Write samples to disk', true)
2021
.option('--exportServices <value>', 'Write services to disk', true)
2122
.option('--exportModels <value>', 'Write models to disk', true)
2223
.option('--exportSchemas <value>', 'Write schemas to disk', false)
@@ -39,6 +40,7 @@ if (OpenAPI) {
3940
useOptions: params.useOptions,
4041
useUnionTypes: params.useUnionTypes,
4142
exportCore: JSON.parse(params.exportCore) === true,
43+
exportSamples: JSON.parse(params.exportSamples) === true,
4244
exportServices: JSON.parse(params.exportServices) === true,
4345
exportModels: JSON.parse(params.exportModels) === true,
4446
exportSchemas: JSON.parse(params.exportSchemas) === true,

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type Options = {
2020
useOptions?: boolean;
2121
useUnionTypes?: boolean;
2222
exportCore?: boolean;
23+
exportSamples?: boolean;
2324
exportServices?: boolean;
2425
exportModels?: boolean;
2526
exportSchemas?: boolean;
@@ -58,6 +59,7 @@ export const generate = async ({
5859
useOptions = false,
5960
useUnionTypes = false,
6061
exportCore = true,
62+
exportSamples = true,
6163
exportServices = true,
6264
exportModels = true,
6365
exportSchemas = false,
@@ -88,6 +90,7 @@ export const generate = async ({
8890
useOptions,
8991
useUnionTypes,
9092
exportCore,
93+
exportSamples,
9194
exportServices,
9295
exportModels,
9396
exportSchemas,
@@ -112,6 +115,7 @@ export const generate = async ({
112115
useOptions,
113116
useUnionTypes,
114117
exportCore,
118+
exportSamples,
115119
exportServices,
116120
exportModels,
117121
exportSchemas,

src/utils/registerHandlebarTemplates.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import xhrGetResponseHeader from '../templates/core/xhr/getResponseHeader.hbs';
5353
import xhrRequest from '../templates/core/xhr/request.hbs';
5454
import xhrSendRequest from '../templates/core/xhr/sendRequest.hbs';
5555
import templateExportModel from '../templates/exportModel.hbs';
56+
import templateExportSample from '../templates/exportSample.hbs';
5657
import templateExportSchema from '../templates/exportSchema.hbs';
5758
import templateExportService from '../templates/exportService.hbs';
5859
import templateIndex from '../templates/index.hbs';
@@ -92,6 +93,7 @@ export interface Templates {
9293
model: Handlebars.TemplateDelegate;
9394
schema: Handlebars.TemplateDelegate;
9495
service: Handlebars.TemplateDelegate;
96+
sample: HandlebarsTemplateDelegate;
9597
};
9698
core: {
9799
settings: Handlebars.TemplateDelegate;
@@ -124,6 +126,7 @@ export const registerHandlebarTemplates = (root: {
124126
model: Handlebars.template(templateExportModel),
125127
schema: Handlebars.template(templateExportSchema),
126128
service: Handlebars.template(templateExportService),
129+
sample: Handlebars.template(templateExportSample),
127130
},
128131
core: {
129132
settings: Handlebars.template(templateCoreSettings),

src/utils/writeClient.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('writeClient', () => {
2222
exports: {
2323
model: () => 'model',
2424
schema: () => 'schema',
25+
sample: () => 'sample',
2526
service: () => 'service',
2627
},
2728
core: {
@@ -47,6 +48,7 @@ describe('writeClient', () => {
4748
true,
4849
true,
4950
true,
51+
true,
5052
Indent.SPACE_4,
5153
'Service',
5254
'AppClient'

src/utils/writeClient.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { writeClientClass } from './writeClientClass';
1111
import { writeClientCore } from './writeClientCore';
1212
import { writeClientIndex } from './writeClientIndex';
1313
import { writeClientModels } from './writeClientModels';
14+
import { writeClientSamples } from './writeClientSamples';
1415
import { writeClientSchemas } from './writeClientSchemas';
1516
import { writeClientServices } from './writeClientServices';
1617

@@ -24,6 +25,7 @@ import { writeClientServices } from './writeClientServices';
2425
* @param useUnionTypes Use union types instead of enums
2526
* @param exportCore Generate core client classes
2627
* @param exportServices Generate services
28+
* @param exportSamples Generate samples
2729
* @param exportModels Generate models
2830
* @param exportSchemas Generate schemas
2931
* @param exportSchemas Generate schemas
@@ -41,6 +43,7 @@ export const writeClient = async (
4143
useOptions: boolean,
4244
useUnionTypes: boolean,
4345
exportCore: boolean,
46+
exportSamples: boolean,
4447
exportServices: boolean,
4548
exportModels: boolean,
4649
exportSchemas: boolean,
@@ -55,6 +58,7 @@ export const writeClient = async (
5558
const outputPathModels = resolve(outputPath, 'models');
5659
const outputPathSchemas = resolve(outputPath, 'schemas');
5760
const outputPathServices = resolve(outputPath, 'services');
61+
const outputPathSamples = resolve(outputPath, 'samples');
5862

5963
if (!isSubDirectory(process.cwd(), output)) {
6064
throw new Error(`Output folder is not a subdirectory of the current working directory`);
@@ -82,6 +86,22 @@ export const writeClient = async (
8286
);
8387
}
8488

89+
if (exportSamples) {
90+
await rmdir(outputPathSamples);
91+
await mkdir(outputPathSamples);
92+
await writeClientSamples(
93+
client.services,
94+
templates,
95+
outputPathSamples,
96+
httpClient,
97+
useUnionTypes,
98+
useOptions,
99+
indent,
100+
'',
101+
clientName
102+
);
103+
}
104+
85105
if (exportSchemas) {
86106
await rmdir(outputPathSchemas);
87107
await mkdir(outputPathSchemas);

src/utils/writeClientClass.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('writeClientClass', () => {
2121
client: () => 'client',
2222
exports: {
2323
model: () => 'model',
24+
sample: () => 'sample',
2425
schema: () => 'schema',
2526
service: () => 'service',
2627
},

src/utils/writeClientCore.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('writeClientCore', () => {
2323
client: () => 'client',
2424
exports: {
2525
model: () => 'model',
26+
sample: () => 'sample',
2627
schema: () => 'schema',
2728
service: () => 'service',
2829
},

src/utils/writeClientIndex.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('writeClientIndex', () => {
1919
client: () => 'client',
2020
exports: {
2121
model: () => 'model',
22+
sample: () => 'sample',
2223
schema: () => 'schema',
2324
service: () => 'service',
2425
},

src/utils/writeClientModels.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe('writeClientModels', () => {
3636
client: () => 'client',
3737
exports: {
3838
model: () => 'model',
39+
sample: () => 'sample',
3940
schema: () => 'schema',
4041
service: () => 'service',
4142
},

src/utils/writeClientSchemas.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe('writeClientSchemas', () => {
3636
client: () => 'client',
3737
exports: {
3838
model: () => 'model',
39+
sample: () => 'sample',
3940
schema: () => 'schema',
4041
service: () => 'service',
4142
},

0 commit comments

Comments
 (0)