Skip to content

Commit

Permalink
feat(fattureincloud-client): add missing option in listInvoices, impr…
Browse files Browse the repository at this point in the history
…ove typings
  • Loading branch information
jackdbd committed Sep 16, 2022
1 parent 867cfbe commit 6b200be
Show file tree
Hide file tree
Showing 15 changed files with 413 additions and 154 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { env } from 'process'
import { basicClient } from '../../lib/info/clients.js'
import { credentials } from '../api-credentials.mjs'

Expand Down
14 changes: 13 additions & 1 deletion packages/fattureincloud-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@
".": "./lib/index.js",
"./customers": "./lib/customers/clients.js",
"./customers/api": "./lib/customers/api.js",
"./error": "./lib/error.js",
"./info": "./lib/info/clients.js",
"./info/api": "./lib/info/api.js",
"./interfaces": "./lib/interfaces.js",
"./invoices": "./lib/invoices/clients.js",
"./invoices/api": "./lib/invoices/api.js",
"./products": "./lib/products/clients.js",
"./products/api": "./lib/products/api.js",
"./package.json": "./package.json"
"./package.json": "./package.json",
"./rate-limit": "./lib/rate-limit.js"
},
"typesVersions": {
"*": {
Expand All @@ -47,12 +50,18 @@
"customers/*": [
"./lib/customers/*.d.ts"
],
"error": [
"./lib/error.d.ts"
],
"info": [
"./lib/info/clients.d.ts"
],
"info/*": [
"./lib/info/*.d.ts"
],
"interfaces": [
"./lib/interfaces.d.ts"
],
"invoices": [
"./lib/invoices/clients.d.ts"
],
Expand All @@ -64,6 +73,9 @@
],
"products/*": [
"./lib/products/*.d.ts"
],
"rate-limit": [
"./lib/rate-limit.d.ts"
]
}
},
Expand Down
75 changes: 75 additions & 0 deletions packages/fattureincloud-client/src/clients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import makeDebug from 'debug'
import type Bottleneck from 'bottleneck'
import type { Credentials } from './interfaces.js'

import {
basicClient as customersBasicClient,
rateLimitedClient as customersRateLimitedClient
} from './customers/clients.js'
import type { Client as CustomersClient } from './customers/clients.js'

import {
basicClient as infoBasicClient,
rateLimitedClient as infoRateLimitedClient
} from './info/clients.js'
import type { Client as InfoClient } from './info/clients.js'

import {
basicClient as invoicesBasicClient,
rateLimitedClient as invoicesRateLimitedClient
} from './invoices/clients.js'
import type { Client as InvoicesClient } from './invoices/clients.js'

import {
basicClient as productsBasicClient,
rateLimitedClient as productsRateLimitedClient
} from './products/clients.js'
import type { Client as ProductsClient } from './products/clients.js'

const debug = makeDebug('fattureincloud-client/clients')

export interface Client {
customers: CustomersClient
info: InfoClient
invoices: InvoicesClient
products: ProductsClient
}

/**
* A basic client for all endpoints of the FattureinCloud API.
*
* @remarks This client is not rate-limiting. If you are calling the
* FattureinCloud API more than once, you might want to use the
* rateLimitedClient instead.
*
* @public
*/
export const basicClient = (credentials: Credentials): Client => {
debug('make FattureInCloud basic API client')

return {
customers: customersBasicClient(credentials),
info: infoBasicClient(credentials),
invoices: invoicesBasicClient(credentials),
products: productsBasicClient(credentials)
}
}

/**
* A rate-limited client for all endpoints of the FattureinCloud API.
*
* @public
*/
export const rateLimitedClient = (
credentials: Credentials,
options?: Bottleneck.ConstructorOptions
): Client => {
debug('make FattureInCloud rate-limited API client')

return {
customers: customersRateLimitedClient(credentials, options),
info: infoRateLimitedClient(credentials, options),
invoices: invoicesRateLimitedClient(credentials, options),
products: productsRateLimitedClient(credentials, options)
}
}
39 changes: 28 additions & 11 deletions packages/fattureincloud-client/src/customers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
APIResponseBodyList,
APIResponseBodyUpdate,
CreateRequestBody,
Customer,
DeleteRequestBody,
ListOptions,
RetrieveConfig,
Expand All @@ -21,17 +22,27 @@ const debug = makeDebug('fattureincloud-client/customers/api')
const API_ENDPOINT = 'https://api.fattureincloud.it/v1/clienti'

/**
* Retrieve a paginated list of customers.
* @public
*/
export interface ListResponseBody {
current_page: number
results: Customer[]
total_pages: number
}

/**
* Retrieves a paginated list of customers.
*
* Each page can contain a maximum of 500 results. The FattureInCloud API does
* not allow to configure how many results to return for each page.
*
* https://api.fattureincloud.it/v1/documentation/dist/#!/Anagrafica/AnagraficaLista
* @see [AnagraficaLista - FattureinCloud.it](https://api.fattureincloud.it/v1/documentation/dist/#!/Anagrafica/AnagraficaLista)
* @public
*/
export const list = async (
{ api_key, api_uid }: Credentials,
options?: ListOptions
) => {
): Promise<ListResponseBody> => {
debug('list options (before validation and defaults) %O', options)

const cf = options?.codice_fiscale || ''
Expand Down Expand Up @@ -94,9 +105,10 @@ export const list = async (
}

/**
* Retrieve a single customer that matches the search criteria.
* Retrieves a single customer that matches the search criteria.
*
* https://api.fattureincloud.it/v1/documentation/dist/#!/Anagrafica/AnagraficaLista
* @see [AnagraficaLista - FattureinCloud.it](https://api.fattureincloud.it/v1/documentation/dist/#!/Anagrafica/AnagraficaLista)
* @public
*/
export const retrieve = async (
{ api_key, api_uid }: Credentials,
Expand Down Expand Up @@ -150,9 +162,10 @@ export const retrieve = async (
}

/**
* Create a new customer.
* Creates a new customer.
*
* https://api.fattureincloud.it/v1/documentation/dist/#!/Anagrafica/AnagraficaNuovoSingolo
* @see [AnagraficaNuovoSingolo - FattureinCloud.it](https://api.fattureincloud.it/v1/documentation/dist/#!/Anagrafica/AnagraficaNuovoSingolo)
* @public
*/
export const create = async (
{ api_key, api_uid }: Credentials,
Expand Down Expand Up @@ -221,9 +234,10 @@ export const create = async (
}

/**
* Update an existing customer.
* Updates an existing customer.
*
* https://api.fattureincloud.it/v1/documentation/dist/#!/Anagrafica/AnagraficaModifica
* @see [AnagraficaNuovoSingolo - FattureinCloud.it](https://api.fattureincloud.it/v1/documentation/dist/#!/Anagrafica/AnagraficaModifica)
* @public
*/
export const update = async (
{ api_key, api_uid }: Credentials,
Expand Down Expand Up @@ -394,9 +408,10 @@ export const update = async (
}

/**
* Delete a customer.
* Deletes a customer.
*
* https://api.fattureincloud.it/v1/documentation/dist/#!/Anagrafica/AnagraficaElimina
* @see [AnagraficaElimina - FattureinCloud.it](https://api.fattureincloud.it/v1/documentation/dist/#!/Anagrafica/AnagraficaElimina)
* @public
*/
export const deleteCustomer = async (
{ api_key, api_uid }: Credentials,
Expand Down Expand Up @@ -427,6 +442,8 @@ export const deleteCustomer = async (

/**
* Autopaginate results.
*
* @public
*/
export async function* listAsyncGenerator(
credentials: Credentials,
Expand Down
63 changes: 43 additions & 20 deletions packages/fattureincloud-client/src/customers/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,72 @@ import {
retrieve as retrieveCustomer,
update as updateCustomer
} from './api.js'
import type { ListResponseBody } from './api.js'
import type {
Customer,
CreateRequestBody,
ListOptions,
DeleteRequestBody,
RetrieveConfig,
UpdateRequestBody
} from './interfaces.js'
import type { BasicClient } from '../interfaces.js'

const debug = makeDebug('fattureincloud-client/customers/client')

export const basicClient = (credentials: Credentials) => {
/**
* @public
*/
export interface Client extends BasicClient {
create: (config: CreateRequestBody) => Promise<{ id: string }>

delete: (config: DeleteRequestBody) => Promise<{ id: string }>

list: (options?: ListOptions) => Promise<ListResponseBody>

listAsyncGenerator: (
options?: ListOptions
) => AsyncGenerator<ListResponseBody>

retrieve: (config: RetrieveConfig) => Promise<Customer>

update: (
config: UpdateRequestBody
) => Promise<{ id: string; campi: string[] }>
}

/**
* A basic client for FattureinCloud customers.
*
* @public
*/
export const basicClient = (credentials: Credentials): Client => {
debug('make FattureInCloud customers API client')

return {
create: (config: CreateRequestBody) => {
return createCustomer(credentials, config)
},
create: (config) => createCustomer(credentials, config),

delete: (config: DeleteRequestBody) => {
return deleteCustomer(credentials, config)
},
delete: (config) => deleteCustomer(credentials, config),

list: (options?: ListOptions) => {
return listCustomers(credentials, options)
},
list: (options) => listCustomers(credentials, options),

listAsyncGenerator: (options?: ListOptions) => {
return listCustomersAsyncGenerator(credentials, options)
},
listAsyncGenerator: (options) =>
listCustomersAsyncGenerator(credentials, options),

retrieve: (config: RetrieveConfig) => {
return retrieveCustomer(credentials, config)
},
retrieve: (config) => retrieveCustomer(credentials, config),

update: (config: UpdateRequestBody) => {
return updateCustomer(credentials, config)
}
update: (config) => updateCustomer(credentials, config)
}
}

/**
* A rate-limited client for FattureinCloud customers.
*
* @public
*/
export const rateLimitedClient = (
credentials: Credentials,
options?: Bottleneck.ConstructorOptions
) => {
): Client => {
return withRateLimit(basicClient(credentials), options)
}
Loading

0 comments on commit 6b200be

Please sign in to comment.