Skip to content

Commit

Permalink
Merge pull request #1 from rdfjs-base/fetch-config-headers
Browse files Browse the repository at this point in the history
feat: added Headers class to fetch, added .config method to fetch
  • Loading branch information
bergos committed Jul 18, 2021
2 parents 0ef38f3 + b282b53 commit c986d6d
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
38 changes: 32 additions & 6 deletions FetchFactory.js
@@ -1,14 +1,40 @@
import fetch from '@rdfjs/fetch-lite'

class FetchFactory {
fetch (url, options = {}) {
const factory = typeof this.dataset === 'function' ? this : null
const formats = this.formats
function createFetch (context) {
const result = function (url, options = {}) {
const factory = typeof context.dataset === 'function' ? context : null

return fetch(url, {
...options,
factory,
fetch: context._fetch.fetch,
formats: context.formats
})
}

return fetch(url, { ...options, factory, formats })
result.config = function (key, value) {
context._fetch[key] = value
}

result.Headers = fetch.Headers

return result
}

FetchFactory.exports = ['fetch']
class FetchFactory {
init () {
this._fetch = {
fetch: null
}

this.fetch = createFetch(this)
}

clone (original) {
for (const [key, value] of Object.entries(original._fetch)) {
this._fetch[key] = value
}
}
}

export default FetchFactory
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -39,6 +39,7 @@
"codecov": "^3.8.2",
"express-as-promise": "^1.2.0",
"mocha": "^9.0.2",
"nodeify-fetch": "^2.2.1",
"stricter-standard": "^0.2.0"
}
}
68 changes: 68 additions & 0 deletions test/FetchFactory.test.js
@@ -1,9 +1,11 @@
import { strictEqual } from 'assert'
import rdf from '@rdfjs/data-model'
import fetch from '@rdfjs/fetch-lite'
import formats from '@rdfjs/formats-common'
import toNT from '@rdfjs/to-ntriples'
import withServer from 'express-as-promise/withServer.js'
import { describe, it } from 'mocha'
import nodeFetch from 'nodeify-fetch'
import DataFactory from '../DataFactory.js'
import DatasetFactory from '../DatasetFactory.js'
import Environment from '../Environment.js'
Expand All @@ -22,6 +24,19 @@ describe('FetchFactory', () => {
strictEqual(typeof FetchFactory, 'function')
})

describe('.clone', () => {
it('should copy the key-value pairs of the config', () => {
const env = new Environment([FetchFactory])
env._fetch.a = '1'
env._fetch.b = {}

const clone = env.clone()

strictEqual(clone._fetch.a, env._fetch.a)
strictEqual(clone._fetch.b, env._fetch.b)
})
})

describe('.fetch', () => {
it('should be a method', () => {
const env = new Environment([FetchFactory])
Expand Down Expand Up @@ -97,5 +112,58 @@ describe('FetchFactory', () => {
strictEqual(called, true)
})
})

it('should use an alternative fetch implementation if set in the config', async () => {
await withServer(async server => {
let called = false
const customFetch = (url, options) => {
called = true

return nodeFetch(url, options)
}
const env = new Environment([DataFactory, FetchFactory, FormatsFactory])
env.fetch.config('fetch', customFetch)
env.formats.import(formats)

server.app.get('/', (req, res) => {
res.set('content-type', 'text/turtle').end(toNT(example.quad))
})

await env.fetch(await server.listen())

strictEqual(called, true)
})
})
})

describe('.fetch.config', () => {
it('should be a function', () => {
const env = new Environment([FetchFactory])

strictEqual(typeof env.fetch.config, 'function')
})

it('should change the given config value', () => {
const value = {}
const env = new Environment([FetchFactory])

env.fetch.config('fetch', value)

strictEqual(env._fetch.fetch, value)
})
})

describe('.fetch.Headers', () => {
it('should be a constructor', () => {
const env = new Environment([FetchFactory])

strictEqual(typeof env.fetch.Headers, 'function')
})

it('should be the Headers class from @rdfjs/fetch-lite', () => {
const env = new Environment([FetchFactory])

strictEqual(env.fetch.Headers, fetch.Headers)
})
})
})

0 comments on commit c986d6d

Please sign in to comment.