Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: export ResponseMap type to allow composition of ofetch #232

Merged
merged 1 commit into from
Jun 6, 2023

Conversation

codebender828
Copy link
Contributor

@codebender828 codebender828 commented Apr 13, 2023

This re-exports the ResponseMap type. The use case for this is to allow consumers to build a type-safe variant of ofetch such we can append methods to a given instance.

Consider this wrapper library below called api.ts. In the code below, the FetchOptions type from ofetch consumes the private type ResponseMap. Building declarations for api.ts types for this would fail because the ResponseMap type is private to ofetch. In this case, re-exporting the type fixes the error and declarations can be built in userland.

// api.ts lib
import { ofetch, FetchOptions } from "ofetch";

export const $fetch = ofetch.create({
  baseURL: SOME_API_BASE_URL,
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json",
  },
  onRequest(context) {
    console.log("context", context.options);
  },
  credentials: "include",
});

/**
 * HTTP API Fetch instance
 * @param request
 * @param options
 * @returns
 */
export function api<T = any, R extends ResponseType = "json">(
  request: FetchRequest,
  options?: FetchOptions<R>
): Promise<MappedType<R, T>> {
  return $fetch<T, R>(request, options) as Promise<MappedType<R, T>>;
}

/* Appends `get` Method to `api` function object */
api.get = function get<T = any, R extends ResponseType = "json">(
  url: string,
  options: Omit<FetchOptions, "method"> = {}
): Promise<MappedType<R, T>> {
  return api(url, {
    ...options,
    method: "GET",
  });
};

@codecov
Copy link

codecov bot commented Apr 13, 2023

Codecov Report

Merging #232 (182414b) into main (6e00405) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main     #232   +/-   ##
=======================================
  Coverage   86.26%   86.26%           
=======================================
  Files           5        5           
  Lines         415      415           
  Branches       68       68           
=======================================
  Hits          358      358           
  Misses         57       57           
Impacted Files Coverage Δ
src/utils.ts 93.15% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@madskronborg
Copy link

When is this expected to release? :)

We are currently blocked in our migration to Nuxt 3, because our core nuxt module needs to expose a shared api client composable as discussed here: #219 (comment)

@danielroe danielroe requested a review from pi0 May 30, 2023 10:58
@pi0 pi0 merged commit 4f0e298 into unjs:main Jun 6, 2023
3 checks passed
@pi0 pi0 changed the title feat: export ResponseMap type to allow composition of ofetch fix: export ResponseMap type to allow composition of ofetch Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants