-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.ts
87 lines (75 loc) · 2.23 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import makeDebug from 'debug'
import phin from 'phin'
import { newErrorFromApiError } from '../error.js'
import { headers } from '../headers.js'
import type { Credentials } from '../interfaces.js'
import type {
APIResponseBodyAccountInfo,
AccountOptions
} from './interfaces.js'
const debug = makeDebug('fattureincloud-client/info/api')
const API_ENDPOINT = 'https://api.fattureincloud.it/v1/info'
/**
* https://api.fattureincloud.it/v1/documentation/dist/#!/Info/InfoLista
*/
export const account = async (
{ api_key, api_uid }: Credentials,
options?: AccountOptions
) => {
const campi = options?.fields || ['durata_licenza', 'nome', 'tipo_licenza']
debug('list options (after validation and defaults) %O', { campi })
const response = await phin<APIResponseBodyAccountInfo>({
data: {
api_key,
api_uid,
campi
},
headers: headers(),
method: 'POST',
parse: 'json' as const,
url: `${API_ENDPOINT}/account`
})
const b = response.body
if (b.error) {
throw newErrorFromApiError({ error: b.error, error_code: b.error_code! })
}
const response_body: any = { success: true } // TODO better type
if (campi.includes('durata_licenza')) {
response_body.giorni_rimanenti_licenza = b.durata_licenza
}
if (campi.includes('nome')) {
response_body.ragione_sociale = b.nome
}
if (campi.includes('tipo_licenza')) {
response_body.piano_licenza = b.tipo_licenza
}
if (campi.includes('lista_conti')) {
response_body.lista_conti = b.lista_conti
}
if (campi.includes('lista_iva')) {
response_body.lista_iva = b.lista_iva
}
if (campi.includes('lista_valute')) {
response_body.lista_valute = b.lista_valute
}
// const {
// lista_template,
// lista_template_ddt,
// lista_template_ftacc,
// } = response.body
// return {
// giorni_rimanenti_licenza: durata_licenza,
// lista_conti: lista_conti || [],
// lista_iva: lista_iva || [],
// lista_template: {
// generic: lista_template || [],
// ddt: lista_template_ddt || [],
// ftacc: lista_template_ftacc || []
// },
// lista_valute: lista_valute || [],
// piano_licenza: tipo_licenza,
// ragione_sociale: nome,
// status_code: 200
// }
return response_body
}