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

Update to ES Modules + TypeScript #118

Merged
merged 22 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
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: 5 additions & 0 deletions .changeset/chatty-lobsters-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hydra-box": minor
---

Convert package to ESM
5 changes: 5 additions & 0 deletions .changeset/moody-eyes-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hydra-box": minor
---

Source changes to TypeScript
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
root = true

[*]
indent_size = 2
indent_style = space
insert_final_newline = true
15 changes: 15 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": [ "@tpluscode" ],
"env": {
"mocha": true
},
"overrides": [
{
"files": ["examples/**/*.js"],
"rules": {
"no-console": "off",
"import/no-extraneous-dependencies": "off"
}
}
]
}
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 18

Expand Down
26 changes: 16 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ jobs:
e2e-tests:
runs-on: [ubuntu-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm i
- run: docker-compose up -d blog
- run: npx wait-on http://localhost:9000 -t 30000
- run: docker-compose run --rm e2e-tests 2>&1
- run: docker-compose logs blog
if: failure()
Expand All @@ -21,23 +19,31 @@ jobs:
runs-on: [ ubuntu-latest ]
strategy:
matrix:
node: [ 12, 14, 16, 18 ]
node: [ 18, 20 ]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm i
- run: npm test
- name: Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm ci
- run: npm run lint

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm ci
- run: npm run build
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.nyc_output
coverage
node_modules
*.d.ts
*.js
!test/**/*.js
!examples/**/*.js
79 changes: 0 additions & 79 deletions Api.js

This file was deleted.

103 changes: 103 additions & 0 deletions Api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* eslint-disable camelcase */
import rdf from '@zazuko/env-node'
import EcmaScriptLoader from 'rdf-loader-code/ecmaScript.js'
import LoaderRegistryImpl, { LoaderRegistry } from 'rdf-loaders-registry'
import EcmaScriptModuleLoader from 'rdf-loader-code/ecmaScriptModule.js'
import EcmaScriptLiteralLoader from 'rdf-loader-code/ecmaScriptLiteral.js'
import type { NamedNode, DatasetCore, Quad_Graph } from '@rdfjs/types'
import { replaceDatasetIRI } from './lib/replaceIRI.js'

interface ApiInit<D extends DatasetCore = DatasetCore> {
term?: NamedNode
dataset?: D
graph?: NamedNode
path?: string
codePath?: string
}

class Api {
initialized: boolean
path: string
codePath: string
graph?: Quad_Graph | undefined
dataset: DatasetCore
private _term: NamedNode | undefined
loaderRegistry: LoaderRegistry
private _initialization?: Promise<void>
readonly tasks: Array<() => Promise<void>>

constructor({ term, dataset, graph, path = '/api', codePath = process.cwd() }: ApiInit = { }) {
this._term = term
this.dataset = dataset || rdf.dataset()
this.graph = graph
this.path = path
this.codePath = codePath
this.loaderRegistry = new LoaderRegistryImpl()
this.tasks = []
this.initialized = false

EcmaScriptLoader.register(this.loaderRegistry)
EcmaScriptModuleLoader.register(this.loaderRegistry)
EcmaScriptLiteralLoader.register(this.loaderRegistry)
}

get term() {
return this._term!
}

set term(term: NamedNode) {
this._term = term
}

async init() {
if (!this._initialization) {
this._initialization = this._beginInit()
}

return this._initialization
}

fromFile(filePath: string) {
this.tasks.push(async () => {
rdf.dataset().addAll.call(this.dataset, await rdf.dataset().import(rdf.fromFile(filePath)))
})

return this
}

rebase(fromBaseIRI: string | NamedNode, toBaseIRI: string | NamedNode) {
this.tasks.push(async () => {
this.dataset = replaceDatasetIRI(fromBaseIRI, toBaseIRI, this.dataset)
})

return this
}

static fromFile(filePath: string, options?: ApiInit) {
const api = new Api(options)

return api.fromFile(filePath)
}

async _beginInit() {
if (!this.dataset) {
this.dataset = rdf.dataset()
}

Check warning on line 85 in Api.ts

View check run for this annotation

Codecov / codecov/patch

Api.ts#L84-L85

Added lines #L84 - L85 were not covered by tests

for (const task of this.tasks) {
await task()
}

const apiDoc = rdf.clownface({ dataset: this.dataset, term: this.term, graph: this.graph })

if (apiDoc.has(rdf.ns.rdf.type, rdf.ns.hydra.ApiDocumentation).terms.length === 0) {
apiDoc.addOut(rdf.ns.rdf.type, rdf.ns.hydra.ApiDocumentation)

apiDoc.any().has(rdf.ns.rdf.type, rdf.ns.hydra.Class).forEach(supportedClass => {
apiDoc.addOut(rdf.ns.hydra.supportedClass, supportedClass)
})
}
}
}

export default Api
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The resource is read using the IRI as named graph filter.
Here an example for a store on the local file system using `rdf-store-fs`:

```javascript
const FlatMultiFileStore = require('rdf-store-fs/FlatMultiFileStore')
import FlatMultiFileStore from 'rdf-store-fs/FlatMultiFileStore.js'

const store = new FlatMultiFileStore({
baseIRI: 'http://localhost:9000/',
Expand Down
56 changes: 0 additions & 56 deletions StoreResourceLoader.js

This file was deleted.

Loading