// Customers. import { collect, request, segment } from "./stripe" import type { Address, Client, Deleted, Page, RequestOptions, SearchPage, StripeError, } from "./stripe" /// A customer. export type Customer = { id: string, email?: string, name?: string, phone?: string, description?: string, address?: Address, metadata?: json, balance?: float, currency?: string, delinquent?: boolean, default_source?: json, invoice_prefix?: string, created?: float, livemode?: boolean, } /// The fields of a customer you can set. `extra` holds any field this type doesn't list. export type CustomerParams = { email?: string, name?: string, phone?: string, description?: string, address?: Address, metadata?: Map, payment_method?: string, preferred_locales?: string[], extra?: json, } /// What to list. `email` is matched exactly. export type CustomerListParams = { email?: string, limit?: float, starting_after?: string, ending_before?: string, extra?: json, } /// Creates a customer. /// /// ```weft /// let customer = try await customers.create(stripe, { email: "ana@example.com", name: "Ana" }) /// ``` export async fn create( c: Client, params: CustomerParams, options: RequestOptions = {}, ): Customer ! StripeError { return try await request(c, "POST", "/v1/customers", params, options, j => try j as Customer) } /// Gets a customer by id. export async fn retrieve( c: Client, id: string, options: RequestOptions = {}, ): Customer ! StripeError { return try await request( c, "GET", `/v1/customers/${segment(id)}`, null, options, j => try j as Customer, ) } /// Changes the fields given. export async fn update( c: Client, id: string, params: CustomerParams, options: RequestOptions = {}, ): Customer ! StripeError { return try await request( c, "POST", `/v1/customers/${segment(id)}`, params, options, j => try j as Customer, ) } /// Deletes a customer, and cancels their subscriptions. export async fn remove(c: Client, id: string, options: RequestOptions = {}): Deleted ! StripeError { return try await request( c, "DELETE", `/v1/customers/${segment(id)}`, null, options, j => try j as Deleted, ) } /// One page of customers, newest first. export async fn list( c: Client, params: CustomerListParams = {}, options: RequestOptions = {}, ): Page ! StripeError { return try await request(c, "GET", "/v1/customers", params, options, j => try j as Page) } /// Every customer matching `params`, up to `max`. export async fn listAll( c: Client, params: CustomerListParams = {}, options: RequestOptions = {}, max: int = 10000, ): Customer[] ! StripeError { return try await collect( c, "/v1/customers", params, options, j => try j as Page, x => x.id, max, ) } /// Customers matching a search query (`email:"ana@example.com"`). export async fn search(c: Client, query: string, options: RequestOptions = {}): SearchPage< Customer, > ! StripeError { return try await request( c, "GET", "/v1/customers/search", { query }, options, j => try j as SearchPage, ) }