Skip to content

Commit 5e6b902

Browse files
committed
Merge branch 'release/3.0.0'.
2 parents 1518262 + 00f64e2 commit 5e6b902

8 files changed

Lines changed: 297 additions & 453 deletions

File tree

package-lock.json

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "forge-nodejs-utils",
3-
"version": "2.2.0",
3+
"version": "3.0.0",
44
"description": "Tools for accessing Autodesk Forge APIs from Node.js apps.",
55
"main": "dist/index.js",
66
"scripts": {

src/authentication.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { DefaultHost, post } from './common';
1+
import * as querystring from 'querystring';
2+
import fetch from 'node-fetch';
23

34
const RootPath = `/authentication/v1`;
45

@@ -12,10 +13,8 @@ export interface ITwoLeggedToken {
1213
expires_in: number;
1314
}
1415

15-
export interface IThreeLeggedToken {
16-
access_token: string;
16+
export interface IThreeLeggedToken extends ITwoLeggedToken {
1717
refresh_token: string;
18-
expires_in: number;
1918
}
2019

2120
/**
@@ -36,13 +35,31 @@ export class AuthenticationClient {
3635
* @param {string} client_secret Forge application client secret.
3736
* @param {string} [host="https://developer.api.autodesk.com"] Forge API host.
3837
*/
39-
constructor(client_id: string, client_secret: string, host = DefaultHost) {
38+
constructor(client_id: string, client_secret: string, host: string = 'https://developer.api.autodesk.com') {
4039
this.client_id = client_id;
4140
this.client_secret = client_secret;
4241
this.host = host;
4342
this._cached = {};
4443
}
4544

45+
// Helper method for POST requests with urlencoded params
46+
protected async post(endpoint: string, params: any, headers: { [name: string]: string } = {}) {
47+
const options = {
48+
method: 'POST',
49+
headers: headers,
50+
body: querystring.stringify(params)
51+
};
52+
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
53+
const response = await fetch(this.host + RootPath + endpoint, options);
54+
if (response.ok) {
55+
const json = await response.json();
56+
return json;
57+
} else {
58+
const msg = await response.text();
59+
throw new Error(msg);
60+
}
61+
}
62+
4663
/**
4764
* Retrieves 2-legged access token for a specific set of scopes
4865
* ({@link https://forge.autodesk.com/en/docs/oauth/v2/reference/http/authenticate-POST|docs}).
@@ -75,7 +92,7 @@ export class AuthenticationClient {
7592
};
7693
const cache = this._cached[key] = {
7794
expires_at: Number.MAX_VALUE,
78-
promise: post(`${this.host}${RootPath}/authenticate`, { urlencoded: params }).then((resp) => {
95+
promise: this.post('/authenticate', params).then((resp) => {
7996
this._cached[key].expires_at = Date.now() + resp.expires_in * 1000;
8097
return resp.access_token;
8198
})
@@ -113,7 +130,7 @@ export class AuthenticationClient {
113130
'code': code,
114131
'redirect_uri': redirectUri
115132
};
116-
const token = await post(`${this.host}${RootPath}/gettoken`, { urlencoded: params });
133+
const token = await this.post(`/gettoken`, params);
117134
return token;
118135
}
119136
}

src/bim360.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { get, DefaultHost } from './common';
1+
import { Region } from './common';
2+
import { ForgeClient, IAuthOptions } from './common';
3+
4+
const ReadTokenScopes = ['data:read'];
25

36
interface IHub {
47
type: string;
@@ -44,24 +47,17 @@ interface IVersion {
4447
* Client providing access to Autodesk Forge
4548
* {@link https://forge.autodesk.com/en/docs/bim360/v1|BIM360 APIs}.
4649
*/
47-
export class BIM360Client {
48-
private token: string;
49-
private host: string;
50-
50+
export class BIM360Client extends ForgeClient {
5151
/**
5252
* Initializes new client with specific authentication method.
53-
* @param {string} token Authentication token.
53+
* @param {IAuthOptions} auth Authentication object,
54+
* containing either `client_id` and `client_secret` properties (for 2-legged authentication),
55+
* or a single `token` property (for 2-legged or 3-legged authentication with pre-generated access token).
5456
* @param {string} [host="https://developer.api.autodesk.com"] Forge API host.
57+
* @param {Region} [region="US"] Forge availability region ("US" or "EMEA").
5558
*/
56-
constructor(token: string, host = DefaultHost) {
57-
this.token = token;
58-
this.host = host;
59-
}
60-
61-
// Helper method for GET requests
62-
private async _get(endpoint: string, headers: { [name: string]: string } = {}) {
63-
headers['Authorization'] = 'Bearer ' + this.token;
64-
return get(this.host + endpoint, headers);
59+
constructor(auth: IAuthOptions, host?: string, region?: Region) {
60+
super('', auth, host, region);
6561
}
6662

6763
// Hub APIs
@@ -73,7 +69,7 @@ export class BIM360Client {
7369
* @returns {Promise<IHub[]>} List of hubs.
7470
*/
7571
async hubs(): Promise<IHub[]> {
76-
const response = await this._get(`/project/v1/hubs`);
72+
const response = await this.get(`/project/v1/hubs`, {}, ReadTokenScopes);
7773
return response.data;
7874
}
7975

@@ -84,7 +80,7 @@ export class BIM360Client {
8480
* @returns {Promise<IHub>} Hub or null if there isn't one.
8581
*/
8682
async hub(id: string): Promise<IHub> {
87-
const response = await this._get(`/project/v1/hubs/${id}`);
83+
const response = await this.get(`/project/v1/hubs/${id}`, {}, ReadTokenScopes);
8884
return response.data;
8985
}
9086

@@ -95,7 +91,7 @@ export class BIM360Client {
9591
* @returns {Promise<IProject[]>} List of projects.
9692
*/
9793
async projects(hub: string): Promise<IProject[]> {
98-
const response = await this._get(`/project/v1/hubs/${hub}/projects`);
94+
const response = await this.get(`/project/v1/hubs/${hub}/projects`, {}, ReadTokenScopes);
9995
return response.data;
10096
}
10197

@@ -107,7 +103,7 @@ export class BIM360Client {
107103
* @returns {Promise<IFolder[]>} List of folder records.
108104
*/
109105
async folders(hub: string, project: string): Promise<IFolder[]> {
110-
const response = await this._get(`/project/v1/hubs/${hub}/projects/${project}/topFolders`);
106+
const response = await this.get(`/project/v1/hubs/${hub}/projects/${project}/topFolders`, {}, ReadTokenScopes);
111107
return response.data;
112108
}
113109

@@ -119,7 +115,7 @@ export class BIM360Client {
119115
* @returns {Promise<IItem[]>} List of folder contents.
120116
*/
121117
async contents(project: string, folder: string): Promise<IItem[]> {
122-
const response = await this._get(`/data/v1/projects/${project}/folders/${folder}/contents`);
118+
const response = await this.get(`/data/v1/projects/${project}/folders/${folder}/contents`, {}, ReadTokenScopes);
123119
return response.data;
124120
}
125121

@@ -131,7 +127,7 @@ export class BIM360Client {
131127
* @returns {Promise<IVersion[]>} List of item versions.
132128
*/
133129
async versions(project: string, item: string): Promise<IVersion[]> {
134-
const response = await this._get(`/data/v1/projects/${project}/items/${item}/versions`);
130+
const response = await this.get(`/data/v1/projects/${project}/items/${item}/versions`, {}, ReadTokenScopes);
135131
return response.data;
136132
}
137133

@@ -143,7 +139,7 @@ export class BIM360Client {
143139
* @returns {Promise<IVersion>} Tip version of the item.
144140
*/
145141
async tip(project: string, item: string): Promise<IVersion> {
146-
const response = await this._get(`/data/v1/projects/${project}/items/${item}/tip`);
142+
const response = await this.get(`/data/v1/projects/${project}/items/${item}/tip`, {}, ReadTokenScopes);
147143
return response.data;
148144
}
149145
}

0 commit comments

Comments
 (0)