Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

tRPC v11 support #455

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
chore: Apply trpc 11 fix.
kdy1 committed Jul 30, 2024
commit cbca7cecd4ced50ba7fc9258591fea13d6090158
4 changes: 2 additions & 2 deletions src/adapters/node-http/core.ts
Original file line number Diff line number Diff line change
@@ -129,7 +129,7 @@ export const createOpenApiNodeHttpHandler = <
const caller = router.createCaller(ctx);

const segments = procedure.path.split('.');
const procedureFn = segments.reduce((acc, curr) => acc[curr], caller as any) as AnyProcedure;
const procedureFn = segments.reduce((acc, curr) => acc[curr], caller) as AnyProcedure;

data = await procedureFn(input);

@@ -165,7 +165,7 @@ export const createOpenApiNodeHttpHandler = <
errors: [error],
});

const errorShape = router.getErrorShape({
const errorShape = router._def.getErrorShape({
error,
type: procedure?.type ?? 'unknown',
path: procedure?.path,
2 changes: 1 addition & 1 deletion src/adapters/node-http/input.ts
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ export const getQuery = (req: NodeHTTPRequest, url: URL): Record<string, string>

// normalize first value in array
Object.keys(req.query).forEach((key) => {
const value = req.query![key];
const value = req.query[key];
if (value) {
if (typeof value === 'string') {
query[key] = value;
20 changes: 2 additions & 18 deletions src/adapters/node-http/procedures.ts
Original file line number Diff line number Diff line change
@@ -15,36 +15,20 @@ export const createProcedureCache = (router: OpenApiRouter) => {
>
>();

const { queries, mutations } = router._def;

forEachOpenApiProcedure(queries, ({ path: queryPath, procedure, openapi }) => {
forEachOpenApiProcedure(router._def.procedures, ({ path: queryPath, procedure, openapi }) => {
const { method } = openapi;
if (!procedureCache.has(method)) {
procedureCache.set(method, new Map());
}
const path = normalizePath(openapi.path);
const pathRegExp = getPathRegExp(path);
procedureCache.get(method)!.set(pathRegExp, {
type: 'query',
type: procedure._def.type,
path: queryPath,
procedure,
});
});

forEachOpenApiProcedure(mutations, ({ path: mutationPath, procedure, openapi }) => {
const { method } = openapi;
if (!procedureCache.has(method)) {
procedureCache.set(method, new Map());
}
const path = normalizePath(openapi.path);
const pathRegExp = getPathRegExp(path);
procedureCache.get(method)!.set(pathRegExp, {
type: 'mutation',
path: mutationPath,
procedure,
});
});

return (method: OpenApiMethod, path: string) => {
const procedureMethodCache = procedureCache.get(method);
if (!procedureMethodCache) {
7 changes: 1 addition & 6 deletions src/utils/procedure.ts
Original file line number Diff line number Diff line change
@@ -19,12 +19,7 @@ export const getInputOutputParsers = (procedure: OpenApiProcedure) => {
};
};

const getProcedureType = (procedure: OpenApiProcedure): ProcedureType => {
if (procedure._def.query) return 'query';
if (procedure._def.mutation) return 'mutation';
if (procedure._def.subscription) return 'subscription';
throw new Error('Unknown procedure type');
};
const getProcedureType = (procedure) => procedure._def.type;

export const forEachOpenApiProcedure = (
procedureRecord: OpenApiProcedureRecord,