-
Notifications
You must be signed in to change notification settings - Fork 15
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
Showing
8 changed files
with
110 additions
and
32 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
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,4 +1,5 @@ | ||
export type PixError = { | ||
readonly error: boolean; | ||
readonly message: string; | ||
readonly throwIfError: () => never; | ||
}; |
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,20 +1,24 @@ | ||
import { PIXFetchResults } from '../dynamicPayload'; | ||
|
||
import { PixDynamicObject, PixStaticObject } from './pixElements'; | ||
import { PixError } from './pixError'; | ||
|
||
export type PixFnDefault = { | ||
export interface PixFnDefault { | ||
readonly toBRCode: () => string; | ||
readonly toImage: () => Promise<string>; | ||
}; | ||
} | ||
|
||
export type PixStaticFn = PixFnDefault; | ||
export interface PixStaticFn extends PixFnDefault { | ||
readonly throwIfError: () => PixStaticObject; | ||
} | ||
|
||
export type PixDynamicFn = PixFnDefault & { | ||
readonly fetchPayload: ({ | ||
DPP, | ||
codMun, | ||
}: { | ||
readonly DPP: string; | ||
readonly codMun: number; | ||
}) => Promise<PIXFetchResults | PixError>; | ||
type FetchPayloadParams = { | ||
DPP: string; | ||
codMun: number; | ||
}; | ||
|
||
export interface PixDynamicFn extends PixFnDefault { | ||
readonly fetchPayload: ( | ||
params: FetchPayloadParams | ||
) => Promise<PIXFetchResults | PixError>; | ||
readonly throwIfError: () => PixDynamicObject; | ||
} |
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 { PixError } from '../types/pixError'; | ||
|
||
export function generateErrorObject(message: string): PixError { | ||
return { | ||
error: true, | ||
message, | ||
throwIfError: () => { | ||
throw new Error(message); | ||
}, | ||
}; | ||
} |
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 { describe, expect, it } from 'vitest'; | ||
import { createStaticPix } from '../src'; | ||
|
||
describe('throwIfError', () => { | ||
it('should throw if merchant city is longer than 15 characters', () => { | ||
const staticPixFn = createStaticPix({ | ||
merchantName: 'Thales Ogliari', | ||
merchantCity: 'SAO MIGUEL DO OESTE', | ||
pixKey: '[email protected]', | ||
infoAdicional: 'Pedido 123', | ||
txid: '', | ||
transactionAmount: 10, | ||
}); | ||
|
||
expect(staticPixFn.throwIfError).toThrow( | ||
'merchantCity character limit exceeded (> 15)' | ||
); | ||
}); | ||
|
||
it('should throw if merchant name is longer than 25 characters', () => { | ||
const staticPixFn = createStaticPix({ | ||
merchantName: 'Thales Ogliari Thales Ogliari Thales Ogliari', | ||
merchantCity: 'SAO MIGUEL', | ||
pixKey: '[email protected]', | ||
infoAdicional: 'Pedido 123', | ||
txid: '', | ||
transactionAmount: 10, | ||
}); | ||
|
||
expect(staticPixFn.throwIfError).toThrow( | ||
'merchantName character limit exceeded (> 25)' | ||
); | ||
}); | ||
|
||
it('should throw if txid is longer than 25 characters', () => { | ||
const staticPixFn = createStaticPix({ | ||
merchantName: 'Thales Ogliari', | ||
merchantCity: 'SAO MIGUEL', | ||
pixKey: '[email protected]', | ||
infoAdicional: 'Pedido 123', | ||
txid: 'F2B8073B0A52461997F53FB2A85FE7E8', | ||
transactionAmount: 10, | ||
}); | ||
|
||
expect(staticPixFn.throwIfError).toThrow( | ||
'txid character limit exceeded (> 25)' | ||
); | ||
}); | ||
}); |