Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(maintenance): migrate commons utility to biome #2805

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions packages/commons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
"build:esm": "tsc --build tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
"build": "npm run build:esm & npm run build:cjs",
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
"lint": "biome lint .",
"lint:fix": "biome check --write .",
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
},
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/commons#readme",
Expand Down Expand Up @@ -52,39 +52,22 @@
},
"typesVersions": {
"*": {
"typeutils": [
"lib/cjs/typeUtils.d.ts",
"lib/esm/typeUtils.d.ts"
],
"utils/base64": [
"lib/cjs/fromBase64.d.ts",
"lib/esm/fromBase64.d.ts"
],
"types": [
"lib/cjs/types/index.d.ts",
"lib/esm/types/index.d.ts"
]
"typeutils": ["lib/cjs/typeUtils.d.ts", "lib/esm/typeUtils.d.ts"],
"utils/base64": ["lib/cjs/fromBase64.d.ts", "lib/esm/fromBase64.d.ts"],
"types": ["lib/cjs/types/index.d.ts", "lib/esm/types/index.d.ts"]
}
},
"types": "./lib/cjs/index.d.ts",
"main": "./lib/cjs/index.js",
"files": [
"lib"
],
"files": ["lib"],
"repository": {
"type": "git",
"url": "git+https://github.com/aws-powertools/powertools-lambda-typescript.git"
},
"bugs": {
"url": "https://github.com/aws-powertools/powertools-lambda-typescript/issues"
},
"keywords": [
"aws",
"lambda",
"powertools",
"serverless",
"nodejs"
],
"keywords": ["aws", "lambda", "powertools", "serverless", "nodejs"],
"devDependencies": {
"@aws-lambda-powertools/testing-utils": "file:../testing"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/commons/src/awsSdkUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PT_VERSION } from './version.js';
import type { MiddlewareArgsLike, SdkClient } from './types/awsSdk.js';
import { PT_VERSION } from './version.js';

const EXEC_ENV = process.env.AWS_EXECUTION_ENV || 'NA';
const middlewareOptions = {
Expand Down Expand Up @@ -96,7 +96,7 @@ const addUserAgentMiddleware = (client: unknown, feature: string): void => {
);
} else {
throw new Error(
`The client provided does not match the expected interface`
'The client provided does not match the expected interface'
);
}
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions packages/commons/src/config/EnvironmentVariablesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ class EnvironmentVariablesService implements ConfigServiceInterface {

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

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

xRayTraceData[key] = value;
});
}

return xRayTraceData;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/commons/src/fromBase64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ const BASE64_REGEX = /^[A-Za-z0-9+/]*={0,2}$/;
*/
const fromBase64 = (input: string, encoding?: BufferEncoding): Uint8Array => {
if ((input.length * 3) % 4 !== 0) {
throw new TypeError(`Incorrect padding on base64 string.`);
throw new TypeError('Incorrect padding on base64 string.');
}
if (!BASE64_REGEX.exec(input)) {
throw new TypeError(`Invalid base64 string.`);
throw new TypeError('Invalid base64 string.');
}
const buffer = encoding ? Buffer.from(input, encoding) : Buffer.from(input);

Expand Down
8 changes: 4 additions & 4 deletions packages/commons/src/middleware/cleanupMiddlewares.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { CleanupFunction, MiddyLikeRequest } from '../types/middy.js';
import {
TRACER_KEY,
METRICS_KEY,
LOGGER_KEY,
IDEMPOTENCY_KEY,
LOGGER_KEY,
METRICS_KEY,
TRACER_KEY,
} from './constants.js';
import type { MiddyLikeRequest, CleanupFunction } from '../types/middy.js';

/**
* Typeguard to assert that an object is of Function type.
Expand Down
35 changes: 22 additions & 13 deletions packages/commons/src/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,21 @@ const isIntegerNumber = (value: unknown): value is number => {
const isTruthy = (value: unknown): boolean => {
if (isString(value)) {
return value !== '';
} else if (isNumber(value)) {
}
if (isNumber(value)) {
return value !== 0;
} else if (typeof value === 'boolean') {
}
if (typeof value === 'boolean') {
return value;
} else if (Array.isArray(value)) {
}
if (Array.isArray(value)) {
return value.length > 0;
} else if (isRecord(value)) {
}
if (isRecord(value)) {
return Object.keys(value).length > 0;
} else {
return false;
}

return false;
};

/**
Expand Down Expand Up @@ -168,19 +172,24 @@ const isNullOrUndefined = (value: unknown): value is null | undefined => {
const getType = (value: unknown): string => {
if (Array.isArray(value)) {
return 'array';
} else if (isRecord(value)) {
}
if (isRecord(value)) {
return 'object';
} else if (isString(value)) {
}
if (isString(value)) {
return 'string';
} else if (isNumber(value)) {
}
if (isNumber(value)) {
return 'number';
} else if (typeof value === 'boolean') {
}
if (typeof value === 'boolean') {
return 'boolean';
} else if (isNull(value)) {
}
if (isNull(value)) {
return 'null';
} else {
return 'unknown';
}

return 'unknown';
};

/**
Expand Down
17 changes: 7 additions & 10 deletions packages/commons/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "../",
"noEmit": true
},
"include": [
"../src/**/*",
"./**/*",
]
}
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "../",
"noEmit": true
},
"include": ["../src/**/*", "./**/*"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Class: EnvironmentVariablesService', () => {

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

// Act
Expand Down Expand Up @@ -84,7 +84,7 @@ describe('Class: EnvironmentVariablesService', () => {

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

// Act
Expand Down Expand Up @@ -124,7 +124,7 @@ describe('Class: EnvironmentVariablesService', () => {

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

// Act
Expand Down
9 changes: 3 additions & 6 deletions packages/commons/tests/unit/LambdaInterface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
*
* @group unit/commons/lambdaInterface
*/
import { Handler } from 'aws-lambda';
import { Callback, Context } from 'aws-lambda';
import context from '@aws-lambda-powertools/testing-utils/context';
import type { Callback, Context, Handler } from 'aws-lambda';
import type {
SyncHandler,
AsyncHandler,
LambdaInterface,
SyncHandler,
} from '../../src/types/index.js';

describe('LambdaInterface with arrow function', () => {
Expand Down Expand Up @@ -105,10 +104,8 @@ describe('LambdaInterface with decorator', () => {
callback,
]);
console.log(`Invoked ${String(_propertyKey)}`);
} catch (error) {
throw error;
} finally {
console.log(`Finally from decorator`);
console.log('Finally from decorator');
}

return result;
Expand Down
16 changes: 0 additions & 16 deletions packages/commons/tests/unit/Utility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ describe('Class: Utility', () => {
describe('Method: getDefaultServiceName', () => {
test('it should return the default service name', () => {
class PowerTool extends Utility {
public constructor() {
super();
}

public dummyMethod(): string {
return this.getDefaultServiceName();
}
Expand Down Expand Up @@ -56,10 +52,6 @@ describe('Class: Utility', () => {
test('when called multiple times on a child class, it returns true the first time, then false afterwards', () => {
// Prepare
class PowerTool extends Utility {
public constructor() {
super();
}

public dummyMethod(): boolean {
return this.getColdStart();
}
Expand Down Expand Up @@ -115,10 +107,6 @@ describe('Class: Utility', () => {
test('when called multiple times on a child class, it returns true the first time, then false afterwards', () => {
// Prepare
class PowerTool extends Utility {
public constructor() {
super();
}

public dummyMethod(): boolean {
return this.isColdStart();
}
Expand Down Expand Up @@ -149,10 +137,6 @@ describe('Class: Utility', () => {

describe('Method: isValidServiceName', () => {
class PowerTool extends Utility {
public constructor() {
super();
}

public dummyMethod(name: string): boolean {
return this.isValidServiceName(name);
}
Expand Down
7 changes: 6 additions & 1 deletion packages/commons/tests/unit/awsSdkUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/**
* Test AWS SDK utilities
*
* @group unit/commons/awsSdkUtils
*/
import { customUserAgentMiddleware } from '../../src/awsSdkUtils.js';
import {
addUserAgentMiddleware,
isSdkClient,
PT_VERSION as version,
} from '../../src/index.js';
import { customUserAgentMiddleware } from '../../src/awsSdkUtils.js';

describe('Helpers: awsSdk', () => {
describe('Function: userAgentMiddleware', () => {
Expand Down
10 changes: 5 additions & 5 deletions packages/commons/tests/unit/cleanupMiddlewares.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
*
* @group unit/commons/cleanupMiddlewares
*/
import context from '@aws-lambda-powertools/testing-utils/context';
import {
cleanupMiddlewares,
TRACER_KEY,
METRICS_KEY,
LOGGER_KEY,
IDEMPOTENCY_KEY,
LOGGER_KEY,
METRICS_KEY,
TRACER_KEY,
cleanupMiddlewares,
} from '../../src/index.js';
import context from '@aws-lambda-powertools/testing-utils/context';

describe('Function: cleanupMiddlewares', () => {
it('calls the cleanup function that are present', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/commons/tests/unit/fromBase64.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Function: fromBase64', () => {

// Assess
expect(result).toThrow(TypeError);
expect(result).toThrow(`Incorrect padding on base64 string.`);
expect(result).toThrow('Incorrect padding on base64 string.');
});

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

// Assess
expect(result).toThrow(TypeError);
expect(result).toThrow(`Invalid base64 string.`);
expect(result).toThrow('Invalid base64 string.');
});

it('uses the provided encoding to create the Uint8Array', () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/commons/tests/unit/typeUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
* @group unit/commons/typeUtils
*/
import {
isRecord,
isTruthy,
isNullOrUndefined,
isString,
isNumber,
getType,
isIntegerNumber,
isNull,
getType,
isNullOrUndefined,
isNumber,
isRecord,
isStrictEqual,
isString,
isTruthy,
} from '../../src/index.js';

describe('Functions: typeUtils', () => {
Expand Down
6 changes: 2 additions & 4 deletions packages/commons/tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@
"rootDir": "./src",
"tsBuildInfoFile": ".tsbuildinfo/esm.json"
},
"include": [
"./src/**/*"
]
}
"include": ["./src/**/*"]
}
Loading