Skip to content

Commit

Permalink
Fancy types when initing SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyxiao committed Dec 2, 2023
1 parent ea7f4d1 commit 3d1dc65
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
8 changes: 7 additions & 1 deletion docs/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ export const github = initSDK(githubSdkDef, {
},
})

export const plaid = initSDK(plaidSdkDef, {
headers: {
'PLAID-CLIENT-ID': '',
'PLAID-SECRET': '',
},
}) // Need clientId & secret

export const discord = initSDK(discordSdkDef)
export const openai = initSDK(openaiSdkDef)
export const plaid = initSDK(plaidSdkDef) // Need clientId & secret
export const slack = initSDK(slackSdkDef)
export const venice = initSDK(veniceSdkDef) // ApiKey or authToken

Expand Down
18 changes: 11 additions & 7 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ export type OpenAPISpec = oas30.OpenAPIObject | oas31.OpenAPIObject

/** Get this from openapi */
export interface SdkDefinition<Paths extends {}> {
_types: {paths: Paths}
_types: {
paths: Paths
}
oas: OpenAPISpec
headers?: Record<string, string>
}

// This is necessary because we cannot publish inferred type otherwise
Expand All @@ -27,13 +30,14 @@ export type SDK<Paths extends {}> = ReturnType<typeof createClient<Paths>> & {
oas: OpenAPISpec
}

export function initSDK<Paths extends {}>(
// Can we make this optional to avoid needing to deal with json?
sdkDef: SdkDefinition<Paths>,
options?: ClientOptions,
): SDK<Paths> {
// Can we make this optional to avoid needing to deal with json?
export function initSDK<TDef extends SdkDefinition<{}>>(
...[sdkDef, options]: 'headers' extends keyof TDef
? [TDef, Omit<ClientOptions, 'headers'> & {headers: TDef['headers']}]
: [TDef] | [TDef, ClientOptions?]
): SDK<TDef['_types']['paths']> {
const {oas} = sdkDef
const client = createClient<Paths>({
const client = createClient<TDef['_types']['paths']>({
baseUrl: oas.servers?.[0]?.url,
...options,
})
Expand Down
4 changes: 4 additions & 0 deletions packages/sdks/sdk-plaid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export interface plaidTypes {
export const plaidSdkDef = {
_types: {} as plaidTypes,
oas: plaidOas as {} as OpenAPISpec,
headers: {} as {
'PLAID-CLIENT-ID': string
'PLAID-SECRET': string
},
} satisfies SdkDefinition<paths>

export default plaidSdkDef
Expand Down

0 comments on commit 3d1dc65

Please sign in to comment.