mobx-tanstack-query-api 0.0.18 → 0.0.20
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 +11 -1
- package/runtime/endpoint-mutation.d.ts +16 -0
- package/runtime/endpoint-mutation.d.ts.map +1 -0
- package/runtime/endpoint-mutation.js +17 -0
- package/runtime/endpoint-query.d.ts +15 -0
- package/runtime/endpoint-query.d.ts.map +1 -0
- package/runtime/endpoint-query.js +54 -0
- package/runtime/endpoint.d.ts +28 -42
- package/runtime/endpoint.d.ts.map +1 -1
- package/runtime/endpoint.js +41 -99
- package/runtime/endpoint.types.d.ts +8 -0
- package/runtime/endpoint.types.d.ts.map +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mobx-tanstack-query-api",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.20",
|
|
4
4
|
"keywords": [],
|
|
5
5
|
"author": "js2me",
|
|
6
6
|
"license": "MIT",
|
|
@@ -80,6 +80,16 @@
|
|
|
80
80
|
"default": "./index.js",
|
|
81
81
|
"types": "./index.d.ts"
|
|
82
82
|
},
|
|
83
|
+
"./runtime/endpoint-mutation": {
|
|
84
|
+
"import": "./runtime/endpoint-mutation.js",
|
|
85
|
+
"default": "./runtime/endpoint-mutation.js",
|
|
86
|
+
"types": "./runtime/endpoint-mutation.d.ts"
|
|
87
|
+
},
|
|
88
|
+
"./runtime/endpoint-query": {
|
|
89
|
+
"import": "./runtime/endpoint-query.js",
|
|
90
|
+
"default": "./runtime/endpoint-query.js",
|
|
91
|
+
"types": "./runtime/endpoint-query.d.ts"
|
|
92
|
+
},
|
|
83
93
|
"./runtime/endpoint": {
|
|
84
94
|
"import": "./runtime/endpoint.js",
|
|
85
95
|
"default": "./runtime/endpoint.js",
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { QueryClient } from '@tanstack/query-core';
|
|
2
|
+
import { MobxMutation, MobxMutationConfig } from 'mobx-tanstack-query';
|
|
3
|
+
import { AllPropertiesOptional, AnyObject, Unpromise } from 'yummies/utils/types';
|
|
4
|
+
import { AnyEndpoint, InferEndpointError, InferEndpointInput, InferEndpointResponse } from './endpoint.types.js';
|
|
5
|
+
export type EndpointMutationInput<TBaseInput extends AnyObject, TMutationMeta extends AnyObject | void = void> = TBaseInput & (TMutationMeta extends void ? {} : AllPropertiesOptional<TMutationMeta> extends true ? {
|
|
6
|
+
meta?: TMutationMeta;
|
|
7
|
+
} : {
|
|
8
|
+
meta: TMutationMeta;
|
|
9
|
+
});
|
|
10
|
+
export interface EndpointMutationOptions<TEndpoint extends AnyEndpoint, TMutationMeta extends AnyObject | void = void> extends Omit<MobxMutationConfig<Unpromise<InferEndpointResponse<TEndpoint>>, EndpointMutationInput<InferEndpointInput<TEndpoint>, TMutationMeta>, InferEndpointError<TEndpoint>>, 'queryClient' | 'mutationFn'> {
|
|
11
|
+
}
|
|
12
|
+
export declare class EndpointMutation<TEndpoint extends AnyEndpoint, TMutationMeta extends AnyObject | void = void> extends MobxMutation<Unpromise<InferEndpointResponse<TEndpoint>>, EndpointMutationInput<InferEndpointInput<TEndpoint>, TMutationMeta>> {
|
|
13
|
+
private endpoint;
|
|
14
|
+
constructor(endpoint: TEndpoint, queryClient: QueryClient, options: EndpointMutationOptions<TEndpoint, TMutationMeta>);
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=endpoint-mutation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"endpoint-mutation.d.ts","sourceRoot":"","sources":["../../src/runtime/endpoint-mutation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACvE,OAAO,EACL,qBAAqB,EACrB,SAAS,EACT,SAAS,EACV,MAAM,qBAAqB,CAAC;AAE7B,OA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constructor(endpoint, queryClient, options) {
|
|
5
|
+
super({
|
|
6
|
+
...options,
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
8
|
+
// @ts-expect-error
|
|
9
|
+
queryClient,
|
|
10
|
+
mutationFn: async (input) => {
|
|
11
|
+
const response = (await endpoint.request(...endpoint.getParamsFromInput(input)));
|
|
12
|
+
return response;
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
this.endpoint = endpoint;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { QueryClient, QueryFunctionContext } from '@tanstack/query-core';
|
|
2
|
+
import { MobxQuery, MobxQueryConfig, MobxQueryDynamicOptions } from 'mobx-tanstack-query';
|
|
3
|
+
import { MaybeFalsy, Unpromise } from 'yummies/utils/types';
|
|
4
|
+
import { AnyEndpoint, InferEndpointError, InferEndpointInput, InferEndpointResponse } from './endpoint.types.js';
|
|
5
|
+
export interface EndpointQueryOptions<TEndpoint extends AnyEndpoint> extends Omit<MobxQueryConfig<Unpromise<InferEndpointResponse<TEndpoint>>, InferEndpointError<TEndpoint>>, 'options' | 'queryFn' | 'queryClient'> {
|
|
6
|
+
input: () => MaybeFalsy<InferEndpointInput<TEndpoint>>;
|
|
7
|
+
}
|
|
8
|
+
export declare class EndpointQuery<TEndpoint extends AnyEndpoint> extends MobxQuery<Unpromise<InferEndpointResponse<TEndpoint>>, InferEndpointError<TEndpoint>> {
|
|
9
|
+
private endpoint;
|
|
10
|
+
constructor(endpoint: TEndpoint, queryClient: QueryClient, { input: getInput, ...queryOptions }: EndpointQueryOptions<TEndpoint>);
|
|
11
|
+
setInput(input: MaybeFalsy<InferEndpointInput<TEndpoint>>): Promise<Unpromise<InferEndpointResponse<TEndpoint>>>;
|
|
12
|
+
protected buildOptionsFromInput(input: MaybeFalsy<InferEndpointInput<TEndpoint>>): MobxQueryDynamicOptions<Unpromise<InferEndpointResponse<TEndpoint>>, InferEndpointError<TEndpoint>>;
|
|
13
|
+
protected buildParamsFromContext(ctx: QueryFunctionContext<any, any>): any;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=endpoint-query.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"endpoint-query.d.ts","sourceRoot":"","sources":["../../src/runtime/endpoint-query.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAEzE,OAAO,EACL,SAAS,EACT,eAAe,EACf,uBAAuB,EACxB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAE5D,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,kBAAkB,EAClB,qBAAqB,EACtB,MAAM,qBAAqB,CAAC;AAE7B,MAAM,WAAW,oBAAoB,CAAC,SAAS,SAAS,WAAW,CACjE,SAAQ,IAAI,CACV,eAAe,CACb,SAAS,CAAC,qBAAqB,CAAC,SAAS,CAAC,CAAC,EAC3C,kBAAkB,CAAC,SAAS,CAAC,CAC9B,EACD,SAAS,GAAG,SAAS,GAAG,aAAa,CACtC;IACD,KAAK,EAAE,MAAM,UAAU,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC,CAAC;CACxD;AAED,qBAAa,aAAa,CAAC,SAAS,SAAS,WAAW,CAAE,SAAQ,SAAS,CACzE,SAAS,CAAC,qBAAqB,CAAC,SAAS,CAAC,CAAC,EAC3C,kBAAkB,CAAC,SAAS,CAAC,CAC9B;IAEG,OAAO,CAAC,QAAQ;gBAAR,QAAQ,EAAE,SAAS,EAC3B,WAAW,EAAE,WAAW,EACxB,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,YAAY,EAAE,EAAE,oBAAoB,CAAC,SAAS,CAAC;IAoCjE,QAAQ,CACZ,KAAK,EAAE,UAAU,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC,GAC/C,OAAO,CAAC,SAAS,CAAC,qBAAqB,CAAC,SAAS,CAAC,CAAC,CAAC;IAMvD,SAAS,CAAC,qBAAqB,CAC7B,KAAK,EAAE,UAAU,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC,GAC/C,uBAAuB,CACxB,SAAS,CAAC,qBAAqB,CAAC,SAAS,CAAC,CAAC,EAC3C,kBAAkB,CAAC,SAAS,CAAC,CAC9B;IAOD,SAAS,CAAC,sBAAsB,CAAC,GAAG,EAAE,oBAAoB,CAAC,GAAG,EAAE,GAAG,CAAC;CAIrE"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { when } from 'mobx';
|
|
2
|
+
import { MobxQuery, } from 'mobx-tanstack-query';
|
|
3
|
+
export class EndpointQuery extends MobxQuery {
|
|
4
|
+
endpoint;
|
|
5
|
+
constructor(endpoint, queryClient, { input: getInput, ...queryOptions }) {
|
|
6
|
+
super({
|
|
7
|
+
...queryOptions,
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
9
|
+
// @ts-expect-error
|
|
10
|
+
queryClient,
|
|
11
|
+
meta: {
|
|
12
|
+
tags: endpoint.tags,
|
|
13
|
+
operationId: endpoint.operationId,
|
|
14
|
+
...queryOptions.meta,
|
|
15
|
+
},
|
|
16
|
+
options: ({ options }) => {
|
|
17
|
+
const willEnableManually = options.enabled === false;
|
|
18
|
+
const input = getInput();
|
|
19
|
+
return this.buildOptionsFromInput(willEnableManually && input);
|
|
20
|
+
},
|
|
21
|
+
queryFn: async (ctx) => {
|
|
22
|
+
const args = this.buildParamsFromContext(ctx);
|
|
23
|
+
const requestParamsIndex = args.length - 1;
|
|
24
|
+
if (args[requestParamsIndex]) {
|
|
25
|
+
if (!args[requestParamsIndex].signal) {
|
|
26
|
+
args[requestParamsIndex].signal = ctx.signal;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
args[requestParamsIndex] = { signal: ctx.signal };
|
|
31
|
+
}
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
const response = await endpoint.request(...args);
|
|
34
|
+
return response;
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
this.endpoint = endpoint;
|
|
38
|
+
}
|
|
39
|
+
async setInput(input) {
|
|
40
|
+
this.update(this.buildOptionsFromInput(input));
|
|
41
|
+
await when(() => !this.result.isFetching);
|
|
42
|
+
return this.result.data;
|
|
43
|
+
}
|
|
44
|
+
buildOptionsFromInput(input) {
|
|
45
|
+
return {
|
|
46
|
+
enabled: !!input,
|
|
47
|
+
queryKey: input ? this.endpoint.getQueryKey(input) : '__SKIP__',
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
buildParamsFromContext(ctx) {
|
|
51
|
+
const input = this.endpoint.getQueryKey(ctx.queryKey);
|
|
52
|
+
return this.endpoint.getParamsFromInput(input);
|
|
53
|
+
}
|
|
54
|
+
}
|
package/runtime/endpoint.d.ts
CHANGED
|
@@ -1,59 +1,45 @@
|
|
|
1
|
-
import { InvalidateOptions, InvalidateQueryFilters, QueryClient
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { InvalidateOptions, InvalidateQueryFilters, QueryClient } from '@tanstack/query-core';
|
|
2
|
+
import { AnyObject } from 'yummies/utils/types';
|
|
3
|
+
import { EndpointMutation, EndpointMutationOptions } from './endpoint-mutation.js';
|
|
4
|
+
import { EndpointQuery, EndpointQueryOptions } from './endpoint-query.js';
|
|
5
5
|
import type { FullRequestParams, HttpClient, HttpResponse } from './http-client.js';
|
|
6
|
-
export interface
|
|
7
|
-
|
|
6
|
+
export interface EndpointConfiguration<TParams extends any[] = any[], TMetaData extends AnyObject = AnyObject> {
|
|
7
|
+
operationId: string;
|
|
8
|
+
meta?: TMetaData;
|
|
9
|
+
params: (...params: TParams) => FullRequestParams;
|
|
10
|
+
keys: (string | {
|
|
11
|
+
name: string;
|
|
12
|
+
param: number;
|
|
13
|
+
} | {
|
|
14
|
+
name: string;
|
|
15
|
+
rest: true;
|
|
16
|
+
})[];
|
|
17
|
+
tags: string[];
|
|
18
|
+
}
|
|
19
|
+
export interface Endpoint<TData, TError, TInput extends AnyObject, TParams extends any[] = any[], TMetaData extends AnyObject = AnyObject> {
|
|
20
|
+
(...params: TParams): ReturnType<Endpoint<TData, TError, TInput, TParams, TMetaData>['request']>;
|
|
8
21
|
}
|
|
9
22
|
export declare class Endpoint<TData, TError, TInput extends AnyObject, TParams extends any[] = any[], TMetaData extends AnyObject = AnyObject> {
|
|
10
|
-
|
|
11
|
-
operationId: string;
|
|
12
|
-
meta?: TMetaData;
|
|
13
|
-
params: (...params: TParams) => FullRequestParams;
|
|
14
|
-
keys: (string | {
|
|
15
|
-
name: string;
|
|
16
|
-
param: number;
|
|
17
|
-
} | {
|
|
18
|
-
name: string;
|
|
19
|
-
rest: true;
|
|
20
|
-
})[];
|
|
21
|
-
tags: string[];
|
|
22
|
-
};
|
|
23
|
+
configuration: EndpointConfiguration<TParams, TMetaData>;
|
|
23
24
|
protected queryClient: QueryClient;
|
|
24
25
|
protected http: HttpClient;
|
|
25
26
|
meta: TMetaData;
|
|
26
|
-
constructor(
|
|
27
|
-
|
|
28
|
-
meta?: TMetaData;
|
|
29
|
-
params: (...params: TParams) => FullRequestParams;
|
|
30
|
-
keys: (string | {
|
|
31
|
-
name: string;
|
|
32
|
-
param: number;
|
|
33
|
-
} | {
|
|
34
|
-
name: string;
|
|
35
|
-
rest: true;
|
|
36
|
-
})[];
|
|
37
|
-
tags: string[];
|
|
38
|
-
}, queryClient: QueryClient, http: HttpClient);
|
|
27
|
+
constructor(configuration: EndpointConfiguration<TParams, TMetaData>, queryClient: QueryClient, http: HttpClient);
|
|
28
|
+
private _hasRestParam?;
|
|
39
29
|
private get hasRestParam();
|
|
40
|
-
|
|
41
|
-
protected buildParamsFromInput(input: TInput): TParams;
|
|
42
|
-
protected buildQueryKeyFromInput(input: TInput): any[];
|
|
43
|
-
protected buildInputFromQueryKey(queryKey: any[]): TInput;
|
|
30
|
+
getParamsFromInput(input: TInput): TParams;
|
|
44
31
|
getFullUrl(input: TInput): string;
|
|
45
32
|
getPath(input: TInput): string;
|
|
46
|
-
|
|
33
|
+
get tags(): string[];
|
|
34
|
+
get operationId(): string;
|
|
47
35
|
request(...params: TParams): Promise<HttpResponse<TData, TError>>;
|
|
48
|
-
|
|
36
|
+
getQueryKey(input: TInput): any[];
|
|
49
37
|
invalidateByOperationId(filters?: Omit<InvalidateQueryFilters<HttpResponse<TData, TError>>, 'queryKey' | 'predicate'>, options?: InvalidateOptions): Promise<void>;
|
|
50
38
|
invalidateByTags(filters?: Omit<InvalidateQueryFilters<HttpResponse<TData, TError>>, 'queryKey' | 'predicate'>, options?: InvalidateOptions): Promise<void>;
|
|
51
39
|
invalidate(input: TInput, filters?: Omit<InvalidateQueryFilters<HttpResponse<TData, TError>>, 'queryKey' | 'predicate'> & {
|
|
52
40
|
queryKeyIndex?: number;
|
|
53
41
|
}, options?: InvalidateOptions): Promise<void>;
|
|
54
|
-
toMutation<TMutationMeta extends AnyObject | void = void>(options:
|
|
55
|
-
toQuery(
|
|
56
|
-
input: () => MaybeFalsy<TInput>;
|
|
57
|
-
}): MobxQuery<HttpResponse<TData, TError>, TError, any>;
|
|
42
|
+
toMutation<TMutationMeta extends AnyObject | void = void>(options: EndpointMutationOptions<typeof this, TMutationMeta>): EndpointMutation<this, TMutationMeta>;
|
|
43
|
+
toQuery(options: EndpointQueryOptions<typeof this>): EndpointQuery<this>;
|
|
58
44
|
}
|
|
59
45
|
//# sourceMappingURL=endpoint.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint.d.ts","sourceRoot":"","sources":["../../src/runtime/endpoint.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,sBAAsB,EACtB,WAAW,
|
|
1
|
+
{"version":3,"file":"endpoint.d.ts","sourceRoot":"","sources":["../../src/runtime/endpoint.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,i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,OAAO,CAAC,EAAE,iBAAiB;IAY7B,UAAU,CAAC,aAAa,SAAS,SAAS,GAAG,IAAI,GAAG,IAAI,EACtD,OAAO,EAAE,uBAAuB,CAAC,OAAO,IAAI,EAAE,aAAa,CAAC;IAK9D,OAAO,CAAC,OAAO,EAAE,oBAAoB,CAAC,OAAO,IAAI,CAAC;CAGnD"}
|
package/runtime/endpoint.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EndpointMutation, } from './endpoint-mutation.js';
|
|
2
|
+
import { EndpointQuery } from './endpoint-query.js';
|
|
2
3
|
export class Endpoint {
|
|
3
|
-
|
|
4
|
+
configuration;
|
|
4
5
|
queryClient;
|
|
5
6
|
http;
|
|
6
7
|
meta;
|
|
7
|
-
constructor(
|
|
8
|
-
this.
|
|
8
|
+
constructor(configuration, queryClient, http) {
|
|
9
|
+
this.configuration = configuration;
|
|
9
10
|
this.queryClient = queryClient;
|
|
10
11
|
this.http = http;
|
|
11
|
-
this.meta =
|
|
12
|
+
this.meta = configuration.meta ?? {};
|
|
12
13
|
// Сохраняем оригинальный инстанс
|
|
13
14
|
const instance = this;
|
|
14
15
|
// Создаем функцию-обертку
|
|
@@ -30,24 +31,24 @@ export class Endpoint {
|
|
|
30
31
|
});
|
|
31
32
|
return callable;
|
|
32
33
|
}
|
|
34
|
+
_hasRestParam;
|
|
33
35
|
get hasRestParam() {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return this.buildParamsFromInput(input);
|
|
36
|
+
if (this._hasRestParam === undefined) {
|
|
37
|
+
this._hasRestParam = this.configuration.keys.some((key) => {
|
|
38
|
+
if (typeof key !== 'string' && 'rest' in key) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return this._hasRestParam;
|
|
43
44
|
}
|
|
44
|
-
|
|
45
|
+
getParamsFromInput(input) {
|
|
45
46
|
const args = [];
|
|
46
47
|
const restParams = {};
|
|
47
48
|
if (this.hasRestParam) {
|
|
48
49
|
args[0] = restParams;
|
|
49
50
|
}
|
|
50
|
-
this.
|
|
51
|
+
this.configuration.keys.forEach((key) => {
|
|
51
52
|
if (typeof key === 'object') {
|
|
52
53
|
if (key.name === 'request') {
|
|
53
54
|
args.push(input[key.name]);
|
|
@@ -64,9 +65,25 @@ export class Endpoint {
|
|
|
64
65
|
});
|
|
65
66
|
return args;
|
|
66
67
|
}
|
|
67
|
-
|
|
68
|
+
getFullUrl(input) {
|
|
69
|
+
const params = this.configuration.params(...this.getParamsFromInput(input));
|
|
70
|
+
return this.http.buildUrl(params);
|
|
71
|
+
}
|
|
72
|
+
getPath(input) {
|
|
73
|
+
return this.configuration.params(...this.getParamsFromInput(input)).path;
|
|
74
|
+
}
|
|
75
|
+
get tags() {
|
|
76
|
+
return this.configuration.tags;
|
|
77
|
+
}
|
|
78
|
+
get operationId() {
|
|
79
|
+
return this.configuration.operationId;
|
|
80
|
+
}
|
|
81
|
+
request(...params) {
|
|
82
|
+
return this.http.request(this.configuration.params(...params));
|
|
83
|
+
}
|
|
84
|
+
getQueryKey(input) {
|
|
68
85
|
const restParams = this.hasRestParam ? { ...input } : {};
|
|
69
|
-
return this.
|
|
86
|
+
return this.configuration.keys.map((key) => {
|
|
70
87
|
if (typeof key === 'string') {
|
|
71
88
|
return key;
|
|
72
89
|
}
|
|
@@ -85,61 +102,23 @@ export class Endpoint {
|
|
|
85
102
|
}
|
|
86
103
|
});
|
|
87
104
|
}
|
|
88
|
-
buildInputFromQueryKey(queryKey) {
|
|
89
|
-
const input = {};
|
|
90
|
-
this.cfg.keys.forEach((key, index) => {
|
|
91
|
-
const keyPart = queryKey[index];
|
|
92
|
-
if (typeof key === 'string') {
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
if (key.name === 'request') {
|
|
96
|
-
input[key.name] = keyPart;
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
if ('rest' in key) {
|
|
100
|
-
Object.assign(input, keyPart);
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
if ('param' in key) {
|
|
104
|
-
input[key.name] = keyPart;
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
return input;
|
|
108
|
-
}
|
|
109
|
-
getFullUrl(input) {
|
|
110
|
-
const params = this.cfg.params(...this.buildParamsFromInput(input));
|
|
111
|
-
return this.http.buildUrl(params);
|
|
112
|
-
}
|
|
113
|
-
getPath(input) {
|
|
114
|
-
return this.cfg.params(...this.buildParamsFromInput(input)).path;
|
|
115
|
-
}
|
|
116
|
-
getTags() {
|
|
117
|
-
return this.cfg.tags;
|
|
118
|
-
}
|
|
119
|
-
request(...params) {
|
|
120
|
-
return this.http.request(this.cfg.params(...params));
|
|
121
|
-
}
|
|
122
|
-
toQueryKey(input) {
|
|
123
|
-
return this.buildQueryKeyFromInput(input);
|
|
124
|
-
}
|
|
125
105
|
invalidateByOperationId(filters, options) {
|
|
126
106
|
return this.queryClient.invalidateQueries({
|
|
127
107
|
...filters,
|
|
128
108
|
predicate: (query) => {
|
|
129
109
|
if (query.meta?.operationId) {
|
|
130
|
-
return query.meta?.operationId === this.
|
|
110
|
+
return query.meta?.operationId === this.configuration.operationId;
|
|
131
111
|
}
|
|
132
112
|
return false;
|
|
133
113
|
},
|
|
134
114
|
}, options);
|
|
135
115
|
}
|
|
136
116
|
invalidateByTags(filters, options) {
|
|
137
|
-
const tags = this.getTags();
|
|
138
117
|
return this.queryClient.invalidateQueries({
|
|
139
118
|
...filters,
|
|
140
119
|
predicate: (query) => {
|
|
141
120
|
if (Array.isArray(query.meta?.tags)) {
|
|
142
|
-
return query.meta.tags.some((tag) => tags.includes(tag));
|
|
121
|
+
return query.meta.tags.some((tag) => this.tags.includes(tag));
|
|
143
122
|
}
|
|
144
123
|
return false;
|
|
145
124
|
},
|
|
@@ -148,51 +127,14 @@ export class Endpoint {
|
|
|
148
127
|
invalidate(input, filters, options) {
|
|
149
128
|
return this.queryClient.invalidateQueries({
|
|
150
129
|
...filters,
|
|
151
|
-
queryKey: this.
|
|
130
|
+
queryKey: this.getQueryKey(input).slice(0, filters?.queryKeyIndex),
|
|
152
131
|
exact: filters?.exact ?? false,
|
|
153
132
|
}, options);
|
|
154
133
|
}
|
|
155
134
|
toMutation(options) {
|
|
156
|
-
return new
|
|
157
|
-
...options,
|
|
158
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
159
|
-
// @ts-expect-error
|
|
160
|
-
queryClient: this.queryClient,
|
|
161
|
-
mutationFn: (input) => this.request(...this.buildParamsFromInput(input)),
|
|
162
|
-
});
|
|
135
|
+
return new EndpointMutation(this, this.queryClient, options);
|
|
163
136
|
}
|
|
164
|
-
toQuery(
|
|
165
|
-
return new
|
|
166
|
-
...options,
|
|
167
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
168
|
-
// @ts-expect-error
|
|
169
|
-
queryClient: this.queryClient,
|
|
170
|
-
options: () => {
|
|
171
|
-
const input = getInput();
|
|
172
|
-
return {
|
|
173
|
-
meta: {
|
|
174
|
-
tags: this.getTags(),
|
|
175
|
-
operationId: this.cfg.operationId,
|
|
176
|
-
...options.meta,
|
|
177
|
-
},
|
|
178
|
-
enabled: !!input,
|
|
179
|
-
queryKey: input ? this.toQueryKey(input) : '__SKIP__',
|
|
180
|
-
};
|
|
181
|
-
},
|
|
182
|
-
queryFn: async (ctx) => {
|
|
183
|
-
const args = this.buildParamsFromContext(ctx);
|
|
184
|
-
const requestParamsIndex = args.length - 1;
|
|
185
|
-
if (args[requestParamsIndex]) {
|
|
186
|
-
if (!args[requestParamsIndex].signal) {
|
|
187
|
-
args[requestParamsIndex].signal = ctx.signal;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
else {
|
|
191
|
-
args[requestParamsIndex] = { signal: ctx.signal };
|
|
192
|
-
}
|
|
193
|
-
const response = await this.request(...args);
|
|
194
|
-
return response;
|
|
195
|
-
},
|
|
196
|
-
});
|
|
137
|
+
toQuery(options) {
|
|
138
|
+
return new EndpointQuery(this, this.queryClient, options);
|
|
197
139
|
}
|
|
198
140
|
}
|
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import { AllPropertiesOptional, AnyObject } from 'yummies/utils/types';
|
|
2
|
+
import type { Endpoint } from './endpoint.js';
|
|
2
3
|
export type EndpointMutationInput<TBaseInput extends AnyObject, TMutationMeta extends AnyObject | void = void> = TBaseInput & (TMutationMeta extends void ? {} : AllPropertiesOptional<TMutationMeta> extends true ? {
|
|
3
4
|
meta?: TMutationMeta;
|
|
4
5
|
} : {
|
|
5
6
|
meta: TMutationMeta;
|
|
6
7
|
});
|
|
8
|
+
export type AnyEndpoint = Endpoint<any, any, any, any, any>;
|
|
9
|
+
export type InferEndpointData<TEndpoint extends AnyEndpoint> = TEndpoint extends Endpoint<infer T, any, any, any, any> ? T : never;
|
|
10
|
+
export type InferEndpointResponse<TEndpoint extends AnyEndpoint> = ReturnType<TEndpoint['request']>;
|
|
11
|
+
export type InferEndpointError<TEndpoint extends AnyEndpoint> = TEndpoint extends Endpoint<any, infer T, any, any, any> ? T : never;
|
|
12
|
+
export type InferEndpointInput<TEndpoint extends AnyEndpoint> = TEndpoint extends Endpoint<any, any, infer T, any, any> ? T : never;
|
|
13
|
+
export type InferEndpointParams<TEndpoint extends AnyEndpoint> = TEndpoint extends Endpoint<any, any, any, infer T, any> ? T : never;
|
|
14
|
+
export type InferEndpointMetaData<TEndpoint extends AnyEndpoint> = TEndpoint extends Endpoint<any, any, any, any, infer T> ? T : never;
|
|
7
15
|
//# sourceMappingURL=endpoint.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint.types.d.ts","sourceRoot":"","sources":["../../src/runtime/endpoint.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,SAAS,EAAE,MAAM,qBAAqB,CA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