From 7ae2147007db5a82a43786604d6f0c2def20b2ff Mon Sep 17 00:00:00 2001 From: Sassoun Derderian Date: Mon, 21 Feb 2022 17:55:32 +0400 Subject: [PATCH] fix: exports Api and NodeApi for NodeJS Api === NodeApi --- README.md | 2 +- package.json | 2 +- src/node.ts | 12 ++++++++---- test/Api.spec.ts | 10 +++++----- test/module.spec.ts | 18 ++++++++++++++++++ 5 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 test/module.spec.ts diff --git a/README.md b/README.md index 35caf030..6264b222 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ const Workfront = require('workfront-api') /** * The console.log statement below will output the following: * { - * NodeApi: [Function: Api], + * Api: [Function: Api], * ResponseHandler: { success: [Function: success], failure: [Function: failure] } * } */ diff --git a/package.json b/package.json index d3e3bd05..5c082aeb 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "typings" ], "sideEffects": false, - "typings": "typings/Api.d.ts", + "typings": "typings/node.d.ts", "dependencies": { "@types/node": "^17.0.8", "form-data": "^4.0.0", diff --git a/src/node.ts b/src/node.ts index 287f93e8..d4523ba3 100644 --- a/src/node.ts +++ b/src/node.ts @@ -16,7 +16,7 @@ import * as NodeFormData from 'form-data' import 'isomorphic-fetch' import {Readable} from 'stream' -import {Api as BaseApi, makeFetchCall} from './Api' +import {Api as BaseApi, makeFetchCall, ResponseHandler} from './Api' /** * Starting from version 2.0 API allows users to upload files. @@ -29,7 +29,7 @@ import {Api as BaseApi, makeFetchCall} from './Api' * @param {fs.ReadStream} stream A readable stream with file contents * @param {String} filename Override the filename */ -export class NodeApi extends BaseApi { +class NodeApi extends BaseApi { constructor(options) { super(options) } @@ -45,6 +45,10 @@ export class NodeApi extends BaseApi { } } -export {ResponseHandler} from './Api' +export {ResponseHandler, NodeApi, NodeApi as Api, makeFetchCall} -export {NodeApi as Api} +export default { + NodeApi, + ResponseHandler, + Api: NodeApi, +} diff --git a/test/Api.spec.ts b/test/Api.spec.ts index a94415ff..413d3cc8 100644 --- a/test/Api.spec.ts +++ b/test/Api.spec.ts @@ -15,11 +15,11 @@ */ import * as should from 'should' -import {NodeApi} from '../src/node' +import {Api} from '../' describe('Create new instance for API', function () { it('should have methods', function () { - const api = new NodeApi({url: 'http://localhost'}) + const api = new Api({url: 'http://localhost'}) should(api.copy).be.a.Function().and.has.lengthOf(5) should(api.count).be.a.Function().and.has.lengthOf(2) should(api.create).be.a.Function().and.has.lengthOf(3) @@ -41,17 +41,17 @@ describe('Create new instance for API', function () { }) it('should set correct API path based on passed configuration (version is passed)', function () { - const api = new NodeApi({url: 'http://localhost', version: '2.0'}) + const api = new Api({url: 'http://localhost', version: '2.0'}) should(api._httpOptions.path).equal('/attask/api/v2.0') }) it('should set correct API path based on passed configuration (version is not passed)', function () { - const api = new NodeApi({url: 'http://localhost'}) + const api = new Api({url: 'http://localhost'}) should(api._httpOptions.path).equal('/attask/api-internal') }) it('should set correct API path based on passed configuration (version="asp")', function () { - const api = new NodeApi({url: 'http://localhost', version: 'asp'}) + const api = new Api({url: 'http://localhost', version: 'asp'}) should(api._httpOptions.path).equal('/attask/api-asp') }) }) diff --git a/test/module.spec.ts b/test/module.spec.ts new file mode 100644 index 00000000..7f5a6785 --- /dev/null +++ b/test/module.spec.ts @@ -0,0 +1,18 @@ +import * as should from 'should' +import * as cjsModule from '../' + +describe('Verify workfront-api cjs exported members', function () { + it('exports cjs required members', function () { + should(cjsModule).properties('Api', 'ResponseHandler', 'makeFetchCall') + }) + it('has NodeApi as Api fallback', function () { + should(cjsModule).properties('Api', 'NodeApi') + should(cjsModule.Api).equals(cjsModule.NodeApi) + }) + it('exports "default" for backward compatibility', function () { + should(cjsModule).properties('default') + should(cjsModule.default).property('Api', cjsModule.Api) + should(cjsModule.default).property('NodeApi', cjsModule.NodeApi) + should(cjsModule.default).property('ResponseHandler', cjsModule.ResponseHandler) + }) +})