-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathget-shop.ts
More file actions
110 lines (99 loc) · 3.61 KB
/
Copy pathget-shop.ts
File metadata and controls
110 lines (99 loc) · 3.61 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
import { z } from 'zod';
import { createAction } from 'nango';
const InputSchema = z.object({});
const ProviderShopSchema = z.object({
name: z.string(),
email: z.string().optional(),
myshopifyDomain: z.string().optional(),
currencyCode: z.string().optional(),
plan: z
.object({
displayName: z.string().optional(),
partnerDevelopment: z.boolean().optional(),
shopifyPlus: z.boolean().optional()
})
.optional()
});
const OutputSchema = z.object({
name: z.string(),
email: z.string().optional(),
myshopifyDomain: z.string().optional(),
currencyCode: z.string().optional(),
plan: z
.object({
displayName: z.string().optional(),
partnerDevelopment: z.boolean().optional(),
shopifyPlus: z.boolean().optional()
})
.optional()
});
const action = createAction({
description: 'Retrieve Shopify shop metadata for the connected store.',
version: '1.0.1',
input: InputSchema,
output: OutputSchema,
scopes: ['read_products'],
exec: async (nango, _input): Promise<z.infer<typeof OutputSchema>> => {
const response = await nango.post({
// https://shopify.dev/docs/api/admin-graphql/2025-01/queries/shop
endpoint: '/admin/api/2025-01/graphql.json',
data: {
query: `
query {
shop {
name
email
myshopifyDomain
currencyCode
plan {
displayName
partnerDevelopment
shopifyPlus
}
}
}
`
},
retries: 3
});
const graphQLResponse = z
.object({
data: z
.object({
shop: z.unknown()
})
.optional(),
errors: z.array(z.unknown()).optional()
})
.parse(response.data);
if (graphQLResponse.errors && graphQLResponse.errors.length > 0) {
throw new nango.ActionError({
type: 'graphql_error',
message: 'Shopify GraphQL query returned errors',
errors: graphQLResponse.errors
});
}
if (!graphQLResponse.data || !graphQLResponse.data.shop) {
throw new nango.ActionError({
type: 'not_found',
message: 'Shop data not found in response'
});
}
const shop = ProviderShopSchema.parse(graphQLResponse.data.shop);
return {
name: shop.name,
...(shop.email !== undefined && { email: shop.email }),
...(shop.myshopifyDomain !== undefined && { myshopifyDomain: shop.myshopifyDomain }),
...(shop.currencyCode !== undefined && { currencyCode: shop.currencyCode }),
...(shop.plan !== undefined && {
plan: {
...(shop.plan.displayName !== undefined && { displayName: shop.plan.displayName }),
...(shop.plan.partnerDevelopment !== undefined && { partnerDevelopment: shop.plan.partnerDevelopment }),
...(shop.plan.shopifyPlus !== undefined && { shopifyPlus: shop.plan.shopifyPlus })
}
})
};
}
});
export type NangoActionLocal = Parameters<(typeof action)['exec']>[0];
export default action;