11import { Command } from "commander" ;
2- import { Client } from "./index" ;
32import {
43 DOC_TYPE_INVOICE ,
54 DOC_TYPE_RECEIPT ,
65 DOC_TYPE_PASSPORT ,
76 DOC_TYPE_FINANCIAL ,
8- Document ,
7+ DOC_TYPE_CUSTOM ,
98} from "./documents" ;
109import {
11- Response ,
12- FinancialResponse ,
10+ CustomResponse ,
11+ FinancialDocResponse ,
1312 InvoiceResponse ,
1413 PassportResponse ,
1514 ReceiptResponse ,
16- CustomResponse ,
15+ STANDARD_API_OWNER ,
1716} from "./api" ;
17+ import { Client } from "./client" ;
18+ import { ProductConfigs , ProductConfig } from "./constants" ;
1819
1920const program = new Command ( ) ;
2021
21- interface OtsCliConfig {
22- help : string ;
23- docType : string ;
24- responseClass : typeof Response < Document > ;
25- fullText : boolean ;
26- }
27-
2822const COMMAND_INVOICE = "invoice" ;
2923const COMMAND_RECEIPT = "receipt" ;
3024const COMMAND_PASSPORT = "passport" ;
3125const COMMAND_FINANCIAL = "financial" ;
3226const COMMAND_CUSTOM = "custom" ;
3327
34- const OTS_DOCUMENTS = new Map < string , OtsCliConfig > ( [
35- [
36- COMMAND_INVOICE ,
37- {
38- help : "Invoice" ,
39- docType : DOC_TYPE_INVOICE ,
40- responseClass : InvoiceResponse ,
41- fullText : true ,
42- } ,
43- ] ,
44- [
45- COMMAND_RECEIPT ,
46- {
47- help : "Expense Receipt" ,
48- docType : DOC_TYPE_RECEIPT ,
49- responseClass : ReceiptResponse ,
50- fullText : true ,
51- } ,
52- ] ,
53- [
54- COMMAND_PASSPORT ,
55- {
56- help : "Passport" ,
57- docType : DOC_TYPE_PASSPORT ,
58- responseClass : PassportResponse ,
59- fullText : false ,
60- } ,
61- ] ,
62- [
63- COMMAND_FINANCIAL ,
64- {
65- help : "Financial Document (receipt or invoice)" ,
66- docType : DOC_TYPE_FINANCIAL ,
67- responseClass : FinancialResponse ,
68- fullText : true ,
69- } ,
70- ] ,
71- [
72- COMMAND_CUSTOM ,
73- {
74- help : "A custom document" ,
75- docType : "" ,
76- responseClass : CustomResponse ,
77- fullText : false ,
78- } ,
79- ] ,
28+ const CLI_COMMAND_CONFIG = new Map < string , ProductConfig > ( [
29+ [ COMMAND_INVOICE , ProductConfigs . getByDocType ( DOC_TYPE_INVOICE ) ] ,
30+ [ COMMAND_RECEIPT , ProductConfigs . getByDocType ( DOC_TYPE_RECEIPT ) ] ,
31+ [ COMMAND_PASSPORT , ProductConfigs . getByDocType ( DOC_TYPE_PASSPORT ) ] ,
32+ [ COMMAND_FINANCIAL , ProductConfigs . getByDocType ( DOC_TYPE_FINANCIAL ) ] ,
33+ [ COMMAND_CUSTOM , ProductConfigs . getByDocType ( DOC_TYPE_CUSTOM ) ] ,
8034] ) ;
8135
8236async function predictCall ( command : string , inputPath : string , options : any ) {
83- const info = OTS_DOCUMENTS . get ( command ) ;
84- if ( ! info ) {
37+ const conf = CLI_COMMAND_CONFIG . get ( command ) ;
38+ if ( conf === undefined ) {
8539 throw new Error ( `Invalid document type ${ command } ` ) ;
8640 }
8741 const mindeeClient = new Client ( {
8842 apiKey : options . apiKey ,
89- debug : options . verbose ,
43+ debug : options . debug ,
9044 } ) ;
45+ if ( command === COMMAND_CUSTOM ) {
46+ mindeeClient . addEndpoint ( {
47+ accountName : options . user ,
48+ documentType : options . documentType ,
49+ } ) ;
50+ }
51+ const doc = mindeeClient . docFromPath ( inputPath ) ;
52+ const params = {
53+ docType : command === COMMAND_CUSTOM ? options . documentType : conf . docType ,
54+ username : command === COMMAND_CUSTOM ? options . user : STANDARD_API_OWNER ,
55+ cutPages : options . cutPages ,
56+ fullText : options . fullText ,
57+ } ;
58+ // Tried setting the response by using the responseClass property in constants.PRODUCTS_CONFIG
59+ // This compiled, but threw an exception:
60+ // TypeError: responseType is not a constructor
61+ //
62+ // So using a switch to explicitly set the response class parameter. Ugly, but works.
63+ // Improvements welcome!
64+ let response ;
9165 switch ( command ) {
92- case COMMAND_INVOICE : {
93- mindeeClient . configInvoice ( ) ;
66+ case COMMAND_INVOICE :
67+ response = await doc . parse ( InvoiceResponse , params ) ;
9468 break ;
95- }
96- case COMMAND_RECEIPT : {
97- mindeeClient . configReceipt ( ) ;
69+ case COMMAND_RECEIPT :
70+ response = await doc . parse ( ReceiptResponse , params ) ;
9871 break ;
99- }
100- case COMMAND_PASSPORT : {
101- mindeeClient . configPassport ( ) ;
72+ case COMMAND_FINANCIAL :
73+ response = await doc . parse ( FinancialDocResponse , params ) ;
10274 break ;
103- }
104- case COMMAND_FINANCIAL : {
105- mindeeClient . configFinancialDoc ( ) ;
75+ case COMMAND_PASSPORT :
76+ response = await doc . parse ( PassportResponse , params ) ;
10677 break ;
107- }
108- case COMMAND_CUSTOM : {
109- mindeeClient . addEndpoint ( {
110- accountName : options . user ,
111- endpointName : options . documentType ,
112- } ) ;
78+ case COMMAND_CUSTOM :
79+ response = await doc . parse ( CustomResponse , params ) ;
11380 break ;
114- }
81+ default :
82+ throw `Unhandled command: ${ command } ` ;
11583 }
116- const doc = mindeeClient . docFromPath ( inputPath ) ;
117- const result = await doc . parse ( info . responseClass , {
118- docType : info . docType || options . documentType ,
119- username : options . user || "mindee" ,
120- cutPages : options . cutPages ,
121- fullText : options . fullText ,
122- } ) ;
123- if ( result . document ) {
124- console . log ( `\n${ result . document } ` ) ;
84+ if ( response . document ) {
85+ console . log ( `\n${ response . document } ` ) ;
12586 }
12687}
12788
12889export function cli ( ) {
12990 program . name ( "mindee" ) ;
130- program . option ( "-v , --verbose " , "high verbosity mode" ) ;
91+ program . option ( "-d , --debug " , "high verbosity mode" ) ;
13192
132- OTS_DOCUMENTS . forEach ( ( info , name ) => {
93+ CLI_COMMAND_CONFIG . forEach ( ( info , name ) => {
13394 const prog = program . command ( name ) ;
134- prog . description ( info . help ) ;
95+ prog . description ( info . description ) ;
13596
13697 prog . option ( "-k, --api-key <api_key>" , "API key for document endpoint" ) ;
13798 prog . option ( "-C, --no-cut-pages" , "Don't cut document pages" ) ;
@@ -143,6 +104,10 @@ export function cli() {
143104 "-u, --user <username>" ,
144105 "API account name for the endpoint"
145106 ) ;
107+ prog . option (
108+ "-v, --version <version>" ,
109+ "API account name for the endpoint"
110+ ) ;
146111 prog . argument ( "<endpoint_name>" , "API endpoint name" ) ;
147112 }
148113 prog . argument ( "<input_path>" , "Full path to the file" ) ;
@@ -157,7 +122,7 @@ export function cli() {
157122 const allOptions = {
158123 ...program . opts ( ) ,
159124 ...options ,
160- endpointName : endpointName ,
125+ documentType : endpointName ,
161126 } ;
162127 predictCall ( command . name ( ) , inputPath , allOptions ) ;
163128 }
0 commit comments