Skip to content

Commit

Permalink
feat: simplify endpoint intitialization
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Sep 16, 2024
1 parent facbd83 commit b72086f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-rats-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kopflos-cms/core": patch
---

Allow initializing clients from one instance of `StreamClient` or `ParsingClient`
8 changes: 4 additions & 4 deletions package-lock.json

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

11 changes: 9 additions & 2 deletions packages/core/lib/Kopflos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Options as EndpointOptions, StreamClient } from 'sparql-http-clien
import type { ParsingClient } from 'sparql-http-client/ParsingClient.js'
import { CONSTRUCT } from '@tpluscode/sparql-builder'
import { IN } from '@tpluscode/sparql-builder/expressions'
import type { Client } from 'sparql-http-client'
import type { KopflosEnvironment } from './env/index.js'
import { createEnv } from './env/index.js'
import type { ResourceShapeLookup, ResourceShapeMatch } from './resourceShape.js'
Expand Down Expand Up @@ -57,7 +58,7 @@ interface Clients {
parsed: ParsingClient
}

type Endpoint = string | EndpointOptions | Clients
type Endpoint = string | EndpointOptions | Clients | Client

export interface KopflosConfig {
sparql: Record<string, Endpoint> & { default: Endpoint }
Expand Down Expand Up @@ -85,7 +86,13 @@ export default class Impl implements Kopflos {

log.info('Kopflos initialized')
log.debug('Options', {
sparqlEndpoints: Object.keys(this.env.sparql),
sparqlEndpoints: Object.fromEntries(Object.entries(this.env.sparql).map(([key, value]) => {
return [key, {
endpointUrl: value.parsed.endpointUrl,
updateUrl: value.parsed.updateUrl,
storeUrl: value.parsed.storeUrl,
}]
})),
resourceShapeLookup: options.resourceShapeLookup?.name ?? 'default',
resourceLoaderLookup: options.resourceLoaderLookup?.name ?? 'default',
handlerLookup: options.handlerLookup?.name ?? 'default',
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/env/SparqlClientFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export default class implements SparqlClientFactory {
}

const endpointConfig = {
factory: this,
...endpointOrClients,
factory: this,
}

return [key, {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@types/chai-as-promised": "^7.1.8",
"@types/sinon": "^17.0.3",
"@types/sinon-chai": "^3.2.12",
"@types/sparql-http-client": "^3.0.0",
"@types/sparql-http-client": "^3.0.4",
"chai": "^4.5.0",
"chai-as-promised": "^7.0.0",
"mocha-chai-rdf": "^0.1.1",
Expand Down
59 changes: 59 additions & 0 deletions packages/core/test/lib/env/SparqlClientFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,63 @@ describe('lib/env/SparqlClientFactory', () => {
expect(factory.sparql.default.parsed).to.have.property('name', 'parsed')
})
})

context('initialised from client single instance', () => {
const endpoints = {
endpointUrl: 'http://example.com/sparql',
updateUrl: 'http://example.com/update',
storeUrl: 'http://example.com/store',
}

const singleClients = [
['stream client', new StreamClient(endpoints)],
['parsing client', new ParsingClient(endpoints)],
]

for (const [name, client] of singleClients) {
context(`of ${name}`, () => {
before(() => {
// given
const Kopflos = class implements KopflosFactory {
static exports = ['kopflos']

get kopflos() {
return {
config: {
sparql: {
default: client,
},
},
}
}
}

// when
factory = new Environment([Kopflos, Factory], { parent: rdf })
})

it('sets both clients', () => {
// then
expect(factory.sparql.default.stream).to.be.ok
expect(factory.sparql.default.parsed).to.be.ok
})

it('uses same endpoint URLs for both clients', () => {
// then
expect(factory.sparql.default.stream).to.have.property('endpointUrl', 'http://example.com/sparql')
expect(factory.sparql.default.parsed).to.have.property('endpointUrl', 'http://example.com/sparql')
expect(factory.sparql.default.stream).to.have.property('updateUrl', 'http://example.com/update')
expect(factory.sparql.default.parsed).to.have.property('updateUrl', 'http://example.com/update')
expect(factory.sparql.default.stream).to.have.property('storeUrl', 'http://example.com/store')
expect(factory.sparql.default.parsed).to.have.property('storeUrl', 'http://example.com/store')
})

it('uses factory itself with sparql clients', () => {
// then
expect(factory.sparql.default.stream).to.have.property('factory', factory)
expect(factory.sparql.default.parsed).to.have.property('factory', factory)
})
})
}
})
})

0 comments on commit b72086f

Please sign in to comment.