-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from docknetwork/DCKA-1135-logs
Request logger
- Loading branch information
Showing
7 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"ignore_dirs": [ | ||
"node_modules" | ||
] | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {getRealm} from '@docknetwork/wallet-sdk-core/lib/core/realm'; | ||
import assert from 'assert'; | ||
import {v4 as uuidv4} from 'uuid'; | ||
export const RequestLogger = (function () { | ||
const exportLog = () => { | ||
const realm = getRealm(); | ||
return realm.objects('RequestLog').toJSON(); | ||
}; | ||
const logRequest = ({ | ||
status, | ||
url, | ||
method, | ||
headers = {}, | ||
body = {}, | ||
response, | ||
}) => { | ||
assert(typeof url === 'string', 'invalid url'); | ||
assert(typeof method === 'string', 'invalid method'); | ||
const id = uuidv4(); | ||
const realm = getRealm(); | ||
|
||
const log = { | ||
id, | ||
url, | ||
method, | ||
status, | ||
headers: JSON.stringify(headers), | ||
body: JSON.stringify(body), | ||
response: JSON.stringify(response), | ||
createdAt: new Date().toISOString(), | ||
}; | ||
|
||
realm.write(() => { | ||
realm.create('RequestLog', log); | ||
}); | ||
return id; | ||
}; | ||
const clearLogs = () => { | ||
const realm = getRealm(); | ||
realm.write(() => { | ||
realm.delete(realm.objects('RequestLog')); | ||
}); | ||
}; | ||
return { | ||
exportLog, | ||
logRequest, | ||
clearLogs, | ||
}; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "@docknetwork/wallet-sdk-request-logger", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"uuid": "^9.0.0" | ||
}, | ||
"devDependencies": { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import {RequestLogger} from '../lib/request-logger'; | ||
import {getRealm} from '@docknetwork/wallet-sdk-core/lib/core/realm'; | ||
jest.mock('@docknetwork/wallet-sdk-core/lib/core/realm', () => { | ||
const realmFunctions = { | ||
write: jest.fn(callback => { | ||
callback(); | ||
}), | ||
create: jest.fn(), | ||
delete: jest.fn(), | ||
objects: jest.fn(() => ({ | ||
filtered: jest.fn(), | ||
toJSON: jest.fn(), | ||
})), | ||
}; | ||
return { | ||
getRealm: () => realmFunctions, | ||
addSchema: jest.fn(), | ||
clearCacheData: jest.fn(), | ||
}; | ||
}); | ||
describe('Request logger', () => { | ||
it('expect to log request', () => { | ||
const realm = getRealm(); | ||
const mockLog = { | ||
url: 'http://localhost', | ||
method: 'get', | ||
headers: {}, | ||
body: {}, | ||
response: {}, | ||
status: 200, | ||
}; | ||
RequestLogger.logRequest(mockLog); | ||
expect(realm.write).toBeCalled(); | ||
expect(realm.create).toBeCalledWith('RequestLog', { | ||
id: expect.any(String), | ||
createdAt: expect.any(String), | ||
url: 'http://localhost', | ||
method: 'get', | ||
headers: '{}', | ||
body: '{}', | ||
response: '{}', | ||
status: 200, | ||
}); | ||
}); | ||
it('expect to export log request', () => { | ||
const realm = getRealm(); | ||
RequestLogger.exportLog(); | ||
expect(realm.objects).toBeCalledWith('RequestLog'); | ||
}); | ||
it('expect to delete logs', () => { | ||
const realm = getRealm(); | ||
RequestLogger.clearLogs(); | ||
expect(realm.write).toBeCalled(); | ||
expect(realm.delete).toBeCalled(); | ||
expect(realm.objects).toBeCalledWith('RequestLog'); | ||
}); | ||
}); |