Skip to content

Commit

Permalink
Emit event for every request.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Mar 7, 2019
1 parent f3931f4 commit 915996d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/__test__/solid-auth-client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/solid-auth-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,6 +23,7 @@ export default class SolidAuthClient extends EventEmitter {
_pendingSession: ?Promise<?Session>

fetch(input: RequestInfo, options?: RequestOptions): Promise<Response> {
this.emit('request', toUrlString(input))
return authnFetch(defaultStorage(), globalFetch, input, options)
}

Expand Down

0 comments on commit 915996d

Please sign in to comment.