-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
customers.ts
52 lines (43 loc) · 1.63 KB
/
customers.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
import type Stripe from "stripe";
import type {
MutationCreateStripeCustomerArgs,
QueryRetrieveStripeCustomerArgs,
QueryStripeCustomerSearchArgs,
} from "../../generated/graphql.js";
import { deepOmitNils } from "../../lib/deepOmitNils.js";
import { lastStripeObject } from "../../lib/lastStripeObject.js";
import { nonNilAssertionError } from "../../lib/nonNilAssertionError.js";
import type { ParsedStripeResponse } from "../../lib/parseStripeResponse.js";
import { parseStripeResponse } from "../../lib/parseStripeResponse.js";
import { stripe } from "../../lib/stripe.js";
export const stripeCustomerSearch = async ({
query,
}: QueryStripeCustomerSearchArgs): Promise<ParsedStripeResponse<Stripe.Customer>> => {
const customer = await stripe.customers.search({
query: query ?? "",
});
return parseStripeResponse(lastStripeObject(customer.data));
};
export const retrieveStripeCustomer = async ({
data,
}: QueryRetrieveStripeCustomerArgs): Promise<ParsedStripeResponse<Stripe.Customer | Stripe.DeletedCustomer>> => {
const { id, addProps } = data ?? {};
if (id !== null) {
throw nonNilAssertionError("retrieveStripeCustomer:id", data);
}
const customer = await stripe.customers.retrieve(
id,
deepOmitNils(addProps) ?? {},
);
return parseStripeResponse(customer);
};
export const createStripeCustomer = async ({
data,
}: MutationCreateStripeCustomerArgs) => {
const params = deepOmitNils(data) ?? {};
if (params.name == null) {
throw nonNilAssertionError("createStripeCustomer:name", params);
}
const newCustomer = await stripe.customers.create(params);
return parseStripeResponse(newCustomer);
};