Skip to content

Commit

Permalink
feat(generator): add middleware option for configuring json-schema-faker
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandryackovlev committed Aug 28, 2020
1 parent bb87524 commit 1f74457
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import {
validateQuery,
validateBody,
} from './middleware';
import { JSFOptions } from './utils';
import { JSFOptions, JSFCallback } from './utils';

export interface MiddlewareOptions {
file: string;
locale?: string;
options?: Partial<JSFOptions>;
cors?: CorsOptions;
jsfCallback?: JSFCallback;
}

export const createMockMiddleware = ({
Expand All @@ -32,13 +33,14 @@ export const createMockMiddleware = ({
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'Origin', 'Accept'],
},
jsfCallback,
}: MiddlewareOptions): express.Router => {
if (!fs.existsSync(file)) {
throw new Error('File with the openapi docs does not exist');
}

const router = createRouter(cors);
const operations = createOperations({ file, locale, options });
const operations = createOperations({ file, locale, options, callback: jsfCallback });

router.use('/{0,}', async (req, res, next) => {
res.locals.operation = await operations.match(req);
Expand Down
10 changes: 7 additions & 3 deletions src/operations/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Request } from 'express';
import SwaggerParser from '@apidevtools/swagger-parser';
import { get, toPairs } from 'lodash';

import { createGenerator, JSFOptions, JSF } from '../utils';
import { createGenerator, JSFOptions, JSF, JSFCallback } from '../utils';

import { Operation, createOperation } from './operation';

Expand All @@ -24,15 +24,17 @@ export class Operations {
file,
locale,
options,
callback,
}: {
file: string;
locale: string;
options: Partial<JSFOptions>;
callback?: JSFCallback;
}) {
this.file = file;
this.locale = locale;
this.watch();
this.generator = createGenerator(locale, options);
this.generator = createGenerator(locale, options, callback);
}

reset(): void {
Expand Down Expand Up @@ -97,8 +99,10 @@ export const createOperations = ({
file,
locale,
options,
callback,
}: {
file: string;
locale: string;
options: Partial<JSFOptions>;
}): Operations => new Operations({ file, locale, options });
callback?: JSFCallback;
}): Operations => new Operations({ file, locale, options, callback });
15 changes: 11 additions & 4 deletions src/utils/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import faker from 'faker';

export { JSONSchema, JSFOptions, JSF } from 'json-schema-faker';

export type JSFCallback = (jsf: JSF, fakerObject: typeof faker) => void;

const defaultOptions = {
optionalsProbability: 0.5,
};
Expand All @@ -18,10 +20,12 @@ export const handleExamples = (value: any): any => {
};
/* eslint-enable @typescript-eslint/no-explicit-any */

export const createGenerator: (locale?: string, options?: Partial<JSFOptions>) => JSF = (
locale = 'en',
options = defaultOptions
) => {
/* eslint-disable @typescript-eslint/no-empty-function */
export const createGenerator: (
locale?: string,
options?: Partial<JSFOptions>,
callback?: JSFCallback
) => JSF = (locale = 'en', options = defaultOptions, callback = <JSFCallback>(() => {})) => {
jsf.option({
...defaultOptions,
...options,
Expand All @@ -38,5 +42,8 @@ export const createGenerator: (locale?: string, options?: Partial<JSFOptions>) =

jsf.define('examples', handleExamples);

callback(jsf, faker);

return jsf;
};
/* eslint-enable @typescript-eslint/no-empty-function */

0 comments on commit 1f74457

Please sign in to comment.