-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathuseUpdateBundleCache.tsx
111 lines (100 loc) · 2.63 KB
/
useUpdateBundleCache.tsx
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
import { useMutation as useMutationV5 } from '@tanstack/react-queryV5'
import { z } from 'zod'
import Api from 'shared/api'
import { rejectNetworkError } from 'shared/api/helpers'
const UpdateBundleCacheInputSchema = z.array(
z.object({
bundleName: z.string(),
isCached: z.boolean(),
})
)
const MutationErrorSchema = z.discriminatedUnion('__typename', [
z.object({
__typename: z.literal('UnauthenticatedError'),
message: z.string(),
}),
z.object({
__typename: z.literal('ValidationError'),
message: z.string(),
}),
])
const MutationRequestSchema = z.object({
updateBundleCacheConfig: z
.object({
results: UpdateBundleCacheInputSchema.nullable(),
error: MutationErrorSchema.nullable(),
})
.nullable(),
})
const query = `
mutation UpdateBundleCacheConfig(
$owner: String!
$repo: String!
$bundles: [BundleCacheConfigInput!]!
) {
updateBundleCacheConfig(
input: { owner: $owner, repoName: $repo, bundles: $bundles }
) {
results {
bundleName
isCached
}
error {
__typename
... on UnauthenticatedError {
message
}
... on ValidationError {
message
}
}
}
}`
interface UseUpdateBundleCacheArgs {
provider: string
owner: string
repo: string
}
export const useUpdateBundleCache = ({
provider,
owner,
repo,
}: UseUpdateBundleCacheArgs) => {
return useMutationV5({
throwOnError: false,
mutationFn: (input: z.infer<typeof UpdateBundleCacheInputSchema>) => {
return Api.graphqlMutation({
provider,
query,
variables: { owner, repo, bundles: input },
mutationPath: 'updateBundleCache',
}).then((res) => {
const parsedData = MutationRequestSchema.safeParse(res.data)
if (!parsedData.success) {
return rejectNetworkError({
status: 400,
error: parsedData.error,
data: {},
dev: 'useUpdateBundleCache - 400 failed to parse data',
})
}
const updateBundleCacheConfig = parsedData.data.updateBundleCacheConfig
if (
updateBundleCacheConfig?.error?.__typename === 'UnauthenticatedError'
) {
return Promise.reject({
error: 'UnauthenticatedError',
message: updateBundleCacheConfig?.error?.message,
})
}
if (updateBundleCacheConfig?.error?.__typename === 'ValidationError') {
return Promise.reject({
error: 'ValidationError',
message: updateBundleCacheConfig?.error?.message,
})
}
return updateBundleCacheConfig?.results ?? []
})
},
})
}