readmeio/api

define the Type of the sdk object.

mewc opened this issue · 3 comments

mewc commented

Im trying to get an inferred type of the whole imported "sdk" object

for example.

import workspaces from '@api/generated-sdk-of-some-kind'

const configureApiModule = (apiModule: any ) => {
  apiModule.server(url, { basePath: 'v1' })
  apiModule.auth(username, password)
}

modules.forEach(configureApiModule)

i want to be able to make the apiModule typed properly, but i can't seem to export or use any of the type SDKInstance = ReturnType<typeof createSDK>; or InstanceOf since the createSDK call is an IIFE.

I also dont want to edit codegen stuff ofc. Am i missing anything?

@kanadgupta Any ideas on how we can handle this? We can't export SDK from the generated index.ts files because that'd collide with the default export in CJS environments, and we can't do the same with an export type APISDK = typeof SDK in the same file for the same reason.

@erunion what if we defined the SDK class in a sdk.ts file and created an export in the package.json for that? And then the IIFE would remain in index.ts and import from sdk.ts?

@kanadgupta That'd work