-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1347 from assemblee-virtuelle/special-endpoint-mixin
New SpecialEndpointMixin
- Loading branch information
Showing
7 changed files
with
129 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const urlJoin = require('url-join'); | ||
const { namedNode, triple } = require('@rdfjs/data-model'); | ||
const { MIME_TYPES } = require('@semapps/mime-types'); | ||
const { parseUrl, parseHeader, negotiateAccept, parseJson, parseTurtle } = require('@semapps/middlewares'); | ||
|
||
module.exports = { | ||
settings: { | ||
baseUrl: null, | ||
settingsDataset: null, | ||
endpoint: { | ||
path: null, // Should start with a dot | ||
initialData: {} | ||
} | ||
}, | ||
dependencies: ['api', 'ldp'], | ||
async started() { | ||
if (!this.settings.baseUrl) throw new Error(`The baseUrl must be specified for service ${this.name}`); | ||
if (!this.settings.settingsDataset) | ||
throw new Error(`The settingsDataset must be specified for service ${this.name}`); | ||
|
||
await this.broker.call('api.addRoute', { | ||
route: { | ||
path: this.settings.endpoint.path, | ||
bodyParsers: false, | ||
authorization: false, | ||
authentication: false, | ||
aliases: { | ||
'GET /': [parseUrl, parseHeader, negotiateAccept, parseJson, parseTurtle, `${this.name}.endpointGet`], | ||
'POST /': this.actions.endpointPost | ||
? [parseUrl, parseHeader, negotiateAccept, parseJson, parseTurtle, `${this.name}.endpointPost`] | ||
: undefined | ||
} | ||
} | ||
}); | ||
|
||
this.endpointUrl = urlJoin(this.settings.baseUrl, this.settings.endpoint.path); | ||
|
||
const endpointExist = await this.broker.call( | ||
'ldp.resource.exist', | ||
{ resourceUri: this.endpointUrl, webId: 'system' }, | ||
{ meta: { dataset: this.settings.settingsDataset } } | ||
); | ||
|
||
if (!endpointExist) { | ||
await this.broker.call( | ||
'ldp.resource.create', | ||
{ | ||
resource: { | ||
id: this.endpointUrl, | ||
...this.settings.endpoint.initialData | ||
}, | ||
contentType: MIME_TYPES.JSON, | ||
webId: 'system' | ||
}, | ||
{ meta: { dataset: this.settings.settingsDataset, skipEmitEvent: true, skipObjectsWatcher: true } } | ||
); | ||
} | ||
}, | ||
actions: { | ||
async endpointAdd(ctx) { | ||
const { predicate, object } = ctx.params; | ||
|
||
await ctx.call( | ||
'ldp.resource.patch', | ||
{ | ||
resourceUri: this.endpointUrl, | ||
triplesToAdd: [triple(namedNode(this.endpointUrl), predicate, object)], | ||
webId: 'system' | ||
}, | ||
{ meta: { dataset: this.settings.settingsDataset, skipEmitEvent: true, skipObjectsWatcher: true } } | ||
); | ||
}, | ||
async endpointGet(ctx) { | ||
ctx.meta.$responseType = ctx.meta.headers?.accept; | ||
|
||
return await ctx.call( | ||
'ldp.resource.get', | ||
{ | ||
resourceUri: this.endpointUrl, | ||
accept: ctx.meta.headers?.accept, | ||
webId: 'system' | ||
}, | ||
{ meta: { dataset: this.settings.settingsDataset } } | ||
); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters