create-typed-react-sdk
Rough Notes
import { createTypedSDK, DoFetch } from "create-typed-sdk";
import { createTypedReactSDK } from "create-typed-react-sdk";
import axios from "axios";
import type * as apiTypes from "./api";
const doFetch: DoFetch = async (p) => {
const r1 = await axios.post(
`http://localhost:3040/example-prefix/${p.path.join("/")}`,
{
argument: p.arg,
}
);
return r1.data;
};
export const apiSDK = createTypedSDK<typeof apiTypes>({ doFetch });
export const reactApiSDK = createTypedReactSDK<typeof apiTypes>({ doFetch });
import fastify from "fastify";
import cors from "@fastify/cors";
import * as api from "./api.js";
import { collectApiFunctions } from "create-typed-sdk";
const app = fastify({
logger: true,
});
app.register(cors, { origin: "*" });
app.get("/", (request, reply) => {
reply.send({ hello: "world" });
});
collectApiFunctions(api).forEach((a) => {
app.post("/example-prefix/" + a.path.join("/"), async (req, resp) => {
if (typeof req.body !== "object" && !(req.body as any).argument) {
throw new Error("Required property `argument` missing in post body!");
}
const val = await a.fn((req.body as any).argument);
return val;
});
});
app.listen({ port: 3040 }, (err, address) => {
if (err) throw err;
console.log(`Server is now listening on ${address}`);
});