Skip to content

Commit

Permalink
entity-renderer: extract getAcceptHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicm67 committed Dec 4, 2024
1 parent e0ab4c4 commit 0dacbee
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 29 deletions.
30 changes: 1 addition & 29 deletions packages/entity-renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,15 @@ import { fileURLToPath } from 'node:url'
import { parsers } from '@rdfjs/formats-common'
import rdf from '@zazuko/env'
import { sparqlSerializeQuadStream, sparqlSupportedTypes, sparqlGetRewriteConfiguration } from 'trifid-core'
import mimeparse from 'mimeparse'

import { getAcceptHeader } from './lib/headers.js'
import { createEntityRenderer } from './renderer/entity.js'
import { createMetadataProvider } from './renderer/metadata.js'

const currentDir = dirname(fileURLToPath(import.meta.url))

const DEFAULT_ENDPOINT_NAME = 'default'

const getAcceptHeader = (req) => {
const queryStringValue = req.query.format

const supportedQueryStringValues = {
ttl: 'text/turtle',
jsonld: 'application/ld+json',
xml: 'application/rdf+xml',
nt: 'application/n-triples',
trig: 'application/trig',
csv: 'text/csv',
html: 'text/html',
}

if (
Object.hasOwnProperty.call(supportedQueryStringValues, queryStringValue)
) {
return supportedQueryStringValues[queryStringValue]
}

const acceptHeader = `${req.headers.accept || ''}`.toLocaleLowerCase()
const selectedHeader = mimeparse.bestMatch([
...sparqlSupportedTypes,
'text/html',
], acceptHeader)

return selectedHeader || acceptHeader
}

const replaceIriInQuery = (query, iri) => {
return query.split('{{iri}}').join(iri)
}
Expand Down
36 changes: 36 additions & 0 deletions packages/entity-renderer/lib/headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import mimeparse from 'mimeparse'
import { sparqlSupportedTypes } from 'trifid-core'

/**
* Get the accept header from the request
*
* @param {{query?: Record<string, any>, headers?: Record<string, any>}} req - The request object
* @returns {string} The accept header
*/
export const getAcceptHeader = (req) => {
const queryStringValue = req.query.format

const supportedQueryStringValues = {
ttl: 'text/turtle',
jsonld: 'application/ld+json',
xml: 'application/rdf+xml',
nt: 'application/n-triples',
trig: 'application/trig',
csv: 'text/csv',
html: 'text/html',
}

if (
Object.hasOwnProperty.call(supportedQueryStringValues, queryStringValue)
) {
return supportedQueryStringValues[queryStringValue]
}

const acceptHeader = `${req.headers?.accept || 'text/html'}`.toLocaleLowerCase()
const selectedHeader = mimeparse.bestMatch([
...sparqlSupportedTypes,
'text/html',
], acceptHeader)

return selectedHeader || acceptHeader
}
188 changes: 188 additions & 0 deletions packages/entity-renderer/test/headers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// @ts-check

import { strictEqual } from 'node:assert'
import { describe, it } from 'node:test'

import { getAcceptHeader } from '../lib/headers.js'

describe('lib/headers', () => {
describe('getAcceptHeader', () => {
it('should return the correct content type based on the query string (ttl)', () => {
const req = {
query: {
format: 'ttl',
},
headers: {},
}
strictEqual(getAcceptHeader(req), 'text/turtle')
})

it('should return the correct content type based on the query string (jsonld)', () => {
const req = {
query: {
format: 'jsonld',
},
headers: {},
}
strictEqual(getAcceptHeader(req), 'application/ld+json')
})

it('should return the correct content type based on the query string (xml)', () => {
const req = {
query: {
format: 'xml',
},
headers: {},
}
strictEqual(getAcceptHeader(req), 'application/rdf+xml')
})

it('should return the correct content type based on the query string (nt)', () => {
const req = {
query: {
format: 'nt',
},
headers: {},
}
strictEqual(getAcceptHeader(req), 'application/n-triples')
})

it('should return the correct content type based on the query string (trig)', () => {
const req = {
query: {
format: 'trig',
},
headers: {},
}
strictEqual(getAcceptHeader(req), 'application/trig')
})

it('should return the correct content type based on the query string (csv)', () => {
const req = {
query: {
format: 'csv',
},
headers: {},
}
strictEqual(getAcceptHeader(req), 'text/csv')
})

it('should return the correct content type based on the query string (html)', () => {
const req = {
query: {
format: 'html',
},
headers: {},
}
strictEqual(getAcceptHeader(req), 'text/html')
})

it('should return the correct content type based on the accept header (ttl)', () => {
const req = {
query: {},
headers: {
accept: 'text/turtle',
},
}
strictEqual(getAcceptHeader(req), 'text/turtle')
})

it('should return the correct content type based on the accept header (jsonld)', () => {
const req = {
query: {},
headers: {
accept: 'application/ld+json',
},
}
strictEqual(getAcceptHeader(req), 'application/ld+json')
})

it('should return the correct content type based on the accept header (xml)', () => {
const req = {
query: {},
headers: {
accept: 'application/rdf+xml',
},
}
strictEqual(getAcceptHeader(req), 'application/rdf+xml')
})

it('should return the correct content type based on the accept header (nt)', () => {
const req = {
query: {},
headers: {
accept: 'application/n-triples',
},
}
strictEqual(getAcceptHeader(req), 'application/n-triples')
})

it('should return the correct content type based on the accept header (trig)', () => {
const req = {
query: {},
headers: {
accept: 'application/trig',
},
}
strictEqual(getAcceptHeader(req), 'application/trig')
})

it('should return the correct content type based on the accept header (csv)', () => {
const req = {
query: {},
headers: {
accept: 'text/csv',
},
}
strictEqual(getAcceptHeader(req), 'text/csv')
})

it('should return the correct content type based on the accept header (html)', () => {
const req = {
query: {},
headers: {
accept: 'text/html',
},
}
strictEqual(getAcceptHeader(req), 'text/html')
})

it('should return the correct content type based on the accept header (html+jsonld)', () => {
const req = {
query: {},
headers: {
accept: 'text/html,application/ld+json',
},
}
strictEqual(getAcceptHeader(req), 'text/html')
})

it('should return the correct content type based on the accept header (jsonld+ttl)', () => {
const req = {
query: {},
headers: {
accept: 'application/ld+json,text/turtle',
},
}
strictEqual(getAcceptHeader(req), 'text/turtle')
})

it('should return the correct content type based on the accept header (plain+ttl)', () => {
const req = {
query: {},
headers: {
accept: 'text/plain,text/turtle',
},
}
strictEqual(getAcceptHeader(req), 'text/turtle')
})

it('should return html by default', () => {
const req = {
query: {},
headers: {},
}
strictEqual(getAcceptHeader(req), 'text/html')
})
})
})

0 comments on commit 0dacbee

Please sign in to comment.