mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	Feat: update request params creation method
This commit is contained in:
		
							parent
							
								
									f39faeaead
								
							
						
					
					
						commit
						2594f0051a
					
				@ -1,76 +1,80 @@
 | 
				
			|||||||
import { SchemaObject } from 'ajv';
 | 
					import { SchemaObject } from 'ajv';
 | 
				
			||||||
import fc, { Arbitrary } from 'fast-check';
 | 
					import fc, { Arbitrary } from 'fast-check';
 | 
				
			||||||
 | 
					import { urlFriendlyString } from '../../../test/arbitraries.test';
 | 
				
			||||||
import {
 | 
					import {
 | 
				
			||||||
    createRequestParameters,
 | 
					    createRequestParameters,
 | 
				
			||||||
    ParameterDescription,
 | 
					 | 
				
			||||||
    ParameterDetails,
 | 
					    ParameterDetails,
 | 
				
			||||||
    Parameters,
 | 
					    Parameters,
 | 
				
			||||||
 | 
					    ParameterType,
 | 
				
			||||||
 | 
					    toParamObject,
 | 
				
			||||||
} from './request-parameters';
 | 
					} from './request-parameters';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const paramDesc = <T, U>(
 | 
					const paramName = urlFriendlyString;
 | 
				
			||||||
    typeName: U,
 | 
					
 | 
				
			||||||
    gen: Arbitrary<T>,
 | 
					const paramDesc = <T>(
 | 
				
			||||||
): Arbitrary<ParameterDescription<U, T>> =>
 | 
					    typeName: ParameterType,
 | 
				
			||||||
 | 
					    arb: Arbitrary<T>,
 | 
				
			||||||
 | 
					): Arbitrary<ParameterDetails<T>> =>
 | 
				
			||||||
    fc.record(
 | 
					    fc.record(
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            type: fc.constant(typeName),
 | 
					            type: fc.constant(typeName),
 | 
				
			||||||
            default: gen,
 | 
					            description: fc.string(),
 | 
				
			||||||
            enum: fc.array(gen, { minLength: 1 }),
 | 
					            default: arb,
 | 
				
			||||||
 | 
					            enum: fc.array(arb, { minLength: 1 }) as Arbitrary<[T, ...T[]]>,
 | 
				
			||||||
 | 
					            example: arb,
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        { requiredKeys: ['type'] },
 | 
					        { requiredKeys: ['type', 'description'] },
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const parameterDescription = (): Arbitrary<ParameterDescription<any, any>> =>
 | 
					const parameterDescription = (): Arbitrary<ParameterDetails<any>> =>
 | 
				
			||||||
    fc.oneof(
 | 
					    fc.oneof(
 | 
				
			||||||
        paramDesc('boolean', fc.boolean()),
 | 
					        paramDesc('boolean', fc.boolean()),
 | 
				
			||||||
        paramDesc('string', fc.string()),
 | 
					        paramDesc('string', fc.string()),
 | 
				
			||||||
        paramDesc('number', fc.integer()),
 | 
					        paramDesc('number', fc.integer()),
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const paramDetails = (): Arbitrary<ParameterDetails> =>
 | 
					 | 
				
			||||||
    fc
 | 
					 | 
				
			||||||
        .tuple(fc.string(), parameterDescription())
 | 
					 | 
				
			||||||
        .map(([description, deets]) => ({
 | 
					 | 
				
			||||||
            description,
 | 
					 | 
				
			||||||
            ...deets,
 | 
					 | 
				
			||||||
        }));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const parameterDetails = (): Arbitrary<Parameters> =>
 | 
					const parameterDetails = (): Arbitrary<Parameters> =>
 | 
				
			||||||
    fc.dictionary(fc.string({ minLength: 1 }), paramDetails());
 | 
					    fc.dictionary(paramName(), parameterDescription());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
describe('request parameter utils', () => {
 | 
					describe('request parameter utils', () => {
 | 
				
			||||||
 | 
					    it('turns a name and a parameter details description into a parameter object', () => {
 | 
				
			||||||
 | 
					        fc.assert(
 | 
				
			||||||
 | 
					            fc.property(
 | 
				
			||||||
 | 
					                paramName(),
 | 
				
			||||||
 | 
					                parameterDescription(),
 | 
				
			||||||
 | 
					                (name, details) => {
 | 
				
			||||||
 | 
					                    const result = toParamObject(name, details);
 | 
				
			||||||
 | 
					                    const schema: SchemaObject = result.schema;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    return (
 | 
				
			||||||
 | 
					                        result.name === name &&
 | 
				
			||||||
 | 
					                        schema.type === details.type &&
 | 
				
			||||||
 | 
					                        result.description === details.description &&
 | 
				
			||||||
 | 
					                        result.example === details.example &&
 | 
				
			||||||
 | 
					                        schema.enum === details.enum &&
 | 
				
			||||||
 | 
					                        schema.default === details.default
 | 
				
			||||||
 | 
					                    );
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					            ),
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    it('turns an object of names and descriptions into a an expected parameter list', () => {
 | 
					    it('turns an object of names and descriptions into a an expected parameter list', () => {
 | 
				
			||||||
        fc.assert(
 | 
					        fc.assert(
 | 
				
			||||||
            fc.property(parameterDetails(), fc.context(), (parameters, ctx) => {
 | 
					            fc.property(parameterDetails(), fc.context(), (parameters, ctx) => {
 | 
				
			||||||
                const result = createRequestParameters(parameters);
 | 
					                const result = createRequestParameters(parameters);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                ctx.log(JSON.stringify(parameters));
 | 
					                ctx.log(JSON.stringify(parameters));
 | 
				
			||||||
                // ctx.log(JSON.stringify(result));
 | 
					                ctx.log(JSON.stringify(result));
 | 
				
			||||||
                return false;
 | 
					
 | 
				
			||||||
                // return result.every((paramsObject) => {
 | 
					                return result.every(
 | 
				
			||||||
                //     return false;
 | 
					                    (paramsObject) =>
 | 
				
			||||||
                // });
 | 
					                        paramsObject.description ===
 | 
				
			||||||
 | 
					                            parameters[paramsObject.name].description &&
 | 
				
			||||||
 | 
					                        (paramsObject.schema as SchemaObject).type ===
 | 
				
			||||||
 | 
					                            parameters[paramsObject.name].type,
 | 
				
			||||||
 | 
					                );
 | 
				
			||||||
            }),
 | 
					            }),
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					 | 
				
			||||||
    // it('assigns parameter descriptions correctly', () => {
 | 
					 | 
				
			||||||
    //     fc.assert(fc.property());
 | 
					 | 
				
			||||||
    // });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // it('says every parameter is of type string and goes in the query', () => {
 | 
					 | 
				
			||||||
    //     fc.assert(
 | 
					 | 
				
			||||||
    //         fc.property(
 | 
					 | 
				
			||||||
    //             fc.dictionary(fc.string({ minLength: 1 }), fc.string()),
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    //             (parameters) => {
 | 
					 | 
				
			||||||
    //                 return createRequestParameters(parameters).every(
 | 
					 | 
				
			||||||
    //                     (paramsObject) =>
 | 
					 | 
				
			||||||
    //                         (paramsObject.schema as SchemaObject).type ===
 | 
					 | 
				
			||||||
    //                             'string' && paramsObject.in === 'query',
 | 
					 | 
				
			||||||
    //                 );
 | 
					 | 
				
			||||||
    //             },
 | 
					 | 
				
			||||||
    //         ),
 | 
					 | 
				
			||||||
    //     );
 | 
					 | 
				
			||||||
    // });
 | 
					 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
@ -1,29 +1,35 @@
 | 
				
			|||||||
import { OpenAPIV3 } from 'openapi-types';
 | 
					import { OpenAPIV3 } from 'openapi-types';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export type ParameterDescription<TypeName, T> = {
 | 
					export type ParameterType = OpenAPIV3.NonArraySchemaObjectType;
 | 
				
			||||||
    type: TypeName;
 | 
					
 | 
				
			||||||
    default?: T;
 | 
					export type ParameterDetails<U> = {
 | 
				
			||||||
    enum?: T[];
 | 
					    description: string;
 | 
				
			||||||
 | 
					    type: ParameterType;
 | 
				
			||||||
 | 
					    default?: U;
 | 
				
			||||||
 | 
					    enum?: [U, ...U[]];
 | 
				
			||||||
 | 
					    example?: U;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export type ParameterDetails = {
 | 
					export type Parameters = {
 | 
				
			||||||
    description: string;
 | 
					    [parameterName: string]: ParameterDetails<unknown>;
 | 
				
			||||||
} & (
 | 
					};
 | 
				
			||||||
    | ParameterDescription<'boolean', boolean>
 | 
					 | 
				
			||||||
    | ParameterDescription<'string', string>
 | 
					 | 
				
			||||||
    | ParameterDescription<'number', number>
 | 
					 | 
				
			||||||
);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
export type Parameters = Record<ParameterName, ParameterDetails>;
 | 
					export const toParamObject = (
 | 
				
			||||||
 | 
					    name: string,
 | 
				
			||||||
type ParameterName = string;
 | 
					    details: ParameterDetails<unknown>,
 | 
				
			||||||
 | 
					): OpenAPIV3.ParameterObject => ({
 | 
				
			||||||
export const createRequestParameters = (
 | 
					 | 
				
			||||||
    params: Parameters,
 | 
					 | 
				
			||||||
): OpenAPIV3.ParameterObject[] =>
 | 
					 | 
				
			||||||
    Object.entries(params).map(([name, deets]) => ({
 | 
					 | 
				
			||||||
    name,
 | 
					    name,
 | 
				
			||||||
        description: deets.description,
 | 
					    example: details.example,
 | 
				
			||||||
        schema: { type: 'string' },
 | 
					    description: details.description,
 | 
				
			||||||
 | 
					    schema: {
 | 
				
			||||||
 | 
					        type: details.type,
 | 
				
			||||||
 | 
					        enum: details.enum,
 | 
				
			||||||
 | 
					        default: details.default,
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
    in: 'query',
 | 
					    in: 'query',
 | 
				
			||||||
    }));
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const createRequestParameters = (params: {
 | 
				
			||||||
 | 
					    [parameterName: string]: ParameterDetails<unknown>;
 | 
				
			||||||
 | 
					}): OpenAPIV3.ParameterObject[] =>
 | 
				
			||||||
 | 
					    Object.entries(params).map(([name, deets]) => toParamObject(name, deets));
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user