Skip to content

Commit c500077

Browse files
committed
refactor: type imports and interface renames
1 parent 89e7526 commit c500077

19 files changed

+107
-111
lines changed

src/builder/HttpQueryBuilder.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { RequestError } from '../http/errors';
2-
import { ExtractPathParams, HttpMethod } from '../http/types';
3-
import { WithOptional } from '../type-utils';
1+
import type { RequestError } from '../http/errors';
2+
import type { ExtractPathParams, HttpMethod } from '../http/types';
3+
import type { WithOptional } from '../type-utils';
44
import { QueryBuilder } from './QueryBuilder';
5-
import { BuilderConfig } from './types';
6-
import { HttpBaseHeaders, HttpBaseParams, HttpBaseSearch, HttpBuilderVars } from './types';
7-
import { createHttpMergeVarsFn, createHttpQueryFn, createHttpQueryKeySanitizer } from './utils';
5+
import { createHttpMergeVarsFn, createHttpQueryFn, createHttpQueryKeySanitizer } from './http-utils';
6+
import type { BuilderConfig } from './types';
7+
import type { HttpBaseHeaders, HttpBaseParams, HttpBaseSearch, HttpBuilderVars } from './types';
88

99
export class HttpQueryBuilder<
1010
TParam = unknown,

src/builder/QueryBuilder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { operateOnTags } from '../tags/operateOnTags';
2-
import { QueryTagObject, QueryTagOption, QueryUpdateTagObject } from '../tags/types';
2+
import type { QueryTagObject, QueryTagOption, QueryUpdateTagObject } from '../tags/types';
33
import { QueryBuilderFrozen } from './QueryBuilderFrozen';
4-
import { MiddlewareFn, createMiddlewareFunction } from './createMiddlewareFunction';
5-
import { PreprocessorFn, createPreprocessorFunction, identityPreprocessor } from './createPreprocessorFunction';
4+
import { type MiddlewareFn, createMiddlewareFunction } from './createMiddlewareFunction';
5+
import { type PreprocessorFn, createPreprocessorFunction, identityPreprocessor } from './createPreprocessorFunction';
66
import { createTagMiddleware } from './createTagMiddleware';
77
import { createUpdateMiddleware } from './createUpdateMiddleware';
8-
import { BuilderConfig } from './types';
8+
import type { BuilderConfig } from './types';
99
import { mergeVars } from './utils';
1010

1111
export class QueryBuilder<TVars, TData, TError, TKey extends unknown[], TTags extends Record<string, unknown>> extends QueryBuilderFrozen<

src/builder/QueryBuilderClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import type {
99
SetDataOptions,
1010
} from '@tanstack/react-query';
1111
import { MutationObserver } from '@tanstack/react-query';
12-
import { QueryBuilderFrozen } from './QueryBuilderFrozen';
13-
import { BuilderConfig } from './types';
12+
import type { QueryBuilderFrozen } from './QueryBuilderFrozen';
13+
import type { BuilderConfig } from './types';
1414

1515
export class QueryBuilderClient<
1616
TVars,

src/builder/QueryBuilderFrozen.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import {
2828
useSuspenseQueries,
2929
useSuspenseQuery,
3030
} from '@tanstack/react-query';
31-
import { FunctionType, TODO, WithRequired } from '../type-utils';
31+
import type { FunctionType, TODO, WithRequired } from '../type-utils';
3232
import { QueryBuilderClient } from './QueryBuilderClient';
3333
import { QueryBuilderTagsManager } from './QueryBuilderTagsManager';
34-
import { BuilderOptions, mergeBuilderOptions } from './options';
35-
import { BuilderConfig, BuilderQueriesResult } from './types';
34+
import { type BuilderOptions, mergeBuilderOptions } from './options';
35+
import type { BuilderConfig, BuilderQueriesResult } from './types';
3636
import { areKeysEqual, getRandomKey, mergeMutationOptions, mergeVars } from './utils';
3737

3838
export class QueryBuilderFrozen<

src/builder/QueryBuilderTagsManager.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { MutationFunction, useMutation, useQueryClient } from '@tanstack/react-query';
1+
import { type MutationFunction, useMutation, useQueryClient } from '@tanstack/react-query';
22
import { operateOnTags } from '../tags/operateOnTags';
3-
import { QueryTagContext, QueryUpdateTag, TagOperationOptions } from '../tags/types';
3+
import type { QueryTagContext, QueryUpdateTag, TagOperationOptions } from '../tags/types';
44
import { updateTags } from '../tags/updateTags';
5-
import { WithOptional } from '../type-utils';
6-
import { QueryBuilderFrozen } from './QueryBuilderFrozen';
7-
import { BuilderConfig } from './types';
5+
import type { WithOptional } from '../type-utils';
6+
import type { QueryBuilderFrozen } from './QueryBuilderFrozen';
7+
import type { BuilderConfig } from './types';
88

99
export class QueryBuilderTagsManager<TVars, TData, TError, TKey extends unknown[], TTags extends Record<string, unknown>> {
1010
private declare _options: BuilderConfig<TVars, TData, TError, TKey>['options'];

src/builder/createMiddlewareFunction.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { BuilderConfig } from './types';
1+
import type { BuilderConfig } from './types';
22
import type { BuilderQueryContext, BuilderQueryFn } from './types';
33

44
export type MiddlewareFn<TVars, TData, TError, TKey extends unknown[]> = (
55
context: MiddlewareContext<TKey>,
66
next: MiddlewareNextFn<any, any, any, any>,
7-
throwError: (error: TError) => never,
87
config: BuilderConfig<TVars, TData, TError, TKey>,
98
) => TData | Promise<TData>;
109

@@ -23,15 +22,11 @@ const createMiddlewareContext = <TKey extends unknown[]>(context: BuilderQueryCo
2322
};
2423
};
2524

26-
const throwError = (error: any): never => {
27-
throw error;
28-
};
29-
3025
export const createMiddlewareFunction = <TVars, TData, TError, TKey extends unknown[]>(
3126
originalFn: BuilderQueryFn<any, any, any, any>,
3227
middleware: MiddlewareFn<TVars, TData, TError, TKey>,
3328
config: BuilderConfig<TVars, TData, TError, TKey>,
3429
): BuilderQueryFn<TVars, TData, TError, TKey> => {
3530
return async (context) =>
36-
middleware(createMiddlewareContext<TKey>(context), async (ctx) => originalFn({ ...context, queryKey: [ctx.vars] }), throwError, config);
31+
middleware(createMiddlewareContext<TKey>(context), async (ctx) => originalFn({ ...context, queryKey: [ctx.vars] }), config);
3732
};

src/builder/createTagMiddleware.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { hashKey } from '@tanstack/react-query';
22
import { resolveTags } from '../tags/resolveTags';
3-
import { QueryTagCache, QueryTagObject, QueryTagOption } from '../tags/types';
4-
import { MiddlewareFn } from './createMiddlewareFunction';
3+
import type { QueryTagCache, QueryTagObject, QueryTagOption } from '../tags/types';
4+
import type { MiddlewareFn } from './createMiddlewareFunction';
55

66
type CreateTagMiddleware = <TVars, TData, TError, TKey extends unknown[]>(
77
tags: QueryTagOption<any, any, any>[],
88
cacheId: string | number,
99
) => MiddlewareFn<TVars, TData, TError, TKey>;
1010

1111
export const createTagMiddleware: CreateTagMiddleware = (tags, cacheId) =>
12-
async function tagMiddlware(ctx, next, _, config) {
12+
async function tagMiddlware(ctx, next, config) {
1313
if (ctx.operationType === 'mutation') return next(ctx);
1414

1515
let resolvedTags: QueryTagObject[] = [];

src/builder/createUpdateMiddleware.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
import { operateOnTags } from '../tags/operateOnTags';
22
import { resolveTags } from '../tags/resolveTags';
3-
import { QueryTagContext, QueryTagOption, QueryUpdateTagObject } from '../tags/types';
4-
import { UpdateTagsUndoer, undoUpdateTags, updateTags } from '../tags/updateTags';
5-
import { MiddlewareFn } from './createMiddlewareFunction';
3+
import type { QueryTagContext, QueryTagOption, QueryUpdateTagObject } from '../tags/types';
4+
import { type UpdateTagsUndoer, undoUpdateTags, updateTags } from '../tags/updateTags';
5+
import type { MiddlewareFn } from './createMiddlewareFunction';
66

77
type CreateUpdateMiddleware = <TVars, TData, TError, TKey extends unknown[], TTags extends Record<string, unknown>>(
88
tags: QueryTagOption<TVars, TData, TError, QueryUpdateTagObject<TVars, TData, TError, TTags>>[],
99
) => MiddlewareFn<TVars, TData, TError, TKey>;
1010

1111
export const createUpdateMiddleware: CreateUpdateMiddleware = (tags) =>
12-
async function updateMiddleware(ctx, next, throwError, config) {
12+
async function updateMiddleware(ctx, next, config) {
1313
if (ctx.operationType !== 'mutation') return next(ctx);
1414

1515
type TagObj = QueryUpdateTagObject<any, any, any, any>;
1616
type TagCtx = QueryTagContext<any>;
1717

1818
let undos: UpdateTagsUndoer[] | null = null;
1919
const invalidates: TagObj[] = [];
20-
const optCtx: TagCtx = { client: ctx.client, vars: ctx.vars, data: undefined };
20+
const preCtx: TagCtx = { client: ctx.client, vars: ctx.vars, data: undefined };
2121

2222
try {
23-
const optUpdates = resolveTags<any, TagObj>({ tags, ...optCtx }).filter((u) => u.optimistic);
23+
const preUpdates = resolveTags({ tags, ...preCtx }).filter((u) => u.optimistic);
2424
undos = updateTags({
2525
queryClient: ctx.client,
26-
tags: optUpdates.filter((x) => x.updater),
27-
ctx: optCtx,
26+
tags: preUpdates.filter((x) => x.updater),
27+
ctx: preCtx,
2828
optimistic: true,
2929
});
30-
invalidates.push(...optUpdates);
30+
invalidates.push(...preUpdates);
3131

32-
const optToInvalidate = optUpdates.filter((tag) => ['pre', 'both'].includes(tag.invalidate || 'both'));
32+
const optToInvalidate = preUpdates.filter((tag) => ['pre', 'both'].includes(tag.invalidate || 'both'));
3333
operateOnTags({ queryClient: ctx.client, tags: optToInvalidate }, { refetchType: 'none' });
3434

3535
const data = await next(ctx);
3636

37-
const pesCtx: TagCtx = { ...optCtx, data };
38-
const pesUpdates = resolveTags<any, TagObj>({ tags, ...pesCtx }).filter((u) => !u.optimistic);
39-
updateTags({ queryClient: ctx.client, tags: pesUpdates.filter((x) => x.updater), ctx: pesCtx });
40-
invalidates.push(...pesUpdates);
37+
const postCtx: TagCtx = { ...preCtx, data };
38+
const postUpdates = resolveTags({ tags, ...postCtx }).filter((u) => !u.optimistic);
39+
updateTags({ queryClient: ctx.client, tags: postUpdates.filter((x) => x.updater), ctx: postCtx });
40+
invalidates.push(...postUpdates);
4141

4242
return data;
4343
} catch (error) {
4444
if (undos?.length) undoUpdateTags(undos, ctx.client);
4545

46-
const pesCtx: TagCtx = { ...optCtx, error };
47-
const pesUpdates = resolveTags<any, TagObj>({ tags, ...pesCtx }).filter((u) => !u.optimistic);
48-
invalidates.push(...pesUpdates);
46+
const postCtx: TagCtx = { ...preCtx, error };
47+
const postUpdates = resolveTags({ tags, ...postCtx }).filter((u) => !u.optimistic);
48+
invalidates.push(...postUpdates);
4949

5050
throw error;
5151
} finally {

src/builder/http-utils.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { httpRequest } from '../http/request';
2+
import { createHttpUrl } from '../http/utils';
3+
import type { BuilderKeySanitizerFn, BuilderMergeVarsFn, BuilderQueryFn, HttpBuilderVars } from './types';
4+
5+
export function createHttpMergeVarsFn<TVars extends HttpBuilderVars>(): BuilderMergeVarsFn<TVars> {
6+
const mergeHttpVars: BuilderMergeVarsFn<TVars> = (v1, v2) => {
7+
return {
8+
...v1,
9+
...v2,
10+
...(v1?.headers || v2?.headers ? { headers: { ...v1?.headers!, ...v2?.headers! } } : {}),
11+
...(v1?.params || v2?.params ? { params: { ...v1?.params!, ...v2?.params! } } : {}),
12+
...(v1?.search || v2?.search ? { search: { ...v1?.search!, ...v2?.search! } } : {}),
13+
...(v1?.meta || v2?.meta ? { meta: { ...v1?.meta!, ...v2?.meta! } } : {}),
14+
};
15+
};
16+
17+
return mergeHttpVars;
18+
}
19+
20+
export function createHttpQueryFn<TVars, TData, TError, TKey extends unknown[]>(
21+
mergeVarsFn: BuilderMergeVarsFn<TVars>,
22+
): BuilderQueryFn<TVars, TData, TError, TKey> {
23+
return async ({ queryKey, signal, pageParam }) => {
24+
const [vars] = queryKey || [];
25+
const mergedVars = mergeVarsFn(vars as any, pageParam as any);
26+
return httpRequest<TData>({ ...(mergedVars as any), signal });
27+
};
28+
}
29+
30+
/**
31+
* A query key sanitizer function that normalizes the query key
32+
* and removes irrelevant options which do not affect the query result.
33+
*/
34+
export function createHttpQueryKeySanitizer<TKey extends [HttpBuilderVars]>(): BuilderKeySanitizerFn<TKey> {
35+
const sanitizer: BuilderKeySanitizerFn<TKey> = function httpQueryKeySanitizer(queryKey) {
36+
const [vars] = queryKey || [];
37+
38+
const { baseUrl, params, search, path, method, body, headers, key } = vars;
39+
const url = createHttpUrl({ path, params, baseUrl, search });
40+
41+
const res = [url, method, body, headers, key];
42+
43+
const lastNullIndex = res.findLastIndex((x) => x != null);
44+
return res.slice(0, lastNullIndex + 1) as TKey;
45+
};
46+
47+
return sanitizer;
48+
}

src/builder/options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { GetNextPageParamFunction, GetPreviousPageParamFunction, UseMutationOptions, UseQueryOptions } from '@tanstack/react-query';
2-
import { FunctionType, WithOptional } from '../type-utils';
1+
import type { GetNextPageParamFunction, GetPreviousPageParamFunction, UseMutationOptions, UseQueryOptions } from '@tanstack/react-query';
2+
import type { FunctionType, WithOptional } from '../type-utils';
33
import { mergeQueryEnabled } from './utils';
44

55
export type BuilderOptions<TVars, TData, TError, TKey extends unknown[]> = WithOptional<

0 commit comments

Comments
 (0)