-
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.
fix(getIdFromHeaders): Return undefined if not available (#6)
- Loading branch information
1 parent
ceae9db
commit e12017c
Showing
1 changed file
with
12 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,28 @@ | ||
/** | ||
* 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'); | ||
} | ||
export function getIDFromHeaders(resp: Response): string | undefined { | ||
if (!resp.headers) return; | ||
const loc = resp.headers.get('location'); | ||
if (!loc) return; | ||
|
||
const url = loc.split('/'); | ||
return url.pop(); | ||
} | ||
const url = loc.split('/'); | ||
return url.pop(); | ||
} |