Skip to content

Commit

Permalink
ckan: more precise types
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicm67 committed Feb 5, 2024
1 parent 3cab541 commit accb0e3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/ckan/src/ckan.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import ParsingClient from 'sparql-http-client/ParsingClient.js'
import { toXML } from './xml.js'
import { datasetsQuery } from './query.js'

export function createAPI (config) {
export const createAPI = (config) => {
const client = new ParsingClient({
endpointUrl: config.endpointUrl,
user: config.user,
password: config.password,
})

async function fetchDatasets (organizationId) {
const fetchDatasets = async (organizationId) => {
const query = datasetsQuery(organizationId)
return await client.query.construct(query.toString())
}
Expand Down
8 changes: 7 additions & 1 deletion packages/ckan/src/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
import { sparql } from '@tpluscode/rdf-string'
import * as ns from './namespace.js'

function datasetsQuery (organizationId) {
/**
* Query to retrieve all datasets for a given organization.
*
* @param {string} organizationId The organization identifier.
* @returns {import('@tpluscode/rdf-string').SparqlTemplateResult}
*/
const datasetsQuery = (organizationId) => {
return sparql`
CONSTRUCT {
?dataset ?p ?o .
Expand Down
27 changes: 22 additions & 5 deletions packages/ckan/src/xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ const serializeTerm = (pointer) => {
})
}

/**
* Serialize a literal.
*
* @param {import('clownface').MultiPointer} pointer Pointer to serialize.
* @return {Record<string, unknown>} Serialized literal.
*/
const serializeLiteral = (pointer) => {
if (!isLiteral(pointer)) return null

Expand All @@ -158,6 +164,12 @@ const serializeLiteral = (pointer) => {
}
}

/**
* Serialize a named node.
*
* @param {import('clownface').MultiPointer} pointer Pointer to serialize.
* @return {Record<string, unknown>} Serialized named node.
*/
const serializeNamedNode = (pointer) => {
if (!isNamedNode(pointer)) return null

Expand All @@ -167,10 +179,11 @@ const serializeNamedNode = (pointer) => {
}

/**
* Serialize a blank node.
*
* @param {import('clownface').MultiPointer} pointer
* @param {Array<import('@rdfjs/types').NamedNode>} [allowedTypesArr]
* @return {Record<string, unknown>}
* @param {import('clownface').MultiPointer} pointer Pointer to serialize.
* @param {Array<import('@rdfjs/types').NamedNode>} [allowedTypesArr] Allowed types for the blank node.
* @return {Record<string, unknown>} Serialized blank node.
*/
const serializeBlankNode = (pointer, allowedTypesArr = []) => {
if (!isBlankNode(pointer)) return null
Expand All @@ -193,10 +206,15 @@ const serializeBlankNode = (pointer, allowedTypesArr = []) => {
}
}

/**
* Convert encoding format to distribution format.
*
* @param {import('clownface').MultiPointer} encodingPointer Pointer to encoding format.
* @return {string} Distribution format.
*/
const distributionFormatFromEncoding = (encodingPointer) => {
const encoding = encodingPointer.values[0] || ''

/* eslint-disable indent */
switch (encoding) {
case 'text/html': {
return 'HTML'
Expand All @@ -208,7 +226,6 @@ const distributionFormatFromEncoding = (encodingPointer) => {
return 'UNKNOWN'
}
}
/* eslint-enable indent */
}

/**
Expand Down

0 comments on commit accb0e3

Please sign in to comment.