Skip to content

Commit

Permalink
feat: Get ID from headers (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
simenandre authored Mar 16, 2021
1 parent 70e433a commit 29115fc
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
34 changes: 34 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@
* [UserinfoToJSON](README.md#userinfotojson)
* [canConsumeForm](README.md#canconsumeform)
* [exists](README.md#exists)
* [getIDFromHeaders](README.md#getidfromheaders)
* [isBlob](README.md#const-isblob)
* [mapValues](README.md#mapvalues)
* [querystring](README.md#querystring)
Expand Down Expand Up @@ -3257,6 +3258,39 @@ Name | Type |

___

### getIDFromHeaders

**getIDFromHeaders**(`resp`: Response): *string*

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);
```

**Parameters:**

Name | Type |
------ | ------ |
`resp` | Response |

**Returns:** *string*

___

### `Const` isBlob

**isBlob**(`value`: any): *boolean*
Expand Down
9 changes: 9 additions & 0 deletions jest.config.js
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',
},
},
};
27 changes: 27 additions & 0 deletions src/__tests__/id-from-header.test.ts
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();
})

});
3 changes: 2 additions & 1 deletion src/index.ts
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';
29 changes: 29 additions & 0 deletions src/libs/id-from-header.ts
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();
}

0 comments on commit 29115fc

Please sign in to comment.