Skip to content

lewis-wow/ptsq

Repository files navigation

ptsq

Public type-safe query

codecov

Installation

npm i @ptsq/server
npm i @ptsq/client
npm i -D @ptsq/introspection-cli
import { ptsq } from '@ptsq/server';
import { Type } from '@sinclair/typebox';
import express, { Request, Response } from 'express';

const app = express();

const createContext = ({ req, res }: { req: Request; res: Response }) => ({
  req,
  res,
});

const { resolver, router, serve } = ptsq({
  ctx: createContext,
}).create();

const testQuery = resolver
  .args(Type.Object({ name: Type.String() }))
  .output(Type.String())
  .query(
    async ({
      ctx /* { req: express.Request, res: express.Response } */,
      input /* { name: string } */,
    }) => {
      return `Hello, ${input.name}`;
    },
  );

const baseRouter = router({
  test: testQuery,
});

app.use((req, res) => serve(baseRouter)(req, res));

app.listen(4000);
import { createProxyClient } from '@ptsq/client';
import type { BaseRouter } from './server';

const client = createProxyClient<BaseRouter>({
  url: 'http://localhost:4000/ptsq/',
});

const result /* string */ = await client.test.query({
  name: /* string */ 'John',
});
import { createProxyClient } from '@ptsq/client';
import type { BaseRouter } from './introspected-schema';

const client = createProxyClient<BaseRouter>({
  url: 'http://localhost:4000/ptsq/',
});

const result /* string */ = await client.test.query({
  name: /* string */ 'John',
});