Nehilor
Nehilor
TtRPC
Created by Nehilor on 4/25/2023 in #❓-help
Looking to fix my tRPC implementation
@Nick Lucas I managed to fix it, thanks so much for the help! it was related with the address: Server
app.use('/trpc', tRPCConfig);
app.use('/trpc', tRPCConfig);
Client
const client = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url: '/trpc',
}),
],
});
const client = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url: '/trpc',
}),
],
});
27 replies
TtRPC
Created by Nehilor on 4/25/2023 in #❓-help
Looking to fix my tRPC implementation
I am following this guide but I am still getting the error: https://codesandbox.io/s/github/trpc/trpc/tree/main/examples/express-server
27 replies
TtRPC
Created by Nehilor on 4/25/2023 in #❓-help
Looking to fix my tRPC implementation
Hi @nlucas sorry for bothering you, please let me know when you have a few mins to help me with this, thanks
27 replies
TtRPC
Created by Nehilor on 4/25/2023 in #❓-help
Looking to fix my tRPC implementation
27 replies
TtRPC
Created by Nehilor on 4/25/2023 in #❓-help
Looking to fix my tRPC implementation
This is where I am not sure
27 replies
TtRPC
Created by Nehilor on 4/25/2023 in #❓-help
Looking to fix my tRPC implementation
My app works on this port: https://localhost.sparkcognition.com:3000/ I mean reactjs, so should I use this config:
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
import { AppRouter } from '../../server/tRPC';

const url = 'https://localhost.sparkcognition.com:3000';

const client = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url,
}),
],
});

export { client };
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
import { AppRouter } from '../../server/tRPC';

const url = 'https://localhost.sparkcognition.com:3000';

const client = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url,
}),
],
});

export { client };
27 replies
TtRPC
Created by Nehilor on 4/25/2023 in #❓-help
Looking to fix my tRPC implementation
Yeah I updated it one sec
27 replies
TtRPC
Created by Nehilor on 4/25/2023 in #❓-help
Looking to fix my tRPC implementation
My app uses 3000
27 replies
TtRPC
Created by Nehilor on 4/25/2023 in #❓-help
Looking to fix my tRPC implementation
27 replies
TtRPC
Created by Nehilor on 4/25/2023 in #❓-help
Looking to fix my tRPC implementation
27 replies
TtRPC
Created by Nehilor on 4/25/2023 in #❓-help
Looking to fix my tRPC implementation
Should be the same prefix @Nick Lucas ? or something different? Sorry I am new on this
27 replies
TtRPC
Created by Nehilor on 4/25/2023 in #❓-help
Looking to fix my tRPC implementation
Whats my goal? I would like to get results using this query for example:
import { client } from '../../tRPC';
const test = client.alerts.featureHealthAlerts.query({ serviceAlias: 'kong' });
test.then(data => {
console.log('data => ', data);
});
import { client } from '../../tRPC';
const test = client.alerts.featureHealthAlerts.query({ serviceAlias: 'kong' });
test.then(data => {
console.log('data => ', data);
});
This is my result:
27 replies
TtRPC
Created by Nehilor on 4/25/2023 in #❓-help
Looking to fix my tRPC implementation
Second file: src/server/tRPC/index.ts Content:
import { inferAsyncReturnType, initTRPC } from '@trpc/server';
import * as trpcExpress from '@trpc/server/adapters/express';
import config from 'config';
import { z } from 'zod';

const createContext = ({
req,
res,
}: trpcExpress.CreateExpressContextOptions) => {
return {
req,
res,
};
};

type Context = inferAsyncReturnType<typeof createContext>;
const t = initTRPC.context<Context>().create();

const { router } = t;
const publicProcedure = t.procedure;

const alertsRouter = router({
featureHealthAlerts: publicProcedure
.input(
z.object({
serviceAlias: z.string().nullish(),
}),
)
.query(async ({ input }) => {
try {
const serviceAlias = input.serviceAlias ?? 'kong';
const baseURL: unknown = config.get(
`coreUI.service.serviceProxies.${serviceAlias}.baseURL`,
);
const response: any = await fetch(`${baseURL}`);
return response;
} catch (error: any) {
throw new Error(error?.message);
}
}),
});

const appRouter = router({
alerts: alertsRouter,
});

export type AppRouter = typeof appRouter;

const tRPCConfig = trpcExpress.createExpressMiddleware({
router: appRouter,
createContext,
onError({ error }) {
console.error('Something went wrong', error);
},
batching: {
enabled: true,
},
});

export { tRPCConfig };
import { inferAsyncReturnType, initTRPC } from '@trpc/server';
import * as trpcExpress from '@trpc/server/adapters/express';
import config from 'config';
import { z } from 'zod';

const createContext = ({
req,
res,
}: trpcExpress.CreateExpressContextOptions) => {
return {
req,
res,
};
};

type Context = inferAsyncReturnType<typeof createContext>;
const t = initTRPC.context<Context>().create();

const { router } = t;
const publicProcedure = t.procedure;

const alertsRouter = router({
featureHealthAlerts: publicProcedure
.input(
z.object({
serviceAlias: z.string().nullish(),
}),
)
.query(async ({ input }) => {
try {
const serviceAlias = input.serviceAlias ?? 'kong';
const baseURL: unknown = config.get(
`coreUI.service.serviceProxies.${serviceAlias}.baseURL`,
);
const response: any = await fetch(`${baseURL}`);
return response;
} catch (error: any) {
throw new Error(error?.message);
}
}),
});

const appRouter = router({
alerts: alertsRouter,
});

export type AppRouter = typeof appRouter;

const tRPCConfig = trpcExpress.createExpressMiddleware({
router: appRouter,
createContext,
onError({ error }) {
console.error('Something went wrong', error);
},
batching: {
enabled: true,
},
});

export { tRPCConfig };
Third file: src/client/tRPC/index.ts Content:
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
import { AppRouter } from '../../server/tRPC';

const url = `/`;
const client = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url,
}),
],
});

export { client };
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
import { AppRouter } from '../../server/tRPC';

const url = `/`;
const client = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url,
}),
],
});

export { client };
27 replies