Skip to content

Commit 8ca3302

Browse files
feat: Support ESM build and tree-shaking (box/box-codegen#762) (#663)
1 parent 1314e6e commit 8ca3302

File tree

12 files changed

+728
-242
lines changed

12 files changed

+728
-242
lines changed

.codegen.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "engineHash": "ba4dd3b", "specHash": "8402463", "version": "1.16.0" }
1+
{ "engineHash": "7d973e2", "specHash": "8402463", "version": "1.16.0" }

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,5 @@ dist
130130
.pnp.*
131131

132132
# box-gen-sdk
133-
lib/
133+
lib/
134+
lib-esm/

docs/authentication.md

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ object with the `token` set to the developer token and construct the client with
4242
<!-- sample x_auth init_with_dev_token -->
4343

4444
```js
45-
const { BoxClient } = require('box-typescript-sdk-gen/lib/client.generated.js');
46-
const {
47-
BoxDeveloperTokenAuth,
48-
} = require('box-typescript-sdk-gen/lib/box/developerTokenAuth.generated.js');
45+
import { BoxClient } from 'box-typescript-sdk-gen/client.generated';
46+
import { BoxDeveloperTokenAuth } from 'box-typescript-sdk-gen/box/developerTokenAuth.generated';
4947

5048
const auth = new BoxDeveloperTokenAuth({ token: 'DEVELOPER_TOKEN_GOES_HERE' });
5149
const client = new BoxClient({ auth });
@@ -78,11 +76,11 @@ Service Account. Call one of static `BoxJwtAuth` method:
7876
or `JwtConfig.fromConfigJsonString(configJsonString)` and pass JSON config file content as string.
7977

8078
```js
81-
const { BoxClient } = require('box-typescript-sdk-gen/lib/client.generated.js');
82-
const {
79+
import { BoxClient } from 'box-typescript-sdk-gen/client.generated';
80+
import {
8381
BoxJwtAuth,
8482
JwtConfig,
85-
} = require('box-typescript-sdk-gen/lib/box/jwtAuth.generated.js');
83+
} from 'box-typescript-sdk-gen/box/jwtAuth.generated';
8684

8785
const jwtConfig = JwtConfig.fromConfigFile('/path/to/settings.json');
8886
const jwtAuth = new BoxJwtAuth({ config: jwtConfig });
@@ -95,11 +93,11 @@ console.log(`My user ID is ${me.id}`);
9593
Otherwise, you'll need to provide the necessary configuration fields directly to the `JwtConfig` constructor:
9694

9795
```js
98-
const { BoxClient } = require('box-typescript-sdk-gen/lib/client.generated.js');
99-
const {
96+
import { BoxClient } from 'box-typescript-sdk-gen/client.generated';
97+
import {
10098
BoxJwtAuth,
10199
JwtConfig,
102-
} = require('box-typescript-sdk-gen/lib/box/jwtAuth.generated.js');
100+
} from 'box-typescript-sdk-gen/box/jwtAuth.generated';
103101

104102
const jwtConfig = new JwtConfig({
105103
clientId: 'YOUR_CLIENT_ID',
@@ -127,11 +125,11 @@ Clients for making calls as an App User can be created with the same JSON JWT co
127125
which is authenticated as the user with provided id, leaving the original object unchanged.
128126

129127
```js
130-
const { BoxClient } = require('box-typescript-sdk-gen/lib/client.generated.js');
131-
const {
128+
import { BoxClient } from 'box-typescript-sdk-gen/client.generated';
129+
import {
132130
BoxJwtAuth,
133131
JwtConfig,
134-
} = require('box-typescript-sdk-gen/lib/box/jwtAuth.generated.js');
132+
} from 'box-typescript-sdk-gen/box/jwtAuth.generated';
135133

136134
const jwtConfig = JwtConfig.fromConfigFile('/path/to/settings.json');
137135
const jwtAuth = new BoxJwtAuth({ config: jwtConfig });
@@ -144,11 +142,11 @@ constructor as in the above examples, similarly to creating a Service Account cl
144142
`userId` instead of `enterpriseId` when constructing the auth config instance:
145143

146144
```js
147-
const { BoxClient } = require('box-typescript-sdk-gen/lib/client.generated.js');
148-
const {
145+
import { BoxClient } from 'box-typescript-sdk-gen/client.generated';
146+
import {
149147
BoxJwtAuth,
150148
JwtConfig,
151-
} = require('box-typescript-sdk-gen/lib/box/jwtAuth.generated.js');
149+
} from 'box-typescript-sdk-gen/box/jwtAuth.generated';
152150

153151
const jwtConfig = new JwtConfig({
154152
clientId: 'YOUR_CLIENT_ID',
@@ -177,11 +175,11 @@ and secret with enterprise or user ID, which allows you to work using service or
177175
You can use `CCGAuth` to initialize a client object the same way as for other authentication types:
178176

179177
```js
180-
const { BoxClient } = require('box-typescript-sdk-gen/lib/client.generated.js');
181-
const {
178+
import { BoxClient } from 'box-typescript-sdk-gen/client.generated';
179+
import {
182180
BoxCcgAuth,
183181
CcgConfig,
184-
} = require('box-typescript-sdk-gen/lib/box/ccgAuth.generated.js');
182+
} from 'box-typescript-sdk-gen/box/ccgAuth.generated';
185183

186184
const ccgConfig = new CcgConfig({
187185
userId: 'YOUR_USER_ID',
@@ -206,11 +204,11 @@ are not accessible in any other account by default, and vice versa.
206204
To obtain service account you will have to provide enterprise ID with client id and secret:
207205

208206
```js
209-
const { BoxClient } = require('box-typescript-sdk-gen/lib/client.generated.js');
210-
const {
207+
import { BoxClient } from 'box-typescript-sdk-gen/client.generated';
208+
import {
211209
BoxCcgAuth,
212210
CcgConfig,
213-
} = require('box-typescript-sdk-gen/lib/box/ccgAuth.generated.js');
211+
} from 'box-typescript-sdk-gen/box/ccgAuth.generated';
214212

215213
const ccgConfig = new CcgConfig({
216214
enterpriseId: 'YOUR_ENTERPRISE_ID',
@@ -230,11 +228,11 @@ select `Generate user access tokens`. Do not forget to re-authorize application
230228
To obtain user account you will have to provide user ID with client id and secret.
231229

232230
```js
233-
const { BoxClient } = require('box-typescript-sdk-gen/lib/client.generated.js');
234-
const {
231+
import { BoxClient } from 'box-typescript-sdk-gen/client.generated';
232+
import {
235233
BoxCcgAuth,
236234
CcgConfig,
237-
} = require('box-typescript-sdk-gen/lib/box/ccgAuth.generated.js');
235+
} from 'box-typescript-sdk-gen/box/ccgAuth.generated';
238236

239237
const ccgConfig = new CcgConfig({
240238
userId: 'YOUR_USER_ID',
@@ -284,10 +282,10 @@ browser or web view) in order to obtain an auth code.
284282
<!-- sample get_authorize -->
285283

286284
```js
287-
const {
285+
import {
288286
BoxOAuth,
289287
OAuthConfig,
290-
} = require('box-typescript-sdk-gen/lib/box/oauth.generated.js');
288+
} from 'box-typescript-sdk-gen/box/oauth.generated';
291289

292290
const config = new OAuthConfig({
293291
clientId: 'OAUTH_CLIENT_ID',

migration-guide.md

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,35 @@
33
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
44
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
55

6-
- [Introduction](#introduction)
7-
- [Installation](#installation)
8-
- [Highlighting the Key Differences](#highlighting-the-key-differences)
9-
- [Native support for async-await and Promises](#native-support-for-async-await-and-promises)
10-
- [Embracing Explicitly Defined Schemas](#embracing-explicitly-defined-schemas)
11-
- [Immutable design](#immutable-design)
12-
- [Diving into Authentication](#diving-into-authentication)
13-
- [Developer Token](#developer-token)
14-
- [JWT Authentication](#jwt-authentication)
15-
- [Leveraging the JWT Configuration File](#leveraging-the-jwt-configuration-file)
16-
- [Manually Providing JWT Configuration](#manually-providing-jwt-configuration)
17-
- [User Authentication Simplified](#user-authentication-simplified)
18-
- [Client Credentials Grant](#client-credentials-grant)
19-
- [Service Account Token Acquisition](#service-account-token-acquisition)
20-
- [User Token Acquisition](#user-token-acquisition)
21-
- [Smooth Switching between Service Account and User](#smooth-switching-between-service-account-and-user)
22-
- [OAuth 2.0 Authentication](#oauth-20-authentication)
23-
- [Fetching the Authorization URL](#fetching-the-authorization-url)
24-
- [Seamless Authentication](#seamless-authentication)
25-
- [Customizable Token Storage and Retrieval Callbacks](#customizable-token-storage-and-retrieval-callbacks)
26-
- [Downscope token](#downscope-token)
27-
- [Revoke token](#revoke-token)
28-
- [Configuration](#configuration)
29-
- [As-User header](#as-user-header)
30-
- [Custom Base URLs](#custom-base-urls)
31-
- [Convenience methods](#convenience-methods)
32-
- [Webhook validation](#webhook-validation)
33-
- [Chunked upload of big files](#chunked-upload-of-big-files)
6+
- [Migration Guide: From `Box Node SDK` to `Box TypeScript SDK`](#migration-guide-from-box-node-sdk-to-box-typescript-sdk)
7+
- [Introduction](#introduction)
8+
- [Installation](#installation)
9+
- [Highlighting the Key Differences](#highlighting-the-key-differences)
10+
- [Native support for async-await and Promises](#native-support-for-async-await-and-promises)
11+
- [Embracing Explicitly Defined Schemas](#embracing-explicitly-defined-schemas)
12+
- [Immutable design](#immutable-design)
13+
- [Diving into Authentication](#diving-into-authentication)
14+
- [Developer Token](#developer-token)
15+
- [JWT Authentication](#jwt-authentication)
16+
- [Leveraging the JWT Configuration File](#leveraging-the-jwt-configuration-file)
17+
- [Manually Providing JWT Configuration](#manually-providing-jwt-configuration)
18+
- [User Authentication Simplified](#user-authentication-simplified)
19+
- [Client Credentials Grant](#client-credentials-grant)
20+
- [Service Account Token Acquisition](#service-account-token-acquisition)
21+
- [User Token Acquisition](#user-token-acquisition)
22+
- [Smooth Switching between Service Account and User](#smooth-switching-between-service-account-and-user)
23+
- [OAuth 2.0 Authentication](#oauth-20-authentication)
24+
- [Fetching the Authorization URL](#fetching-the-authorization-url)
25+
- [Seamless Authentication](#seamless-authentication)
26+
- [Customizable Token Storage and Retrieval Callbacks](#customizable-token-storage-and-retrieval-callbacks)
27+
- [Downscope token](#downscope-token)
28+
- [Revoke token](#revoke-token)
29+
- [Configuration](#configuration)
30+
- [As-User header](#as-user-header)
31+
- [Custom Base URLs](#custom-base-urls)
32+
- [Convenience methods](#convenience-methods)
33+
- [Webhook validation](#webhook-validation)
34+
- [Chunked upload of big files](#chunked-upload-of-big-files)
3435

3536
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
3637

@@ -144,7 +145,7 @@ var client = sdk.getBasicClient('YOUR-DEVELOPER-TOKEN');
144145
The new SDK offers a more streamlined approach:
145146

146147
```typescript
147-
const { BoxClient, BoxDeveloperTokenAuth } = require('box-typescript-sdk-gen');
148+
import { BoxClient, BoxDeveloperTokenAuth } from 'box-typescript-sdk-gen';
148149

149150
const auth = new BoxDeveloperTokenAuth({ token: 'DEVELOPER_TOKEN_GOES_HERE' });
150151
const client = new BoxClient({ auth });
@@ -171,7 +172,7 @@ var serviceAccountClient = sdk.getAppAuthClient('enterprise');
171172
The new SDK provides a more organized approach:
172173

173174
```typescript
174-
const { BoxClient, BoxJwtAuth, JwtConfig } = require('box-typescript-sdk-gen');
175+
import { BoxClient, BoxJwtAuth, JwtConfig } from 'box-typescript-sdk-gen';
175176

176177
const jwtConfig = JwtConfig.fromConfigFile('/path/to/config.json');
177178
const auth = new BoxJwtAuth({ config: jwtConfig });
@@ -207,7 +208,7 @@ var serviceAccountClient = sdk.getAppAuthClient(
207208
The new SDK introduces a more structured approach:
208209

209210
```typescript
210-
const { BoxJwtAuth, JwtConfig } = require('box-typescript-sdk-gen');
211+
import { BoxJwtAuth, JwtConfig } from 'box-typescript-sdk-gen';
211212

212213
const jwtConfig = new JwtConfig({
213214
clientId: 'YOUR_CLIENT_ID',
@@ -269,7 +270,7 @@ const client = sdk.getAnonymousClient();
269270
The new SDK offers a more organized structure:
270271

271272
```typescript
272-
const { CcgConfig, BoxCcgAuth, BoxClient } = require('box-typescript-sdk-gen');
273+
import { CcgConfig, BoxCcgAuth, BoxClient } from 'box-typescript-sdk-gen';
273274

274275
const ccgConfig = new CcgConfig({
275276
clientId: 'YOUR_CLIENT_ID',
@@ -303,7 +304,7 @@ const client = sdk.getCCGClientForUser('USER_ID');
303304
The new SDK streamlines the process:
304305

305306
```typescript
306-
const { CcgConfig, BoxCcgAuth, BoxClient } = require('box-typescript-sdk-gen');
307+
import { CcgConfig, BoxCcgAuth, BoxClient } from 'box-typescript-sdk-gen';
307308

308309
const ccgConfig = new CcgConfig({
309310
clientId: 'YOUR_CLIENT_ID',
@@ -369,7 +370,7 @@ var authorize_url = sdk.getAuthorizeURL({
369370
The new SDK provides more flexibility:
370371

371372
```typescript
372-
const { BoxOAuth, OAuthConfig } = require('box-typescript-sdk-gen');
373+
import { BoxOAuth, OAuthConfig } from 'box-typescript-sdk-gen';
373374

374375
const config = new OAuthConfig({
375376
clientId: 'OAUTH_CLIENT_ID',
@@ -452,13 +453,9 @@ TokenStore.prototype.clear = function (callback) {
452453
The new SDK allows developers to define custom classes for token storage:
453454

454455
```typescript
455-
const { BoxOAuth } = require('box-typescript-sdk-gen');
456-
const {
457-
TokenStorage,
458-
} = require('box-typescript-sdk-gen/lib/box/tokenStorage.generated.js');
459-
const {
460-
AccessToken,
461-
} = require('box-typescript-sdk-gen/lib/schemas/accessToken.generated.js');
456+
import { BoxOAuth } from 'box-typescript-sdk-gen';
457+
import { TokenStorage } from 'box-typescript-sdk-gen/box/tokenStorage.generated';
458+
import { AccessToken } from 'box-typescript-sdk-gen/schemas/accessToken.generated';
462459

463460
class CustomTokenStorage extends TokenStorage {
464461
async store(token: AccessToken): Promise<undefined> {
@@ -647,7 +644,7 @@ as the `file` parameter, and the `fileName` and `fileSize` parameters are now pa
647644
The `parentFolderId` parameter is also required to specify the folder where the file will be uploaded.
648645

649646
```typescript
650-
import { File } from 'box-typescript-sdk-gen/lib/schemas/file.generated.js';
647+
import { File } from 'box-typescript-sdk-gen/schemas/file.generated';
651648

652649
var fileByteStream = fs.createReadStream('/path/to/file.txt');
653650
var fileName = 'new_name.txt';

0 commit comments

Comments
 (0)