UNPKG

@grafana/alerting

Version:

Grafana Alerting Library – Build vertical integrations on top of the industry-leading alerting solution

1 lines 2.58 kB
{"version":3,"file":"useContactPoints.mjs","sources":["../../../../../../src/grafana/contactPoints/hooks/v0alpha1/useContactPoints.tsx"],"sourcesContent":["import {\n type TypedUseMutationResult,\n type TypedUseQueryHookResult,\n fetchBaseQuery,\n} from '@reduxjs/toolkit/query/react';\nimport { OverrideProperties } from 'type-fest';\n\nimport { CreateReceiverApiArg, type ListReceiverApiArg, alertingAPI } from '../../../api/v0alpha1/api.gen';\nimport type { ContactPoint, EnhancedListReceiverApiResponse } from '../../../api/v0alpha1/types';\n\n// this is a workaround for the fact that the generated types are not narrow enough\ntype ListContactPointsHookResult = TypedUseQueryHookResult<\n EnhancedListReceiverApiResponse,\n ListReceiverApiArg,\n ReturnType<typeof fetchBaseQuery>\n>;\n\n/**\n * useListContactPoints is a hook that fetches a list of contact points\n *\n * This function wraps the alertingAPI.useListReceiverQuery with proper typing\n * to ensure that the returned ContactPoints are correctly typed in the data.items array.\n *\n * It automatically uses the configured namespace for the query.\n */\nexport function useListContactPoints() {\n return alertingAPI.useListReceiverQuery<ListContactPointsHookResult>({});\n}\n\n// type narrowing mutations requires us to define a few helper types\ntype CreateContactPointArgs = OverrideProperties<\n CreateReceiverApiArg,\n { receiver: Omit<ContactPoint, 'status' | 'metadata'> }\n>;\n\ntype CreateContactPointMutation = TypedUseMutationResult<\n ContactPoint,\n CreateContactPointArgs,\n ReturnType<typeof fetchBaseQuery>\n>;\n\ntype UseCreateContactPointOptions = Parameters<\n typeof alertingAPI.endpoints.createReceiver.useMutation<CreateContactPointMutation>\n>[0];\n\n/**\n * useCreateContactPoint is a hook that creates a new contact point with one or more integrations\n *\n * This function wraps the alertingAPI.useCreateReceiverMutation with proper typing\n * to ensure that the payload supports type narrowing.\n */\nexport function useCreateContactPoint(options?: UseCreateContactPointOptions) {\n const [updateFn, result] = alertingAPI.endpoints.createReceiver.useMutation<CreateContactPointMutation>(options);\n\n const typedUpdateFn = (args: CreateContactPointArgs) => {\n // @ts-expect-error this one is just impossible for me to figure out\n const response = updateFn(args);\n return response;\n };\n\n return [typedUpdateFn, result] as const;\n}\n"],"names":["alertingAPI"],"mappings":";;AAyBO,SAAS,oBAAuB,GAAA;AACrC,EAAO,OAAAA,cAAA,CAAY,oBAAkD,CAAA,EAAE,CAAA;AACzE;;;;"}