omnivore/packages/web/lib/networking/mutations/updateSubscriptionMutation.ts

68 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

2023-07-19 10:45:48 +00:00
import { gql } from 'graphql-request'
import { gqlFetcher } from '../networkHelpers'
2023-08-01 14:07:53 +00:00
import {
FetchContentType,
2023-08-01 14:07:53 +00:00
Subscription,
SubscriptionStatus,
} from '../queries/useGetSubscriptionsQuery'
2023-07-19 10:45:48 +00:00
interface UpdateSubscriptionResult {
updateSubscription: UpdateSubscription
}
2023-07-20 05:56:41 +00:00
export enum UpdateSubscriptionErrorCode {
BadRequest = 'BAD_REQUEST',
NotFound = 'NOT_FOUND',
Unauthorized = 'UNAUTHORIZED',
2023-07-20 05:56:41 +00:00
}
2023-07-19 10:45:48 +00:00
interface UpdateSubscription {
2023-07-20 05:56:41 +00:00
subscription?: Subscription
errorCodes?: UpdateSubscriptionErrorCode[]
2023-07-19 10:45:48 +00:00
}
2023-11-15 10:12:52 +00:00
export interface UpdateSubscriptionInput {
2023-07-19 10:45:48 +00:00
id: string
lastFetchedAt?: Date
name?: string
description?: string
2023-08-01 14:07:53 +00:00
status?: SubscriptionStatus
2023-11-15 10:12:52 +00:00
autoAddToLibrary?: boolean
isPrivate?: boolean
fetchContentType?: FetchContentType
2023-07-19 10:45:48 +00:00
}
export async function updateSubscriptionMutation(
input: UpdateSubscriptionInput
2023-07-20 05:56:41 +00:00
): Promise<UpdateSubscriptionResult> {
2023-07-19 10:45:48 +00:00
const mutation = gql`
mutation UpdateSubscription($input: UpdateSubscriptionInput!) {
updateSubscription(input: $input) {
... on UpdateSubscriptionSuccess {
subscription {
id
lastFetchedAt
}
}
... on UpdateSubscriptionError {
errorCodes
}
}
}
`
try {
const data = (await gqlFetcher(mutation, {
input,
})) as UpdateSubscriptionResult
2023-07-20 05:56:41 +00:00
return data
2023-07-19 10:45:48 +00:00
} catch (error) {
console.log('updateSubscriptionMutation error', error)
2023-07-20 05:56:41 +00:00
return {
updateSubscription: {
errorCodes: [UpdateSubscriptionErrorCode.BadRequest],
2023-07-20 05:56:41 +00:00
},
}
2023-07-19 10:45:48 +00:00
}
}