Code Monkey home page Code Monkey logo

up-fetch's Introduction

Hi I'm Laurent ๐Ÿ‘‹

Frontend Dev based in Italy


github stats



Pinned Repos




Top Techs


typescript javascript react nextjs github contribution grid snake animation tailwind sass vitest jest cypress figma

Contributions

github contribution grid snake animation

up-fetch's People

Contributors

hufei365 avatar l-blondy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

hufei365

up-fetch's Issues

type inference when using a curried generic on parseResponse

Using a non generic curried parser there is no issue

const upfetch = up(fetch)
const curried = () => (res: Response) => res.text()

const data = await upfetch('', {
   parseResponse: curried(),
   transform: (data) => data,
   onSuccess(data) {
      // โœ… ok
      expectTypeOf(data).toEqualTypeOf<string>()
   },
}) 
// โœ… ok
expectTypeOf(data).toEqualTypeOf<string>()

When using a curried parser everything is ok...

const data = await upfetch('', {
   parseResponse: withZod(z.object({ a: z.string() })),
})
// โœ… ok
expectTypeOf(data).toEqualTypeOf<{ a: string }>()

...until either onSuccess or transform are defined

const data = await upfetch('', {
   parseResponse: withZod(z.object({ a: z.string() })),
   transform: (data) => {
      // โŒ This fails, data is `any`
      expectTypeOf(data).toEqualTypeOf<{ a: string }>()
   }
})
// โŒ This fails, data is `any`
expectTypeOf(data).toEqualTypeOf<{ a: string }>()

but it works fine when using the functional version of the fetcher options

const data = await upfetch('', () => ({
   parseResponse: withZod(z.object({ a: z.string() })),
   transform: (data) => {
      // โœ… OK
      expectTypeOf(data).toEqualTypeOf<{ a: string }>()
   }
}))
// โœ… OK
expectTypeOf(data).toEqualTypeOf<{ a: string }>()

Also fine when unwrapping the curried function

const data = await upfetch('', {
   parseResponse: (res, opts) => withZod(z.object({ a: z.string() }))(res, opts),
   transform: (data) => {
      // โœ… OK
      expectTypeOf(data).toEqualTypeOf<{ a: string }>()
   }
})
// โœ… OK
expectTypeOf(data).toEqualTypeOf<{ a: string }>()

When using a default parser the data type in the callbacks come from the default parser, not the fetcher's

const upfetch = up(fetch, () => ({
   parseResponse: () => 1
}))

const data = await upfetch('', {
   โŒ Error on `parseResponse`
   parseResponse: withZod(z.object({ a: z.string() })),
   transform: (data) => {
      // โŒ This fails, data is `number`
      expectTypeOf(data).toEqualTypeOf<{ a: string }>()
   },
   onSuccess: (data) => {
      // โŒ This fails, data is `number`
      expectTypeOf(data).toEqualTypeOf<{ a: string }>()
   }
})
// โŒ This fails, data is `any`
expectTypeOf(data).toEqualTypeOf<{ a: string }>()

Same thing with the functional options, it works fine

const upfetch = up(fetch, () => ({
   parseResponse: () => 1
}))

const data = await upfetch('', {
   parseResponse: withZod(z.object({ a: z.string() })),
   transform: (data) => {
      // โœ… OK
      expectTypeOf(data).toEqualTypeOf<{ a: string }>()
   },
   onSuccess: (data) => {
      // โœ… OK
      expectTypeOf(data).toEqualTypeOf<{ a: string }>()
   }
})
// โœ… OK
expectTypeOf(data).toEqualTypeOf<{ a: string }>()

That's quite strange...

Add another entrypoint for parsers

Since the withValibot parser imports the parse function from valibot, valibot cannot be an optional peerDependency.

Even though this has no impact on the bundle size of up-fetch, in order to declare valibot as an optional peerDependency it would be better to expose the parsers via another entrypoint, eg import { withValibot, withZod } from 'up-fetch/parsers'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.