UNPKG

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