>['value']>
+ }
config?: {
/**
* The base url
*/
- baseUrl?: string;
+ baseUrl?: string
/**
* Any aditional headers
*/
headers?: {
- [key: string]: any;
- };
+ [key: string]: any
+ }
/**
* The caching mechanism
*/
- cacheProvider?: CacheStoreType;
- };
+ cacheProvider?: CacheStoreType
+ }
}
) {
- type QuerysType = typeof queries;
+ type QuerysType = typeof queries
return function useQuery(
queryName: P,
otherConfig?: Omit<
FetchInit<
QuerysType[P] extends ReturnType
- ? QuerysType[P]["value"]
+ ? QuerysType[P]['value']
: any
>,
- "url"
+ 'url'
> & {
default?: QuerysType[P] extends ReturnType
- ? QuerysType[P]["value"]
- : any;
+ ? QuerysType[P]['value']
+ : any
variables?: QuerysType[P] extends ReturnType
- ? QuerysType[P]["variables"]
- : any;
- graphqlPath?: string;
+ ? QuerysType[P]['variables']
+ : any
+ graphqlPath?: string
}
) {
- const { defaults } = providerConfig || {};
+ const { defaults } = providerConfig || {}
- const thisDefaults = (defaults || ({} as any))?.[queryName];
+ const thisDefaults = (defaults || ({} as any))?.[queryName]
const queryVariables = {
...thisDefaults?.variables,
- ...(otherConfig as any)?.variables,
- };
+ ...(otherConfig as any)?.variables
+ }
- const { config = {} } = providerConfig || {};
+ const { config = {} } = providerConfig || {}
- const { cacheProvider, ...others } = config;
+ const { cacheProvider, ...others } = config
const g = useGql(queries[queryName] as any, {
cacheProvider: config?.cacheProvider,
@@ -270,7 +276,7 @@ export function queryProvider(
headers: {
...others?.headers,
...thisDefaults?.headers,
- ...otherConfig?.headers,
+ ...otherConfig?.headers
},
...{ __fromProvider: true },
default: {
@@ -281,38 +287,38 @@ export function queryProvider(
* 'value' property (when using the `gql` function)
*/
// @ts-ignore
- otherConfig?.default) as R[P]["value"],
+ otherConfig?.default) as R[P]['value']
},
- variables: queryVariables,
- });
+ variables: queryVariables
+ })
const thisData = useMemo(
() => ({
...g?.data,
- variables: queryVariables,
+ variables: queryVariables
}),
[serialize({ data: g?.data, queryVariables })]
- );
+ )
return {
...g,
config: {
...g?.config,
- config: undefined,
+ config: undefined
},
- data: thisData,
- } as Omit & {
+ data: thisData
+ } as Omit & {
data: {
data: QuerysType[P] extends ReturnType
- ? QuerysType[P]["value"]
- : any;
- errors?: any[];
+ ? QuerysType[P]['value']
+ : any
+ errors?: any[]
variables: QuerysType[P] extends ReturnType
- ? QuerysType[P]["variables"]
- : any;
- };
- };
- };
+ ? QuerysType[P]['variables']
+ : any
+ }
+ }
+ }
}
/**
@@ -323,45 +329,50 @@ export function mutateData(
) {
for (let pair of pairs) {
try {
- const [k, v, _revalidate] = pair;
- const key = serialize({ idString: serialize(k) });
- const requestCallId = "";
+ const [k, v, _revalidate] = pair
+ const key = serialize({ idString: serialize(k) })
+ const requestCallId = ''
if (isFunction(v)) {
- let newVal = v(cacheForMutation.get(key));
- runningMutate.set(key, undefined);
+ let newVal = v(cacheForMutation.get(key))
+ runningMutate.set(key, undefined)
requestsProvider.emit(key, {
data: newVal,
isMutating: true,
- requestCallId,
- });
+ requestCallId
+ })
if (_revalidate) {
- previousConfig.set(key, undefined);
- requestsProvider.emit(serialize(k), {});
+ previousConfig.set(key, undefined)
+ requestsProvider.emit(serialize(k), {})
}
queue(() => {
- valuesMemory.set(key, newVal);
- cacheForMutation.set(key, newVal);
- });
+ valuesMemory.set(key, newVal)
+ cacheForMutation.set(key, newVal)
+ })
} else {
- runningMutate.set(key, undefined);
+ runningMutate.set(key, undefined)
requestsProvider.emit(key, {
requestCallId,
isMutating: true,
- data: v,
- });
+ data: v
+ })
if (_revalidate) {
- previousConfig.set(key, undefined);
- requestsProvider.emit(serialize(k), {});
+ previousConfig.set(key, undefined)
+ requestsProvider.emit(serialize(k), {})
}
queue(() => {
- valuesMemory.set(key, v);
- cacheForMutation.set(key, v);
- });
+ valuesMemory.set(key, v)
+ cacheForMutation.set(key, v)
+ })
}
} catch (err) {}
}
}
-export function fetchOptions(options: FetchInit) {
- return options;
+export function fetchOptions(
+ init: string | FetchConfigType,
+ options?: Omit, 'url'>
+) {
+ return typeof init === 'string'
+ ? { url: init, ...options }
+ : (init as FetchConfigType)
}