-
Notifications
You must be signed in to change notification settings - Fork 3
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
e5abee3
commit 1ef2a40
Showing
11 changed files
with
355 additions
and
37 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
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,60 @@ | ||
import { addDays } from 'date-fns'; | ||
import * as dotenv from 'dotenv'; | ||
import nock from 'nock'; | ||
import { invariant } from 'ts-invariant'; | ||
import { formatDate } from '../../../utils'; | ||
import { TripletexToken } from '../token'; | ||
|
||
dotenv.config(); | ||
|
||
const baseUrl = process.env.BASE_URL || 'https://api.tripletex.io/'; | ||
|
||
describe('token class', () => { | ||
let tokenClass: TripletexToken; | ||
|
||
beforeEach(() => { | ||
tokenClass = new TripletexToken({ baseUrl }); | ||
}); | ||
|
||
it('should create session token', async () => { | ||
const expirationDate = addDays(new Date(), 2); | ||
nock(baseUrl, { encodedQueryParams: true }) | ||
.put('/v2/token/session/:create') | ||
.query({ | ||
employeeToken: String(process.env.EMPLOYEE_TOKEN), | ||
consumerToken: String(process.env.CONSUMER_TOKEN), | ||
expirationDate: formatDate(expirationDate), | ||
}) | ||
.reply(200, { | ||
value: { | ||
id: 19015016, | ||
version: 1, | ||
url: 'api.tripletex.io/v2/token/session/19015016', | ||
consumerToken: { | ||
id: 1754, | ||
url: 'api.tripletex.io/v2/token/consumer/1754', | ||
}, | ||
employeeToken: { | ||
id: 2913, | ||
url: 'api.tripletex.io/v2/token/employee/2913', | ||
}, | ||
expirationDate: '2022-04-12', | ||
token: | ||
'eyJ0b2tlbklkIjoxOTAxNTAxNiwidG9rZW4iOiJ0ZXN0LWIzMjU0NTg5LTU1OTctNDY0Mi04MmU5LTUyNDkyOWZlZGViNSJ9', | ||
encryptionKey: null, | ||
}, | ||
}); | ||
|
||
const token = await tokenClass.createSessionToken({ | ||
employeeToken: String(process.env.EMPLOYEE_TOKEN), | ||
consumerToken: String(process.env.CONSUMER_TOKEN), | ||
expirationDate, | ||
}); | ||
|
||
invariant(token.success); | ||
|
||
expect(token.body.value.token).toEqual( | ||
'eyJ0b2tlbklkIjoxOTAxNTAxNiwidG9rZW4iOiJ0ZXN0LWIzMjU0NTg5LTU1OTctNDY0Mi04MmU5LTUyNDkyOWZlZGViNSJ9', | ||
); | ||
}); | ||
}); |
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,11 @@ | ||
import * as rt from 'runtypes'; | ||
import { singleValueEnvelope } from '../../../utils'; | ||
|
||
const sessionTokenRt = rt.Record({ | ||
expirationDate: rt.String, | ||
token: rt.String, | ||
}); | ||
|
||
export type SessionToken = rt.Static<typeof sessionTokenRt>; | ||
|
||
export const getTokenResponseRt = singleValueEnvelope(sessionTokenRt); |
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,28 @@ | ||
import { formatDate, withRuntype } from '../../utils'; | ||
import { TripletexBase } from '../base'; | ||
import { getTokenResponseRt } from './models/session-token'; | ||
|
||
export interface CreateSessionTokenInput { | ||
employeeToken: string; | ||
consumerToken: string; | ||
expirationDate: Date; | ||
} | ||
|
||
export class TripletexToken extends TripletexBase { | ||
createSessionToken(args: CreateSessionTokenInput) { | ||
const call = this.unauthenticatedCall() // | ||
.args<{ | ||
input: CreateSessionTokenInput; | ||
}>() | ||
.path('/v2/token/session/:create') | ||
.query(args => ({ | ||
...args.input, | ||
expirationDate: formatDate(args.input.expirationDate), | ||
})) | ||
.method('put') | ||
.parseJson(withRuntype(getTokenResponseRt)) | ||
.build(); | ||
|
||
return this.performRequest(() => call({ input: args })); | ||
} | ||
} |
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,3 +1,4 @@ | ||
import { TripletexClientConfig } from "./types"; | ||
|
||
/** | ||
* Tripletex Client | ||
|
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{ | ||
"extends": "@tsconfig/node16/tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist" | ||
"outDir": "dist", | ||
"esModuleInterop": true | ||
}, | ||
"include": ["src/**/*.ts"] | ||
} |
Oops, something went wrong.