{"version":3,"file":"RestrictedGrafanaApis.cjs","sources":["../../../../src/context/plugins/RestrictedGrafanaApis.tsx"],"sourcesContent":["import { createContext, ReactElement, PropsWithChildren, useMemo, useContext } from 'react';\n\n// Generic schema type to avoid zod dependency in @grafana/data\ninterface ZodSchema {\n  parse: (data: unknown) => unknown;\n  safeParse: (data: unknown) => { success: boolean; data?: unknown; error?: unknown };\n}\n\nexport interface RestrictedGrafanaApisContextTypeInternal {\n  // Add types for restricted Grafana APIs here\n  // (Make sure that they are typed as optional properties)\n  // e.g. addPanel?: (vizPanel: VizPanel) => void;\n  alertingAlertRuleFormSchema?: ZodSchema;\n}\n\n// We are exposing this through a \"type validation\", to make sure that all APIs are optional (which helps plugins catering for scenarios when they are not available).\ntype RequireAllPropertiesOptional<T> = keyof T extends never\n  ? T\n  : { [K in keyof T]-?: undefined extends T[K] ? never : K }[keyof T] extends never\n    ? T\n    : 'Error: all properties of `RestrictedGrafanaApisContextTypeInternal` must be marked as optional, as their availability is controlled via a configuration parameter. Please have a look at `RestrictedGrafanaApisContextTypeInternal`.';\nexport type RestrictedGrafanaApisContextType = RequireAllPropertiesOptional<RestrictedGrafanaApisContextTypeInternal>;\n\n// A type for allowing / blocking plugins for a given API\nexport type RestrictedGrafanaApisAllowList = Partial<\n  Record<keyof RestrictedGrafanaApisContextType | string, Array<string | RegExp>>\n>;\n\nexport const RestrictedGrafanaApisContext = createContext<RestrictedGrafanaApisContextType>({});\n\nexport type Props = {\n  pluginId: string;\n  apis: RestrictedGrafanaApisContextType;\n  // Use it to share APIs with plugins (TAKES PRECEDENCE over `apiBlockList`)\n  apiAllowList?: RestrictedGrafanaApisAllowList;\n  // Use it to disable sharing APIs with plugins.\n  apiBlockList?: RestrictedGrafanaApisAllowList;\n};\n\nexport function RestrictedGrafanaApisContextProvider(props: PropsWithChildren<Props>): ReactElement {\n  const { children, pluginId, apis, apiAllowList, apiBlockList } = props;\n  const allowedApis = useMemo(() => {\n    const allowedApis: RestrictedGrafanaApisContextType = {};\n\n    // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n    for (const api of Object.keys(apis) as Array<keyof RestrictedGrafanaApisContextType>) {\n      if (\n        apiAllowList &&\n        apiAllowList[api] &&\n        (apiAllowList[api].includes(pluginId) ||\n          apiAllowList[api].some((keyword) => keyword instanceof RegExp && keyword.test(pluginId)))\n      ) {\n        allowedApis[api] = apis[api];\n        continue;\n      }\n\n      // IF no allow list is defined (only block list), then we only omit the blocked APIs\n      if (\n        (!apiAllowList || Object.keys(apiAllowList).length === 0) &&\n        apiBlockList &&\n        apiBlockList[api] &&\n        !(\n          apiBlockList[api].includes(pluginId) ||\n          apiBlockList[api].some((keyword) => keyword instanceof RegExp && keyword.test(pluginId))\n        )\n      ) {\n        allowedApis[api] = apis[api];\n      }\n    }\n\n    return allowedApis;\n  }, [apis, apiAllowList, apiBlockList, pluginId]);\n\n  return <RestrictedGrafanaApisContext.Provider value={allowedApis}>{children}</RestrictedGrafanaApisContext.Provider>;\n}\n\nexport function useRestrictedGrafanaApis(): RestrictedGrafanaApisContextType {\n  const context = useContext(RestrictedGrafanaApisContext);\n\n  if (!context) {\n    throw new Error(\n      'useRestrictedGrafanaApis() can only be used inside a plugin context (The `RestrictedGrafanaApisContext` is not available).'\n    );\n  }\n\n  return context;\n}\n"],"names":["createContext","useMemo","allowedApis","useContext"],"mappings":";;;;;;;;AA4BO,MAAM,4BAAA,GAA+BA,mBAAA,CAAgD,EAAE;AAWvF,SAAS,qCAAqC,KAAA,EAA+C;AAClG,EAAA,MAAM,EAAE,QAAA,EAAU,QAAA,EAAU,IAAA,EAAM,YAAA,EAAc,cAAa,GAAI,KAAA;AACjE,EAAA,MAAM,WAAA,GAAcC,cAAQ,MAAM;AAChC,IAAA,MAAMC,eAAgD,EAAC;AAGvD,IAAA,KAAA,MAAW,GAAA,IAAO,MAAA,CAAO,IAAA,CAAK,IAAI,CAAA,EAAoD;AACpF,MAAA,IACE,YAAA,IACA,aAAa,GAAG,CAAA,KACf,aA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