Skip to content

Commit

Permalink
feat: export parse function (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lakhdar authored May 20, 2022
1 parent fac916c commit cb1f9f1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/help/function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const noop = () => {};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export {DocumentBuilder} from './documentBuilder';
export * from './securityIdentityBuilder';
export {PlatformEnvironment, Region, PlatformUrlOptions} from './environment';
export {SourceVisibility} from '@coveord/platform-client';

export {parseAndGetDocumentBuilderFromJSONDocument} from './validation/parseFile';
5 changes: 5 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {AxiosResponse} from 'axios';
import {PathLike} from 'fs';
import type {DocumentBuilder} from './documentBuilder';
import type {Transformer} from './validation/transformers/transformer';
// Used for Documentation.
Expand Down Expand Up @@ -45,6 +46,10 @@ export interface ParseDocumentOptions {
* @default BuiltInTransformers.identity
*/
fieldNameTransformer?: Transformer;
/**
* Callback to execute everytime a new {@link DocumentBuilder} is created
*/
callback?: (docBuilder: DocumentBuilder, documentPath: PathLike) => void;
}
export interface ConcurrentProcessing {
/**
Expand Down
2 changes: 2 additions & 0 deletions src/source/documentUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import axios, {AxiosRequestConfig, AxiosResponse} from 'axios';
import {FieldAnalyser} from '..';
import {DocumentBuilder} from '../documentBuilder';
import {createFieldsFromReport} from '../fieldAnalyser/fieldUtils';
import {noop} from '../help/function';
import {
BatchUpdateDocuments,
BatchUpdateDocumentsFromFiles,
Expand All @@ -19,6 +20,7 @@ const defaultBatchFromFileOptions: Required<BatchUpdateDocumentsFromFiles> = {
...defaultBatchOptions,
fieldNameTransformer: BuiltInTransformers.identity,
maxConcurrent: 10,
callback: noop,
};

export async function uploadDocument(
Expand Down
20 changes: 15 additions & 5 deletions src/validation/parseFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,27 @@ import {ParseDocumentOptions} from '../interfaces';
export const parseAndGetDocumentBuilderFromJSONDocument = (
documentPath: PathLike,
options?: ParseDocumentOptions
) => {
): DocumentBuilder[] => {
ensureFileIntegrity(documentPath);

const fileContent = safeJSONParse(documentPath);

const executeCallback = (docBuilder: DocumentBuilder) => {
if (options?.callback) {
options?.callback(docBuilder, documentPath);
}
};

if (Array.isArray(fileContent)) {
return fileContent.map((doc) =>
processDocument(doc, documentPath, options)
);
return fileContent.map((doc) => {
const docBuilder = processDocument(doc, documentPath, options);
executeCallback(docBuilder);
return docBuilder;
});
} else {
return [processDocument(fileContent, documentPath, options)];
const docBuilder = processDocument(fileContent, documentPath, options);
executeCallback(docBuilder);
return [docBuilder];
}
};

Expand Down

0 comments on commit cb1f9f1

Please sign in to comment.