l-blondy / up-fetch Goto Github PK
View Code? Open in Web Editor NEWTiny & Composable fetch configuration tool with sensible default.
License: MIT License
Tiny & Composable fetch configuration tool with sensible default.
License: MIT License
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...
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'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.