Skip to content

Commit

Permalink
feat: convert to typescript (okta#44)
Browse files Browse the repository at this point in the history
OKTA-323466
<<<Jenkins Check-In of Tested SHA: d24d45e for [email protected]>>>
Artifact: okta-react
  • Loading branch information
shuowu authored and eng-prod-CI-bot-okta committed Nov 20, 2020
1 parent e24844c commit 7c25dfb
Show file tree
Hide file tree
Showing 38 changed files with 752 additions and 537 deletions.
68 changes: 24 additions & 44 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@ const packageJson = require('./package.json');
const devDependencies = Object.keys(packageJson.devDependencies || {});

module.exports = {
root: true,
extends: [
'eslint:recommended',
'plugin:react/recommended',
'plugin:node/recommended-script',
'plugin:jest/recommended'
],
plugins: [
'react',
'react-hooks',
'node',
'jest'
'react-hooks'
],
rules: {
'react/prop-types': 0,
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'warn',
'semi': ['error', 'always'],
},
settings: {
react: {
Expand All @@ -29,52 +23,38 @@ module.exports = {
},
overrides: [
{
// ES6/browser processed by Babel
// NodeJS build tools
files: [
'src/**/*',
'test/jest/**/*',
'test/e2e/harness/src/**/*',
'test/e2e/harness/e2e/**/*',
'rollup.config.js'
'build.js',
'env.js',
'util/**/*',
'config-overrides.js',
'protractor.conf.js',
'.eslintrc.js',
'babel.config.js',
'jest.config.js'
],
parser: '@babel/eslint-parser',
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module'
},
plugins: ['node'],
env: {
browser: true,
es6: true,
node: false
node: true,
es6: true
},
rules: {
'node/no-unsupported-features/es-syntax': 0,
'node/no-unsupported-features/node-builtins': 0,
'node/no-unpublished-import': ['error', {
'node/no-unpublished-require': ['error', {
'allowModules': devDependencies
}]
}]
}
},
{
// Jest specs
files: ['test/jest/**/*'],
env: {
jest: true
// rollup.config.js
files: ['rollup.config.js'],
parserOptions: {
sourceType: 'module',
ecmaVersion: 2020
},
rules: {
'node/no-unpublished-import': ['error', {
'allowModules': devDependencies
}]
}
},
{
// NodeJS build tools
files: ['build.js', 'env.js', 'util/**/*'],
rules: {
'node/no-unpublished-require': ['error', {
'allowModules': devDependencies
}]
env: {
node: true
}
}
]
};
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ testenv
/test-reports
.yalc
yalc.lock
.yarnrc
.yarn
12 changes: 10 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ module.exports = {
'./src/**',
'!./test/**'
],
globals: {
'ts-jest': {
diagnostics: {
warnOnly: true
}
}
},
reporters: [
'default',
'jest-junit'
Expand All @@ -19,7 +26,8 @@ module.exports = {
'./test/jest'
],
setupFiles: [
'./test/jest/setup.js'
'./test/jest/setup.ts'
],
testEnvironment: 'jsdom'
testEnvironment: 'jsdom',
transform: { '^.+\\.tsx?$': 'ts-jest' }
};
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
],
"main": "dist/bundles/okta-react.cjs.js",
"module": "dist/bundles/okta-react.esm.js",
"types": "dist/bundles/types",
"author": "",
"license": "Apache-2.0",
"bugs": {
Expand All @@ -42,6 +43,7 @@
},
"peerDependencies": {
"@okta/okta-auth-js": "^4.1.0",
"@types/react-router-dom": "^5.1.6",
"react": ">=16.8.0",
"react-dom": ">=16.8.0",
"react-router-dom": ">=5.1.0"
Expand All @@ -58,6 +60,12 @@
"@okta/okta-auth-js": "^4.1.2",
"@rollup/plugin-babel": "^5.2.1",
"@rollup/plugin-replace": "^2.3.4",
"@types/enzyme": "^3.10.8",
"@types/enzyme-adapter-react-16": "^1.0.6",
"@types/jest": "^26.0.15",
"@types/react-router-dom": "^5.1.6",
"@typescript-eslint/eslint-plugin": "^4.8.1",
"@typescript-eslint/parser": "^4.8.1",
"axios": "^0.21.0",
"babel-jest": "^26.6.3",
"chalk": "^4.1.0",
Expand Down Expand Up @@ -85,7 +93,10 @@
"rollup": "^2.33.1",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-terser": "^7.0.2",
"shelljs": "^0.8.4"
"rollup-plugin-typescript2": "^0.29.0",
"shelljs": "^0.8.4",
"ts-jest": "^26.4.4",
"typescript": "^4.0.5"
},
"jest-junit": {
"outputDirectory": "./test-reports/unit/",
Expand Down
43 changes: 29 additions & 14 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,59 @@ import babel from '@rollup/plugin-babel';
import replace from '@rollup/plugin-replace';
import cleanup from 'rollup-plugin-cleanup';
import { terser } from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript2';
import pkg from "./package.json";

require('./env'); // set variables in process.env

const external = [
...Object.keys(pkg.peerDependencies || {}),
...Object.keys(pkg.dependencies || {}),
];
const makeExternalPredicate = () => {
const externalArr = [
...Object.keys(pkg.peerDependencies || {}),
...Object.keys(pkg.dependencies || {}),
];

const makeExternalPredicate = externalArr => {
if (externalArr.length === 0) {
return () => false;
}
const pattern = new RegExp(`^(${externalArr.join("|")})($|/)`);
return id => pattern.test(id);
};

const extensions = ['js', 'jsx', 'ts', 'tsx'];

const input = 'src/index.ts';
const external = makeExternalPredicate();
const commonPlugins = [
typescript({
// eslint-disable-next-line node/no-unpublished-require
typescript: require('typescript'),
useTsconfigDeclarationDir: true
}),
replace({
'process.env.PACKAGE_NAME': JSON.stringify(process.env.PACKAGE_NAME),
'process.env.PACKAGE_VERSION': JSON.stringify(process.env.PACKAGE_VERSION)
}),
cleanup()
cleanup({
extensions,
comments: 'none'
})
];

export default [
{
input: 'src/index.js',
external: makeExternalPredicate(external),
input,
external,
plugins: [
...commonPlugins,
babel({
babelrc: false,
babelHelpers: 'bundled',
presets: [
'@babel/preset-env',
'@babel/preset-react'
]
],
extensions
}),
...commonPlugins,
terser()
],
output: {
Expand All @@ -57,9 +71,10 @@ export default [
}
},
{
input: 'src/index.js',
external: makeExternalPredicate(external),
input: 'src/index.ts',
external,
plugins: [
...commonPlugins,
babel({
babelHelpers: 'runtime',
presets: [
Expand All @@ -68,9 +83,9 @@ export default [
],
plugins: [
'@babel/plugin-transform-runtime'
]
],
extensions
}),
...commonPlugins
],
output: [
{
Expand Down
11 changes: 11 additions & 0 deletions src/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module'
},
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended'
]
}
10 changes: 6 additions & 4 deletions src/LoginCallback.js → src/LoginCallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
* See the License for the specific language governing permissions and limitations under the License.
*/

import React, { useEffect } from 'react';
import * as React from 'react';
import { useOktaAuth } from './OktaContext';
import OktaError from './OktaError';

const LoginCallback = ({ errorComponent }) => {
const LoginCallback: React.FC<{
errorComponent?: React.ComponentType<{ error: Error }>
}> = ({ errorComponent }) => {
const { oktaAuth, authState } = useOktaAuth();
const authStateReady = !authState.isPending;

let ErrorReporter = errorComponent || OktaError;
const ErrorReporter = errorComponent || OktaError;

useEffect(() => {
React.useEffect(() => {
oktaAuth.handleLoginRedirect();
}, [oktaAuth]);

Expand Down
15 changes: 12 additions & 3 deletions src/OktaContext.js → src/OktaContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@
*
* See the License for the specific language governing permissions and limitations under the License.
*/
import React, { useContext } from 'react';
import * as React from 'react';
import { AuthState, OktaAuth } from '@okta/okta-auth-js';

const OktaContext = React.createContext();
export type OnAuthRequiredFunction = (oktaAuth: OktaAuth) => Promise<void>;

export const useOktaAuth = () => useContext(OktaContext);
export interface IOktaContext {
oktaAuth: OktaAuth;
authState: AuthState;
_onAuthRequired: OnAuthRequiredFunction;
}

const OktaContext = React.createContext<IOktaContext | null>(null);

export const useOktaAuth = (): IOktaContext => React.useContext(OktaContext);

export default OktaContext;
4 changes: 2 additions & 2 deletions src/OktaError.js → src/OktaError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
* See the License for the specific language governing permissions and limitations under the License.
*/

import React from 'react';
import * as React from 'react';

const OktaError = ({ error }) => {
const OktaError: React.FC<{ error: Error }> = ({ error }) => {
if(error.name && error.message) {
return <p>{error.name}: {error.message}</p>;
}
Expand Down
Loading

0 comments on commit 7c25dfb

Please sign in to comment.