Skip to content

Commit

Permalink
add templates and helpers for types generation
Browse files Browse the repository at this point in the history
add generated models and factories types (#225)

* WIP on generating TS type declarations for operations

* add templates for type files and add template helper functions

* move typings to src/types

* re-use signature getter for models and generated client

* add types index and remove unused templates

* unify export syntax

* reduce blocklisted models filtering duplication

* export model types

* add factories templates and push generated model types

* switch to named model exports

* actually run eslint on templates dir

* set proper return type for Collection and mark optional arguments

* mark read-only properties

* do default export models along with named export

* expose Client type and add missing types

* add re-generated models and remove manually-generated types

* restore array type value and read-only modifier

* bring models named export back

* generate factories types

* export GeneratedClient
* to be switched to Client in the downnstream branch

* specify correct types for Model constructor arguments

* import Client for all models

* remove default exports from models

* remove models and factories index template

* add extra newline for generated models, remove indents from type map, do not re-export model types

* move eslint target for templates to downstream branch

* add missing 'autogenerated' warning banner to templates

* remove extra whitespaces, regenerate files

* parametrize Collection return type in generated files

* include generic type into imports list and re-generate types

* preserve non-optinal query parameters

* correct return type to be Response instead of undefined, include CRUD return type into imports

* CR: remove copyright banners from templates
Add type definitions for core objects, add typescript-eslint-plugin + tsd (re-opened) (#230)

* WIP on generating TS type declarations for operations

* WIP on adding types for core objects

* update type references in core objects

* WIP on adding types for core objects

* update type references in core objects

* run typescript-eslint on src/types

* WIP on fixing ts-lint errors

* represent object type as Record<string, unknown>

* define RequestOptions type

* define more specific type for core objects

* add basic tsd test

* install @types/node-fetch types

* specify sourceType: module for types eslint config

* add more tsd assertions

* complete basic coverage for core object types assertions

* include tsd run into test target

* export types matching code exports

* fix syntactic error in command

* fix indents and apply basic eslint rules to definitions files

* parametrize Collection type definition

* list known config properties, update jwt function return type

* widen type for cache middleware next function

* replace MemoryStore references with CacheStorage

* extract IRequestExecutor interface, make MemoryStore parameters optional

* allow request executors w/o event emission

* correct types for collection subscribers

* CR: cascade eslint configuration

* fix revealed eslint errors

* move type tests to a separate directory

* CR: break down type tests

* add type assertions for generated client methods

* use named import for Response

* allow imports for create and delete operations only

* remove unused import

* fix rebase artifact
remove irrelevant changes
update changelog entry for 4.5.0

OKTA-373751
<<<Jenkins Check-In of Tested SHA: 1013098 for [email protected]>>>
Artifact: okta-sdk-nodejs
  • Loading branch information
oleksandrpravosudko-okta authored and eng-prod-CI-bot-okta committed Mar 16, 2021
1 parent dc5283c commit 6714bc4
Show file tree
Hide file tree
Showing 385 changed files with 13,462 additions and 270 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/jest/coverage/
12 changes: 2 additions & 10 deletions src/.eslintrc → .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"parserOptions": {
"parserOptions": {
"ecmaVersion": 2017
},
"rules": {
Expand Down Expand Up @@ -68,17 +68,9 @@
"space-in-parens": [2, "never"],
"space-infix-ops": 2
},
"globals": {
"after": true,
"afterEach": true,
"before": true,
"beforeEach": true,
"describe": true,
"it": true
},
"env": {
"es6": true,
"node": true
},
"extends": "eslint:recommended"
}
}
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## 4.5.0

- [#220](https://github.com/okta/okta-sdk-nodejs/pull/220) Rename private instance variable
### Features

- [#231](https://github.com/okta/okta-sdk-nodejs/pull/231) Adds TypeScript type definitions

## 4.4.0

Expand Down
20 changes: 15 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,28 @@
"README.md"
],
"main": "src/index.js",
"types": "src/types/index.d.ts",
"scripts": {
"banners": "./utils/maintain-banners.js",
"prebuild": "rimraf ./src/models ./src/factories ./src/generated-client.js",
"build": "okta-sdk-generator -t templates/ -o .",
"eslint": "eslint -c src/.eslintrc src/ && eslint -c test/.eslintrc test/it test/unit && eslint -c test/jest/.eslintrc test/jest/ && eslint -c src/.eslintrc utils/",
"build": "okta-sdk-generator -t templates/ -o . && yarn banners",
"eslint": "eslint .",
"jest": "JEST_JUNIT_OUTPUT_DIR=./test-reports jest --coverage --ci --testResultsProcessor=jest-junit test/jest/*.js",
"predocs": "rimraf ./jsdocs && mkdir jsdocs/ && ./utils/make-jsdoc-readme.js > ./jsdocs/jsdoc-temp.md",
"docs": "./node_modules/.bin/jsdoc src/ -c ./docs/config.json -d ./jsdocs/ -P ./package.json -R ./jsdocs/jsdoc-temp.md -r",
"test:integration": "yarn test:integration:oauth && yarn test:integration:ssws",
"test:integration:ssws": "MOCHA_FILE=./test-reports/it-results.xml OKTA_CLIENT_AUTHORIZATIONMODE=SSWS nyc --reporter=text --reporter=html mocha --reporter=mocha-junit-reporter --reporter-options toConsole=true --retries 2 test/it/*.js --no-timeouts",
"test:integration:oauth": "OKTA_CLIENT_AUTHORIZATIONMODE=PrivateKey nyc --reporter=text --reporter=html mocha test/it/user-get.js --no-timeouts",
"test:unit": "MOCHA_FILE=./test-reports/junit-results.xml nyc --reporter=text --reporter=html mocha --reporter=mocha-junit-reporter test/unit/*.js --no-timeouts",
"test": "npm run eslint && npm run test:unit && npm run test:integration && npm run jest",
"test:types": "tsd",
"test": "yarn eslint && yarn test:types && yarn test:unit && yarn test:integration && yarn jest",
"aftertest": "mocha test/delete-resources.js --no-timeouts"
},
"keywords": [],
"license": "Apache-2.0",
"repository": "https://github.com/okta/okta-sdk-nodejs",
"dependencies": {
"@types/node-fetch": "^2.5.8",
"deep-copy": "^1.4.2",
"isomorphic-fetch": "^3.0.0",
"js-yaml": "^3.14.1",
Expand All @@ -41,8 +44,10 @@
},
"devDependencies": {
"@okta/openapi": "^2.1.6",
"@typescript-eslint/eslint-plugin": "^4.15.2",
"@typescript-eslint/parser": "^4.15.2",
"chai": "^4.2.0",
"eslint": "^7.15.0",
"eslint": "^7.20.0",
"eslint-plugin-jest": "^24.1.3",
"fake-fs": "^0.5.0",
"faker": "^5.1.0",
Expand All @@ -56,13 +61,18 @@
"nyc": "^15.1.0",
"rimraf": "^3.0.2",
"sinon": "^9.2.1",
"speakeasy": "^2.0.0"
"speakeasy": "^2.0.0",
"tsd": "^0.14.0",
"typescript": "^4.2.2"
},
"jest": {
"restoreMocks": true,
"rootDir": "./test/jest",
"setupFiles": [
"jest-date-mock"
]
},
"tsd": {
"directory": "test/type"
}
}
469 changes: 469 additions & 0 deletions src/generated-client.d.ts

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions src/types/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"new-cap": [
2,
{
"capIsNew": false,
"properties": false
}
]
},
"plugins": [
"@typescript-eslint"
],
"extends": [
"plugin:@typescript-eslint/recommended"
]
}
30 changes: 30 additions & 0 deletions src/types/api-error.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

/*!
* Copyright (c) 2017-2021, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/
import { Headers } from 'node-fetch';

declare class OktaApiError extends Error {
constructor(url: string, status: number, responseBody: Record<string, unknown>, headers: Headers);
name: string;
status: number;
errorCode: string | number;
errorSummary: string;
errorCauses: string;
errorLink: string;
errorId: string;
url: string;
headers: Headers;
stack: string;
message: string;
}

export default OktaApiError;
46 changes: 46 additions & 0 deletions src/types/client.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*!
* Copyright (c) 2017-2021, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/

import GeneratedApiClient from './generated-client';
import Oauth from './oauth';
import Http from './http';
import { IRequestExecutor } from './request-executor';
import defaultCacheMiddleware from './default-cache-middleware';
import { CacheStorage } from './memory-store';

interface ConfigProperties {
orgUrl?: string,
token?: string,
clientId?: string,
scopes?: string[],
requestExecutor?: IRequestExecutor,
authorizationMode?: string,
privateKey?: string | Record<string, unknown>
cacheStore?: CacheStorage,
cacheMiddleware?: typeof defaultCacheMiddleware | unknown
}

declare class Client extends GeneratedApiClient {
constructor(config?: ConfigProperties);

requestExecutor: IRequestExecutor;
authorizationMode: string;
baseUrl: string;
apiToken: string;
clientId: string;
scopes: string[];
privateKey: string;
oauth: Oauth;
http: Http;
}

export default Client;
48 changes: 48 additions & 0 deletions src/types/collection.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*!
* Copyright (c) 2017-2021, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/

import RequestOptions from './request-options';
import ModelFactory from './model-factory';
import ModelResolutionFactory from './resolution-factory';
import Client from './client';

declare class Collection<T> {
constructor(client: Client, uri: string, factory: ModelFactory | ModelResolutionFactory, request?: RequestOptions);

nextUri: string;
client: Client;
factory: ModelFactory | ModelResolutionFactory;
currentItems: Record<string, unknown>[];
request: RequestOptions;
next(): Promise<{
done: boolean,
value: T | null
}>;
[Symbol.asyncIterator](): {
next: () => Promise<{
done: boolean,
value: T | null
}>;
};
getNextPage(): Promise<Record<string, unknown>>;
each(iterator: (item: T) => Promise<unknown> | boolean | unknown): Promise<unknown>;
subscribe(config: {
interval: number;
next: (item: T) => unknown | Promise<unknown>;
error: (e: Error) => unknown | Promise<unknown>;
complete: () => void;
}): {
unsubscribe(): void;
};
}

export default Collection;
31 changes: 31 additions & 0 deletions src/types/config-loader.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*!
* Copyright (c) 2017-2021, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/

declare class ConfigLoader {
prefix: string;
config: {
client: {
authorizationMode: string;
orgUrl: string;
token: string;
clientId: string;
scopes: string;
privateKey: string;
};
};
applyDefaults(): void;
applyEnvVars(): void;
applyYamlFile(path: string): void;
apply(config: Record<string, unknown>): void;
}

export default ConfigLoader;
25 changes: 25 additions & 0 deletions src/types/default-cache-middleware.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*!
* Copyright (c) 2017-2021, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/

import { Response } from 'node-fetch';
import { CacheStorage } from './memory-store';
import RequestOptions from './request-options';

declare function defaultCacheMiddleware(ctx: {
isCollection?: boolean,
resources?: string[],
res?: Response,
req: RequestOptions,
cacheStore: CacheStorage,
}, next: () => unknown): Promise<Response>;

export default defaultCacheMiddleware;
39 changes: 39 additions & 0 deletions src/types/default-request-executor.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*!
* Copyright (c) 2017-2021, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/

import { Response } from 'node-fetch';
import RequestExecutor from './request-executor';
import RequestOptions from './request-options';


declare class DefaultRequestExecutor extends RequestExecutor {
constructor(config?: {
maxRetries: number,
requestTimeout: number,
});

requestTimeout: number;
maxRetries: number;
retryCountHeader: string;
retryForHeader: string;
buildRetryRequest(request: RequestOptions, requestId: string, delayMs: number): RequestOptions;
validateRetryResponseHeaders(response: Response): boolean;
getOktaRequestId(response: Response): string;
getRateLimitReset(response: Response): string;
getResponseDate(response: Response): string;
getRetryDelayMs(response: Response): number;
parseResponse(request: RequestOptions, response: Response): Response | Promise<Response | Error>;
maxRetriesReached(request: RequestOptions): boolean;
retryRequest(request: RequestOptions, response: Response, delayMs: number): Promise<Response>;
}

export default DefaultRequestExecutor;
21 changes: 21 additions & 0 deletions src/types/factories/ApplicationFactory.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*!
* Copyright (c) 2017-2021, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/


/* THIS FILE IS AUTO-GENERATED - SEE CONTRIBUTOR DOCUMENTATION */

import ModelResolutionFactory from '../resolution-factory';

declare class ApplicationFactory extends ModelResolutionFactory {
}

export default ApplicationFactory;
21 changes: 21 additions & 0 deletions src/types/factories/BrowserPluginApplicationFactory.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*!
* Copyright (c) 2017-2021, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/


/* THIS FILE IS AUTO-GENERATED - SEE CONTRIBUTOR DOCUMENTATION */

import ModelResolutionFactory from '../resolution-factory';

declare class BrowserPluginApplicationFactory extends ModelResolutionFactory {
}

export default BrowserPluginApplicationFactory;
Loading

0 comments on commit 6714bc4

Please sign in to comment.