Skip to content

Commit 4bfdb53

Browse files
yoitsrokanadgupta
andauthored
feat(api): export SDK (#856)
* feat(api): export SDK In some use cases, the SDK class needs to be instantiated several times. For instance, in a multi-tenancy scenario, there may be multiple instances of the same API server, but serving different data from different base URLs. In this case, we need to instantiate multiple clients with different server URLs. * fix(api): export sdk in a separate file --------- Co-authored-by: Kanad Gupta <[email protected]>
1 parent 036f95f commit 4bfdb53

File tree

28 files changed

+2673
-2595
lines changed

28 files changed

+2673
-2595
lines changed

packages/api/src/codegen/languages/typescript/index.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ export default class TSGenerator extends CodeGenerator {
210210
async generate() {
211211
const srcDirectory = this.project.createDirectory('src');
212212
const sdkSource = this.createSDKSource(srcDirectory);
213+
this.createIndexSource(srcDirectory);
213214

214215
this.createGitIgnore();
215216
this.createPackageJSON();
@@ -278,14 +279,32 @@ export default class TSGenerator extends CodeGenerator {
278279
return snippet;
279280
}
280281

282+
/**
283+
* Create our main `index.ts` file that will be the entrypoint for our SDK.
284+
* This file will be used to export the SDK and any types that are generated.
285+
*
286+
*/
287+
private createIndexSource(sourceDirectory: Directory) {
288+
const sourceFile = sourceDirectory.createSourceFile('index.ts', '');
289+
290+
sourceFile.addImportDeclaration({
291+
defaultImport: 'SDK',
292+
moduleSpecifier: './sdk.js',
293+
});
294+
295+
this.createSDKExport(sourceFile);
296+
297+
return sourceFile;
298+
}
299+
281300
/**
282301
* Create our main SDK source file.
283302
*
284303
*/
285304
private createSDKSource(sourceDirectory: Directory) {
286305
const { operations } = this.loadOperationsAndMethods();
287306

288-
const sourceFile = sourceDirectory.createSourceFile('index.ts', '');
307+
const sourceFile = sourceDirectory.createSourceFile('sdk.ts', '');
289308

290309
sourceFile.addImportDeclarations([
291310
// This import will be automatically removed later if the SDK ends up not having any types.
@@ -302,6 +321,8 @@ export default class TSGenerator extends CodeGenerator {
302321
this.sdk = sourceFile.addClass({
303322
name: 'SDK',
304323
properties: [{ name: 'core', type: 'APICore' }],
324+
isDefaultExport: true,
325+
isExported: true,
305326
});
306327

307328
this.sdk.addConstructor({
@@ -405,8 +426,6 @@ sdk.server('https://eu.api.example.com/v14');`),
405426
this.createOperationAccessor(data.operation, operationId, data.types.params, data.types.responses);
406427
});
407428

408-
this.createSDKExport(sourceFile);
409-
410429
return sourceFile;
411430
}
412431

@@ -595,6 +614,7 @@ dist/
595614
dts: true,
596615
entry: [
597616
'./src/index.ts',
617+
'./src/sdk.ts',
598618
// If this SDK has schemas and generated types then we should also export those too so
599619
// they're available to use.
600620
hasTypes ? './src/types.ts' : '',
@@ -626,6 +646,10 @@ dist/
626646
import: './dist/index.js',
627647
require: './dist/index.cjs',
628648
},
649+
'./sdk': {
650+
import: './dist/sdk.js',
651+
require: './dist/sdk.cjs',
652+
},
629653
...(hasTypes
630654
? {
631655
'./types': {

packages/test-utils/sdks/alby/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
"import": "./dist/index.js",
1111
"require": "./dist/index.cjs"
1212
},
13+
"./sdk": {
14+
"import": "./dist/sdk.js",
15+
"require": "./dist/sdk.cjs"
16+
},
1317
"./types": {
1418
"import": "./dist/types.js",
1519
"require": "./dist/types.cjs"
@@ -36,6 +40,7 @@
3640
"dts": true,
3741
"entry": [
3842
"./src/index.ts",
43+
"./src/sdk.ts",
3944
"./src/types.ts"
4045
],
4146
"format": [

0 commit comments

Comments
 (0)