Skip to content

ilijaNL/typed-doc

Repository files navigation

Typed-Doc

Fastify usage

  1. Install dependencies
npm i @typed-doc/core @typed-doc/fastify
  1. Define contract
// can be standalone package
export const contract = createContract({
  test: {
    methodType: 'query',
    input: Type.Object({
      test: Type.String(),
    }),
    output: Type.Object({
      response: Type.String(),
    }),
  },
});
  1. Create fastify plugin from contract
import { contract } from '@my-contracts';
import { toPlugin } from '@typed-doc/fastify';

const server = Fastify();

type Context = { fastify: FastifyInstance };

const plugin = toPlugin(contract)<Context>({
  test: {
    resolve: async ({ input: { test } }) => ({ response: test }),
  },
});

server.register(plugin, {
  // create context
  contextFactory: (req): Context => ({ fastify: req.server }),
});
  1. Call the contract
import { createRPCClient } from '@typed-doc/core';
import { contract } from "@my-contracts";
const contractClient = createRPCClient(contract, /* fetch fn implementation */, '/');

// the input and the output will be typed
const { response } = await contractClient.mutate.test({ input: 'input' });

TODO

  • Usage Documentation
  • Implemtation documentation
  • NextJS API middleware
  • ExpressJS middleware
  • React Query implementation
  • Example