-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcreate-customer.ts
More file actions
131 lines (115 loc) · 4.33 KB
/
Copy pathcreate-customer.ts
File metadata and controls
131 lines (115 loc) · 4.33 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { z } from 'zod';
import { createAction, ProxyConfiguration } from 'nango';
const AddressInputSchema = z.object({
address1: z.string().optional(),
address2: z.string().optional(),
city: z.string().optional(),
company: z.string().optional(),
countryCode: z.string().optional(),
firstName: z.string().optional(),
lastName: z.string().optional(),
phone: z.string().optional(),
provinceCode: z.string().optional(),
zip: z.string().optional()
});
const InputSchema = z.object({
firstName: z.string().optional(),
lastName: z.string().optional(),
email: z.string().optional(),
phone: z.string().optional(),
tags: z.array(z.string()).optional(),
addresses: z.array(AddressInputSchema).optional(),
taxExempt: z.boolean().optional(),
note: z.string().optional(),
locale: z.string().optional()
});
const OutputSchema = z.object({
success: z.boolean()
});
const action = createAction({
description: 'Create a Shopify customer record.',
version: '1.0.1',
input: InputSchema,
output: OutputSchema,
scopes: ['write_customers'],
exec: async (nango, input): Promise<z.infer<typeof OutputSchema>> => {
const query = `
mutation customerCreate($input: CustomerInput!) {
customerCreate(input: $input) {
userErrors {
field
message
}
}
}
`;
const variables = {
input: {
...(input.firstName !== undefined && { firstName: input.firstName }),
...(input.lastName !== undefined && { lastName: input.lastName }),
...(input.email !== undefined && { email: input.email }),
...(input.phone !== undefined && { phone: input.phone }),
...(input.tags !== undefined && { tags: input.tags }),
...(input.addresses !== undefined && { addresses: input.addresses }),
...(input.taxExempt !== undefined && { taxExempt: input.taxExempt }),
...(input.note !== undefined && { note: input.note }),
...(input.locale !== undefined && { locale: input.locale })
}
};
const config: ProxyConfiguration = {
// https://shopify.dev/docs/api/admin-graphql/2025-10/mutations/customerCreate
endpoint: '/admin/api/2025-10/graphql.json',
data: {
query,
variables
},
retries: 1
};
const response = await nango.post(config);
const BodySchema = z.object({
data: z.object({
customerCreate: z
.object({
userErrors: z.array(
z.object({
field: z.union([z.string(), z.array(z.string())]).nullable(),
message: z.string()
})
)
})
.nullable()
}),
errors: z.array(z.unknown()).optional()
});
const parsedBody = BodySchema.safeParse(response.data);
if (!parsedBody.success) {
throw new nango.ActionError({
type: 'provider_error',
message: 'Unexpected response structure from Shopify'
});
}
const body = parsedBody.data;
if (body.errors && body.errors.length > 0) {
throw new nango.ActionError({
type: 'provider_error',
message: 'GraphQL errors occurred during customer creation'
});
}
if (!body.data.customerCreate) {
throw new nango.ActionError({
type: 'provider_error',
message: 'No customer creation response from Shopify'
});
}
if (body.data.customerCreate.userErrors.length > 0) {
throw new nango.ActionError({
type: 'validation_error',
message: body.data.customerCreate.userErrors.map((e) => e.message).join(', '),
errors: body.data.customerCreate.userErrors
});
}
return { success: true };
}
});
export type NangoActionLocal = Parameters<(typeof action)['exec']>[0];
export default action;