Skip to content

Commit 6f96711

Browse files
authored
feat(parameters): review types and exports (#1528)
* feat: base types * chore: getMultiple void return * wip: ssm/dynamo * chore: add missing await * fix: types import in ssm * feat: imports * feat: imports * tests: coverage * refactor: transformValue * chore: test coverage for commons * docs: typedocs * chore: remove isUint8Array for nodejs14 compat * docs: explained transformValue fn
1 parent d47c3ec commit 6f96711

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1073
-668
lines changed

Diff for: packages/commons/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './config';
44
export * as ContextExamples from './samples/resources/contexts';
55
export * as Events from './samples/resources/events';
66
export * from './types/middy';
7+
export * from './types/utils';

Diff for: packages/commons/src/types/utils.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Returns true if the passed value is a record (object).
3+
*
4+
* @param value
5+
*/
6+
const isRecord = (value: unknown): value is Record<string, unknown> => {
7+
return (
8+
Object.prototype.toString.call(value) === '[object Object]' &&
9+
!Object.is(value, null)
10+
);
11+
};
12+
13+
/**
14+
* Returns true if the passed value is truthy.
15+
*
16+
* @param value
17+
*/
18+
const isTruthy = (value: unknown): boolean => {
19+
if (typeof value === 'string') {
20+
return value !== '';
21+
} else if (typeof value === 'number') {
22+
return value !== 0;
23+
} else if (typeof value === 'boolean') {
24+
return value;
25+
} else if (Array.isArray(value)) {
26+
return value.length > 0;
27+
} else if (isRecord(value)) {
28+
return Object.keys(value).length > 0;
29+
} else {
30+
return false;
31+
}
32+
};
33+
34+
/**
35+
* Returns true if the passed value is null or undefined.
36+
*
37+
* @param value
38+
*/
39+
const isNullOrUndefined = (value: unknown): value is null | undefined => {
40+
return Object.is(value, null) || Object.is(value, undefined);
41+
};
42+
43+
/**
44+
* Returns true if the passed value is a string.
45+
* @param value
46+
* @returns
47+
*/
48+
const isString = (value: unknown): value is string => {
49+
return typeof value === 'string';
50+
};
51+
52+
export { isRecord, isString, isTruthy, isNullOrUndefined };
53+
54+
type JSONPrimitive = string | number | boolean | null | undefined;
55+
type JSONValue = JSONPrimitive | JSONObject | JSONArray;
56+
type JSONObject = { [key: string]: JSONValue };
57+
type JSONArray = Array<JSONValue>;
58+
59+
export type { JSONPrimitive, JSONValue, JSONObject, JSONArray };

Diff for: packages/commons/tests/unit/utils.test.ts

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* Test utils functions
3+
*
4+
* @group unit/commons/utils
5+
*/
6+
import {
7+
isRecord,
8+
isTruthy,
9+
isNullOrUndefined,
10+
isString,
11+
} from '../../src/types/utils';
12+
13+
describe('Functions: utils', () => {
14+
beforeEach(() => {
15+
jest.clearAllMocks();
16+
jest.resetModules();
17+
});
18+
19+
describe('Function: isRecord', () => {
20+
it('returns true when the passed object is a Record', () => {
21+
// Prepare
22+
const obj = { a: 1, b: 2, c: 3 };
23+
24+
// Act
25+
const result = isRecord(obj);
26+
27+
// Assert
28+
expect(result).toBe(true);
29+
});
30+
31+
it('returns false when the passed object is not a Record', () => {
32+
// Prepare
33+
const obj = [1, 2, 3];
34+
35+
// Act
36+
const result = isRecord(obj);
37+
38+
// Assert
39+
expect(result).toBe(false);
40+
});
41+
});
42+
43+
describe('Function: isTruthy', () => {
44+
it.each(['hello', 1, true, [1], { foo: 1 }])(
45+
'returns true when the passed value is truthy',
46+
(testValue) => {
47+
// Prepare
48+
const value = testValue;
49+
50+
// Act
51+
const result = isTruthy(value);
52+
53+
// Assert
54+
expect(result).toBe(true);
55+
}
56+
);
57+
58+
it.each(['', 0, false, [], {}, Symbol])(
59+
'returns true when the passed value is falsy',
60+
(testValue) => {
61+
// Prepare
62+
const value = testValue;
63+
64+
// Act
65+
const result = isTruthy(value);
66+
67+
// Assert
68+
expect(result).toBe(false);
69+
}
70+
);
71+
});
72+
73+
describe('Function: isNullOrUndefined', () => {
74+
it('returns true when the passed value is null or undefined', () => {
75+
// Prepare
76+
const value = undefined;
77+
78+
// Act
79+
const result = isNullOrUndefined(value);
80+
81+
// Assert
82+
expect(result).toBe(true);
83+
});
84+
85+
it('returns false when the passed value is not null or undefined', () => {
86+
// Prepare
87+
const value = 'hello';
88+
89+
// Act
90+
const result = isNullOrUndefined(value);
91+
92+
// Assert
93+
expect(result).toBe(false);
94+
});
95+
});
96+
97+
describe('Function: isString', () => {
98+
it('returns true when the passed value is a string', () => {
99+
// Prepare
100+
const value = 'hello';
101+
102+
// Act
103+
const result = isString(value);
104+
105+
// Assert
106+
expect(result).toBe(true);
107+
});
108+
109+
it('returns false when the passed value is not a string', () => {
110+
// Prepare
111+
const value = 123;
112+
113+
// Act
114+
const result = isString(value);
115+
116+
// Assert
117+
expect(result).toBe(false);
118+
});
119+
});
120+
});

Diff for: packages/parameters/package.json

+38-10
Original file line numberDiff line numberDiff line change
@@ -34,57 +34,85 @@
3434
"import": "./lib/index.js",
3535
"require": "./lib/index.js"
3636
},
37+
"./base/types": {
38+
"import": "./lib/types/BaseProvider.d.ts",
39+
"require": "./lib/types/BaseProvider.d.ts"
40+
},
3741
"./base": {
3842
"import": "./lib/base/index.js",
3943
"require": "./lib/base/index.js"
4044
},
45+
"./ssm/types": {
46+
"import": "./lib/types/SSMProvider.d.ts",
47+
"require": "./lib/types/SSMProvider.d.ts"
48+
},
4149
"./ssm": {
4250
"import": "./lib/ssm/index.js",
4351
"require": "./lib/ssm/index.js"
4452
},
53+
"./secrets/types": {
54+
"import": "./lib/types/SecretsProvider.d.ts",
55+
"require": "./lib/types/SecretsProvider.d.ts"
56+
},
4557
"./secrets": {
4658
"import": "./lib/secrets/index.js",
4759
"require": "./lib/secrets/index.js"
4860
},
61+
"./dynamodb/types": {
62+
"import": "./lib/types/AppConfigProvider.d.ts",
63+
"require": "./lib/types/AppConfigProvider.d.ts"
64+
},
4965
"./dynamodb": {
5066
"import": "./lib/dynamodb/index.js",
5167
"require": "./lib/dynamodb/index.js"
5268
},
69+
"./appconfig/types": {
70+
"import": "./lib/appconfig/index.js",
71+
"require": "./lib/appconfig/index.js"
72+
},
5373
"./appconfig": {
5474
"import": "./lib/appconfig/index.js",
5575
"require": "./lib/appconfig/index.js"
5676
},
5777
"./errors": {
58-
"import": "./lib/Exceptions.js",
59-
"require": "./lib/Exceptions.js"
60-
},
61-
"./types": {
62-
"import": "./lib/types/index.d.ts",
63-
"require": "./lib/types/index.d.ts"
78+
"import": "./lib/errors.js",
79+
"require": "./lib/errors.js"
6480
}
6581
},
6682
"typesVersions": {
6783
"*": {
84+
"base/types": [
85+
"lib/types/BaseProvider.d.ts"
86+
],
6887
"base": [
6988
"lib/base/index.d.ts"
7089
],
90+
"ssm/types": [
91+
"lib/types/SSMProvider.d.ts"
92+
],
7193
"ssm": [
7294
"lib/ssm/index.d.ts"
7395
],
96+
"secrets/types": [
97+
"lib/types/SecretsProvider.d.ts"
98+
],
7499
"secrets": [
75100
"lib/secrets/index.d.ts"
76101
],
102+
"dynamodb/types": [
103+
"./lib/types/DynamoDBProvider.d.ts"
104+
],
77105
"dynamodb": [
78106
"lib/dynamodb/index.d.ts"
79107
],
108+
"appconfig/types": [
109+
"./lib/types/AppConfigProvider.d.ts"
110+
],
80111
"appconfig": [
81112
"lib/appconfig/index.d.ts"
82113
],
83114
"errors": [
84-
"lib/Exceptions.d.ts"
85-
],
86-
"types": [
87-
"lib/types/index.d.ts"
115+
"lib/errors.d.ts"
88116
]
89117
}
90118
},

0 commit comments

Comments
 (0)