FBI-459: improvements

This commit is contained in:
Rishi Tank 2023-06-22 09:24:17 +01:00
parent c4133cfdf5
commit 1d4c0dc242
No known key found for this signature in database
GPG Key ID: BF025217B93532E7

View File

@ -12,7 +12,8 @@ interface CsvExampleProps {
samplerOptions: object; samplerOptions: object;
} }
const hasSameHeaders = (headers, sample) => Object.keys(sample).every(key => headers.includes(key)); const hasSameHeaders = (headers: string, sample: OpenAPIExample) =>
Object.keys(sample).every(key => headers.includes(key));
const getCsvRows = (sample: OpenAPIExample): string => { const getCsvRows = (sample: OpenAPIExample): string => {
const headers = Object.keys(sample?.[0] ?? sample).join(','); const headers = Object.keys(sample?.[0] ?? sample).join(',');
@ -22,10 +23,10 @@ const getCsvRows = (sample: OpenAPIExample): string => {
); );
if (!hasValidHeaders) return ''; if (!hasValidHeaders) return '';
let values; let values: string;
if (Array.isArray(sample)) { if (Array.isArray(sample)) {
values = sample.map(row => Object.values(row)).join('\n'); values = sample.map(Object.values).join('\n');
} else { } else {
values = Object.values(sample).join(','); values = Object.values(sample).join(',');
} }
@ -33,7 +34,7 @@ const getCsvRows = (sample: OpenAPIExample): string => {
}; };
const cleanUpExamples = (examples: Example[]): Example[] => const cleanUpExamples = (examples: Example[]): Example[] =>
examples.filter(example => example.exampleValue !== ''); examples.filter(({ exampleValue }) => exampleValue);
export const generateCsvExample = ({ export const generateCsvExample = ({
parser, parser,
@ -50,20 +51,20 @@ export const generateCsvExample = ({
), ),
); );
const processSamplesWithSchema = subSchema => { const processSamplesWithSchema = (subSchema: OpenAPISchema) => {
if (subSchema) { if (!subSchema) {
const subItems = subSchema.items as OpenAPISchema; return;
}
const subItems = subSchema.items as OpenAPISchema;
if (subSchema.type === 'array' && subItems && depthCount < MAX_ITEM_DEPTH) { if (subSchema.type === 'array' && subItems && depthCount < MAX_ITEM_DEPTH) {
depthCount++; depthCount++;
processSamplesWithSchema(subItems); processSamplesWithSchema(subItems);
} }
const metadata = { const metadata = {
exampleDescription: subSchema.description || schema.description || '', exampleDescription: subSchema.description || schema.description || '',
exampleSummary: subSchema.title || schema.title || 'Example CSV', exampleSummary: subSchema.title || schema.title || 'Example CSV',
}; };
if (subSchema.allOf) { if (subSchema.allOf) {
const resolved: OpenAPISchema = { const resolved: OpenAPISchema = {
...schema, ...schema,
@ -110,7 +111,6 @@ export const generateCsvExample = ({
...metadata, ...metadata,
}); });
} }
}
}; };
if (isValidSample) { if (isValidSample) {