drwpow/openapi-fetch

I would like access to types

busheezy opened this issue · 3 comments

Description

I would like access to PathsWith and FetchOptions types so I can wrap the client with the same parameter contraints.

Proposal

Before I make a PR, would you consider this to be in spec or perhaps you can think of a better way?

import useSWR from 'swr';
import { paths } from '../admin-client';
import { FetchOptions, PathsWith } from './apiFetch';
import { api } from './api';

export function useSWRAPI<
  U extends PathsWith<paths, 'get'>,
  M extends keyof paths[U]
>(url: U, opts: FetchOptions<paths[U][M]>) {
  return useSWR(url, async () => {
    const { data, error } = await api.get(url, opts);

    if (error) {
      throw error;
    }

    return data;
  });
}

Checklist

Sure! I have no problem exposing all the types. But I’d like to not mention them in the README as I would want to discourage most users from seeking these out. The people (probably like yourself) who are going to find them are going to find them, but I would never want to advertise this as a “happy path” as there are ways to use these types that may result in invalid types.

New version exposes all internal types used!

Thank you!