From 915996d1331c98f517dfc66bd4dd9e0ec5699e38 Mon Sep 17 00:00:00 2001 From: Ruben Verborgh Date: Thu, 7 Mar 2019 21:58:47 +0100 Subject: [PATCH] Emit event for every request. --- src/__test__/solid-auth-client.spec.js | 14 ++++++++++++++ src/solid-auth-client.js | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/__test__/solid-auth-client.spec.js b/src/__test__/solid-auth-client.spec.js index 905fc9f..3e8accc 100644 --- a/src/__test__/solid-auth-client.spec.js +++ b/src/__test__/solid-auth-client.spec.js @@ -507,6 +507,20 @@ describe('fetch', () => { ) } + it('fires the request event', async () => { + nock('https://third-party.com') + .get('/resource') + .reply(200) + + const allArgs = [] + const collectArgs = allArgs.push.bind(allArgs) + instance.on('request', collectArgs) + await instance.fetch('https://third-party.com/resource') + instance.removeListener('request', collectArgs) + + expect(allArgs).toEqual(['https://third-party.com/resource']) + }) + it('handles 401s from WebID-OIDC resources by resending with credentials', async () => { expect.assertions(1) await saveSession(window.localStorage)(fakeSession) diff --git a/src/solid-auth-client.js b/src/solid-auth-client.js index 711570f..5d22639 100644 --- a/src/solid-auth-client.js +++ b/src/solid-auth-client.js @@ -7,7 +7,7 @@ import type { Session } from './session' import { getSession, saveSession, clearSession } from './session' import type { AsyncStorage } from './storage' import { defaultStorage } from './storage' -import { currentUrlNoParams } from './url-util' +import { toUrlString, currentUrlNoParams } from './url-util' import * as WebIdOidc from './webid-oidc' // Store the global fetch, so the user is free to override it @@ -23,6 +23,7 @@ export default class SolidAuthClient extends EventEmitter { _pendingSession: ?Promise fetch(input: RequestInfo, options?: RequestOptions): Promise { + this.emit('request', toUrlString(input)) return authnFetch(defaultStorage(), globalFetch, input, options) }