Skip to content

Commit

Permalink
feat(generator): add option for examples randomization in responses
Browse files Browse the repository at this point in the history
fix #52
  • Loading branch information
aleksandryackovlev committed Jun 25, 2024
1 parent b805ce5 commit 6bb8a1e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { JSFOptions, JSFCallback } from './utils';

export interface MiddlewareOptions {
spec?: string | OpenAPIV3.Document;
options?: Partial<JSFOptions>;
options?: Partial<JSFOptions & { randomizeExamples: boolean }>;
configure?: JSFCallback;
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('generator', () => {
describe('handleExamples', () => {
it('should return the first example in the given examples object', () => {
expect(
handleExamples({
handleExamples(false)({
first: {
value: {
id: 3,
Expand All @@ -20,7 +20,7 @@ describe('generator', () => {
});

it('should return an empty string on incorrect examples', () => {
expect(handleExamples('string')).toBe('');
expect(handleExamples(false)('string')).toBe('');
});
});
});
32 changes: 19 additions & 13 deletions src/utils/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,29 @@ const defaultOptions = {

/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
export const handleExamples = (value: any): any => {
if (typeof value === 'object' && value !== null && Object.keys(value).length) {
return value[Object.keys(value)[0]].value;
}

return '';
};
export const handleExamples =
(randomizeExamples: boolean) =>
(value: any): any => {
if (typeof value === 'object' && value !== null && Object.keys(value).length) {
return randomizeExamples
? value[Object.keys(value)[Math.floor(Math.random() * Object.keys(value).length)]].value
: value[Object.keys(value)[0]].value;
}

return '';
};
/* eslint-enable @typescript-eslint/no-explicit-any */

/* eslint-disable @typescript-eslint/no-empty-function */
export const createGenerator: (options?: Partial<JSFOptions>, callback?: JSFCallback) => JSF = (
options = defaultOptions,
callback = <JSFCallback>(() => {})
) => {
export const createGenerator: (
options?: Partial<JSFOptions & { randomizeExamples: boolean }>,
callback?: JSFCallback
) => JSF = (options = defaultOptions, callback = <JSFCallback>(() => {})) => {
const { randomizeExamples, ...jsfOptions } = options;

const applyedOptions = {
...defaultOptions,
...options,
...jsfOptions,
};

const generator = cloneDeep(jsf);
Expand All @@ -37,7 +43,7 @@ export const createGenerator: (options?: Partial<JSFOptions>, callback?: JSFCall
if (applyedOptions.useExamplesValue) {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
generator.define('example', (value: any) => value);
generator.define('examples', handleExamples);
generator.define('examples', handleExamples(!!randomizeExamples));
}

callback(generator);
Expand Down

0 comments on commit 6bb8a1e

Please sign in to comment.