-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70e433a
commit 29115fc
Showing
5 changed files
with
101 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
globals: { | ||
'ts-jest': { | ||
tsconfig: 'tsconfig.json', | ||
}, | ||
}, | ||
}; |
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,27 @@ | ||
import { getIDFromHeaders } from '../libs/id-from-header'; | ||
import { Response } from 'node-fetch'; | ||
describe('id-from-header.ts', () => { | ||
const url = | ||
'https://api.fiken.no/api/v2/companies/fiken-demo-historisk-fasong-as1/bankAccounts/1933231237'; | ||
|
||
it('should resolve location header', () => { | ||
const response = new Response( | ||
null, | ||
{ | ||
headers: { | ||
location: url, | ||
}, | ||
}, | ||
); | ||
|
||
expect(getIDFromHeaders(response)).toBe('1933231237'); | ||
}); | ||
|
||
it('should fail if no location is in headers', () => { | ||
const response = new Response(null, { | ||
headers: {}, | ||
}); | ||
expect(() => getIDFromHeaders(response)).toThrowError(); | ||
}) | ||
|
||
}); |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './generated'; | ||
export * from './generated'; | ||
export * from './libs/id-from-header'; |
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,29 @@ | ||
/** | ||
* Get ID from `location` header | ||
* | ||
* This function is used to get the ID resources | ||
* created in Folio (where you are redirected | ||
* afterwards). | ||
* | ||
* @example | ||
* ```typescript | ||
* import { getIDFromHeaders } from 'fiken'; | ||
* | ||
* ... | ||
* | ||
* const res = await fiken.createBankAccountRaw({ | ||
* ... | ||
* }); | ||
* | ||
* const id = getIDFromHeaders(res.raw); | ||
* ``` | ||
*/ | ||
export function getIDFromHeaders(resp: Response): string { | ||
const loc = resp.headers.get('location'); | ||
if (!loc) { | ||
throw new Error('location header was not found'); | ||
} | ||
|
||
const url = loc.split('/'); | ||
return url.pop(); | ||
} |