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

Introduce the defaultInterfaceSuffix setting #376

Merged
merged 1 commit into from
Oct 18, 2023
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ export interface Settings {
* Use .label('InterfaceName') instead of .meta({className:'InterfaceName'}) for interface names
*/
useLabelAsInterfaceName: boolean;
/**
* If defined, when a schema name ends with "schema", replaces the ending in the generated type by default
* with this string.
* E.g. when this setting is "Interface", a `TestSchema` object generates a `TestInterface` type
*/
defaultInterfaceSuffix?: string;
/**
* Should interface properties be defaulted to optional or required
* @default false
Expand Down
43 changes: 43 additions & 0 deletions src/__tests__/defaultInterfaceSuffix/defaultInterfaceSuffix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { existsSync, readFileSync, rmdirSync } from 'fs';

import { convertFromDirectory } from '../../index';

const typeOutputDirectory = './src/__tests__/defaultInterfaceSuffix/interfaces';

describe('Create interfaces from schema files and applies a default interface suffix', () => {
beforeAll(() => {
if (existsSync(typeOutputDirectory)) {
rmdirSync(typeOutputDirectory, { recursive: true });
}
});

test('generates interfaces', async () => {
const result = await convertFromDirectory({
schemaDirectory: './src/__tests__/defaultInterfaceSuffix/schemas',
typeOutputDirectory,
defaultInterfaceSuffix: 'Interface'
});

expect(result).toBe(true);

const oneContent = readFileSync(`${typeOutputDirectory}/One.ts`).toString();
expect(oneContent).toBe(
`/**
* This file was automatically generated by joi-to-typescript
* Do not modify this file manually
*/

/**
* a test schema definition
*/
export interface TestInterface {
name?: string;
}

export interface TestWithMetaInterface {
name?: string;
}
`
);
});
});
11 changes: 11 additions & 0 deletions src/__tests__/defaultInterfaceSuffix/schemas/OneSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Joi from 'joi';

export const TestSchema = Joi.object({
name: Joi.string().optional()
}).description('a test schema definition');

export const TestWithMetaSchema = Joi.object({
name: Joi.string().optional()
}).meta({
myMeta: 'Hello'
});
15 changes: 10 additions & 5 deletions src/joiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ export function ensureInterfaceorTypeName(settings: Settings, details: Describe,
details.flags.label = interfaceOrTypeName;
}
} else {
// Set the meta[].className from the exportedName if missing
if (!details.metas || details.metas.length === 0) {
details.metas = [{ className: interfaceOrTypeName }];
} else {
const className = details.metas.find(meta => meta.className)?.className;
details.metas = [];
}

if (!className) {
const className = details.metas.find(meta => meta.className)?.className;

// Set the meta[].className from the exportedName if missing
if (!className) {
if (settings.defaultInterfaceSuffix && interfaceOrTypeName.toLowerCase().endsWith('schema')) {
const nameWithNewSuffix = interfaceOrTypeName.slice(0, -6) + settings.defaultInterfaceSuffix;
details.metas.push({ className: nameWithNewSuffix });
} else {
details.metas.push({ className: interfaceOrTypeName });
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export interface Settings {
* Use .label('InterfaceName') instead of .meta({className:'InterfaceName'}) for interface names
*/
readonly useLabelAsInterfaceName: boolean;
/**
* If defined, when a schema name ends with "schema", replaces the ending in the generated type by default
* with this string.
* E.g. when this setting is "Interface", a `TestSchema` object generates a `TestInterface` type
*/
readonly defaultInterfaceSuffix?: string;
/**
* Should interface properties be defaulted to optional or required
* @default false
Expand Down