Skip to content

Commit

Permalink
chore: script to update imports into the docs package
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyxiao committed Dec 2, 2023
1 parent 4dea111 commit 3a64210
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 20 deletions.
41 changes: 41 additions & 0 deletions bin/updateDocImports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* We have to generate the connector list into actual files because webpack / next.js is extremely not performant
* when importing modules with dynamic paths at runtime
*/

import * as fs from 'node:fs'
import {join as pathJoin} from 'node:path'
import prettier from 'prettier'

const sdkList = fs
.readdirSync(pathJoin(__dirname, '../sdks'), {
withFileTypes: true,
})
.filter((r) => r.isDirectory())
.map((d) => {
const path = pathJoin(__dirname, '../sdks', d.name)
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const pkgJson: {name: string} = JSON.parse(
fs.readFileSync(pathJoin(path, 'package.json'), 'utf-8'),
)
return pkgJson.name
})

async function updateDocImports() {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const pkgJson: {dependencies: {}} = require('../docs/package.json')
pkgJson.dependencies = {
...pkgJson.dependencies,
...Object.fromEntries(sdkList.map((s) => [s, 'workspace:*'])),
}
fs.writeFileSync(
pathJoin(__dirname, '../docs/package.json'),
await prettier.format(JSON.stringify(pkgJson), {
...(require('../prettier.config') as {}),
parser: 'json',
}),
)
}

// console.log(sdkList)
void updateDocImports()
6 changes: 6 additions & 0 deletions docs/example.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {initSDK} from '@opensdks/core'
import {apolloSdkDef} from '@opensdks/sdk-apollo'
import {discordSdkDef} from '@opensdks/sdk-discord'
import {githubSdkDef} from '@opensdks/sdk-github'
import {openaiSdkDef} from '@opensdks/sdk-openai'
Expand All @@ -23,6 +24,9 @@ export const plaid = initSDK(plaidSdkDef, {
export const discord = initSDK(discordSdkDef)
export const openai = initSDK(openaiSdkDef)
export const slack = initSDK(slackSdkDef)
export const apollo = initSDK(apolloSdkDef, {
api_key: process.env['APOLLO_API_KEY']!,
})
export const venice = initSDK(veniceSdkDef, {
headers: {'x-apikey': process.env['VENICE_API_KEY']},
})
Expand All @@ -34,3 +38,5 @@ void github
})

void venice.GET('/core/resource').then((r) => console.log(r.data))

void apollo.GET('/v1/email_accounts').then((r) => console.log(r.data))
13 changes: 7 additions & 6 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"name": "@opensdks/docs",
"dependencies": {
"@opensdks/core": "workspace:*",
"@opensdks/sdk-discord": "workspace*",
"@opensdks/sdk-github": "workspace*",
"@opensdks/sdk-openai": "workspace*",
"@opensdks/sdk-plaid": "workspace*",
"@opensdks/sdk-slack": "workspace*",
"@opensdks/sdk-venice": "workspace*"
"@opensdks/sdk-apollo": "workspace:*",
"@opensdks/sdk-discord": "workspace:*",
"@opensdks/sdk-github": "workspace:*",
"@opensdks/sdk-openai": "workspace:*",
"@opensdks/sdk-plaid": "workspace:*",
"@opensdks/sdk-slack": "workspace:*",
"@opensdks/sdk-venice": "workspace:*"
}
}
7 changes: 5 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ export type SDK<Paths extends {}> = ReturnType<typeof createClient<Paths>> & {
// Can we make this optional to avoid needing to deal with json?
export function initSDK<TDef extends SdkDefinition<{}>>(
...[sdkDef, options]: 'options' extends keyof TDef
? [TDef, Omit<ClientOptions, keyof TDef['options']> & TDef['options']]
: [TDef] | [TDef, ClientOptions?]
? [
sdkDef: TDef,
options: Omit<ClientOptions, keyof TDef['options']> & TDef['options'],
]
: [sdkDef: TDef] | [sdkDef: TDef, options?: ClientOptions]
): SDK<TDef['_types']['paths']> {
const {oas} = sdkDef
const client = createClient<TDef['_types']['paths']>({
Expand Down
15 changes: 9 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions sdks/sdk-apollo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ export const apolloSdkDef = {
_types: {} as apolloTypes,
oas: apolloOas as {} as OpenAPISpec,
options: {
headers: {} as {
'x-apikey'?: string
'x-resource-id'?: string
/** Bearer token */
Authorization?: string
},
api_key: '',
},
} satisfies SdkDefinition<paths>

Expand Down

0 comments on commit 3a64210

Please sign in to comment.