Skip to content

Commit dd833b7

Browse files
committed
Merge branch 'hotfix/v2.1.11'
2 parents 6062b4a + 027ab38 commit dd833b7

11 files changed

Lines changed: 160 additions & 4 deletions

File tree

dist/utils/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export { default as isEmpty } from './isEmpty';
88
export { default as logger } from './logger';
99
export { default as validateFields } from './validateFields';
1010
export { default as safePromise } from './safePromise';
11+
export { default as typeSafePromise } from './typeSafePromise';

dist/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export { default as isEmpty } from './isEmpty';
88
export { default as logger } from './logger';
99
export { default as validateFields } from './validateFields';
1010
export { default as safePromise } from './safePromise';
11+
export { default as typeSafePromise } from './typeSafePromise';

dist/utils/safePromise.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
*
2+
* @deprecated use typeSafePromise instead
33
* @param promise
44
* @returns [err, res]
55
* @reference https://github.com/arthurfiorette/proposal-safe-assignment-operator

dist/utils/safePromise.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var __awaiter =
3232
});
3333
};
3434
/**
35-
*
35+
* @deprecated use typeSafePromise instead
3636
* @param promise
3737
* @returns [err, res]
3838
* @reference https://github.com/arthurfiorette/proposal-safe-assignment-operator

dist/utils/typeSafePromise.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
*
3+
* @param promise
4+
* @returns {success: true, data: TData} | {success: false, error: TError}
5+
* @reference https://github.com/arthurfiorette/proposal-safe-assignment-operator
6+
*
7+
* @example
8+
* ```typescript
9+
* import { typeSafePromise } from 'lesgo/utils';
10+
*
11+
* const res = await typeSafePromise(fetch('https://example.com/'));
12+
* if (res.success) {
13+
* console.log(res.data);
14+
* } else {
15+
* console.error(res.error);
16+
* }
17+
* ```
18+
*/
19+
type ReturnTypeSafePromise<TData, TError> = {
20+
success: true;
21+
data: TData;
22+
error?: undefined;
23+
} | {
24+
success: false;
25+
data?: undefined;
26+
error: TError;
27+
};
28+
declare const typeSafePromise: <TData, TError = Error>(promise: Promise<TData>) => Promise<ReturnTypeSafePromise<TData, TError>>;
29+
export default typeSafePromise;

dist/utils/typeSafePromise.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var __awaiter =
2+
(this && this.__awaiter) ||
3+
function (thisArg, _arguments, P, generator) {
4+
function adopt(value) {
5+
return value instanceof P
6+
? value
7+
: new P(function (resolve) {
8+
resolve(value);
9+
});
10+
}
11+
return new (P || (P = Promise))(function (resolve, reject) {
12+
function fulfilled(value) {
13+
try {
14+
step(generator.next(value));
15+
} catch (e) {
16+
reject(e);
17+
}
18+
}
19+
function rejected(value) {
20+
try {
21+
step(generator['throw'](value));
22+
} catch (e) {
23+
reject(e);
24+
}
25+
}
26+
function step(result) {
27+
result.done
28+
? resolve(result.value)
29+
: adopt(result.value).then(fulfilled, rejected);
30+
}
31+
step((generator = generator.apply(thisArg, _arguments || [])).next());
32+
});
33+
};
34+
const typeSafePromise = promise =>
35+
__awaiter(void 0, void 0, void 0, function* () {
36+
try {
37+
const res = yield promise;
38+
return {
39+
success: true,
40+
data: res,
41+
};
42+
} catch (err) {
43+
return {
44+
success: false,
45+
error: err,
46+
};
47+
}
48+
});
49+
export default typeSafePromise;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lesgo",
3-
"version": "2.1.10",
3+
"version": "2.1.11",
44
"description": "Core framework for lesgo node.js serverless framework.",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import typeSafePromise from '../typeSafePromise';
2+
3+
describe('typeSafePromise', () => {
4+
it('should return success as true with data when promise resolves', async () => {
5+
const mockData = { message: 'Success' };
6+
const promise = Promise.resolve(mockData);
7+
8+
const result = await typeSafePromise(promise);
9+
10+
expect(result).toEqual({
11+
success: true,
12+
data: mockData,
13+
});
14+
});
15+
16+
it('should return success as false with error when promise rejects', async () => {
17+
const mockError = new Error('Failure');
18+
const promise = Promise.reject(mockError);
19+
20+
const result = await typeSafePromise(promise);
21+
22+
expect(result).toEqual({
23+
success: false,
24+
error: mockError,
25+
});
26+
});
27+
});

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export { default as isEmpty } from './isEmpty';
88
export { default as logger } from './logger';
99
export { default as validateFields } from './validateFields';
1010
export { default as safePromise } from './safePromise';
11+
export { default as typeSafePromise } from './typeSafePromise';

src/utils/safePromise.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
*
2+
* @deprecated use typeSafePromise instead
33
* @param promise
44
* @returns [err, res]
55
* @reference https://github.com/arthurfiorette/proposal-safe-assignment-operator

0 commit comments

Comments
 (0)