@malloy-publisher/sdk 0.0.59 → 0.0.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@malloy-publisher/sdk",
3
3
  "description": "Malloy Publisher SDK",
4
- "version": "0.0.59",
4
+ "version": "0.0.60",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs.js",
7
7
  "module": "dist/index.es.js",
@@ -29,6 +29,7 @@ import { useNotebookStorage } from "./NotebookStorageProvider";
29
29
 
30
30
  import * as Malloy from "@malloydata/malloy-interfaces";
31
31
  import { ModelPicker } from "./ModelPicker";
32
+ import { getAxiosConfig } from "../../hooks";
32
33
 
33
34
  const modelsApi = new ModelsApi(new Configuration());
34
35
 
@@ -52,7 +53,7 @@ export default function MutableNotebook({
52
53
  }: MutableNotebookProps) {
53
54
  const navigate = useRouterClickHandler();
54
55
  const { projectName, packageName, versionId } = usePackage();
55
- const { server, accessToken } = useServer();
56
+ const { server, getAccessToken } = useServer();
56
57
  const { notebookStorage, userContext } = useNotebookStorage();
57
58
  if (!projectName || !packageName) {
58
59
  throw new Error(
@@ -81,13 +82,6 @@ export default function MutableNotebook({
81
82
  );
82
83
  const [menuIndex, setMenuIndex] = React.useState<number | null>(null);
83
84
  const menuOpen = Boolean(menuAnchorEl);
84
- const handleMenuClick = (
85
- event: React.MouseEvent<HTMLButtonElement>,
86
- index: number,
87
- ) => {
88
- setMenuAnchorEl(event.currentTarget);
89
- setMenuIndex(index);
90
- };
91
85
  const handleMenuClose = () => {
92
86
  setMenuAnchorEl(null);
93
87
  setMenuIndex(null);
@@ -147,10 +141,13 @@ export default function MutableNotebook({
147
141
  console.log("Fetching model from Publisher", model);
148
142
  promises.push(
149
143
  modelsApi
150
- .getModel(projectName, packageName, model, versionId, {
151
- baseURL: server,
152
- withCredentials: !accessToken,
153
- })
144
+ .getModel(
145
+ projectName,
146
+ packageName,
147
+ model,
148
+ versionId,
149
+ await getAxiosConfig(server, getAccessToken),
150
+ )
154
151
  .then((data) => ({
155
152
  modelPath: model,
156
153
  sourceInfos: data.data.sourceInfos.map((source) =>
@@ -175,7 +172,15 @@ export default function MutableNotebook({
175
172
  };
176
173
 
177
174
  fetchModels();
178
- }, [accessToken, notebookData, packageName, projectName, server, versionId]);
175
+ }, [
176
+ // Note this cannot depend on sourceAndPaths because it will cause an infinite loop.
177
+ getAccessToken,
178
+ notebookData,
179
+ packageName,
180
+ projectName,
181
+ server,
182
+ versionId,
183
+ ]);
179
184
 
180
185
  React.useEffect(() => {
181
186
  if (!notebookPath) {
@@ -2,25 +2,25 @@ import React, { createContext, useContext, ReactNode } from "react";
2
2
 
3
3
  export interface ServerContextValue {
4
4
  server: string;
5
- accessToken?: string;
5
+ getAccessToken?: () => Promise<string>;
6
6
  }
7
7
 
8
8
  const ServerContext = createContext<ServerContextValue | undefined>(undefined);
9
9
 
10
10
  export interface ServerProviderProps {
11
11
  server?: string;
12
- accessToken?: string;
12
+ getAccessToken?: () => Promise<string>;
13
13
  children: ReactNode;
14
14
  }
15
15
 
16
16
  export const ServerProvider: React.FC<ServerProviderProps> = ({
17
17
  server = `${window.location.protocol}//${window.location.host}/api/v0`,
18
- accessToken,
18
+ getAccessToken,
19
19
  children,
20
20
  }) => {
21
21
  const value: ServerContextValue = {
22
22
  server,
23
- accessToken,
23
+ getAccessToken,
24
24
  };
25
25
 
26
26
  return (
@@ -0,0 +1 @@
1
+ export { getAxiosConfig } from "./useQueryWithApiError";
@@ -25,25 +25,32 @@ const globalQueryClient = new QueryClient({
25
25
  },
26
26
  });
27
27
 
28
- export function useQueryWithApiError<TData = unknown, TError = ApiError>(
29
- options: Omit<UseQueryOptions<TData, TError>, "throwOnError" | "retry"> & {
30
- queryFn: (config: RawAxiosRequestConfig) => Promise<TData>;
31
- },
32
- ): UseQueryResult<TData, TError> {
33
- const { server, accessToken } = useServer();
34
- const config = {
28
+ export const getAxiosConfig = async (
29
+ server: string,
30
+ getAccessToken: () => Promise<string>,
31
+ ): Promise<RawAxiosRequestConfig> => {
32
+ const accessToken = getAccessToken ? await getAccessToken() : undefined;
33
+ return {
35
34
  baseURL: server,
36
35
  withCredentials: !accessToken,
37
36
  headers: {
38
37
  Authorization: accessToken && `Bearer ${accessToken}`,
39
38
  },
40
39
  } as RawAxiosRequestConfig;
40
+ };
41
+ export function useQueryWithApiError<TData = unknown, TError = ApiError>(
42
+ options: Omit<UseQueryOptions<TData, TError>, "throwOnError" | "retry"> & {
43
+ queryFn: (config: RawAxiosRequestConfig) => Promise<TData>;
44
+ },
45
+ ): UseQueryResult<TData, TError> {
46
+ const { server, getAccessToken } = useServer();
41
47
  const enhancedOptions: UseQueryOptions<TData, TError> = {
42
48
  ...options,
43
49
  // Add in the server to the query key so that we can have a per-server caches.
44
50
  queryKey: [...options.queryKey, server],
45
51
  queryFn: async () => {
46
52
  try {
53
+ const config = await getAxiosConfig(server, getAccessToken);
47
54
  return await options.queryFn(config);
48
55
  } catch (err) {
49
56
  // Standardized error handling for axios errors
@@ -90,18 +97,12 @@ export function useMutationWithApiError<
90
97
  ) => Promise<TData>;
91
98
  },
92
99
  ): UseMutationResult<TData, TError, TVariables> {
93
- const { server, accessToken } = useServer();
94
- const config = {
95
- baseURL: server,
96
- withCredentials: !accessToken,
97
- headers: {
98
- Authorization: accessToken && `Bearer ${accessToken}`,
99
- },
100
- } as RawAxiosRequestConfig;
100
+ const { server, getAccessToken } = useServer();
101
101
  const enhancedOptions: UseMutationOptions<TData, TError, TVariables> = {
102
102
  ...options,
103
103
  mutationFn: async (variables: TVariables) => {
104
104
  try {
105
+ const config = await getAxiosConfig(server, getAccessToken);
105
106
  return await options.mutationFn(variables, config);
106
107
  } catch (err) {
107
108
  // Standardized error handling for axios errors