Skip to content

Commit 773b925

Browse files
authored
ship intercompat type declaraions (#292)
have type declarations that work with TS v3.8, and above Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 5841695 commit 773b925

File tree

20 files changed

+217
-74
lines changed

20 files changed

+217
-74
lines changed

.github/workflows/nodejs.yml

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
name: Node CI
44

5-
on:
5+
on:
66
push:
77
branches: [ main ]
88
pull_request:
@@ -17,13 +17,14 @@ jobs:
1717
build:
1818
name: build ${{ matrix.target }}
1919
runs-on: "ubuntu-latest"
20-
timeout-minutes: 30
2120
strategy:
2221
fail-fast: false
2322
matrix:
2423
target:
2524
- node
2625
- web
26+
- d
27+
timeout-minutes: 10
2728
steps:
2829
- name: Checkout
2930
# see https://github.com/actions/checkout
@@ -49,7 +50,7 @@ jobs:
4950
test-standard:
5051
name: test standard
5152
runs-on: ubuntu-latest
52-
timeout-minutes: 30
53+
timeout-minutes: 10
5354
steps:
5455
- name: Checkout
5556
# see https://github.com/actions/checkout
@@ -99,11 +100,11 @@ jobs:
99100
- "16" # active LTS
100101
- "14"
101102
- "14.0.0" # lowest supported
102-
os:
103+
os:
103104
- ubuntu-latest
104105
- macos-latest
105106
- windows-latest
106-
timeout-minutes: 30
107+
timeout-minutes: 10
107108
steps:
108109
- name: Checkout
109110
# see https://github.com/actions/checkout
@@ -127,3 +128,39 @@ jobs:
127128
run: npm run test:node
128129
# test-web:
129130
# TODO via https://github.com/CycloneDX/cyclonedx-javascript-library/issues/51
131+
test-declaration:
132+
needs: [ 'build' ]
133+
name: digest declarations (TS${{ matrix.typescript-version }})
134+
runs-on: ubuntu-latest
135+
strategy:
136+
fail-fast: false
137+
matrix:
138+
typescript-version:
139+
- '^4' # latest 4.X
140+
- '~4.0' # some 4.0.x
141+
- '^3' # latest 3.X
142+
- '3.8.2' # some 3.8.x - lowest reasonable number that works
143+
env:
144+
EXAMPLE_DIR: examples/node-typescript
145+
timeout-minutes: 10
146+
steps:
147+
- name: Checkout
148+
# see https://github.com/actions/checkout
149+
uses: actions/checkout@v3
150+
- name: Setup Node.js ${{ env.NODE_ACTIVE_LTS }}
151+
# see https://github.com/actions/setup-node
152+
uses: actions/setup-node@v3
153+
with:
154+
node-version: ${{ env.NODE_ACTIVE_LTS }}
155+
- name: setup project
156+
run: npm i --no-save 'typescript@${{ matrix.typescript-version }}'
157+
working-directory: ${{ env.EXAMPLE_DIR }}
158+
- name: fetch build artifact
159+
# see https://github.com/actions/download-artifact
160+
uses: actions/download-artifact@v3
161+
with:
162+
name: dist.d
163+
path: dist.d
164+
- name: use type declarations
165+
run: npm run build
166+
working-directory: ${{ env.EXAMPLE_DIR }}

.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ dist
158158
!/dist.*/
159159

160160
# TypeScript source files are intended to be shipped,
161-
# as they provide definition context and typing for downstream users.
161+
# as they can provide additional context for downstream users.
162162
# The compiler configs are shipped, so downstream users can inherit from them.
163163
!/src/
164164
!/tsconfig.*

HISTORY.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44

55
## unreleased
66

7+
* Added
8+
* Shipped TypeScript declarations are usable by TypeScript v3.8 and above now. ([#291] via [#292])
9+
Previously the source code was abused as type declarations, so they required a certain version of TypeScript 4.
10+
11+
[#291]: https://github.com/CycloneDX/cyclonedx-javascript-library/issues/291
12+
[#292]: https://github.com/CycloneDX/cyclonedx-javascript-library/pull/292
13+
714
## 1.6.0 - 2022-09-31
815

916
* Changed

README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ See extended [examples].
9797
### As _Node.js_ package
9898

9999
```javascript
100-
const cdx = require('@cyclonedx/cyclonedx-library')
100+
const CDX = require('@cyclonedx/cyclonedx-library')
101101

102-
const bom = new cdx.Models.Bom()
103-
bom.metadata.component = new cdx.Models.Component(
104-
cdx.Enums.ComponentType.Application,
102+
const bom = new CDX.Models.Bom()
103+
bom.metadata.component = new CDX.Models.Component(
104+
CDX.Enums.ComponentType.Application,
105105
'MyProject'
106106
)
107-
const componentA = new cdx.Models.Component(
108-
cdx.Enums.ComponentType.Library,
107+
const componentA = new CDX.Models.Component(
108+
CDX.Enums.ComponentType.Library,
109109
'myComponentA',
110110
)
111111
bom.components.add(componentA)
@@ -115,19 +115,17 @@ bom.metadata.component.dependencies.add(componentA.bomRef)
115115
### In _WebBrowsers_
116116

117117
```html
118-
<script src="path-to-this-package/dist.web/lib.js">
119-
// full Library is available as `CycloneDX_library`, per default
120-
</script>
118+
<script src="path-to-this-package/dist.web/lib.js"></script>
121119
<script type="application/javascript">
122-
const cdx = CycloneDX_library
120+
const CDX = CycloneDX_library
123121
124-
let bom = new cdx.Models.Bom()
125-
bom.metadata.component = new cdx.Models.Component(
126-
cdx.Enums.ComponentType.Application,
122+
let bom = new CDX.Models.Bom()
123+
bom.metadata.component = new CDX.Models.Component(
124+
CDX.Enums.ComponentType.Application,
127125
'MyProject'
128126
)
129-
const componentA = new cdx.Models.Component(
130-
cdx.Enums.ComponentType.Library,
127+
const componentA = new CDX.Models.Component(
128+
CDX.Enums.ComponentType.Library,
131129
'myComponentA',
132130
)
133131
bom.components.add(componentA)

examples/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
## Usage
44

5-
* [`example.cjs`](node/example.cjs) showcases the usage in node, commonjs style.
6-
* [`example.mjs`](node/example.mjs) showcases the usage in node, module style.
7-
* [`web-browser.html`](web-browser.html) showcases the usage in a web-browser.
5+
* `node-javascript` showcases the usage in node, JavaScript
6+
* [`example.cjs`](node-javascript/example.cjs) showcases the usage in node, JavaScript, "commonjs" style.
7+
* [`example.mjs`](node-javascript/example.mjs) showcases the usage in node, JavaScript, "module" style.
8+
* `node-typescript` showcases the usage in node, TypeScript
9+
* [`example.ts`](node-typescript/example.ts) showcases the usage in node, TypeScript `^3.8 || ^4`.
10+
* `web` showcases the usage in a web-browser.
11+
* [`web-browser.html`](web-browser.html) showcases the usage in a web-browser.
812

913
## Data models
1014

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
*
22
!/.gitignore
3-
!/example.js
43
!/example.mjs
54
!/example.cjs
65
!/package.json
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict'
2+
/*!
3+
This file is part of CycloneDX JavaScript Library.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
SPDX-License-Identifier: Apache-2.0
18+
Copyright (c) OWASP Foundation. All Rights Reserved.
19+
*/
20+
21+
/** Example how to serialize a Bom to JSON / XML. */
22+
23+
const CDX = require('@cyclonedx/cyclonedx-library')
24+
// full Library is available as `CDX`, now
25+
26+
const bom = new CDX.Models.Bom()
27+
bom.metadata.component = new CDX.Models.Component(
28+
CDX.Enums.ComponentType.Application,
29+
'MyProject'
30+
)
31+
const componentA = new CDX.Models.Component(
32+
CDX.Enums.ComponentType.Library,
33+
'myComponentA'
34+
)
35+
bom.components.add(componentA)
36+
bom.metadata.component.dependencies.add(componentA.bomRef)
37+
38+
const jsonSerializer = new CDX.Serialize.JsonSerializer(
39+
new CDX.Serialize.JSON.Normalize.Factory(
40+
CDX.Spec.Spec1dot4))
41+
const serialized = jsonSerializer.serialize(bom)
42+
console.log(serialized)
43+
44+
const xmlSerializer = new CDX.Serialize.XmlSerializer(
45+
new CDX.Serialize.XML.Normalize.Factory(
46+
CDX.Spec.Spec1dot4))
47+
const serializedXML = xmlSerializer.serialize(bom)
48+
console.log(serializedXML)

examples/node/example.mjs renamed to examples/node-javascript/example.mjs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,29 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
2020

2121
/** Example how to serialize a Bom to JSON / XML. */
2222

23-
import * as cdx from '@cyclonedx/cyclonedx-library'
24-
// full Library is available as `cdx`, now
23+
import * as CDX from '@cyclonedx/cyclonedx-library'
24+
// full Library is available as `CDX`, now
2525

26-
const bom = new cdx.Models.Bom()
27-
bom.metadata.component = new cdx.Models.Component(
28-
cdx.Enums.ComponentType.Application,
26+
const bom = new CDX.Models.Bom()
27+
bom.metadata.component = new CDX.Models.Component(
28+
CDX.Enums.ComponentType.Application,
2929
'MyProject'
3030
)
31-
const componentA = new cdx.Models.Component(
32-
cdx.Enums.ComponentType.Library,
33-
'myComponentA',
31+
const componentA = new CDX.Models.Component(
32+
CDX.Enums.ComponentType.Library,
33+
'myComponentA'
3434
)
3535
bom.components.add(componentA)
3636
bom.metadata.component.dependencies.add(componentA.bomRef)
3737

38-
const jsonSerializer = new cdx.Serialize.JsonSerializer(
39-
new cdx.Serialize.JSON.Normalize.Factory(
40-
cdx.Spec.Spec1dot4))
38+
const jsonSerializer = new CDX.Serialize.JsonSerializer(
39+
new CDX.Serialize.JSON.Normalize.Factory(
40+
CDX.Spec.Spec1dot4))
4141
const serialized = jsonSerializer.serialize(bom)
4242
console.log(serialized)
4343

44-
const xmlSerializer = new cdx.Serialize.XmlSerializer(
45-
new cdx.Serialize.XML.Normalize.Factory(
46-
cdx.Spec.Spec1dot4))
44+
const xmlSerializer = new CDX.Serialize.XmlSerializer(
45+
new CDX.Serialize.XML.Normalize.Factory(
46+
CDX.Spec.Spec1dot4))
4747
const serializedXML = xmlSerializer.serialize(bom)
4848
console.log(serializedXML)
File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!/.gitignore
3+
!/example.ts
4+
!/package.json
5+
!/tsconfig.json

0 commit comments

Comments
 (0)