From b282b5363aab1ca578c1fccd5fe72f69f0f42714 Mon Sep 17 00:00:00 2001 From: bergi Date: Thu, 15 Jul 2021 21:49:01 +0200 Subject: [PATCH] feat: added Headers class to fetch, added .config method to fetch --- FetchFactory.js | 38 ++++++++++++++++++---- package.json | 1 + test/FetchFactory.test.js | 68 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 6 deletions(-) diff --git a/FetchFactory.js b/FetchFactory.js index 430f158..bd75ae7 100644 --- a/FetchFactory.js +++ b/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 diff --git a/package.json b/package.json index 512734c..b1e65e5 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/FetchFactory.test.js b/test/FetchFactory.test.js index 25f3d46..2bbe11a 100644 --- a/test/FetchFactory.test.js +++ b/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' @@ -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]) @@ -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) + }) }) })