Tree shaking object based API SDK

Hey guys, I use this internal sdk I built all the time, it works great but Im trying to get it treehsaken correctly. Heres a small snippet of what it looks like
export const api = {
get: {
one: async (endpoint:string) => //..... get one function,
many: async (endpoint:string) => // ..... get many function,
//.... many more get functions
},
document: {
getByPath: async (path:string) => // ....Get by path function
},
// .... many more functions
}
export const api = {
get: {
one: async (endpoint:string) => //..... get one function,
many: async (endpoint:string) => // ..... get many function,
//.... many more get functions
},
document: {
getByPath: async (path:string) => // ....Get by path function
},
// .... many more functions
}
Then in my code, I import my api and use a function from the object
import {api} from "api"
const records = await api.get.many("party")
import {api} from "api"
const records = await api.get.many("party")
^^^ This is a great dx and works great for our use case. However, in the above example, in the built JS I get the API object with every function on there, including the ones I didn't use (get:many, document:getByPath and everything else on the API obj) I understand this may be an unconventional way of developing a SDK, I think classes are more common here, and maybe treeshake better? I just avoid classes at all costs. Is there a way to achieve treeshaking with this achitecture? Obviously, I could just have each function on the api exported without the api object, but the benefit of being able to type api. and having autocomplete for every function on the object is too good
1 Reply
Neto
Neto4mo ago
you can't treeshake a class did you check the build size? with tsup correctly configured you can make something like this
ESM dist/toggle/index.jsx 171.00 B
ESM dist/tooltip/index.jsx 813.00 B
ESM dist/sheet/index.jsx 4.01 KB
ESM dist/toggle/index.jsx 171.00 B
ESM dist/tooltip/index.jsx 813.00 B
ESM dist/sheet/index.jsx 4.01 KB
instead of a giant 100kb single export
Want results from more Discord servers?
Add your server