Skip to content

Commit 6e5dd9a

Browse files
authored
chore(maintenance): migrate commons utility to biome (#2805)
1 parent 91bd574 commit 6e5dd9a

17 files changed

+84
-118
lines changed

packages/commons/package.json

+7-24
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
2020
"build:esm": "tsc --build tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
2121
"build": "npm run build:esm & npm run build:cjs",
22-
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
23-
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
22+
"lint": "biome lint .",
23+
"lint:fix": "biome check --write .",
2424
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
2525
},
2626
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/commons#readme",
@@ -52,39 +52,22 @@
5252
},
5353
"typesVersions": {
5454
"*": {
55-
"typeutils": [
56-
"lib/cjs/typeUtils.d.ts",
57-
"lib/esm/typeUtils.d.ts"
58-
],
59-
"utils/base64": [
60-
"lib/cjs/fromBase64.d.ts",
61-
"lib/esm/fromBase64.d.ts"
62-
],
63-
"types": [
64-
"lib/cjs/types/index.d.ts",
65-
"lib/esm/types/index.d.ts"
66-
]
55+
"typeutils": ["lib/cjs/typeUtils.d.ts", "lib/esm/typeUtils.d.ts"],
56+
"utils/base64": ["lib/cjs/fromBase64.d.ts", "lib/esm/fromBase64.d.ts"],
57+
"types": ["lib/cjs/types/index.d.ts", "lib/esm/types/index.d.ts"]
6758
}
6859
},
6960
"types": "./lib/cjs/index.d.ts",
7061
"main": "./lib/cjs/index.js",
71-
"files": [
72-
"lib"
73-
],
62+
"files": ["lib"],
7463
"repository": {
7564
"type": "git",
7665
"url": "git+https://github.com/aws-powertools/powertools-lambda-typescript.git"
7766
},
7867
"bugs": {
7968
"url": "https://github.com/aws-powertools/powertools-lambda-typescript/issues"
8069
},
81-
"keywords": [
82-
"aws",
83-
"lambda",
84-
"powertools",
85-
"serverless",
86-
"nodejs"
87-
],
70+
"keywords": ["aws", "lambda", "powertools", "serverless", "nodejs"],
8871
"devDependencies": {
8972
"@aws-lambda-powertools/testing-utils": "file:../testing"
9073
}

packages/commons/src/awsSdkUtils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { PT_VERSION } from './version.js';
21
import type { MiddlewareArgsLike, SdkClient } from './types/awsSdk.js';
2+
import { PT_VERSION } from './version.js';
33

44
const EXEC_ENV = process.env.AWS_EXECUTION_ENV || 'NA';
55
const middlewareOptions = {
@@ -96,7 +96,7 @@ const addUserAgentMiddleware = (client: unknown, feature: string): void => {
9696
);
9797
} else {
9898
throw new Error(
99-
`The client provided does not match the expected interface`
99+
'The client provided does not match the expected interface'
100100
);
101101
}
102102
} catch (error) {

packages/commons/src/config/EnvironmentVariablesService.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ class EnvironmentVariablesService implements ConfigServiceInterface {
108108

109109
const xRayTraceData: Record<string, string> = {};
110110

111-
xRayTraceEnv.split(';').forEach((field) => {
111+
for (const field of xRayTraceEnv.split(';')) {
112112
const [key, value] = field.split('=');
113113

114114
xRayTraceData[key] = value;
115-
});
115+
}
116116

117117
return xRayTraceData;
118118
}

packages/commons/src/fromBase64.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ const BASE64_REGEX = /^[A-Za-z0-9+/]*={0,2}$/;
2222
*/
2323
const fromBase64 = (input: string, encoding?: BufferEncoding): Uint8Array => {
2424
if ((input.length * 3) % 4 !== 0) {
25-
throw new TypeError(`Incorrect padding on base64 string.`);
25+
throw new TypeError('Incorrect padding on base64 string.');
2626
}
2727
if (!BASE64_REGEX.exec(input)) {
28-
throw new TypeError(`Invalid base64 string.`);
28+
throw new TypeError('Invalid base64 string.');
2929
}
3030
const buffer = encoding ? Buffer.from(input, encoding) : Buffer.from(input);
3131

packages/commons/src/middleware/cleanupMiddlewares.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import type { CleanupFunction, MiddyLikeRequest } from '../types/middy.js';
12
import {
2-
TRACER_KEY,
3-
METRICS_KEY,
4-
LOGGER_KEY,
53
IDEMPOTENCY_KEY,
4+
LOGGER_KEY,
5+
METRICS_KEY,
6+
TRACER_KEY,
67
} from './constants.js';
7-
import type { MiddyLikeRequest, CleanupFunction } from '../types/middy.js';
88

99
/**
1010
* Typeguard to assert that an object is of Function type.

packages/commons/src/typeUtils.ts

+22-13
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,21 @@ const isIntegerNumber = (value: unknown): value is number => {
9999
const isTruthy = (value: unknown): boolean => {
100100
if (isString(value)) {
101101
return value !== '';
102-
} else if (isNumber(value)) {
102+
}
103+
if (isNumber(value)) {
103104
return value !== 0;
104-
} else if (typeof value === 'boolean') {
105+
}
106+
if (typeof value === 'boolean') {
105107
return value;
106-
} else if (Array.isArray(value)) {
108+
}
109+
if (Array.isArray(value)) {
107110
return value.length > 0;
108-
} else if (isRecord(value)) {
111+
}
112+
if (isRecord(value)) {
109113
return Object.keys(value).length > 0;
110-
} else {
111-
return false;
112114
}
115+
116+
return false;
113117
};
114118

115119
/**
@@ -168,19 +172,24 @@ const isNullOrUndefined = (value: unknown): value is null | undefined => {
168172
const getType = (value: unknown): string => {
169173
if (Array.isArray(value)) {
170174
return 'array';
171-
} else if (isRecord(value)) {
175+
}
176+
if (isRecord(value)) {
172177
return 'object';
173-
} else if (isString(value)) {
178+
}
179+
if (isString(value)) {
174180
return 'string';
175-
} else if (isNumber(value)) {
181+
}
182+
if (isNumber(value)) {
176183
return 'number';
177-
} else if (typeof value === 'boolean') {
184+
}
185+
if (typeof value === 'boolean') {
178186
return 'boolean';
179-
} else if (isNull(value)) {
187+
}
188+
if (isNull(value)) {
180189
return 'null';
181-
} else {
182-
return 'unknown';
183190
}
191+
192+
return 'unknown';
184193
};
185194

186195
/**

packages/commons/tests/tsconfig.json

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
{
2-
"extends": "../tsconfig.json",
3-
"compilerOptions": {
4-
"rootDir": "../",
5-
"noEmit": true
6-
},
7-
"include": [
8-
"../src/**/*",
9-
"./**/*",
10-
]
11-
}
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "../",
5+
"noEmit": true
6+
},
7+
"include": ["../src/**/*", "./**/*"]
8+
}

packages/commons/tests/unit/EnvironmentVariablesService.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('Class: EnvironmentVariablesService', () => {
3232

3333
test('When the variable IS NOT present, it returns an empty string', () => {
3434
// Prepare
35-
delete process.env.CUSTOM_VARIABLE;
35+
process.env.CUSTOM_VARIABLE = undefined;
3636
const service = new EnvironmentVariablesService();
3737

3838
// Act
@@ -84,7 +84,7 @@ describe('Class: EnvironmentVariablesService', () => {
8484

8585
test('It returns the value of the Root X-Ray segment ID properly formatted', () => {
8686
// Prepare
87-
delete process.env._X_AMZN_TRACE_ID;
87+
process.env._X_AMZN_TRACE_ID = undefined;
8888
const service = new EnvironmentVariablesService();
8989

9090
// Act
@@ -124,7 +124,7 @@ describe('Class: EnvironmentVariablesService', () => {
124124

125125
it('It returns false when no _X_AMZN_TRACE_ID environment variable is present', () => {
126126
// Prepare
127-
delete process.env._X_AMZN_TRACE_ID;
127+
process.env._X_AMZN_TRACE_ID = undefined;
128128
const service = new EnvironmentVariablesService();
129129

130130
// Act

packages/commons/tests/unit/LambdaInterface.test.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
*
44
* @group unit/commons/lambdaInterface
55
*/
6-
import { Handler } from 'aws-lambda';
7-
import { Callback, Context } from 'aws-lambda';
86
import context from '@aws-lambda-powertools/testing-utils/context';
7+
import type { Callback, Context, Handler } from 'aws-lambda';
98
import type {
10-
SyncHandler,
119
AsyncHandler,
1210
LambdaInterface,
11+
SyncHandler,
1312
} from '../../src/types/index.js';
1413

1514
describe('LambdaInterface with arrow function', () => {
@@ -105,10 +104,8 @@ describe('LambdaInterface with decorator', () => {
105104
callback,
106105
]);
107106
console.log(`Invoked ${String(_propertyKey)}`);
108-
} catch (error) {
109-
throw error;
110107
} finally {
111-
console.log(`Finally from decorator`);
108+
console.log('Finally from decorator');
112109
}
113110

114111
return result;

packages/commons/tests/unit/Utility.test.ts

-16
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ describe('Class: Utility', () => {
1414
describe('Method: getDefaultServiceName', () => {
1515
test('it should return the default service name', () => {
1616
class PowerTool extends Utility {
17-
public constructor() {
18-
super();
19-
}
20-
2117
public dummyMethod(): string {
2218
return this.getDefaultServiceName();
2319
}
@@ -56,10 +52,6 @@ describe('Class: Utility', () => {
5652
test('when called multiple times on a child class, it returns true the first time, then false afterwards', () => {
5753
// Prepare
5854
class PowerTool extends Utility {
59-
public constructor() {
60-
super();
61-
}
62-
6355
public dummyMethod(): boolean {
6456
return this.getColdStart();
6557
}
@@ -115,10 +107,6 @@ describe('Class: Utility', () => {
115107
test('when called multiple times on a child class, it returns true the first time, then false afterwards', () => {
116108
// Prepare
117109
class PowerTool extends Utility {
118-
public constructor() {
119-
super();
120-
}
121-
122110
public dummyMethod(): boolean {
123111
return this.isColdStart();
124112
}
@@ -149,10 +137,6 @@ describe('Class: Utility', () => {
149137

150138
describe('Method: isValidServiceName', () => {
151139
class PowerTool extends Utility {
152-
public constructor() {
153-
super();
154-
}
155-
156140
public dummyMethod(name: string): boolean {
157141
return this.isValidServiceName(name);
158142
}

packages/commons/tests/unit/awsSdkUtils.test.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
/**
2+
* Test AWS SDK utilities
3+
*
4+
* @group unit/commons/awsSdkUtils
5+
*/
6+
import { customUserAgentMiddleware } from '../../src/awsSdkUtils.js';
17
import {
28
addUserAgentMiddleware,
39
isSdkClient,
410
PT_VERSION as version,
511
} from '../../src/index.js';
6-
import { customUserAgentMiddleware } from '../../src/awsSdkUtils.js';
712

813
describe('Helpers: awsSdk', () => {
914
describe('Function: userAgentMiddleware', () => {

packages/commons/tests/unit/cleanupMiddlewares.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
*
44
* @group unit/commons/cleanupMiddlewares
55
*/
6+
import context from '@aws-lambda-powertools/testing-utils/context';
67
import {
7-
cleanupMiddlewares,
8-
TRACER_KEY,
9-
METRICS_KEY,
10-
LOGGER_KEY,
118
IDEMPOTENCY_KEY,
9+
LOGGER_KEY,
10+
METRICS_KEY,
11+
TRACER_KEY,
12+
cleanupMiddlewares,
1213
} from '../../src/index.js';
13-
import context from '@aws-lambda-powertools/testing-utils/context';
1414

1515
describe('Function: cleanupMiddlewares', () => {
1616
it('calls the cleanup function that are present', async () => {

packages/commons/tests/unit/fromBase64.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('Function: fromBase64', () => {
3434

3535
// Assess
3636
expect(result).toThrow(TypeError);
37-
expect(result).toThrow(`Incorrect padding on base64 string.`);
37+
expect(result).toThrow('Incorrect padding on base64 string.');
3838
});
3939

4040
it('throws a TypeError when the base64 string is invalid', () => {
@@ -46,7 +46,7 @@ describe('Function: fromBase64', () => {
4646

4747
// Assess
4848
expect(result).toThrow(TypeError);
49-
expect(result).toThrow(`Invalid base64 string.`);
49+
expect(result).toThrow('Invalid base64 string.');
5050
});
5151

5252
it('uses the provided encoding to create the Uint8Array', () => {

packages/commons/tests/unit/typeUtils.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
* @group unit/commons/typeUtils
55
*/
66
import {
7-
isRecord,
8-
isTruthy,
9-
isNullOrUndefined,
10-
isString,
11-
isNumber,
7+
getType,
128
isIntegerNumber,
139
isNull,
14-
getType,
10+
isNullOrUndefined,
11+
isNumber,
12+
isRecord,
1513
isStrictEqual,
14+
isString,
15+
isTruthy,
1616
} from '../../src/index.js';
1717

1818
describe('Functions: typeUtils', () => {

packages/commons/tsconfig.esm.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,5 @@
66
"rootDir": "./src",
77
"tsBuildInfoFile": ".tsbuildinfo/esm.json"
88
},
9-
"include": [
10-
"./src/**/*"
11-
]
12-
}
9+
"include": ["./src/**/*"]
10+
}

0 commit comments

Comments
 (0)