Skip to content

Commit

Permalink
Add Typescript declarations for couchimport (#177)
Browse files Browse the repository at this point in the history
* Add initial types for app.js

* Consolidate types into index.d.ts

* Add more specific types for delimiters

* Add return type for stream import

* Rename callback types

* Remove auto-generated declaration file

* Add EOL newline

* Add types to package.json
  • Loading branch information
benjspriggs committed Feb 24, 2020
1 parent 0933148 commit ddd3320
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -29,6 +29,7 @@
"standard": "^14.3.1"
},
"main": "./app.js",
"types": "./types/index.d.ts",
"bin": {
"couchimport": "bin/couchimport.bin.js",
"couchexport": "bin/couchexport.bin.js"
Expand Down
50 changes: 50 additions & 0 deletions types/index.d.ts
@@ -0,0 +1,50 @@
import { Options } from 'csv-parse';
import { Stream, Transform } from 'stream';

declare module couchimport {
type Delimiter = '?' | '\t' | ',';

type ImportCallback = (err: Error, data: { total: number, totalFailed: number }) => void;
type ExportCallback = (err: Error, data: never) => void;
type UrlPreviewCallback = (err: Error, data: any, delimiter: Delimiter) => void;
type StreamPreviewCallback = (err: Error, data: any, delimiter: Delimiter) => void;

export interface Config {
url?: string;
database?: string;
delimiter?: Options['delimiter'];
type?: 'json' | 'jsonl' | 'text';
buffer?: number;
jsonpath?: string;
transform?: (data: any, meta: {}) => any;
meta?: {};
parallelism?: number;
preview?: boolean;
ignorefields?: string[];
overwrite?: boolean;
}

function importStream(rs: Stream, callback: ImportCallback): Transform;
function importStream(rs: Stream, opts: Config, callback: ImportCallback): Transform;

function importFile(filename: string, callback: ImportCallback): Transform;
function importFile(filename: string, opts: Config, callback: ImportCallback): Transform;

function exportStream(ws: Stream, callback: ExportCallback): void;
function exportStream(ws: Stream, opts: Config, callback: ExportCallback): void;

function exportFile(filename: string, callback: ExportCallback): void;
function exportFile(filename: string, opts: Config, callback: ExportCallback): void;

function previewURL(u: string, opts: never, callback: UrlPreviewCallback): void;

function previewStream(rs: Stream, callback: StreamPreviewCallback): void;
function previewStream(rs: Stream, opts: Config, callback: StreamPreviewCallback): void;

function previewCSVFile(filename: string, callback: StreamPreviewCallback): void;
function previewCSVFile(filename: string, opts: Config, callback: StreamPreviewCallback): void;
}

declare module "couchimport" {
export = couchimport;
}

0 comments on commit ddd3320

Please sign in to comment.