Skip to content

Commit

Permalink
feat(core): provide a lightweight and simple way to configure SDK (#1)
Browse files Browse the repository at this point in the history
closes #4
  • Loading branch information
RomanReznichenko authored and derevnjuk committed Apr 1, 2022
1 parent 389af93 commit 5aed5cf
Show file tree
Hide file tree
Showing 27 changed files with 798 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
"import/no-extraneous-dependencies": [
"error",
{
"packageDir": ".",
"devDependencies": false,
"optionalDependencies": false,
"peerDependencies": false
Expand Down Expand Up @@ -258,6 +259,7 @@
"import/no-extraneous-dependencies": [
"error",
{
"packageDir": ".",
"devDependencies": true
}
]
Expand Down
79 changes: 63 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@
},
"homepage": "https://github.com/NeuraLegion/secbox-sdk-js#readme",
"dependencies": {
"tslib": "~2.3.1"
"reflect-metadata": "^0.1.13",
"tslib": "~2.3.1",
"tsyringe": "^4.6.0"
},
"devDependencies": {
"@commitlint/cli": "^16.2.1",
Expand Down Expand Up @@ -110,6 +112,7 @@
"prettier": "2.6.0",
"semantic-release": "~19.0.2",
"ts-jest": "^27.1.3",
"ts-mockito": "^2.6.1",
"typescript": "~4.5.2"
}
}
3 changes: 3 additions & 0 deletions packages/core/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["@nrwl/web/babel", { "useBuiltIns": "usage" }]]
}
21 changes: 21 additions & 0 deletions packages/core/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"parserOptions": {
"project": ["packages/core/tsconfig.*?.json"]
},
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
111 changes: 111 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# @secbox/core

The core package can be used to obtain a config including credentials from different sources, and provide a simplified abstraction to handle events and commands.

## Setup

```bash
npm i -s @secbox/core
```

## Usage

### Configuration

First, you need to generate a new instance of `Configuration`.

```ts
import { Configuration } from '@secbox/core';

const config = new Configuration({
cluster: 'app.neuralegion.com',
credentials: {
token: 'your API key'
}
});
```

After that, you can resolve the configuration using the IoC container.

```ts
const config = config.container.resolve(Configuration);
```

#### Options

Configuration can be customized using the following options:

```ts
export interface ConfigurationOptions {
cluster: string;
credentials?: Credentials;
credentialProviders?: CredentialProvider[];
}
```

The default configuration is as follows:

```js
{
credentialProviders: [new EnvCredentialProvider()];
}
```

#### cluster

- type: `string`

Set the application name (domain name), that is used to establish connection with. By default, the option is equal to `app.neuralegion.com`.

```ts
import { Configuration } from '@secbox/core';

const config = new Configuration({
cluster: 'app.neuralegion.com'
});
```

#### credentials

- type: `Credentials`

Set credentials to access the application.

```ts
import { Configuration } from '@secbox/core';

const config = new Configuration({
credentials: {
token: 'your API key'
}
});
```

More info about [setting up an API key](https://docs.neuralegion.com/docs/manage-your-organization#manage-organization-apicli-authentication-tokens)

#### credentialProviders

- type: `CredentialProvider[]`

Allows you to provide credentials and load it in runtime. The configuration will invoke one provider at a time and only continue to the next if no credentials have been located. For example, if the process finds values defined via the `BRIGHT_TOKEN` environment variables, the file at `.secboxrc` will not be read.

#### EnvCredentialProvider

Use this provider to read credentials from the following environment variable: `BRIGHT_TOKEN`

If the `BRIGHT_TOKEN` environment variable is not set or contains a falsy value, it will return undefined.

```ts
import { Configuration, EnvCredentialProvider } from '@secbox/core';

const credentialsProvider = new EnvCredentialProvider();
const config = new Configuration({
credentialProviders: [credentialsProvider]
});
```

## License

Copyright © 2022 [NeuraLegion](https://github.com/NeuraLegion).

This project is licensed under the MIT License - see the [LICENSE file](LICENSE) for details.
15 changes: 15 additions & 0 deletions packages/core/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
displayName: 'core',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json'
}
},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]sx?$': 'ts-jest'
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/packages/core'
};
4 changes: 4 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@secbox/core",
"version": "0.0.1"
}
40 changes: 40 additions & 0 deletions packages/core/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"root": "packages/core",
"sourceRoot": "packages/core/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nrwl/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/core",
"tsConfig": "packages/core/tsconfig.lib.json",
"packageJson": "packages/core/package.json",
"main": "packages/core/src/index.ts",
"assets": ["packages/core/*.md"]
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/core/**/*.ts"]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["coverage/packages/core"],
"options": {
"jestConfig": "packages/core/jest.config.js",
"passWithNoTests": true
}
},
"publish": {
"executor": "./tools/executors:publish",
"options": {
"dist": "dist/packages/core"
}
}
},
"tags": []
}
Loading

0 comments on commit 5aed5cf

Please sign in to comment.