-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(dev): small changes to .editorconfig, package.json * feat: add toMatchSuccessResult jest macher * feat: extra functions,types, classes
- Loading branch information
Showing
14 changed files
with
220 additions
and
24 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
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,7 @@ | ||
import { OrderDirection } from "./types"; | ||
|
||
export interface GetQueryOptions<T> { | ||
orderBy?: Record<keyof T, OrderDirection>; | ||
limit?: number; | ||
offset?: number; | ||
} |
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,35 @@ | ||
import { AppError, AppErrorCode } from './AppError'; | ||
import { AR, ARP, OKA, ERRA, PS } from './AsyncResult'; | ||
|
||
export interface RetryOptions { | ||
id: string; | ||
maxAttempts?: number; | ||
retryDelay?: number | ((attempt: number, maxAttempts: number) => Promise<void>); | ||
} | ||
|
||
const defaultRetryDelayFn = (delay: number, attempt: number, maxAttempts: number) => { | ||
return new Promise((resolve) => setTimeout(resolve, delay)); | ||
}; | ||
|
||
export class RetryHelper { | ||
public static async retryAsync<T>(fn: () => AR<T>, options: RetryOptions): ARP<T> { | ||
const retryDelayFn = typeof options.retryDelay === 'function' ? options.retryDelay : defaultRetryDelayFn.bind(options.retryDelay); | ||
let lastError: AppError; | ||
for (let attempt = 1; attempt <= options.maxAttempts; attempt++) { | ||
const result = await fn(); | ||
if (result.isSuccess()) { | ||
return OKA(result.v); | ||
} | ||
|
||
lastError = result.e; | ||
await retryDelayFn(attempt, options.maxAttempts); | ||
} | ||
|
||
return ERRA({ | ||
type: 'core.util.retry_helper.retry_max_attempts', | ||
code: AppErrorCode.INTERNAL_ERROR, | ||
cause: lastError, | ||
data: {id: options.id} | ||
}); | ||
} | ||
} |
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,17 +1,19 @@ | ||
export * from './AppMeta'; | ||
export * from './AppError'; | ||
export * from './Error'; | ||
|
||
export * from './Result'; | ||
export * from './AsyncResult'; | ||
|
||
export * from './CurrentTime'; | ||
export * from './Validator'; | ||
export * from './SanitizeHelper'; | ||
export * from './functions'; | ||
export * from './types'; | ||
export * from './CurrentTime'; | ||
export * from './Dto'; | ||
export * from '../Log'; | ||
export * from './AppMeta'; | ||
export * from './Error/index'; | ||
export * from './RetryHelper'; | ||
export * from './QueryHelper'; | ||
|
||
export * from './Dto'; | ||
export * from './JsonSerialize'; | ||
export * from './StringTemplate'; | ||
|
||
export * from './types'; | ||
export * from './functions'; |
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,69 @@ | ||
/** | ||
* @group unit | ||
*/ | ||
|
||
import { ERR, OK, stripAnsiColors } from '@'; | ||
|
||
describe('Matchers', () => { | ||
describe('toMatchSuccessResult', () => { | ||
test('When success result and same value', () => { | ||
const current = OK(10); | ||
|
||
expect(current).toMatchSuccessResult(10); | ||
}); | ||
|
||
test('When success result and other value, the throw', () => { | ||
expect.assertions(2); | ||
|
||
try { | ||
const current = OK(10); | ||
expect(current).toMatchSuccessResult('test'); | ||
} catch (e) { | ||
const expected = [ | ||
'Expected result value to be:', | ||
' "test"', | ||
'Received:', | ||
' 10', | ||
'', | ||
' Comparing two different types of values. Expected string but received number.' | ||
].join('\n'); | ||
expect(stripAnsiColors(e.message)).toEqual(expected); | ||
} | ||
}); | ||
|
||
test('When success result and negation, then throw', () => { | ||
expect.assertions(2); | ||
|
||
try { | ||
const current = OK(10); | ||
expect(current).not.toMatchSuccessResult(10); | ||
} catch (e) { | ||
const expected = ['Expected result value to be not:', ' 10', 'Received:', ' 10'].join('\n'); | ||
expect(stripAnsiColors(e.message)).toEqual(expected); | ||
} | ||
}); | ||
|
||
test('When error result, then throw', () => { | ||
expect.assertions(2); | ||
const current = ERR('test.error'); | ||
try { | ||
|
||
expect(current).toMatchSuccessResult(10); | ||
} catch (e) { | ||
const expected = [ | ||
'Expected result value to be:', | ||
' 10', | ||
'Received error:', | ||
` ${JSON.stringify(current.e, undefined, 2)}`, | ||
].join('\n'); | ||
expect(stripAnsiColors(e.message)).toEqual(expected); | ||
} | ||
}); | ||
|
||
test('When error result and negation', () => { | ||
const current = ERR('test.error'); | ||
|
||
expect(current).not.toMatchSuccessResult(10); | ||
}); | ||
}); | ||
}); |
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