@orpc/pinia-colada 2.0.0-beta.47 → 2.0.0-beta.49
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/README.md +6 -7
- package/dist/contract-utils.d.ts +37 -0
- package/dist/contract-utils.js +42 -0
- package/dist/index.d.ts +20 -501
- package/dist/index.js +14 -0
- package/dist/key.d.ts +45 -0
- package/dist/key.js +47 -0
- package/dist/live-query.d.ts +8 -0
- package/dist/live-query.js +38 -0
- package/dist/meta.d.ts +42 -0
- package/dist/meta.js +55 -0
- package/dist/plugin.d.ts +46 -0
- package/dist/plugin.js +25 -0
- package/dist/procedure-utils.d.ts +190 -0
- package/dist/procedure-utils.js +399 -0
- package/dist/router-utils.d.ts +67 -0
- package/dist/router-utils.js +69 -0
- package/dist/shared-utils.d.ts +13 -0
- package/dist/shared-utils.js +21 -0
- package/dist/ssr.d.ts +22 -0
- package/dist/ssr.js +24 -0
- package/dist/stream-query.d.ts +42 -0
- package/dist/stream-query.js +59 -0
- package/dist/types.d.ts +80 -0
- package/dist/types.js +7 -0
- package/package.json +31 -33
- package/dist/index.d.mts +0 -501
- package/dist/index.mjs +0 -622
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { throwIfAborted } from '@orpc/shared';
|
|
2
|
+
/**
|
|
3
|
+
* A Pinia Colada query function that accumulates streamed chunks into an array,
|
|
4
|
+
* publishing each chunk to the cache entry as it arrives.
|
|
5
|
+
*
|
|
6
|
+
* Inspired by [TanStack Query streamedQuery](https://tanstack.com/query/latest/docs/reference/streamedQuery) where:
|
|
7
|
+
* - Options are serializable
|
|
8
|
+
* - The output is predictable
|
|
9
|
+
*/
|
|
10
|
+
export function serializableStreamedQuery(queryFn, { refetchMode = 'reset', maxChunks = Number.POSITIVE_INFINITY, settleOnFirstData = false, } = {}) {
|
|
11
|
+
return async (context) => {
|
|
12
|
+
const { entry, signal } = context;
|
|
13
|
+
const write = (data) => {
|
|
14
|
+
entry.state.value = { status: 'success', data, error: null };
|
|
15
|
+
return data;
|
|
16
|
+
};
|
|
17
|
+
// a fetch cut short (cancelled, superseded, or its last component left) reopens when used again
|
|
18
|
+
const markStale = () => {
|
|
19
|
+
entry.when = 0;
|
|
20
|
+
};
|
|
21
|
+
if (signal.aborted) {
|
|
22
|
+
markStale();
|
|
23
|
+
}
|
|
24
|
+
signal.addEventListener('abort', markStale, { once: true });
|
|
25
|
+
const stream = await queryFn(context);
|
|
26
|
+
// close without reading, so a generator the query no longer wants never starts
|
|
27
|
+
if (signal.aborted) {
|
|
28
|
+
await stream[Symbol.asyncIterator]().return?.();
|
|
29
|
+
throw signal.reason;
|
|
30
|
+
}
|
|
31
|
+
const current = entry.state.value.data;
|
|
32
|
+
let data = write(refetchMode === 'append' && current ? limitArraySize(current, maxChunks) : []);
|
|
33
|
+
if (settleOnFirstData) {
|
|
34
|
+
await stream[Symbol.asyncIterator]().return?.();
|
|
35
|
+
return data;
|
|
36
|
+
}
|
|
37
|
+
for await (const chunk of stream) {
|
|
38
|
+
throwIfAborted(signal);
|
|
39
|
+
// read the entry, so writes made during the stream are kept
|
|
40
|
+
const current = entry.state.value.data ?? [];
|
|
41
|
+
data = write(limitArraySize([...current, chunk], maxChunks));
|
|
42
|
+
}
|
|
43
|
+
// a stream that ends on abort must not be stored as fresh data
|
|
44
|
+
throwIfAborted(signal);
|
|
45
|
+
// Colada writes the returned data again, so return the entry data to keep later writes
|
|
46
|
+
const cached = entry.state.value.data;
|
|
47
|
+
if (cached === undefined) {
|
|
48
|
+
return data;
|
|
49
|
+
}
|
|
50
|
+
// only an array is trimmed; another value written after the stream (e.g. `null`) is kept as is
|
|
51
|
+
return Array.isArray(cached) ? limitArraySize(cached, maxChunks) : cached;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function limitArraySize(items, maxSize) {
|
|
55
|
+
if (items.length <= maxSize) {
|
|
56
|
+
return items;
|
|
57
|
+
}
|
|
58
|
+
return items.slice(items.length - maxSize);
|
|
59
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { ClientContext } from '@orpc/client';
|
|
2
|
+
import type { DefineInfiniteQueryOptions, DefineInfiniteQueryOptionsTagged, DefineMutationOptionsTagged, DefineQueryOptions, DefineQueryOptionsTagged, EntryKey, UseInfiniteQueryData, UseMutationOptions, UseMutationOptionsGlobal, UseQueryOptions } from '@pinia/colada';
|
|
3
|
+
import type { SerializableStreamedQueryOptions, SettleOnFirstDataOptions } from './stream-query.ts';
|
|
4
|
+
export type InferStreamedQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U[] : never;
|
|
5
|
+
export type InferLiveQueryOutput<TOutput> = TOutput extends AsyncIterable<infer U> ? U : never;
|
|
6
|
+
export type UseQueryFnContext = Parameters<UseQueryOptions<any>['query']>[0];
|
|
7
|
+
/**
|
|
8
|
+
* The context passed to the mutation function, including the merged
|
|
9
|
+
* `UseMutationGlobalContext` values provided by a global `onMutate` hook.
|
|
10
|
+
*/
|
|
11
|
+
export type UseMutationFnContext = Parameters<NonNullable<UseMutationOptionsGlobal['onSuccess']>>[2];
|
|
12
|
+
/**
|
|
13
|
+
* The symbol under which Pinia Colada utils attach operation details
|
|
14
|
+
* (key and operation type) to the client context.
|
|
15
|
+
*
|
|
16
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#client-context | Pinia Colada Integration - Client Context}
|
|
17
|
+
*/
|
|
18
|
+
export declare const OPERATION_CONTEXT_SYMBOL: unique symbol;
|
|
19
|
+
export type OperationType = 'query' | 'streamed' | 'live' | 'infinite' | 'mutation';
|
|
20
|
+
/**
|
|
21
|
+
* A client context automatically populated by Pinia Colada utils,
|
|
22
|
+
* exposing the operation key and type of the calling operation.
|
|
23
|
+
*
|
|
24
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#client-context | Pinia Colada Integration - Client Context}
|
|
25
|
+
*/
|
|
26
|
+
export interface OperationContext {
|
|
27
|
+
[OPERATION_CONTEXT_SYMBOL]?: {
|
|
28
|
+
key: EntryKey;
|
|
29
|
+
type: OperationType;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export type QueryKeyOptions<TInput> = (undefined extends TInput ? {
|
|
33
|
+
input?: TInput;
|
|
34
|
+
} : {
|
|
35
|
+
input: TInput;
|
|
36
|
+
}) | {
|
|
37
|
+
key: EntryKey;
|
|
38
|
+
};
|
|
39
|
+
export type QueryOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TInitialData extends TOutput | undefined> = (undefined extends TInput ? {
|
|
40
|
+
input?: TInput;
|
|
41
|
+
} : {
|
|
42
|
+
input: TInput;
|
|
43
|
+
}) & (object extends TClientContext ? {
|
|
44
|
+
context?: TClientContext | undefined;
|
|
45
|
+
} : {
|
|
46
|
+
context: TClientContext;
|
|
47
|
+
}) & Omit<DefineQueryOptions<TOutput, TError, TInitialData>, 'key' | 'query'> & Partial<Pick<DefineQueryOptions<TOutput, TError, TInitialData>, 'key' | 'query'>>;
|
|
48
|
+
export type QueryOptionsOut<TOutput, TError, TInitialData extends TOutput | undefined> = DefineQueryOptionsTagged<TOutput, TError, TInitialData>;
|
|
49
|
+
export type StreamedKeyOptions<TInput> = ((undefined extends TInput ? {
|
|
50
|
+
input?: TInput;
|
|
51
|
+
} : {
|
|
52
|
+
input: TInput;
|
|
53
|
+
}) & {
|
|
54
|
+
fnOptions?: SerializableStreamedQueryOptions | undefined;
|
|
55
|
+
}) | {
|
|
56
|
+
key: EntryKey;
|
|
57
|
+
};
|
|
58
|
+
export type StreamedOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TInitialData extends TOutput | undefined> = QueryOptionsIn<TClientContext, TInput, TOutput, TError, TInitialData> & {
|
|
59
|
+
fnOptions?: SerializableStreamedQueryOptions | undefined;
|
|
60
|
+
} & SettleOnFirstDataOptions;
|
|
61
|
+
export type StreamedOptionsOut<TOutput, TError, TInitialData extends TOutput | undefined> = QueryOptionsOut<TOutput, TError, TInitialData>;
|
|
62
|
+
export type LiveOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TInitialData extends TOutput | undefined> = QueryOptionsIn<TClientContext, TInput, TOutput, TError, TInitialData> & SettleOnFirstDataOptions;
|
|
63
|
+
export type InfiniteKeyOptions<TInput, TPageParam> = Pick<InfiniteOptionsIn<any, TInput, any, any, TPageParam, any>, 'input' | 'initialPageParam'> | {
|
|
64
|
+
key: EntryKey;
|
|
65
|
+
};
|
|
66
|
+
export type InfiniteOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TPageParam, TInitialData extends UseInfiniteQueryData<TOutput, TPageParam> | undefined> = {
|
|
67
|
+
input: (pageParam: TPageParam) => TInput;
|
|
68
|
+
} & (object extends TClientContext ? {
|
|
69
|
+
context?: TClientContext | undefined;
|
|
70
|
+
} : {
|
|
71
|
+
context: TClientContext;
|
|
72
|
+
}) & Omit<DefineInfiniteQueryOptions<TOutput, TError, TPageParam, TInitialData>, 'key' | 'query'> & Partial<Pick<DefineInfiniteQueryOptions<TOutput, TError, TPageParam, TInitialData>, 'key' | 'query'>>;
|
|
73
|
+
export type InfiniteOptionsOut<TOutput, TError, TPageParam, TInitialData extends UseInfiniteQueryData<TOutput, TPageParam> | undefined> = DefineInfiniteQueryOptionsTagged<TOutput, TError, TPageParam, TInitialData>;
|
|
74
|
+
export type MutationKeyOptions<TInput> = Pick<MutationOptionsIn<any, TInput, any, any, any>, 'key'>;
|
|
75
|
+
export type MutationOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError, TMutationContext extends Record<any, any>> = (object extends TClientContext ? {
|
|
76
|
+
context?: TClientContext | undefined;
|
|
77
|
+
} : {
|
|
78
|
+
context: TClientContext;
|
|
79
|
+
}) & Omit<UseMutationOptions<TOutput, TInput, TError, TMutationContext>, 'mutation'> & Partial<Pick<UseMutationOptions<TOutput, TInput, TError, TMutationContext>, 'mutation'>>;
|
|
80
|
+
export type MutationOptionsOut<TInput, TOutput, TError, TMutationContext extends Record<any, any>> = DefineMutationOptionsTagged<TOutput, TInput, TError, TMutationContext>;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The symbol under which Pinia Colada utils attach operation details
|
|
3
|
+
* (key and operation type) to the client context.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://orpc.dev/docs/integrations/pinia-colada#client-context | Pinia Colada Integration - Client Context}
|
|
6
|
+
*/
|
|
7
|
+
export const OPERATION_CONTEXT_SYMBOL = /* @__PURE__ */ Symbol.for('ORPC_PINIA_COLADA_OPERATION_CONTEXT');
|
package/package.json
CHANGED
|
@@ -1,59 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/pinia-colada",
|
|
3
|
-
"
|
|
4
|
-
"version": "2.0.0-beta.47",
|
|
3
|
+
"version": "2.0.0-beta.49",
|
|
5
4
|
"description": "Pinia Colada integration for oRPC: typesafe queries and mutations for Vue",
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"mutation",
|
|
7
|
+
"orpc",
|
|
8
|
+
"pinia",
|
|
9
|
+
"pinia-colada",
|
|
10
|
+
"query",
|
|
11
|
+
"typesafe",
|
|
12
|
+
"typescript",
|
|
13
|
+
"vue"
|
|
10
14
|
],
|
|
11
15
|
"homepage": "https://orpc.dev",
|
|
16
|
+
"license": "MIT",
|
|
12
17
|
"repository": {
|
|
13
18
|
"type": "git",
|
|
14
19
|
"url": "git+https://github.com/middleapi/orpc.git",
|
|
15
20
|
"directory": "packages/pinia-colada"
|
|
16
21
|
},
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"pinia",
|
|
21
|
-
"pinia-colada",
|
|
22
|
-
"query",
|
|
23
|
-
"mutation",
|
|
24
|
-
"typescript",
|
|
25
|
-
"typesafe"
|
|
22
|
+
"funding": [
|
|
23
|
+
"https://github.com/sponsors/dinwwwh",
|
|
24
|
+
"https://opencollective.com/middleapi"
|
|
26
25
|
],
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"type": "module",
|
|
27
30
|
"sideEffects": false,
|
|
28
31
|
"exports": {
|
|
29
32
|
"./package.json": "./package.json",
|
|
30
33
|
".": {
|
|
31
|
-
"types": "./dist/index.d.
|
|
32
|
-
"import": "./dist/index.
|
|
33
|
-
"default": "./dist/index.
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"import": "./dist/index.js",
|
|
36
|
+
"default": "./dist/index.js"
|
|
34
37
|
}
|
|
35
38
|
},
|
|
36
|
-
"files": [
|
|
37
|
-
"dist"
|
|
38
|
-
],
|
|
39
|
-
"peerDependencies": {
|
|
40
|
-
"@pinia/colada": ">=1.2.0",
|
|
41
|
-
"vue": ">=3.5.17"
|
|
42
|
-
},
|
|
43
39
|
"dependencies": {
|
|
44
|
-
"@orpc/client": "2.0.0-beta.
|
|
45
|
-
"@orpc/contract": "2.0.0-beta.
|
|
46
|
-
"@orpc/openapi": "2.0.0-beta.
|
|
47
|
-
"@orpc/shared": "2.0.0-beta.
|
|
40
|
+
"@orpc/client": "2.0.0-beta.49",
|
|
41
|
+
"@orpc/contract": "2.0.0-beta.49",
|
|
42
|
+
"@orpc/openapi": "2.0.0-beta.49",
|
|
43
|
+
"@orpc/shared": "2.0.0-beta.49"
|
|
48
44
|
},
|
|
49
45
|
"devDependencies": {
|
|
50
|
-
"@pinia/colada": "^1.4.
|
|
46
|
+
"@pinia/colada": "^1.4.9",
|
|
51
47
|
"@vue/test-utils": "^2.5.1",
|
|
52
48
|
"pinia": "^4.1.0",
|
|
53
49
|
"vue": "^3.5.44",
|
|
54
50
|
"zod": "^4.6.7"
|
|
55
51
|
},
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"@pinia/colada": ">=1.4.9",
|
|
54
|
+
"vue": ">=3.5.17"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {}
|
|
59
57
|
}
|