UNPKG

2.36 kBTypeScriptView Raw
1/// <reference types="node" />
2import { Readable, TransformOptions } from "stream";
3
4import JSON2CSVParser = require("./JSON2CSVParser");
5import JSON2CSVAsyncParser = require("./JSON2CSVAsyncParser");
6import JSON2CSVTransform = require("./JSON2CSVTransform");
7import flatten = require("./transforms/flatten");
8import unwind = require("./transforms/unwind");
9
10export as namespace json2csv;
11
12export { JSON2CSVAsyncParser as AsyncParser, JSON2CSVParser as Parser, JSON2CSVTransform as Transform };
13
14// Convenience method to keep the API similar to version 3.X
15export function parse<T>(data: Readonly<T> | readonly T[], opts?: json2csv.Options<T>): string;
16
17export function parseAsync<T>(
18 data: Readonly<T> | readonly T[] | Readable,
19 opts?: json2csv.Options<T>,
20 transformOpts?: TransformOptions,
21): Promise<string>;
22
23export const transforms: {
24 flatten: typeof flatten;
25 unwind: typeof unwind;
26};
27
28export interface FieldValueCallbackInfo {
29 label: string;
30 default?: string | undefined;
31}
32
33export type FieldValueCallback<T> = FieldValueCallbackWithoutField<T> | FieldValueCallbackWithField<T>;
34
35export interface FieldValueCallbackWithoutField<T> {
36 (row: T): any;
37}
38
39export interface FieldValueCallbackWithField<T> {
40 (row: T, field: FieldValueCallbackInfo): any;
41}
42
43export interface FieldInfo<T> {
44 label?: string | undefined;
45 default?: string | undefined;
46 value: string | FieldValueCallback<T>;
47}
48
49export interface NormalizedFieldInfo<T> {
50 label: string;
51 value: FieldValueCallback<T>;
52}
53
54export interface Options<T> {
55 fields?: Array<string | FieldInfo<T>> | undefined;
56 ndjson?: boolean | undefined;
57 defaultValue?: string | undefined;
58 quote?: string | undefined;
59 escapedQuote?: string | undefined;
60 delimiter?: string | undefined;
61 eol?: string | undefined;
62 excelStrings?: boolean | undefined;
63 header?: boolean | undefined;
64 includeEmptyRows?: boolean | undefined;
65 withBOM?: boolean | undefined;
66 transforms?: Array<Json2CsvTransform<any, any>> | undefined;
67}
68
69export interface FlattenOptions {
70 objects?: boolean | undefined;
71 arrays?: boolean | undefined;
72 separator?: string | undefined;
73}
74
75export type Json2CsvTransform<T1, T2> = (item: T1) => T2 | T2[];
76
77export interface UnwindOptions {
78 paths?: string[] | undefined;
79 blankOut?: boolean | undefined;
80}