Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add typescript declarations #413

Merged
merged 4 commits into from
Jul 29, 2020
Merged

Conversation

aarongranick-okta
Copy link
Contributor

@aarongranick-okta aarongranick-okta commented Jul 10, 2020

The main visible change from this refactor is that the module uses named exports instead of a default export. This allows users to import the main OktaAuth class but also any exported types.

@aarongranick-okta aarongranick-okta force-pushed the ag-ts-4.0-OKTA-241766 branch 3 times, most recently from 91a192b to 2bf1ec1 Compare July 23, 2020 07:00
@aarongranick-okta aarongranick-okta marked this pull request as ready for review July 23, 2020 07:32
export { OktaAuthBrowser as OktaAuth };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: some syntax sugar, export { default as OktaAuth } from './browser';

lib/clock.ts Outdated
}

// factory method. Create an instance of a clock from current context.
SdkClock.create = function(/* sdk, options */) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep it static in class?

constructor(err, xhr = null) {
const message = err.errorSummary;
super(message); // 'Error' breaks prototype chain here
Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what's the issue with extending the Error class, but looks like there is a hack needed to make it work. Wondering if we can use any babel plugin to handle it or move the logic to a BaseError class to make future changes easier?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A search for "typescript extend error prototype" will show more information on the issue, here is an example that also shows this solution: https://stackoverflow.com/questions/41102060/typescript-extending-error-class

A base CustomError class could possibly make this slightly simpler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll vote for a base error class, also I think it would be better to add the StackOverflow link in code.

lib/errors/AuthApiError.js Outdated Show resolved Hide resolved
export { OktaAuthNode as OktaAuth };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: export { default as OktaAuth } from './server';

import { getUserAgent } from '../builderUtil';
import serverStorage from './serverStorage';
import PACKAGE_JSON from '../../package.json';
const PACKAGE_JSON = require('../../package.json');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import * as PACKAGE_JSON from '../../package.json' may work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, the issue is that importing a JSON file via typescript requires the esModuleInterop flag to be turned on. I specifically want this flag off because if we have it on the requirement of using this flag is passed along to other users of our ES module, such as okta-angular.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the same issue for node-cache?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, node-cache is a commonJS module. It can only be imported by using the allowSyntheticDefaultImports and esModuleInterop flags.

@@ -11,26 +11,15 @@ var commonConfig = require('./webpack.common.config');
var license = fs.readFileSync('lib/license-header.txt', 'utf8');

module.exports = _.extend({}, _.cloneDeep(commonConfig), {
entry: './lib/browser/index.js',
mode: 'production',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah, I was meaning this flag in the last review.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes webpack was upgraded to version 4 in this PR

@shuowu
Copy link
Contributor

shuowu commented Jul 24, 2020

Great progress with Typescript! Several comments here:

  1. I saw some import statements change in tests, they all look good to me, just want to be careful that we are not introducing breaking changes for downstream SDKs/Apps.
  2. Some files have been converted to ts, but some are not. What the rule you followed to convert files?
  3. Tests and exported packages from package.json both look good. Types also should be fine. That's the list I can think of, just want to make sure we covered everything before release.

@aarongranick-okta
Copy link
Contributor Author

Great progress with Typescript! Several comments here:

  1. I saw some import statements change in tests, they all look good to me, just want to be careful that we are not introducing breaking changes for downstream SDKs/Apps.

There is a breaking change. We have switched from a default export to named exports. So commonJS code like

var OktaAuth = require('@okta/okta-auth-js') must now be something like
var OktaAuth = require('@okta/okta-auth-js').OktaAuth.

Similarly imports of
import OktaAuth = from '@okta/okta-auth-js' must now use
import { OktaAuth } = from '@okta/okta-auth-js'.

This is necessary to provide types as separate named exports, and to enable tree-shaking.

  1. Some files have been converted to ts, but some are not. What the rule you followed to convert files?

Files which are used by NodeJS are retaining ES5 compatibility. This is not code in our library, but as part of config, test runners or dev servers, etc. Eventually we will probably convert all library code to .ts. However, the .js files are being processed by Typescript compiler. The rule is we must convert to .ts as soon as we use some Typescript syntax, such as return value or optional params. Since I will be addressing other comments, I may just finish conversion of the remaining files under /lib in this PR.

The unit tests, as well, would have some value in being converted to .ts as we would get some extra type checking. However I did not want to change the unit tests in this PR, Since this is a large refactor, I am counting on the unit tests to sanity check that we haven't broken anything.

I did convert the E2E test app to Typescript, since this validates that our exported types are consumable from another app.

@aarongranick-okta
Copy link
Contributor Author

@shuowu feedback addressed, also converted remaining files under lib/ to .ts extension

@shuowu
Copy link
Contributor

shuowu commented Jul 24, 2020

LGTM!

@@ -16,6 +19,14 @@
"setInterval": true,
"clearInterval": true
},
"settings": {
"jsdoc": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have updates to README or CONTRIBUTING regarding the typescript? Is the build/inclusion process any different?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. There should be some updates to the README. Maybe a "migrating from 3.x" section. I will add it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@swiftone Please take a look at changes added to README

README.md Outdated
@@ -24,10 +25,13 @@ You can learn more on the [Okta + JavaScript][lang-landing] page in our document

This library uses semantic versioning and follows Okta's [library version policy](https://developer.okta.com/code/library-versions/).

## Release Status

:heavy_check_mark: The current stable major version series is: `3.x`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this line

README.md Outdated
:heavy_check_mark: The current stable major version series is: `3.x`

| Version | Status |
| ------- | -------------------------------- |
| `4.x` | :fire: Latest release |
| `3.x` | :heavy_check_mark: Stable |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't 3.x get a retirement date in ~9 months?

* Now using named exports. You should change code like

```javascript
import OktaAuth from '@okta/okta-auth-js'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps put a comment in, like: // previous 3.x way - people will skim for the code blocks and may not notice that this is the "old" block, not the "new" block

README.md Outdated
const OktaAuth = require('@okta/okta-auth-js').OktaAuth;
```

* Typescript definitions are now included. If you were providing your own definitions for `OktaAuth` you should remove these in favor of the types exported by this library.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put in a mention that these are optional to reassure people who don't want TS

Copy link
Contributor

@swiftone swiftone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits on the README

@aarongranick-okta aarongranick-okta merged commit b491c64 into dev-4.0 Jul 29, 2020
@aarongranick-okta aarongranick-okta deleted the ag-ts-4.0-OKTA-241766 branch July 29, 2020 02:49
aarongranick-okta added a commit that referenced this pull request Aug 1, 2020
* feat: support typescript, use named exports
aarongranick-okta added a commit that referenced this pull request Aug 1, 2020
* feat: support typescript, use named exports
aarongranick-okta added a commit that referenced this pull request Aug 1, 2020
* feat: support typescript, use named exports
aarongranick-okta added a commit that referenced this pull request Aug 11, 2020
* feat: support typescript, use named exports
aarongranick-okta added a commit that referenced this pull request Aug 11, 2020
* feat: support typescript, use named exports
aarongranick-okta added a commit that referenced this pull request Aug 12, 2020
* feat: support typescript, use named exports
aarongranick-okta added a commit that referenced this pull request Aug 12, 2020
* feat: support typescript, use named exports
aarongranick-okta added a commit that referenced this pull request Aug 14, 2020
* bump version to 4.0.0

* ES6 Refactor (#388)

* move module from packages to repo root

* rename browser/server index

* update eslint, use ecmaVersion 2020

* feat: add typescript declarations (#413)

* feat: support typescript, use named exports

* removes "onSessionExpired" option (#444)

implements active autoRenew

* Builds to "build" folder, CDN content in "build/dist"

* Fixes compatibility with CDN / static app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants