Skip to content

Commit c6fd693

Browse files
committed
refactor and move http types
1 parent f02326a commit c6fd693

File tree

10 files changed

+37
-35
lines changed

10 files changed

+37
-35
lines changed

packages/tanstack-query-builder/src/builder/types.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { QueryClient, QueryFunctionContext, QueryKey, UseQueryResult } from '@tanstack/react-query';
2-
import type { HttpRequestOptions } from '../http/types';
3-
import type { HasFlag, Prettify } from '../type-utils';
2+
import type { HasFlag } from '../type-utils';
43
import type { BuilderOptions, BuilderPaginationOptions } from './options';
54

65
export type BuilderConfig<TVars, TData, TError, TKey extends unknown[]> = {
@@ -15,31 +14,6 @@ export type BuilderConfig<TVars, TData, TError, TKey extends unknown[]> = {
1514
paginationOptions?: BuilderPaginationOptions<TVars, TData, TError, TKey>;
1615
};
1716

18-
export type HttpBaseHeaders = Record<string, string | string[]>;
19-
export type HttpBaseParams = Record<string | number, unknown>;
20-
export type HttpBaseSearch = Record<string | number, unknown>;
21-
22-
export type HttpBuilderBaseVars = Omit<HttpRequestOptions, 'body' | 'headers' | 'params' | 'search' | 'meta'>;
23-
24-
type WhenRequired<T, TReq> = T extends undefined
25-
? Partial<TReq>
26-
: unknown extends T
27-
? Partial<TReq>
28-
: T extends null
29-
? Partial<TReq>
30-
: Partial<T> extends T
31-
? Partial<TReq>
32-
: TReq;
33-
34-
export type HttpBuilderVars<TParam = unknown, TSearch = unknown, TBody = unknown, THeaders = unknown, TMeta = unknown> = Prettify<
35-
HttpBuilderBaseVars &
36-
WhenRequired<TParam, { params: TParam }> &
37-
WhenRequired<TSearch, { search: TSearch }> &
38-
WhenRequired<TBody, { body: TBody }> &
39-
WhenRequired<THeaders, { headers: THeaders }> &
40-
WhenRequired<TMeta, { meta: TMeta }>
41-
>;
42-
4317
export type BuilderMergeVarsFn<TVars> = (vars1: TVars | Partial<TVars>, vars2: TVars | Partial<TVars>) => TVars | Partial<TVars>;
4418

4519
type QueriesResultItemType<TVars, TData, TError, TKey> = UseQueryResult<TData, TError>;

packages/tanstack-query-builder/src/http/HttpQueryBuilder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { QueryClient } from '@tanstack/react-query';
22
import { QueryBuilder } from '../builder/QueryBuilder';
33
import type { BuilderPaginationOptions } from '../builder/options';
44
import type { BuilderConfig, BuilderFlags } from '../builder/types';
5-
import type { HttpBaseHeaders, HttpBaseParams, HttpBaseSearch, HttpBuilderVars } from '../builder/types';
65
import type { RequestError } from '../http/errors';
7-
import type { ExtractPathParams, HttpMethod } from '../http/types';
86
import type { WithOptional } from '../type-utils';
7+
import type { HttpBaseHeaders, HttpBaseParams, HttpBaseSearch, HttpBuilderVars } from './builder-types';
98
import { createHttpMergeVarsFn, createHttpQueryFn, createHttpQueryKeySanitizer } from './builder-utils';
9+
import type { ExtractPathParams, HttpMethod } from './request-types';
1010

1111
export class HttpQueryBuilder<
1212
TParam = unknown,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { Prettify, WhenRequired } from '../type-utils';
2+
import type { HttpRequestOptions } from './request-types';
3+
4+
export type HttpBaseHeaders = Record<string, string | string[]>;
5+
export type HttpBaseParams = Record<string | number, unknown>;
6+
export type HttpBaseSearch = Record<string | number, unknown>;
7+
8+
export type HttpBuilderBaseVars = Omit<HttpRequestOptions, 'body' | 'headers' | 'params' | 'search' | 'meta'>;
9+
10+
export type HttpBuilderVars<TParam = unknown, TSearch = unknown, TBody = unknown, THeaders = unknown, TMeta = unknown> = Prettify<
11+
HttpBuilderBaseVars &
12+
WhenRequired<TParam, { params: TParam }> &
13+
WhenRequired<TSearch, { search: TSearch }> &
14+
WhenRequired<TBody, { body: TBody }> &
15+
WhenRequired<THeaders, { headers: THeaders }> &
16+
WhenRequired<TMeta, { meta: TMeta }>
17+
>;

packages/tanstack-query-builder/src/http/builder-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { BuilderKeySanitizerFn, BuilderMergeVarsFn, BuilderQueryFn, HttpBuilderVars } from '../builder/types';
1+
import type { BuilderKeySanitizerFn, BuilderMergeVarsFn, BuilderQueryFn } from '../builder/types';
2+
import type { HttpBuilderVars } from './builder-types';
23
import { httpRequest } from './request';
34
import { createHttpUrl } from './request-utils';
45

packages/tanstack-query-builder/src/http/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { HttpRequestOptions } from './types';
1+
import type { HttpRequestOptions } from './request-types';
22

33
type RequestErrorInit = Pick<RequestError, 'options' | 'request' | 'response' | 'body' | 'cause'>;
44

File renamed without changes.

packages/tanstack-query-builder/src/http/request-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RequestError } from './errors';
2-
import type { HttpRequestOptions, HttpRequestPathParams } from './types';
2+
import type { HttpRequestOptions, HttpRequestPathParams } from './request-types';
33

44
function prepareParams(params: Record<string, unknown>): Record<string, string> {
55
const newParams: Record<string, string> = {};

packages/tanstack-query-builder/src/http/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { requestConcurrency } from './concurrency';
22
import { Deferred } from './deferred';
33
import { AbortError, RequestError, TimeoutError } from './errors';
4+
import type { HttpRequestOptions } from './request-types';
45
import { createHttpUrl, inferErrorMessage, resolveCredentials } from './request-utils';
5-
import type { HttpRequestOptions } from './types';
66

77
const CONTENT_TYPE_HEADER = 'content-type';
88
const CONTENT_LENGTH_HEADER = 'content-length';

packages/tanstack-query-builder/src/type-utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,13 @@ export type KeysOfValue<T, TCondition> = {
2222
export type HasFlag<T extends string, TFlag extends string, TTrue = true, TFalse = never> = T extends `${string}${TFlag}${string}`
2323
? TTrue
2424
: TFalse;
25+
26+
export type WhenRequired<T, TReq> = T extends undefined
27+
? Partial<TReq>
28+
: unknown extends T
29+
? Partial<TReq>
30+
: T extends null
31+
? Partial<TReq>
32+
: Partial<T> extends T
33+
? Partial<TReq>
34+
: TReq;

packages/tanstack-query-builder/tests/HttpQueryBuilder.test-d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { HttpBuilderBaseVars } from '../src/builder/types';
21
import { HttpQueryBuilder } from '../src/http/HttpQueryBuilder';
3-
import { PathParam } from '../src/http/types';
2+
import { HttpBuilderBaseVars } from '../src/http/builder-types';
3+
import { PathParam } from '../src/http/request-types';
44
import { Prettify } from '../src/type-utils';
55

66
describe('HttpQueryBuilder', () => {

0 commit comments

Comments
 (0)