Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add index.ts files for models, schemas, and services #137

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/orange-keys-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": minor
---

Add `index.ts` file to models, schemas, and services
3 changes: 0 additions & 3 deletions rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ export function handlebarsPlugin(): Plugin {
escapeDescription: true,
escapeNewline: true,
exactArray: true,
exportsModels: true,
exportsSchemas: true,
exportsServices: true,
ifdef: true,
ifOperationDataOptional: true,
intersection: true,
Expand Down
14 changes: 9 additions & 5 deletions src/templates/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ export { OpenAPI } from './core/OpenAPI';
export type { OpenAPIConfig } from './core/OpenAPI';
{{/if}}

{{{exportsModels}}}

{{{exportsSchemas}}}

{{{exportsServices}}}
{{#if @root.$config.exportModels}}
export * from './models'
{{/if}}
{{#if @root.$config.exportSchemas}}
export * from './schemas'
{{/if}}
{{#if @root.$config.exportServices}}
export * from './services'
{{/if}}
59 changes: 0 additions & 59 deletions src/utils/handlebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,53 +99,6 @@ const escapeComment = (value: string) =>
.replace(/\/\*/g, '*')
.replace(/\r?\n(.*)/g, (_, w) => `${EOL} * ${w.trim()}`);

const exportsModels = (config: Config, client: Client) => {
if (!config.exportModels) {
return '';
}
const path = './models/';
const output = client.models.map(model => {
const importedModel = config.postfixModels
? `${model.name} as ${model.name + config.postfixModels}`
: model.name;
let result = [`export type { ${importedModel} } from '${path + model.name}';`];
if (config.enums && (model.enum.length || model.enums.length)) {
const names = model.enums.map(enumerator => enumerator.name).filter(Boolean);
const enumExports = names.length ? names : [model.name];
const enumExportsString = enumExports.map(name => enumName(name)).join(', ');
result = [...result, `export { ${enumExportsString} } from '${path + model.name}';`];
}
return result.join('\n');
});
return output.join('\n');
};

const exportsSchemas = (config: Config, client: Client) => {
if (!config.exportSchemas) {
return '';
}
const path = './schemas/';
const output = client.models.map(model => {
const name = `$${model.name}`;
const result = [`export { ${name} } from '${path + name}';`];
return result.join('\n');
});
return output.join('\n');
};

const exportsServices = (config: Config, client: Client) => {
if (!config.exportServices) {
return '';
}
const path = './services/';
const output = client.services.map(service => {
const name = service.name + config.postfixServices;
const result = [`export { ${name} } from '${path + name}';`];
return result.join('\n');
});
return output.join('\n');
};

const modelImports = (model: Model | Service, path: string) => {
const output = model.imports.map(item => `import type { ${item} } from '${path + item}';`);
return output.join('\n');
Expand Down Expand Up @@ -237,18 +190,6 @@ export const registerHandlebarHelpers = (config: Config, client: Client): void =
return options.inverse(this);
});

Handlebars.registerHelper('exportsModels', function () {
return exportsModels(config, client);
});

Handlebars.registerHelper('exportsSchemas', function () {
return exportsSchemas(config, client);
});

Handlebars.registerHelper('exportsServices', function () {
return exportsServices(config, client);
});

Handlebars.registerHelper('ifdef', function (this: unknown, ...args): string {
const options = args.pop();
if (!args.every(value => !value)) {
Expand Down
24 changes: 2 additions & 22 deletions src/utils/write/__tests__/class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { writeFileSync } from 'node:fs';
import { describe, expect, it, vi } from 'vitest';

import { writeClientClass } from '../class';
import { mockTemplates } from './mocks';

vi.mock('node:fs');

Expand All @@ -15,28 +16,7 @@ describe('writeClientClass', () => {
services: [],
};

const templates: Parameters<typeof writeClientClass>[1] = {
client: () => 'client',
core: {
apiError: () => 'apiError',
apiRequestOptions: () => 'apiRequestOptions',
apiResult: () => 'apiResult',
baseHttpRequest: () => 'baseHttpRequest',
cancelablePromise: () => 'cancelablePromise',
httpRequest: () => 'httpRequest',
request: () => 'request',
settings: () => 'settings',
types: () => 'types',
},
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
index: () => 'index',
};

await writeClientClass(client, templates, './dist', {
await writeClientClass(client, mockTemplates, './dist', {
client: 'fetch',
enums: true,
exportCore: true,
Expand Down
24 changes: 2 additions & 22 deletions src/utils/write/__tests__/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { describe, expect, it, vi } from 'vitest';

import { writeClient } from '../client';
import { mockTemplates } from './mocks';

vi.mock('node:fs');

Expand All @@ -15,28 +16,7 @@ describe('writeClient', () => {
services: [],
};

const templates: Parameters<typeof writeClient>[1] = {
client: () => 'client',
core: {
apiError: () => 'apiError',
apiRequestOptions: () => 'apiRequestOptions',
apiResult: () => 'apiResult',
baseHttpRequest: () => 'baseHttpRequest',
cancelablePromise: () => 'cancelablePromise',
httpRequest: () => 'httpRequest',
request: () => 'request',
settings: () => 'settings',
types: () => 'types',
},
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
index: () => 'index',
};

await writeClient(client, templates, {
await writeClient(client, mockTemplates, {
client: 'fetch',
enums: true,
exportCore: true,
Expand Down
23 changes: 2 additions & 21 deletions src/utils/write/__tests__/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,14 @@ import path from 'node:path';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { writeClientCore } from '../core';
import { mockTemplates } from './mocks';

vi.mock('node:fs');

describe('writeClientCore', () => {
let templates: Parameters<typeof writeClientCore>[1];
beforeEach(() => {
const _templates: Parameters<typeof writeClientCore>[1] = {
client: () => 'client',
core: {
apiError: () => 'apiError',
apiRequestOptions: () => 'apiRequestOptions',
apiResult: () => 'apiResult',
baseHttpRequest: () => 'baseHttpRequest',
cancelablePromise: () => 'cancelablePromise',
httpRequest: () => 'httpRequest',
request: () => 'request',
settings: vi.fn().mockReturnValue('settings'),
types: () => 'types',
},
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
index: () => 'index',
};
templates = _templates;
templates = mockTemplates;
});

it('should write to filesystem', async () => {
Expand Down
24 changes: 2 additions & 22 deletions src/utils/write/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'node:path';
import { describe, expect, it, vi } from 'vitest';

import { writeClientIndex } from '../index';
import { mockTemplates } from './mocks';

vi.mock('node:fs');

Expand All @@ -16,28 +17,7 @@ describe('writeClientIndex', () => {
services: [],
};

const templates: Parameters<typeof writeClientIndex>[1] = {
client: () => 'client',
core: {
apiError: () => 'apiError',
apiRequestOptions: () => 'apiRequestOptions',
apiResult: () => 'apiResult',
baseHttpRequest: () => 'baseHttpRequest',
cancelablePromise: () => 'cancelablePromise',
httpRequest: () => 'httpRequest',
request: () => 'request',
settings: () => 'settings',
types: () => 'types',
},
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
index: () => 'index',
};

await writeClientIndex(client, templates, '/', {
await writeClientIndex(client, mockTemplates, '/', {
client: 'fetch',
enums: true,
exportCore: true,
Expand Down
24 changes: 24 additions & 0 deletions src/utils/write/__tests__/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { vi } from 'vitest';

import type { Templates } from '../../handlebars';

export const mockTemplates: Templates = {
client: vi.fn().mockReturnValue('client'),
core: {
apiError: vi.fn().mockReturnValue('apiError'),
apiRequestOptions: vi.fn().mockReturnValue('apiRequestOptions'),
apiResult: vi.fn().mockReturnValue('apiResult'),
baseHttpRequest: vi.fn().mockReturnValue('baseHttpRequest'),
cancelablePromise: vi.fn().mockReturnValue('cancelablePromise'),
httpRequest: vi.fn().mockReturnValue('httpRequest'),
request: vi.fn().mockReturnValue('request'),
settings: vi.fn().mockReturnValue('settings'),
types: vi.fn().mockReturnValue('types'),
},
exports: {
model: vi.fn().mockReturnValue('model'),
schema: vi.fn().mockReturnValue('schema'),
service: vi.fn().mockReturnValue('service'),
},
index: vi.fn().mockReturnValue('index'),
};
24 changes: 2 additions & 22 deletions src/utils/write/__tests__/models.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'node:path';
import { describe, expect, it, vi } from 'vitest';

import { writeClientModels } from '../models';
import { mockTemplates } from './mocks';

vi.mock('node:fs');

Expand Down Expand Up @@ -35,28 +36,7 @@ describe('writeClientModels', () => {
services: [],
};

const templates: Parameters<typeof writeClientModels>[1] = {
client: () => 'client',
core: {
apiError: () => 'apiError',
apiRequestOptions: () => 'apiRequestOptions',
apiResult: () => 'apiResult',
baseHttpRequest: () => 'baseHttpRequest',
cancelablePromise: () => 'cancelablePromise',
httpRequest: () => 'httpRequest',
request: () => 'request',
settings: () => 'settings',
types: () => 'types',
},
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
index: () => 'index',
};

await writeClientModels(client, templates, '/', {
await writeClientModels(client, mockTemplates, '/', {
client: 'fetch',
enums: true,
exportCore: true,
Expand Down
24 changes: 2 additions & 22 deletions src/utils/write/__tests__/schemas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'node:path';
import { describe, expect, it, vi } from 'vitest';

import { writeClientSchemas } from '../schemas';
import { mockTemplates } from './mocks';

vi.mock('node:fs');

Expand Down Expand Up @@ -35,28 +36,7 @@ describe('writeClientSchemas', () => {
services: [],
};

const templates: Parameters<typeof writeClientSchemas>[1] = {
client: () => 'client',
core: {
apiError: () => 'apiError',
apiRequestOptions: () => 'apiRequestOptions',
apiResult: () => 'apiResult',
baseHttpRequest: () => 'baseHttpRequest',
cancelablePromise: () => 'cancelablePromise',
httpRequest: () => 'httpRequest',
request: () => 'request',
settings: () => 'settings',
types: () => 'types',
},
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
index: () => 'index',
};

await writeClientSchemas(client, templates, '/', {
await writeClientSchemas(client, mockTemplates, '/', {
client: 'fetch',
enums: true,
exportCore: true,
Expand Down
24 changes: 2 additions & 22 deletions src/utils/write/__tests__/services.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'node:path';
import { describe, expect, it, vi } from 'vitest';

import { writeClientServices } from '../services';
import { mockTemplates } from './mocks';

vi.mock('node:fs');

Expand All @@ -23,28 +24,7 @@ describe('writeClientServices', () => {
],
};

const templates: Parameters<typeof writeClientServices>[1] = {
client: () => 'client',
core: {
apiError: () => 'apiError',
apiRequestOptions: () => 'apiRequestOptions',
apiResult: () => 'apiResult',
baseHttpRequest: () => 'baseHttpRequest',
cancelablePromise: () => 'cancelablePromise',
httpRequest: () => 'httpRequest',
request: () => 'request',
settings: () => 'settings',
types: () => 'types',
},
exports: {
model: () => 'model',
schema: () => 'schema',
service: () => 'service',
},
index: () => 'index',
};

await writeClientServices(client, templates, '/', {
await writeClientServices(client, mockTemplates, '/', {
client: 'fetch',
enums: false,
exportCore: true,
Expand Down
Loading