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

82 lines
1.7 KiB
TypeScript
Raw Permalink Normal View History

2022-10-22 23:54:10 +00:00
import { gql } from 'graphql-request'
import { gqlFetcher } from '../networkHelpers'
import { IntegrationType } from '../queries/useGetIntegrationsQuery'
2022-10-22 23:54:10 +00:00
export enum ImportItemState {
All = 'ALL',
Archived = 'ARCHIVED',
Unarchived = 'UNARCHIVED',
Unread = 'UNREAD'
}
2022-10-22 23:54:10 +00:00
export type SetIntegrationInput = {
id?: string
name: string
2022-10-22 23:54:10 +00:00
type: IntegrationType
token: string
2022-10-22 23:54:10 +00:00
enabled: boolean
importItemState?: ImportItemState
2024-03-13 07:55:34 +00:00
settings?: any
2022-10-22 23:54:10 +00:00
}
type SetIntegrationResult = {
2024-03-25 09:43:57 +00:00
setIntegration: SetIntegrationData
}
export enum SetIntegrationErrorCode {
AlreadyExists = 'ALREADY_EXISTS',
BadRequest = 'BAD_REQUEST',
InvalidToken = 'INVALID_TOKEN',
NotFound = 'NOT_FOUND',
Unauthorized = 'UNAUTHORIZED',
}
type SetIntegrationData = {
2022-10-22 23:54:10 +00:00
integration: Integration
errorCodes?: SetIntegrationErrorCode[]
2022-10-22 23:54:10 +00:00
}
type Integration = {
id: string
name: string
2022-10-22 23:54:10 +00:00
type: IntegrationType
token: string
enabled: boolean
createdAt: Date
updatedAt: Date
}
export async function setIntegrationMutation(
input: SetIntegrationInput
2024-03-25 09:43:57 +00:00
): Promise<Integration> {
2022-10-22 23:54:10 +00:00
const mutation = gql`
mutation SetIntegration($input: SetIntegrationInput!) {
2022-10-22 23:54:10 +00:00
setIntegration(input: $input) {
... on SetIntegrationSuccess {
integration {
id
name
2022-10-22 23:54:10 +00:00
type
token
enabled
createdAt
updatedAt
2024-03-13 07:55:34 +00:00
settings
2022-10-22 23:54:10 +00:00
}
}
... on SetIntegrationError {
errorCodes
}
}
}
`
const data = (await gqlFetcher(mutation, { input })) as SetIntegrationResult
2024-03-25 09:43:57 +00:00
const error = data.setIntegration.errorCodes?.find(() => true)
if (error) {
throw new Error(error)
2022-10-22 23:54:10 +00:00
}
2024-03-25 09:43:57 +00:00
return data.setIntegration.integration
2022-10-22 23:54:10 +00:00
}