@trpc/next 10.25.0 → 10.26.0
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/app-dir/client/index.d.ts +1 -0
- package/app-dir/client/index.js +1 -0
- package/app-dir/server/index.d.ts +1 -0
- package/app-dir/server/index.js +1 -0
- package/dist/app-dir/client.d.ts +9 -0
- package/dist/app-dir/client.d.ts.map +1 -0
- package/dist/app-dir/client.js +75 -0
- package/dist/app-dir/client.mjs +71 -0
- package/dist/app-dir/server.d.ts +5 -0
- package/dist/app-dir/server.d.ts.map +1 -0
- package/dist/app-dir/server.js +28 -0
- package/dist/app-dir/server.mjs +24 -0
- package/dist/app-dir/shared.d.ts +27 -0
- package/dist/app-dir/shared.d.ts.map +1 -0
- package/package.json +26 -15
- package/src/app-dir/client.ts +93 -0
- package/src/app-dir/server.ts +34 -0
- package/src/app-dir/shared.ts +61 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../dist/app-dir/client';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../../dist/app-dir/client');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../dist/app-dir/server';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../../dist/app-dir/server');
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AnyRouter } from '@trpc/server';
|
|
2
|
+
import { CreateTRPCNextAppRouterOptions, UseProcedureRecord } from './shared';
|
|
3
|
+
export declare function experimental_createTRPCNextAppDirClient<TRouter extends AnyRouter>(opts: CreateTRPCNextAppRouterOptions<TRouter>): import("@trpc/server").ProtectedIntersection<{
|
|
4
|
+
use: {
|
|
5
|
+
<TData extends Promise<unknown>[]>(cb: (t: UseProcedureRecord<TRouter>) => [...TData]): { [TKey in keyof TData]: import("@trpc/server").ThenArg<TData[TKey]>; };
|
|
6
|
+
<TData_1 extends Promise<unknown>>(cb: (t: UseProcedureRecord<TRouter>) => TData_1): import("@trpc/server").ThenArg<TData_1>;
|
|
7
|
+
};
|
|
8
|
+
}, UseProcedureRecord<TRouter>>;
|
|
9
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/app-dir/client.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAEL,8BAA8B,EAC9B,kBAAkB,EAEnB,MAAM,UAAU,CAAC;AAkBlB,wBAAgB,uCAAuC,CACrD,OAAO,SAAS,SAAS,EACzB,IAAI,EAAE,8BAA8B,CAAC,OAAO,CAAC;;;;;gCA6D9C"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var client = require('@trpc/client');
|
|
6
|
+
var shared = require('@trpc/server/shared');
|
|
7
|
+
|
|
8
|
+
function createUseProxy(client) {
|
|
9
|
+
return shared.createRecursiveProxy((opts)=>{
|
|
10
|
+
const path = opts.path.join('.');
|
|
11
|
+
return client.query(path, ...opts.args);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function normalizePromiseArray(promise) {
|
|
16
|
+
if (Array.isArray(promise)) {
|
|
17
|
+
return Promise.all(promise);
|
|
18
|
+
}
|
|
19
|
+
return promise;
|
|
20
|
+
}
|
|
21
|
+
// ts-prune-ignore-next
|
|
22
|
+
function experimental_createTRPCNextAppDirClient(opts) {
|
|
23
|
+
const client$1 = client.createTRPCUntypedClient(opts.config());
|
|
24
|
+
const useProxy = createUseProxy(client$1);
|
|
25
|
+
const cache = new Map();
|
|
26
|
+
return shared.createFlatProxy((key)=>{
|
|
27
|
+
if (key === 'use') {
|
|
28
|
+
return (cb)=>{
|
|
29
|
+
const promise = normalizePromiseArray(cb(useProxy));
|
|
30
|
+
throw promise;
|
|
31
|
+
// const [data, setData] = useState<unknown | unknown[]>();
|
|
32
|
+
// useEffect(() => {
|
|
33
|
+
// const promise = normalizePromiseArray(cb(useProxy));
|
|
34
|
+
// void promise.then(setData).catch((err) => {
|
|
35
|
+
// throw err;
|
|
36
|
+
// });
|
|
37
|
+
// // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
38
|
+
// }, []);
|
|
39
|
+
// return data;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
return shared.createRecursiveProxy(({ path , args })=>{
|
|
43
|
+
const pathCopy = [
|
|
44
|
+
key,
|
|
45
|
+
...path
|
|
46
|
+
];
|
|
47
|
+
const procedureType = client.clientCallTypeToProcedureType(pathCopy.pop());
|
|
48
|
+
if (procedureType === 'query') {
|
|
49
|
+
const queryCacheKey = JSON.stringify([
|
|
50
|
+
path,
|
|
51
|
+
args[0]
|
|
52
|
+
]);
|
|
53
|
+
const cached = cache.get(queryCacheKey);
|
|
54
|
+
if (cached?.promise) {
|
|
55
|
+
return cached.promise;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const fullPath = pathCopy.join('.');
|
|
59
|
+
const promise = client$1[procedureType](fullPath, ...args);
|
|
60
|
+
if (procedureType !== 'query') {
|
|
61
|
+
return promise;
|
|
62
|
+
}
|
|
63
|
+
const queryCacheKey1 = JSON.stringify([
|
|
64
|
+
path,
|
|
65
|
+
args[0]
|
|
66
|
+
]);
|
|
67
|
+
cache.set(queryCacheKey1, {
|
|
68
|
+
promise
|
|
69
|
+
});
|
|
70
|
+
return promise;
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
exports.experimental_createTRPCNextAppDirClient = experimental_createTRPCNextAppDirClient;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { createTRPCUntypedClient, clientCallTypeToProcedureType } from '@trpc/client';
|
|
2
|
+
import { createRecursiveProxy, createFlatProxy } from '@trpc/server/shared';
|
|
3
|
+
|
|
4
|
+
function createUseProxy(client) {
|
|
5
|
+
return createRecursiveProxy((opts)=>{
|
|
6
|
+
const path = opts.path.join('.');
|
|
7
|
+
return client.query(path, ...opts.args);
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function normalizePromiseArray(promise) {
|
|
12
|
+
if (Array.isArray(promise)) {
|
|
13
|
+
return Promise.all(promise);
|
|
14
|
+
}
|
|
15
|
+
return promise;
|
|
16
|
+
}
|
|
17
|
+
// ts-prune-ignore-next
|
|
18
|
+
function experimental_createTRPCNextAppDirClient(opts) {
|
|
19
|
+
const client = createTRPCUntypedClient(opts.config());
|
|
20
|
+
const useProxy = createUseProxy(client);
|
|
21
|
+
const cache = new Map();
|
|
22
|
+
return createFlatProxy((key)=>{
|
|
23
|
+
if (key === 'use') {
|
|
24
|
+
return (cb)=>{
|
|
25
|
+
const promise = normalizePromiseArray(cb(useProxy));
|
|
26
|
+
throw promise;
|
|
27
|
+
// const [data, setData] = useState<unknown | unknown[]>();
|
|
28
|
+
// useEffect(() => {
|
|
29
|
+
// const promise = normalizePromiseArray(cb(useProxy));
|
|
30
|
+
// void promise.then(setData).catch((err) => {
|
|
31
|
+
// throw err;
|
|
32
|
+
// });
|
|
33
|
+
// // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
34
|
+
// }, []);
|
|
35
|
+
// return data;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return createRecursiveProxy(({ path , args })=>{
|
|
39
|
+
const pathCopy = [
|
|
40
|
+
key,
|
|
41
|
+
...path
|
|
42
|
+
];
|
|
43
|
+
const procedureType = clientCallTypeToProcedureType(pathCopy.pop());
|
|
44
|
+
if (procedureType === 'query') {
|
|
45
|
+
const queryCacheKey = JSON.stringify([
|
|
46
|
+
path,
|
|
47
|
+
args[0]
|
|
48
|
+
]);
|
|
49
|
+
const cached = cache.get(queryCacheKey);
|
|
50
|
+
if (cached?.promise) {
|
|
51
|
+
return cached.promise;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const fullPath = pathCopy.join('.');
|
|
55
|
+
const promise = client[procedureType](fullPath, ...args);
|
|
56
|
+
if (procedureType !== 'query') {
|
|
57
|
+
return promise;
|
|
58
|
+
}
|
|
59
|
+
const queryCacheKey1 = JSON.stringify([
|
|
60
|
+
path,
|
|
61
|
+
args[0]
|
|
62
|
+
]);
|
|
63
|
+
cache.set(queryCacheKey1, {
|
|
64
|
+
promise
|
|
65
|
+
});
|
|
66
|
+
return promise;
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export { experimental_createTRPCNextAppDirClient };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { CreateTRPCProxyClient } from '@trpc/client';
|
|
2
|
+
import { AnyRouter } from '@trpc/server';
|
|
3
|
+
import { CreateTRPCNextAppRouterOptions } from './shared';
|
|
4
|
+
export declare function experimental_createTRPCNextAppDirServer<TRouter extends AnyRouter>(opts: CreateTRPCNextAppRouterOptions<TRouter>): CreateTRPCProxyClient<TRouter>;
|
|
5
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/app-dir/server.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,qBAAqB,EAGtB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAGzC,OAAO,EAAE,8BAA8B,EAAE,MAAM,UAAU,CAAC;AAG1D,wBAAgB,uCAAuC,CACrD,OAAO,SAAS,SAAS,EACzB,IAAI,EAAE,8BAA8B,CAAC,OAAO,CAAC,kCAkB9C"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var client = require('@trpc/client');
|
|
6
|
+
var shared = require('@trpc/server/shared');
|
|
7
|
+
var React = require('react');
|
|
8
|
+
|
|
9
|
+
/// <reference types="next" />
|
|
10
|
+
// ts-prune-ignore-next
|
|
11
|
+
function experimental_createTRPCNextAppDirServer(opts) {
|
|
12
|
+
const getClient = React.cache(()=>{
|
|
13
|
+
const config = opts.config();
|
|
14
|
+
return client.createTRPCUntypedClient(config);
|
|
15
|
+
});
|
|
16
|
+
return shared.createRecursiveProxy((callOpts)=>{
|
|
17
|
+
// lazily initialize client
|
|
18
|
+
const client$1 = getClient();
|
|
19
|
+
const pathCopy = [
|
|
20
|
+
...callOpts.path
|
|
21
|
+
];
|
|
22
|
+
const procedureType = client.clientCallTypeToProcedureType(pathCopy.pop());
|
|
23
|
+
const fullPath = pathCopy.join('.');
|
|
24
|
+
return client$1[procedureType](fullPath, ...callOpts.args);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
exports.experimental_createTRPCNextAppDirServer = experimental_createTRPCNextAppDirServer;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createTRPCUntypedClient, clientCallTypeToProcedureType } from '@trpc/client';
|
|
2
|
+
import { createRecursiveProxy } from '@trpc/server/shared';
|
|
3
|
+
import { cache } from 'react';
|
|
4
|
+
|
|
5
|
+
/// <reference types="next" />
|
|
6
|
+
// ts-prune-ignore-next
|
|
7
|
+
function experimental_createTRPCNextAppDirServer(opts) {
|
|
8
|
+
const getClient = cache(()=>{
|
|
9
|
+
const config = opts.config();
|
|
10
|
+
return createTRPCUntypedClient(config);
|
|
11
|
+
});
|
|
12
|
+
return createRecursiveProxy((callOpts)=>{
|
|
13
|
+
// lazily initialize client
|
|
14
|
+
const client = getClient();
|
|
15
|
+
const pathCopy = [
|
|
16
|
+
...callOpts.path
|
|
17
|
+
];
|
|
18
|
+
const procedureType = clientCallTypeToProcedureType(pathCopy.pop());
|
|
19
|
+
const fullPath = pathCopy.join('.');
|
|
20
|
+
return client[procedureType](fullPath, ...callOpts.args);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { experimental_createTRPCNextAppDirServer };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { CreateTRPCClientOptions, Resolver, TRPCUntypedClient } from '@trpc/client';
|
|
2
|
+
import { AnyQueryProcedure, AnyRouter, Filter, ProtectedIntersection, ThenArg } from '@trpc/server';
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare type UseProcedureRecord<TRouter extends AnyRouter> = {
|
|
7
|
+
[TKey in keyof Filter<TRouter['_def']['record'], AnyRouter | AnyQueryProcedure>]: TRouter['_def']['record'][TKey] extends AnyRouter ? UseProcedureRecord<TRouter['_def']['record'][TKey]> : Resolver<TRouter['_def']['record'][TKey]>;
|
|
8
|
+
};
|
|
9
|
+
export declare function createUseProxy<TRouter extends AnyRouter>(client: TRPCUntypedClient<TRouter>): UseProcedureRecord<TRouter>;
|
|
10
|
+
declare type NextAppRouterUse<TRouter extends AnyRouter> = {
|
|
11
|
+
<TData extends Promise<unknown>[]>(cb: (t: UseProcedureRecord<TRouter>) => [...TData]): {
|
|
12
|
+
[TKey in keyof TData]: ThenArg<TData[TKey]>;
|
|
13
|
+
};
|
|
14
|
+
<TData extends Promise<unknown>>(cb: (t: UseProcedureRecord<TRouter>) => TData): ThenArg<TData>;
|
|
15
|
+
};
|
|
16
|
+
declare type CreateTRPCNextAppRouterBase<TRouter extends AnyRouter> = {
|
|
17
|
+
use: NextAppRouterUse<TRouter>;
|
|
18
|
+
};
|
|
19
|
+
export declare type CreateTRPCNextAppRouter<TRouter extends AnyRouter> = ProtectedIntersection<CreateTRPCNextAppRouterBase<TRouter>, UseProcedureRecord<TRouter>>;
|
|
20
|
+
/**
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export interface CreateTRPCNextAppRouterOptions<TRouter extends AnyRouter> {
|
|
24
|
+
config: () => CreateTRPCClientOptions<TRouter>;
|
|
25
|
+
}
|
|
26
|
+
export {};
|
|
27
|
+
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../src/app-dir/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,QAAQ,EACR,iBAAiB,EAClB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,iBAAiB,EACjB,SAAS,EACT,MAAM,EACN,qBAAqB,EACrB,OAAO,EACR,MAAM,cAAc,CAAC;AAGtB;;GAEG;AACH,oBAAY,kBAAkB,CAAC,OAAO,SAAS,SAAS,IAAI;KACzD,IAAI,IAAI,MAAM,MAAM,CACnB,OAAO,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,EACzB,SAAS,GAAG,iBAAiB,CAC9B,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,SAAS,SAAS,GACjD,kBAAkB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,GACnD,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC;CAC9C,CAAC;AAEF,wBAAgB,cAAc,CAAC,OAAO,SAAS,SAAS,EACtD,MAAM,EAAE,iBAAiB,CAAC,OAAO,CAAC,+BAOnC;AAED,aAAK,gBAAgB,CAAC,OAAO,SAAS,SAAS,IAAI;IACjD,CAAC,KAAK,SAAS,OAAO,CAAC,OAAO,CAAC,EAAE,EAC/B,EAAE,EAAE,CAAC,CAAC,EAAE,kBAAkB,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,GACjD;SACA,IAAI,IAAI,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;KAC5C,CAAC;IACF,CAAC,KAAK,SAAS,OAAO,CAAC,OAAO,CAAC,EAC7B,EAAE,EAAE,CAAC,CAAC,EAAE,kBAAkB,CAAC,OAAO,CAAC,KAAK,KAAK,GAC5C,OAAO,CAAC,KAAK,CAAC,CAAC;CACnB,CAAC;AACF,aAAK,2BAA2B,CAAC,OAAO,SAAS,SAAS,IAAI;IAC5D,GAAG,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC;CAChC,CAAC;AACF,oBAAY,uBAAuB,CAAC,OAAO,SAAS,SAAS,IAC3D,qBAAqB,CACnB,2BAA2B,CAAC,OAAO,CAAC,EACpC,kBAAkB,CAAC,OAAO,CAAC,CAC5B,CAAC;AAEJ;;GAEG;AACH,MAAM,WAAW,8BAA8B,CAAC,OAAO,SAAS,SAAS;IACvE,MAAM,EAAE,MAAM,uBAAuB,CAAC,OAAO,CAAC,CAAC;CAChD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trpc/next",
|
|
3
|
-
"version": "10.
|
|
3
|
+
"version": "10.26.0",
|
|
4
4
|
"description": "The tRPC Next.js library",
|
|
5
5
|
"author": "KATT",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,19 +34,30 @@
|
|
|
34
34
|
"import": "./dist/index.mjs",
|
|
35
35
|
"require": "./dist/index.js",
|
|
36
36
|
"default": "./dist/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./app-dir/server": {
|
|
39
|
+
"import": "./dist/app-dir/server.mjs",
|
|
40
|
+
"require": "./dist/app-dir/server.js",
|
|
41
|
+
"default": "./dist/app-dir/server.js"
|
|
42
|
+
},
|
|
43
|
+
"./app-dir/client": {
|
|
44
|
+
"import": "./dist/app-dir/client.mjs",
|
|
45
|
+
"require": "./dist/app-dir/client.js",
|
|
46
|
+
"default": "./dist/app-dir/client.js"
|
|
37
47
|
}
|
|
38
48
|
},
|
|
39
49
|
"files": [
|
|
40
50
|
"dist",
|
|
41
51
|
"src",
|
|
42
52
|
"README.md",
|
|
43
|
-
"package.json"
|
|
53
|
+
"package.json",
|
|
54
|
+
"app-dir"
|
|
44
55
|
],
|
|
45
56
|
"peerDependencies": {
|
|
46
57
|
"@tanstack/react-query": "^4.18.0",
|
|
47
|
-
"@trpc/client": "10.
|
|
48
|
-
"@trpc/react-query": "10.
|
|
49
|
-
"@trpc/server": "10.
|
|
58
|
+
"@trpc/client": "10.26.0",
|
|
59
|
+
"@trpc/react-query": "10.26.0",
|
|
60
|
+
"@trpc/server": "10.26.0",
|
|
50
61
|
"next": "*",
|
|
51
62
|
"react": ">=16.8.0",
|
|
52
63
|
"react-dom": ">=16.8.0"
|
|
@@ -56,24 +67,24 @@
|
|
|
56
67
|
},
|
|
57
68
|
"devDependencies": {
|
|
58
69
|
"@tanstack/react-query": "^4.18.0",
|
|
59
|
-
"@trpc/client": "10.
|
|
60
|
-
"@trpc/react-query": "10.
|
|
61
|
-
"@trpc/server": "10.
|
|
62
|
-
"@types/express": "^4.17.
|
|
70
|
+
"@trpc/client": "10.26.0",
|
|
71
|
+
"@trpc/react-query": "10.26.0",
|
|
72
|
+
"@trpc/server": "10.26.0",
|
|
73
|
+
"@types/express": "^4.17.17",
|
|
63
74
|
"@types/node": "^18.7.20",
|
|
64
|
-
"@types/react": "^18.
|
|
65
|
-
"@types/react-dom": "^18.
|
|
66
|
-
"eslint": "^8.
|
|
75
|
+
"@types/react": "^18.2.6",
|
|
76
|
+
"@types/react-dom": "^18.2.4",
|
|
77
|
+
"eslint": "^8.40.0",
|
|
67
78
|
"express": "^4.17.1",
|
|
68
|
-
"next": "^13.
|
|
79
|
+
"next": "^13.3.4",
|
|
69
80
|
"react": "^18.2.0",
|
|
70
81
|
"react-dom": "^18.2.0",
|
|
71
82
|
"rollup": "^2.79.1",
|
|
72
|
-
"tsx": "^3.12.
|
|
83
|
+
"tsx": "^3.12.7",
|
|
73
84
|
"zod": "^3.0.0"
|
|
74
85
|
},
|
|
75
86
|
"publishConfig": {
|
|
76
87
|
"access": "public"
|
|
77
88
|
},
|
|
78
|
-
"gitHead": "
|
|
89
|
+
"gitHead": "877c5b499676e21e7ebdf79eacde300ffc03325d"
|
|
79
90
|
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
clientCallTypeToProcedureType,
|
|
3
|
+
createTRPCUntypedClient,
|
|
4
|
+
} from '@trpc/client';
|
|
5
|
+
import { AnyRouter } from '@trpc/server';
|
|
6
|
+
import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared';
|
|
7
|
+
import {
|
|
8
|
+
CreateTRPCNextAppRouter,
|
|
9
|
+
CreateTRPCNextAppRouterOptions,
|
|
10
|
+
UseProcedureRecord,
|
|
11
|
+
createUseProxy,
|
|
12
|
+
} from './shared';
|
|
13
|
+
|
|
14
|
+
function normalizePromiseArray<TValue>(
|
|
15
|
+
promise: Promise<TValue> | Promise<TValue>[],
|
|
16
|
+
) {
|
|
17
|
+
if (Array.isArray(promise)) {
|
|
18
|
+
return Promise.all(promise);
|
|
19
|
+
}
|
|
20
|
+
return promise;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type QueryResult = {
|
|
24
|
+
data?: unknown;
|
|
25
|
+
error?: unknown;
|
|
26
|
+
promise?: Promise<unknown>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// ts-prune-ignore-next
|
|
30
|
+
export function experimental_createTRPCNextAppDirClient<
|
|
31
|
+
TRouter extends AnyRouter,
|
|
32
|
+
>(opts: CreateTRPCNextAppRouterOptions<TRouter>) {
|
|
33
|
+
const client = createTRPCUntypedClient<TRouter>(opts.config());
|
|
34
|
+
const useProxy = createUseProxy<TRouter>(client);
|
|
35
|
+
|
|
36
|
+
const cache = new Map<string, QueryResult>();
|
|
37
|
+
return createFlatProxy<CreateTRPCNextAppRouter<TRouter>>((key) => {
|
|
38
|
+
if (key === 'use') {
|
|
39
|
+
return (
|
|
40
|
+
cb: (
|
|
41
|
+
t: UseProcedureRecord<TRouter>,
|
|
42
|
+
) => Promise<unknown> | Promise<unknown>[],
|
|
43
|
+
) => {
|
|
44
|
+
const promise = normalizePromiseArray(cb(useProxy));
|
|
45
|
+
throw promise;
|
|
46
|
+
// const [data, setData] = useState<unknown | unknown[]>();
|
|
47
|
+
|
|
48
|
+
// useEffect(() => {
|
|
49
|
+
// const promise = normalizePromiseArray(cb(useProxy));
|
|
50
|
+
|
|
51
|
+
// void promise.then(setData).catch((err) => {
|
|
52
|
+
// throw err;
|
|
53
|
+
// });
|
|
54
|
+
// // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
55
|
+
// }, []);
|
|
56
|
+
|
|
57
|
+
// return data;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return createRecursiveProxy(({ path, args }) => {
|
|
62
|
+
const pathCopy = [key, ...path];
|
|
63
|
+
const procedureType = clientCallTypeToProcedureType(pathCopy.pop()!);
|
|
64
|
+
|
|
65
|
+
if (procedureType === 'query') {
|
|
66
|
+
const queryCacheKey = JSON.stringify([path, args[0]]);
|
|
67
|
+
const cached = cache.get(queryCacheKey);
|
|
68
|
+
|
|
69
|
+
if (cached?.promise) {
|
|
70
|
+
return cached.promise;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const fullPath = pathCopy.join('.');
|
|
75
|
+
|
|
76
|
+
const promise: Promise<unknown> = (client as any)[procedureType](
|
|
77
|
+
fullPath,
|
|
78
|
+
...args,
|
|
79
|
+
);
|
|
80
|
+
if (procedureType !== 'query') {
|
|
81
|
+
return promise;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const queryCacheKey = JSON.stringify([path, args[0]]);
|
|
85
|
+
|
|
86
|
+
cache.set(queryCacheKey, {
|
|
87
|
+
promise,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
return promise;
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/// <reference types="next" />
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
CreateTRPCProxyClient,
|
|
5
|
+
clientCallTypeToProcedureType,
|
|
6
|
+
createTRPCUntypedClient,
|
|
7
|
+
} from '@trpc/client';
|
|
8
|
+
import { AnyRouter } from '@trpc/server';
|
|
9
|
+
import { createRecursiveProxy } from '@trpc/server/shared';
|
|
10
|
+
import { cache } from 'react';
|
|
11
|
+
import { CreateTRPCNextAppRouterOptions } from './shared';
|
|
12
|
+
|
|
13
|
+
// ts-prune-ignore-next
|
|
14
|
+
export function experimental_createTRPCNextAppDirServer<
|
|
15
|
+
TRouter extends AnyRouter,
|
|
16
|
+
>(opts: CreateTRPCNextAppRouterOptions<TRouter>) {
|
|
17
|
+
const getClient = cache(() => {
|
|
18
|
+
const config = opts.config();
|
|
19
|
+
return createTRPCUntypedClient(config);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return createRecursiveProxy((callOpts) => {
|
|
23
|
+
// lazily initialize client
|
|
24
|
+
const client = getClient();
|
|
25
|
+
|
|
26
|
+
const pathCopy = [...callOpts.path];
|
|
27
|
+
const procedureType = clientCallTypeToProcedureType(
|
|
28
|
+
pathCopy.pop() as string,
|
|
29
|
+
);
|
|
30
|
+
const fullPath = pathCopy.join('.');
|
|
31
|
+
|
|
32
|
+
return (client[procedureType] as any)(fullPath, ...callOpts.args);
|
|
33
|
+
}) as CreateTRPCProxyClient<TRouter>;
|
|
34
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CreateTRPCClientOptions,
|
|
3
|
+
Resolver,
|
|
4
|
+
TRPCUntypedClient,
|
|
5
|
+
} from '@trpc/client';
|
|
6
|
+
import {
|
|
7
|
+
AnyQueryProcedure,
|
|
8
|
+
AnyRouter,
|
|
9
|
+
Filter,
|
|
10
|
+
ProtectedIntersection,
|
|
11
|
+
ThenArg,
|
|
12
|
+
} from '@trpc/server';
|
|
13
|
+
import { createRecursiveProxy } from '@trpc/server/shared';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
export type UseProcedureRecord<TRouter extends AnyRouter> = {
|
|
19
|
+
[TKey in keyof Filter<
|
|
20
|
+
TRouter['_def']['record'],
|
|
21
|
+
AnyRouter | AnyQueryProcedure
|
|
22
|
+
>]: TRouter['_def']['record'][TKey] extends AnyRouter
|
|
23
|
+
? UseProcedureRecord<TRouter['_def']['record'][TKey]>
|
|
24
|
+
: Resolver<TRouter['_def']['record'][TKey]>;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export function createUseProxy<TRouter extends AnyRouter>(
|
|
28
|
+
client: TRPCUntypedClient<TRouter>,
|
|
29
|
+
) {
|
|
30
|
+
return createRecursiveProxy((opts) => {
|
|
31
|
+
const path = opts.path.join('.');
|
|
32
|
+
|
|
33
|
+
return client.query(path, ...opts.args);
|
|
34
|
+
}) as UseProcedureRecord<TRouter>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type NextAppRouterUse<TRouter extends AnyRouter> = {
|
|
38
|
+
<TData extends Promise<unknown>[]>(
|
|
39
|
+
cb: (t: UseProcedureRecord<TRouter>) => [...TData],
|
|
40
|
+
): {
|
|
41
|
+
[TKey in keyof TData]: ThenArg<TData[TKey]>;
|
|
42
|
+
};
|
|
43
|
+
<TData extends Promise<unknown>>(
|
|
44
|
+
cb: (t: UseProcedureRecord<TRouter>) => TData,
|
|
45
|
+
): ThenArg<TData>;
|
|
46
|
+
};
|
|
47
|
+
type CreateTRPCNextAppRouterBase<TRouter extends AnyRouter> = {
|
|
48
|
+
use: NextAppRouterUse<TRouter>;
|
|
49
|
+
};
|
|
50
|
+
export type CreateTRPCNextAppRouter<TRouter extends AnyRouter> =
|
|
51
|
+
ProtectedIntersection<
|
|
52
|
+
CreateTRPCNextAppRouterBase<TRouter>,
|
|
53
|
+
UseProcedureRecord<TRouter>
|
|
54
|
+
>;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @internal
|
|
58
|
+
*/
|
|
59
|
+
export interface CreateTRPCNextAppRouterOptions<TRouter extends AnyRouter> {
|
|
60
|
+
config: () => CreateTRPCClientOptions<TRouter>;
|
|
61
|
+
}
|