diff --git a/import.ts b/import.ts index a23532dc..38df076f 100644 --- a/import.ts +++ b/import.ts @@ -1,4 +1,4 @@ -export { useDeep, DeepProvider, useDeepGenerator, DeepClient, DeepContext, DeepNamespaceProvider, DeepNamespaceContext, useDeepNamespaces, useDeepNamespace, useTransparentState, useDebouncedInput, useDeepQuery, useDeepSubscription, useDeepId, useSearch, useAuthNode, useLink, useLinks, random, parseJwt, serializeQuery, serializeWhere, pathToWhere, useCan } from "./imports/client.js"; +export { useDeep, DeepProvider, useDeepGenerator, DeepClient, DeepContext, DeepNamespaceProvider, DeepNamespaceContext, useDeepNamespaces, useDeepNamespace, useTransparentState, useDebouncedInput, useDeepQuery, useDeepSubscription, useDeepId, useSearch, useAuthNode, useLink, useLinks, random, parseJwt, serializeQuery, serializeWhere, pathToWhere, useCan, getServerSidePropsDeep, useDeepPath, useDeepToken } from "./imports/client.js"; export type { Handler, SubscriptionI as Subscription, Observer, DeepClientOptions, DeepClientResult, DeepSearchOptions, DeepClientInstance, DeepClientAuthResult, DeepClientGuestOptions, DeepClientJWTOptions, UseDeepSubscriptionResult, DeepClientPackageSelector, DeepClientPackageContain, DeepClientLinkId, DeepClientStartItem, DeepClientPathItem, SelectTable, InsertTable, UpdateTable, DeleteTable, OperationType, SerialOperationType, Table, ValueForTable, ExpForTable, SerialOperationDetails, SerialOperation, DeepSerialOperation, AsyncSerialParams, INamespaces, Exp, UpdateValue, InsertObjects, Options, ReadOptions, WriteOptions } from "./imports/client.js"; export { MinilinksLink, MinilinkCollection, minilinks, MinilinksContext, toPlain, Minilinks, useMinilinksConstruct, useMinilinksFilter, useMinilinksHandle, useMinilinksApply, useMinilinksQuery, useMinilinksSubscription, useMinilinksGenerator, MinilinksProvider, useMinilinks, useMinilinksId } from "./imports/minilinks.js"; diff --git a/imports/client.tsx b/imports/client.tsx index 6a264e87..9b74b3ea 100644 --- a/imports/client.tsx +++ b/imports/client.tsx @@ -26,6 +26,7 @@ import { useDebounce } from '@react-hook/debounce'; import { Packages } from './packages.js'; import { useFiles, Files } from './files.js'; import { serializeError } from 'serialize-error'; +import { ApolloClientTokenizedProvider } from '@deep-foundation/react-hasura/apollo-client-tokenized-provider.js'; const moduleLog = debug.extend('client'); const log = debug.extend('log'); @@ -1005,9 +1006,9 @@ export class DeepClient = Link> implements DeepClientInst const secret = this.secret || this?.deep?.secret; const ssl = this.ssl || this?.deep?.ssl; const ws = this.ws || this?.deep?.ws; - if (!token && !secret) { - throw new Error('!token && !secret - invalid auth'); - } + // if (!token && !secret) { + // throw new Error('!token && !secret - invalid auth'); + // } this.apolloClient = generateApolloClient({ // @ts-ignore path: (options.path || this.deep?.apolloClient?.path || '').replace(/(^\w+:|^)\/\//, ''), @@ -2519,6 +2520,7 @@ export function DeepNamespaceProvider({ children }: { children: any }) { export const DeepContext = createContext>>(undefined); export function useDeepGenerator(generatorOptions?: DeepClientOptions>) { + // console.log('useDeepGenerator', generatorOptions); const { apolloClient: apolloClientProps, minilinks, ...otherGeneratorOptions } = generatorOptions; const log = debug.extend(useDeepGenerator.name) const apolloClientHook = useApolloClient(); @@ -2576,27 +2578,100 @@ export function useDeepGenerator(generatorOptions?: DeepClientOptions>) // return children; // }, () => true) -export function DeepProvider({ - apolloClient: apolloClientProps, +export function DeepProviderCore({ + path, + token, + secret, + ws, + ssl, minilinks: inputMinilinks, namespace, children, }: { - apolloClient?: IApolloClient, - minilinks?: MinilinkCollection, + path?: string; + token?: string; + secret?: string; + ws?: boolean; + ssl?: boolean; + minilinks?: MinilinkCollection; namespace?: string; children?: any; -}) { +}) { const providedMinilinks = useMinilinks(); const deep = useDeepGenerator({ - apolloClient: apolloClientProps, + path, token, secret, ssl, ws, minilinks: inputMinilinks || providedMinilinks, namespace, }); + useDeepNamespace(namespace, deep); - return - {children} - ; + return <> + + {children} + + ; +}; + +export function prepareOptions(o: { + path: string; + token?: string; + secret?: string; + ws?: boolean; + ssl?: boolean; +}) { + const _path = getPath(o.path); + const path = (_path || '').replace(/(^\w+:|^)\/\//, ''); + const token = o.token; + const secret = o.secret; + const ws = typeof(o.ws) === 'boolean' ? o.ws : typeof(window) == 'object'; + const ssl = typeof(o.ssl) === 'boolean' ? o.ssl : !!_path.includes('https'); + return { + path, token, secret, ws, ssl, + }; +} + +export function DeepProvider({ + path, + token, + secret, + ws, + ssl, + minilinks, + namespace, + children, +}: { + path?: string; + token?: string; + secret?: string; + ws?: boolean; + ssl?: boolean; + apolloClient?: IApolloClient; + minilinks?: MinilinkCollection; + namespace?: string; + children?: any; +}) { + const __path = getPath(path); + + const [_path] = useDeepPath(__path); + const [_token] = useDeepToken(token); + + const options = useMemo(() => prepareOptions({ + path: _path, + token: _token, + secret, + ws, + ssl, + }), [_path, _token, secret, ws, ssl]); + + return <> + {!!_path ? ( + + + {children} + + + ) : <>{children}} + ; } export function useDeep() { @@ -2977,3 +3052,24 @@ export const Query = memo(function Query({ query, options, onChange }: any): any }, [result]); return null; }, isEqual); + +export function useDeepPath(defaultValue: string | undefined = process?.env?.NEXT_PUBLIC_GRAPHQL_URL) { + const r = useLocalStore('dc-dg-path', defaultValue); + // console.log('useDeepPath', 'defaultValue', defaultValue, 'result', r[0]); + return r; +} + +export function useDeepToken(defaultValue: string | undefined = process?.env?.NEXT_PUBLIC_DEEP_TOKEN) { + return useTokenController(defaultValue); +} + +export function getServerSidePropsDeep(arg, result: any = {}) { + result.props = result.props || {}; + result.props.path = getPath(); + // console.log('getServerSidePropsDeep', result.props); + return result; +} + +export function getPath(path?: string) { + return path || process?.env?.NEXT_PUBLIC_GRAPHQL_URL || process?.env?.GQL; +} diff --git a/package.json b/package.json index 2656461f..71d206a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@deep-foundation/deeplinks", - "version": "0.0.607", + "version": "0.0.608", "license": "Unlicense", "type": "module", "main": "import.js",