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

Feature/env #109

Merged
merged 20 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1adca59
feat(env): new `@Env` parameter decorator to fetch cds env directly i…
dragolea Nov 12, 2024
6e47d9c
chore(commitlint): removed mandatory scopes
dragolea Nov 12, 2024
0f91921
chore(deps): updated package.json versions
dragolea Nov 12, 2024
a9ee53d
chore(lint): added `dist` and `@dispatcher` folder to lint ignore
dragolea Nov 12, 2024
fbb4a72
chore(test): test workflow now tests the newly `@Env` decorator on al…
dragolea Nov 12, 2024
da9cf33
feat(postinstall): `npm install` will generate `@dispatcher` folder w…
dragolea Nov 12, 2024
5a089f9
chore(test): updated tests for the newly `@Env` decorator
dragolea Nov 12, 2024
5e0817b
chore(readme): updated readme with the usage of the new `@Env` decorator
dragolea Nov 12, 2024
a86d262
chore(gitignore): updated .gitignore
dragolea Nov 12, 2024
17e5b8f
ci: version bump to 3.2.0
gh-action-bump-version Nov 12, 2024
cb454d7
fix(tests): fixed tests by adding a `npm run build` before testing th…
dragolea Nov 12, 2024
9c72976
fix(tests): added `npm run build` before e2e tests
dragolea Nov 12, 2024
5cd0dcc
chore(deps): updated package lock
dragolea Nov 12, 2024
f72236e
chore(tsup): split compilation into 2 parts, one for postinstall one …
dragolea Nov 12, 2024
cb92da2
chore(dist): postInstall will always be pushed to dist as is needed f…
dragolea Nov 12, 2024
c8cd82b
chore(gitignore): excluded from gitingore the `./dist/postinstall` as…
dragolea Nov 12, 2024
46e62fa
chore(tsconfig): excluded form compilation the `./dist/postinstall`
dragolea Nov 12, 2024
120cee5
chore(deps): various command added
dragolea Nov 12, 2024
d38bc96
chore(deps): fixed package.json scripts
dragolea Nov 12, 2024
65bd63d
fix(test): excluded from ignore the `@dispatcher folder` for test
dragolea Nov 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .commitlintrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import { RuleConfigSeverity } from '@commitlint/types';
const Configuration: UserConfig = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
RuleConfigSeverity.Error,
'always',
['build', 'chore', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'revert', 'style', 'test', 'delete'],
],
'scope-empty': [RuleConfigSeverity.Error, 'never'],
'subject-empty': [RuleConfigSeverity.Error, 'never'],
},
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/enforce-labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ jobs:
with:
REQUIRED_LABELS_ANY: 'patch,minor,major,norelease'
REQUIRED_LABELS_ANY_DESCRIPTION: "Select at least one label ['patch', 'minor', 'major', 'norelease']"

if-label-norelease-stop:
name: No bump version if label 'norelease'
needs: enforce-label-version-release
needs: enforce-label-version-release
if: ${{ !contains(github.event.pull_request.labels.*.name, 'norelease') }}
runs-on: ubuntu-latest
steps:
Expand Down
40 changes: 34 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,41 @@ jobs:
uses: actions/checkout@v4

- name: Install dependencies
run: npm install
run: |
npm install -g @sap/cds-dk ts-node typescript
npm install

- name: Instal SAP CDS-DK
run: npm i -g @sap/cds-dk ts-node

- name: Instal Globally typescript
run: npm i -g typescript
- name: Build project
run: npm run build

- name: Start server and start e2e tests
run: npm run test:e2e

# This decorator is a special decorator which needs an extensive testing due to creation of:
# - @dispatcher folder
# - injection of @dispatcher folder inside of the package.json - imports
# - injection of @dispatcher folder inside of the tsconfig.json - include
# - injection of @dispatcher folder inside of the .gitignore
test-env-decorator:
name: Test @Env decorator
strategy:
matrix:
version: [20, 18]
platform: [ubuntu-latest, windows-latest, macos-latest]

runs-on: ${{ matrix.platform }}

steps:
- name: Get code
uses: actions/checkout@v4

- name: Install dependencies
run: |
npm install -g @sap/cds-dk ts-node typescript
npm install

- name: Build project
run: npm run build

- name: Verify @decorator folder, index.ts file, and content
run: ts-node ./postinstall/util/Verifier.ts
13 changes: 11 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ out

# Nuxt.js build / generate output
.nuxt
dist

# Ignore everything in dist
/dist/*

# But not the postinstall folder
!/dist/postinstall/

# Gatsby files
.cache/
Expand Down Expand Up @@ -132,4 +137,8 @@ dist
.yarn/install-state.gz
.pnp.*
.npmrc
@cds-models
@cds-models
@dispatcher

# But not the @dispatcher folder from the test
!/test/bookshop/@dispatcher
122 changes: 102 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ The goal of **CDS-TS-Dispatcher** is to significantly reduce the boilerplate cod
- [@SingleInstanceSwitch](#singleinstanceswitch)
- [@ValidationResults](#validationresults)
- [@Locale](#locale)
- [@Env](#env)
- [`Method`-`active entity`](#method-active-entity)
- [`Before`](#before)
- [@BeforeCreate](#beforecreate)
Expand Down Expand Up @@ -169,26 +170,32 @@ npm install @dxfrontier/cds-ts-dispatcher
"esModuleInterop": true,
"skipLibCheck": true,
"allowJs": true,
"resolveJsonModule": true,
"isolatedModules": true,
"strictNullChecks": true,
"strictPropertyInitialization": false,
"forceConsistentCasingInFileNames": true,
"allowSyntheticDefaultImports": true,
"strictNullChecks": true,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",

/* Allow decorators */
/* Strictness */
"strict": true,

/* Decorators */
"experimentalDecorators": true,
"emitDecoratorMetadata": true,

/* Strictness */
"strict": true,
"target": "ES2021",
"module": "NodeNext",
"moduleResolution": "NodeNext",

"lib": ["es2022"],
"outDir": "./gen/srv",
"rootDir": ".",

"outDir": "./gen/srv"
"paths": {
"#cds-models/*": ["./@cds-models/*/index.ts"]
}
},
"include": ["./srv"]
"include": ["./srv", "./@dispatcher"]
}
```

Expand Down Expand Up @@ -256,27 +263,34 @@ tsc --init
"esModuleInterop": true,
"skipLibCheck": true,
"allowJs": true,
"resolveJsonModule": true,
"isolatedModules": true,
"strictNullChecks": true,
"strictPropertyInitialization": false,
"forceConsistentCasingInFileNames": true,
"allowSyntheticDefaultImports": true,
"strictNullChecks": true,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",

/* Allow decorators */
/* Strictness */
"strict": true,

/* Decorators */
"experimentalDecorators": true,
"emitDecoratorMetadata": true,

/* Strictness */
"strict": true,
"target": "ES2021",
"module": "NodeNext",
"moduleResolution": "NodeNext",

"lib": ["es2022"],
"outDir": "./gen/srv",
"rootDir": ".",

"outDir": "./gen/srv"
"paths": {
"#cds-models/*": ["./@cds-models/*/index.ts"]
}
},
"include": ["./srv"]
"include": ["./srv", "./@dispatcher"]
}

```

7. Run the `CDS-TS` server
Expand Down Expand Up @@ -1670,6 +1684,74 @@ public async beforeCreate(
```
<p align="right">(<a href="#table-of-contents">back to top</a>)</p>

##### @Env

**@Env**

The `@Env` decorator is a parameter decorator used to inject values from the cds.env configuration object directly into a method parameter.

`Parameters`

- `env (string)`: A string path representing a property from `cds.env`. This path follows the format `property string path`, which allows access to deeply nested configuration properties.
- E.g. : `'requires.db.credentials.url'` corresponds to **cds.env.requires.db.credentials.url** object.


`Return` :

- The decorator returns the value of the specified `cds.env` property value.

`Example`

```ts
// Generated import inside of your package.json when you run 'npm install' or install the cds-ts-dispatcher
import { CDS_ENV } from '@dispatcher';

@BeforeCreate()
public async beforeCreate(
@Req() req: TypedRequest<MyEntity>,
@Env<CDS_ENV>('requires.db.credentials.url') dbUrl: CDS_ENV['requires']['db']['credentials']['url'],
// or @Env<CDS_ENV>('requires.db.credentials.url') dbUrl: string
// or @Env<CDS_ENV>('requires.db.credentials.url') dbUrl: any
// or any other type if you do not want to use the CDS_ENV generated types
) {
if (dbUrl) {
// handle logic using dbUrl
}
}
```
> [!NOTE]
> When you install cds-ts-dispatcher `(e.g. npm install @dxfrontier/cds-ts-dispatcher)` or run a general `npm install`, the following will be generated or updated :
> - New `@dispatcher` folder is generated at the project ***root***.
> This folder contains the `CDS ENV TS interfaces`, generated based on the structure of your current `cds.env` project specific configuration (retrieved from `cds env get` cli command).
> ```text
> ...
> @dispatcher
> ...
> ```
> - `package.json` will be updated with a new `import`:
> ```json
> "imports": {
> "#dispatcher": "./@dispatcher/index.js"
> }
> ```
> - `tsconfig.json` will be updated:
> ```json
> "include": [
> "...",
> "./@dispatcher"
> ]
> ```
> - `.gitignore` will be updated::
> ```text
> ...
> @dispatcher
> ```

> [!NOTE]
> The `@dispatcher` folder is ***regenerated each time you run npm install.***

<p align="right">(<a href="#table-of-contents">back to top</a>)</p>

#### `Method`-`active entity`

##### `Before`
Expand Down
Loading
Loading