Skip to content

Commit

Permalink
chore(maintenance): migrate parameters utility to biome (#2812)
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamorosi authored Jul 23, 2024
1 parent 4d44b91 commit abd3832
Show file tree
Hide file tree
Showing 39 changed files with 194 additions and 253 deletions.
28 changes: 7 additions & 21 deletions packages/parameters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,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/parameters#readme",
Expand Down Expand Up @@ -89,26 +89,17 @@
"lib/cjs/types/BaseProvider.d.ts",
"lib/esm/types/BaseProvider.d.ts"
],
"base": [
"lib/cjs/base/index.d.ts",
"lib/esm/base/index.d.ts"
],
"base": ["lib/cjs/base/index.d.ts", "lib/esm/base/index.d.ts"],
"ssm/types": [
"lib/cjs/types/SSMProvider.d.ts",
"lib/esm/types/SSMProvider.d.ts"
],
"ssm": [
"lib/cjs/ssm/index.d.ts",
"lib/esm/ssm/index.d.ts"
],
"ssm": ["lib/cjs/ssm/index.d.ts", "lib/esm/ssm/index.d.ts"],
"secrets/types": [
"lib/cjs/types/SecretsProvider.d.ts",
"lib/esm/types/SecretsProvider.d.ts"
],
"secrets": [
"lib/cjs/secrets/index.d.ts",
"lib/esm/secrets/index.d.ts"
],
"secrets": ["lib/cjs/secrets/index.d.ts", "lib/esm/secrets/index.d.ts"],
"dynamodb/types": [
"./lib/cjs/types/DynamoDBProvider.d.ts",
"./lib/esm/types/DynamoDBProvider.d.ts"
Expand All @@ -125,17 +116,12 @@
"lib/cjs/appconfig/index.d.ts",
"lib/esm/appconfig/index.d.ts"
],
"errors": [
"lib/cjs/errors.d.ts",
"lib/esm/errors.d.ts"
]
"errors": ["lib/cjs/errors.d.ts", "lib/esm/errors.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"
Expand Down
11 changes: 6 additions & 5 deletions packages/parameters/src/appconfig/AppConfigProvider.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { BaseProvider } from '../base/BaseProvider.js';
import {
AppConfigDataClient,
StartConfigurationSessionCommand,
GetLatestConfigurationCommand,
StartConfigurationSessionCommand,
} from '@aws-sdk/client-appconfigdata';
import type { StartConfigurationSessionCommandInput } from '@aws-sdk/client-appconfigdata';
import { BaseProvider } from '../base/BaseProvider.js';
import { APPCONFIG_TOKEN_EXPIRATION } from '../constants.js';
import type {
AppConfigProviderOptions,
AppConfigGetOptions,
AppConfigGetOutput,
AppConfigProviderOptions,
} from '../types/AppConfigProvider.js';
import { APPCONFIG_TOKEN_EXPIRATION } from '../constants.js';

/**
* ## Intro
Expand Down Expand Up @@ -292,6 +292,7 @@ class AppConfigProvider extends BaseProvider {
): Promise<Uint8Array | undefined> {
if (
!this.configurationTokenStore.has(name) ||
// biome-ignore lint/style/noNonNullAssertion: we check if the value is in the map before accessing it
this.configurationTokenStore.get(name)!.expiration <= Date.now()
) {
const sessionOptions: StartConfigurationSessionCommandInput = {
Expand Down Expand Up @@ -356,7 +357,7 @@ class AppConfigProvider extends BaseProvider {
protected async _getMultiple(
_path: string,
_sdkOptions?: unknown
): Promise<void> {
): Promise<Record<string, unknown> | undefined> {
throw new Error('Method not implemented.');
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/parameters/src/appconfig/getAppConfig.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AppConfigProvider } from './AppConfigProvider.js';
import { DEFAULT_PROVIDERS } from '../base/index.js';
import type {
AppConfigGetOutput,
GetAppConfigOptions,
} from '../types/AppConfigProvider.js';
import { AppConfigProvider } from './AppConfigProvider.js';

/**
* ## Intro
Expand Down
21 changes: 10 additions & 11 deletions packages/parameters/src/base/BaseProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import {
isSdkClient,
isString,
} from '@aws-lambda-powertools/commons';
import { GetOptions } from './GetOptions.js';
import { GetMultipleOptions } from './GetMultipleOptions.js';
import { ExpirableValue } from './ExpirableValue.js';
import { GetParameterError, TransformParameterError } from '../errors.js';
import { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
import { transformValue } from './transformValue.js';
import { GetParameterError, TransformParameterError } from '../errors.js';
import type {
BaseProviderInterface,
GetMultipleOptionsInterface,
GetOptionsInterface,
} from '../types/BaseProvider.js';
import { ExpirableValue } from './ExpirableValue.js';
import { GetMultipleOptions } from './GetMultipleOptions.js';
import { GetOptions } from './GetOptions.js';
import { transformValue } from './transformValue.js';

/**
* Base class for all providers.
Expand Down Expand Up @@ -97,7 +97,7 @@ abstract class BaseProvider implements BaseProviderInterface {
name: string,
options?: GetOptionsInterface
): Promise<unknown | undefined> {
const configs = new GetOptions(options, this.envVarsService);
const configs = new GetOptions(this.envVarsService, options);
const key = [name, configs.transform].toString();

if (!configs.forceFetch && !this.hasKeyExpiredInCache(key)) {
Expand Down Expand Up @@ -136,16 +136,15 @@ abstract class BaseProvider implements BaseProviderInterface {
path: string,
options?: GetMultipleOptionsInterface
): Promise<unknown> {
const configs = new GetMultipleOptions(options, this.envVarsService);
const configs = new GetMultipleOptions(this.envVarsService, options);
const key = [path, configs.transform].toString();

if (!configs.forceFetch && !this.hasKeyExpiredInCache(key)) {
// If the code enters in this block, then the key must exist & not have been expired
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
// biome-ignore lint/style/noNonNullAssertion: If the code enters in this block, then the key must exist & not have been expired
return this.store.get(key)!.value as Record<string, unknown>;
}

let values;
let values: Record<string, unknown> | undefined;
try {
values = await this._getMultiple(path, options);
if (!isRecord(values)) {
Expand Down Expand Up @@ -216,7 +215,7 @@ abstract class BaseProvider implements BaseProviderInterface {
protected abstract _getMultiple(
path: string,
options?: unknown
): Promise<Record<string, unknown> | void>;
): Promise<Record<string, unknown> | undefined>;
}

export { BaseProvider };
10 changes: 5 additions & 5 deletions packages/parameters/src/base/GetMultipleOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetOptions } from './GetOptions.js';
import { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
import type { GetMultipleOptionsInterface } from '../types/BaseProvider.js';
import { GetOptions } from './GetOptions.js';

/**
* Options for the `getMultiple` method.
Expand All @@ -14,10 +14,10 @@ class GetMultipleOptions
public throwOnTransformError = false;

public constructor(
options: GetMultipleOptionsInterface = {},
envVarsService: EnvironmentVariablesService
envVarsService: EnvironmentVariablesService,
options: GetMultipleOptionsInterface = {}
) {
super(options, envVarsService);
super(envVarsService, options);

if (options.throwOnTransformError !== undefined) {
this.throwOnTransformError = options.throwOnTransformError;
Expand Down
6 changes: 3 additions & 3 deletions packages/parameters/src/base/GetOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
import { DEFAULT_MAX_AGE_SECS } from '../constants.js';
import type {
GetOptionsInterface,
Expand All @@ -17,8 +17,8 @@ class GetOptions implements GetOptionsInterface {
public transform?: TransformOptions;

public constructor(
options: GetOptionsInterface = {},
envVarsService: EnvironmentVariablesService
envVarsService: EnvironmentVariablesService,
options: GetOptionsInterface = {}
) {
Object.assign(this, options);

Expand Down
18 changes: 11 additions & 7 deletions packages/parameters/src/base/transformValue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
import { isString } from '@aws-lambda-powertools/commons';
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
import { fromBase64 } from '@aws-lambda-powertools/commons/utils/base64';
import {
TRANSFORM_METHOD_BINARY,
Expand Down Expand Up @@ -58,16 +58,20 @@ const transformValue = (

try {
// If the value is a Uint8Array, decode it to a string first
if (value instanceof Uint8Array) {
value = new TextDecoder('utf-8').decode(value);
}
const valueToTransform =
value instanceof Uint8Array
? new TextDecoder('utf-8').decode(value)
: value;

// If the transform is `json` or `auto` and the key ends with `.json`, parse the value as JSON
if (isJsonTransform || isAutoJsonTransform) {
return JSON.parse(value) as JSONValue;
return JSON.parse(valueToTransform) as JSONValue;
// If the transform is `binary` or `auto` and the key ends with `.binary`, decode the value from base64
} else if (isBinaryTransform || isAutoBinaryTransform) {
return new TextDecoder('utf-8').decode(fromBase64(value, 'base64'));
}
if (isBinaryTransform || isAutoBinaryTransform) {
return new TextDecoder('utf-8').decode(
fromBase64(valueToTransform, 'base64')
);
}
} catch (error) {
if (throwOnTransformError)
Expand Down
8 changes: 4 additions & 4 deletions packages/parameters/src/config/EnvironmentVariablesService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConfigServiceInterface } from '../types/ConfigServiceInterface.js';
import { DEFAULT_MAX_AGE_SECS } from '../constants.js';
import { EnvironmentVariablesService as CommonEnvironmentVariablesService } from '@aws-lambda-powertools/commons';
import { DEFAULT_MAX_AGE_SECS } from '../constants.js';
import type { ConfigServiceInterface } from '../types/ConfigServiceInterface.js';

class EnvironmentVariablesService
extends CommonEnvironmentVariablesService
Expand All @@ -20,8 +20,8 @@ class EnvironmentVariablesService

if (maxAge.length === 0) return undefined;

const maxAgeAsNumber = parseInt(maxAge, 10);
if (isNaN(maxAgeAsNumber)) {
const maxAgeAsNumber = Number.parseInt(maxAge, 10);
if (Number.isNaN(maxAgeAsNumber)) {
console.warn(
`Invalid value for ${this.parametersMaxAgeVariable} environment variable: [${maxAge}], using default value of ${DEFAULT_MAX_AGE_SECS} seconds`
);
Expand Down
23 changes: 11 additions & 12 deletions packages/parameters/src/dynamodb/DynamoDBProvider.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { BaseProvider } from '../base/BaseProvider.js';
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
import {
DynamoDBClient,
type DynamoDBPaginationConfiguration,
GetItemCommand,
paginateQuery,
type DynamoDBPaginationConfiguration,
} from '@aws-sdk/client-dynamodb';
import type {
GetItemCommandInput,
QueryCommandInput,
} from '@aws-sdk/client-dynamodb';
import { marshall, unmarshall } from '@aws-sdk/util-dynamodb';
import { BaseProvider } from '../base/BaseProvider.js';
import type {
DynamoDBProviderOptions,
DynamoDBGetOptions,
DynamoDBGetMultipleOptions,
DynamoDBGetOutput,
DynamoDBGetMultipleOutput,
DynamoDBGetOptions,
DynamoDBGetOutput,
DynamoDBProviderOptions,
} from '../types/DynamoDBProvider.js';
import type {
GetItemCommandInput,
QueryCommandInput,
} from '@aws-sdk/client-dynamodb';
import type { JSONValue } from '@aws-lambda-powertools/commons/types';

/**
* ## Intro
Expand Down Expand Up @@ -248,9 +248,8 @@ class DynamoDBProvider extends BaseProvider {
*/
public constructor(config: DynamoDBProviderOptions) {
super({
awsSdkV3Client: config.awsSdkV3Client,
clientConfig: config.clientConfig,
proto: DynamoDBClient as new (config?: unknown) => DynamoDBClient,
...config,
});

const { tableName, keyAttr, sortAttr, valueAttr } = config;
Expand Down
8 changes: 4 additions & 4 deletions packages/parameters/src/secrets/SecretsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { BaseProvider } from '../base/BaseProvider.js';
import {
SecretsManagerClient,
GetSecretValueCommand,
SecretsManagerClient,
} from '@aws-sdk/client-secrets-manager';
import type { GetSecretValueCommandInput } from '@aws-sdk/client-secrets-manager';
import { BaseProvider } from '../base/BaseProvider.js';
import type {
SecretsProviderOptions,
SecretsGetOptions,
SecretsGetOutput,
SecretsProviderOptions,
} from '../types/SecretsProvider.js';

/**
Expand Down Expand Up @@ -245,7 +245,7 @@ class SecretsProvider extends BaseProvider {
protected async _getMultiple(
_path: string,
_options?: unknown
): Promise<void> {
): Promise<Record<string, unknown> | undefined> {
throw new Error('Method not implemented.');
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/parameters/src/secrets/getSecret.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DEFAULT_PROVIDERS } from '../base/DefaultProviders.js';
import { SecretsProvider } from './SecretsProvider.js';
import type {
SecretsGetOptions,
SecretsGetOutput,
} from '../types/SecretsProvider.js';
import { SecretsProvider } from './SecretsProvider.js';

/**
* ## Intro
Expand Down
Loading

0 comments on commit abd3832

Please sign in to comment.