npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

openapi-ff

v0.1.3

Published

openapi-ff is a type-safe tiny wrapper around [effector](https://effector.dev/) and [farfetched](https://ff.effector.dev/) to work with OpenAPI schema.

Downloads

252

Readme

openapi-ff

openapi-ff is a type-safe tiny wrapper around effector and farfetched to work with OpenAPI schema.

It works by using openapi-fetch and openapi-typescript.

Setup

pnpm i openapi-ff @farfetched/core effector openapi-fetch
pnpm i -D openapi-typescript typescript

Next, generate TypeScript types from your OpenAPI schema using openapi-typescript:

npx openapi-typescript ./path/to/api/v1.yaml -o ./src/shared/api/schema.d.ts

Usage

import createFetchClient from "openapi-fetch";
import { createClient } from "openapi-ff";
import { createQuery } from "@farfetched/core";
import type { paths } from "./schema"; // generated by openapi-typescript

export const client = createFetchClient<paths>({
  baseUrl: "https://myapi.dev/v1/",
});
export const { createApiEffect } = createClient(client);

const blogpostQuery = createQuery({
  effect: createApiEffect("get", "/blogposts/{post_id}"),
});
import { useUnit } from "effector-react";

function Post() {
  const { data: post, pending } = useUnit(blogpostQuery);

  if (pending) {
    return <Loader />;
  }

  return (
    <section>
      <p>{post.title}</p>
    </section>
  );
}

Advanced Usage:

import { chainRoute } from "atomic-router";
import { startChain } from "@farfetched/atomic-router";
import { isApiError } from "openapi-ff";

const getBlogpostFx = createApiEffect("get", "/blogposts/{post_id}", {
  mapParams: (args: { postId: string }) => ({ params: { path: { post_id: args.postId } } }),
});
const blogpostQuery = createQuery({
  effect: getBlogpostFx,
  mapData: ({ result, params }) => ({ ...result, ...params }),
  initialData: { body: "-", title: "-", postId: "0" },
});

export const blogpostRoute = chainRoute({
  route: routes.blogpost.item,
  ...startChain(blogpostQuery),
});

const apiError = sample({
  clock: softwareQuery.finished.failure,
  filter: isApiError,
});

Runtime Validation

openapi-ff does not handle runtime validation, as openapi-typescript does not support it.

openapi-typescript by its design generates runtime-free static types, and only static types.

However, openapi-ff allows adding a contract factory when creating a client and provides a corresponding method, createApiEffectWithContract:

const { createApiEffectWithContract } = createClient(fetchClient, {
  createContract(method, path) {
    // ... create your own contract
    return contract; // Contract<unknown, unknown>
  },
});

const query = createQuery({
  ...createApiEffectWithContract("get", "/blogposts"),
});

typed-openapi example

npx typed-openapi path/to/api.yaml -o src/zod.ts -r zod # Generate zod schemas
pnpm install zod @farfetched/zod
import { EndpointByMethod } from "./zod";
import { zodContract } from "@farfetched/zod";

const { createApiEffectWithContract } = createClient(fetchClient, {
  createContract(method, path) {
    const endpoints = EndpointByMethod[method] as any;
    const response = endpoints[path]?.response;
    if (!response) {
      throw new Error(`Response schema for route "${method} ${path}" doesn't exist`);
    }
    return zodContract(response);
  },
});

const query = createQuery({
  ...createApiEffectWithContract("get", "/blogposts"),
});

orval example

Alternatively, you can simply add any contract to a query:

pnpm install zod @farfetched/zod
npx orval --input path/to/api.yaml --output src/zod.ts --client zod --mode single
import { zodContract } from "@farfetched/zod";
import { getBlogpostsResponseItem } from "./zod";

const blogpostQuery = createQuery({
  effect: createApiEffect("get", "/blogposts/{post_id}"),
  contract: zodContract(getBlogpostsResponseItem),
});

TODO

Add createApiQuery:

createApiQuery({
  method: "get",
  path: "/blogposts/{post_id}",
  mapParams: (args: { postId: string }) => ({ params: { path: { post_id: args.postId } } }),
  mapData: ({ result, params }) => ({ ...result, ...params }),
  initialData: { body: "-", title: "-", postId: "0" },
});