Skip to content

Commit 72803e2

Browse files
authored
chore(maintenance): migrate parser utility to biome (#2822)
1 parent 22203cc commit 72803e2

File tree

125 files changed

+1344
-1849
lines changed

Some content is hidden

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

125 files changed

+1344
-1849
lines changed

packages/parser/package.json

+4-9
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
1818
"build:esm": "tsc --build tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
1919
"build": "npm run build:esm & npm run build:cjs",
20-
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
21-
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
20+
"lint": "biome lint .",
21+
"lint:fix": "biome check --write .",
2222
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
2323
},
2424
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/parser#readme",
@@ -62,10 +62,7 @@
6262
},
6363
"typesVersions": {
6464
"*": {
65-
"types": [
66-
"./lib/cjs/types/index.d.ts",
67-
"./lib/esm/types/index.d.ts"
68-
],
65+
"types": ["./lib/cjs/types/index.d.ts", "./lib/esm/types/index.d.ts"],
6966
"middleware": [
7067
"./lib/cjs/middleware/parser.d.ts",
7168
"./lib/esm/middleware/parser.d.ts"
@@ -90,9 +87,7 @@
9087
},
9188
"main": "./lib/cjs/index.js",
9289
"types": "./lib/cjs/index.d.ts",
93-
"files": [
94-
"lib"
95-
],
90+
"files": ["lib"],
9691
"repository": {
9792
"type": "git",
9893
"url": "git+https://github.com/aws-powertools/powertools-lambda-typescript.git"
+11-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
import { Envelope } from './envelope.js';
2-
import { z, type ZodSchema } from 'zod';
1+
import type { ZodSchema, z } from 'zod';
2+
import { ParseError } from '../errors.js';
33
import { APIGatewayProxyEventSchema } from '../schemas/apigw.js';
44
import type { ParsedResult } from '../types/parser.js';
5-
import { ParseError } from '../errors.js';
5+
import { Envelope } from './envelope';
66

77
/**
88
* API Gateway envelope to extract data within body key
99
*/
10-
export class ApiGatewayEnvelope extends Envelope {
11-
public static parse<T extends ZodSchema>(
12-
data: unknown,
13-
schema: T
14-
): z.infer<T> {
15-
return super.parse(APIGatewayProxyEventSchema.parse(data).body, schema);
16-
}
10+
export const ApiGatewayEnvelope = {
11+
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T> {
12+
return Envelope.parse(APIGatewayProxyEventSchema.parse(data).body, schema);
13+
},
1714

18-
public static safeParse<T extends ZodSchema>(
15+
safeParse<T extends ZodSchema>(
1916
data: unknown,
2017
schema: T
2118
): ParsedResult<unknown, z.infer<T>> {
@@ -30,7 +27,7 @@ export class ApiGatewayEnvelope extends Envelope {
3027
};
3128
}
3229

33-
const parsedBody = super.safeParse(parsedEnvelope.data.body, schema);
30+
const parsedBody = Envelope.safeParse(parsedEnvelope.data.body, schema);
3431

3532
if (!parsedBody.success) {
3633
return {
@@ -43,5 +40,5 @@ export class ApiGatewayEnvelope extends Envelope {
4340
}
4441

4542
return parsedBody;
46-
}
47-
}
43+
},
44+
};
+14-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
import { z, type ZodSchema } from 'zod';
1+
import type { ZodSchema, z } from 'zod';
2+
import { ParseError } from '../errors.js';
23
import { APIGatewayProxyEventV2Schema } from '../schemas/apigwv2.js';
3-
import { Envelope } from './envelope.js';
44
import type { ParsedResult } from '../types/index.js';
5-
import { ParseError } from '../errors.js';
5+
import { Envelope } from './envelope.js';
66

77
/**
88
* API Gateway V2 envelope to extract data within body key
99
*/
10-
export class ApiGatewayV2Envelope extends Envelope {
11-
public static parse<T extends ZodSchema>(
12-
data: unknown,
13-
schema: T
14-
): z.infer<T> {
15-
return super.parse(APIGatewayProxyEventV2Schema.parse(data).body, schema);
16-
}
10+
export const ApiGatewayV2Envelope = {
11+
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T> {
12+
return Envelope.parse(
13+
APIGatewayProxyEventV2Schema.parse(data).body,
14+
schema
15+
);
16+
},
1717

18-
public static safeParse<T extends ZodSchema>(
19-
data: unknown,
20-
schema: T
21-
): ParsedResult {
18+
safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
2219
const parsedEnvelope = APIGatewayProxyEventV2Schema.safeParse(data);
2320
if (!parsedEnvelope.success) {
2421
return {
@@ -30,7 +27,7 @@ export class ApiGatewayV2Envelope extends Envelope {
3027
};
3128
}
3229

33-
const parsedBody = super.safeParse(parsedEnvelope.data.body, schema);
30+
const parsedBody = Envelope.safeParse(parsedEnvelope.data.body, schema);
3431

3532
if (!parsedBody.success) {
3633
return {
@@ -43,5 +40,5 @@ export class ApiGatewayV2Envelope extends Envelope {
4340
}
4441

4542
return parsedBody;
46-
}
47-
}
43+
},
44+
};
+12-19
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { z, type ZodSchema } from 'zod';
2-
import { Envelope } from './envelope.js';
1+
import type { ZodSchema, z } from 'zod';
2+
import { ParseError } from '../errors.js';
33
import { CloudWatchLogsSchema } from '../schemas/index.js';
44
import type { ParsedResult } from '../types/index.js';
5-
import { ParseError } from '../errors.js';
5+
import { Envelope } from './envelope.js';
66

77
/**
88
* CloudWatch Envelope to extract a List of log records.
@@ -13,22 +13,16 @@ import { ParseError } from '../errors.js';
1313
*
1414
* Note: The record will be parsed the same way so if model is str
1515
*/
16-
export class CloudWatchEnvelope extends Envelope {
17-
public static parse<T extends ZodSchema>(
18-
data: unknown,
19-
schema: T
20-
): z.infer<T>[] {
16+
export const CloudWatchEnvelope = {
17+
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T>[] {
2118
const parsedEnvelope = CloudWatchLogsSchema.parse(data);
2219

2320
return parsedEnvelope.awslogs.data.logEvents.map((record) => {
24-
return super.parse(record.message, schema);
21+
return Envelope.parse(record.message, schema);
2522
});
26-
}
23+
},
2724

28-
public static safeParse<T extends ZodSchema>(
29-
data: unknown,
30-
schema: T
31-
): ParsedResult {
25+
safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
3226
const parsedEnvelope = CloudWatchLogsSchema.safeParse(data);
3327

3428
if (!parsedEnvelope.success) {
@@ -43,7 +37,7 @@ export class CloudWatchEnvelope extends Envelope {
4337
const parsedLogEvents: z.infer<T>[] = [];
4438

4539
for (const record of parsedEnvelope.data.awslogs.data.logEvents) {
46-
const parsedMessage = super.safeParse(record.message, schema);
40+
const parsedMessage = Envelope.safeParse(record.message, schema);
4741
if (!parsedMessage.success) {
4842
return {
4943
success: false,
@@ -52,14 +46,13 @@ export class CloudWatchEnvelope extends Envelope {
5246
}),
5347
originalEvent: data,
5448
};
55-
} else {
56-
parsedLogEvents.push(parsedMessage.data);
5749
}
50+
parsedLogEvents.push(parsedMessage.data);
5851
}
5952

6053
return {
6154
success: true,
6255
data: parsedLogEvents,
6356
};
64-
}
65-
}
57+
},
58+
};
+23-21
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
1-
import { z, type ZodSchema } from 'zod';
1+
import type { ZodSchema, z } from 'zod';
2+
import { ParseError } from '../errors.js';
23
import { DynamoDBStreamSchema } from '../schemas/index.js';
4+
import type { DynamoDBStreamEnvelopeResponse } from '../types/envelope.js';
35
import type { ParsedResult, ParsedResultError } from '../types/index.js';
46
import { Envelope } from './envelope.js';
5-
import { ParseError } from '../errors.js';
6-
import type { DynamoDBStreamEnvelopeResponse } from '../types/envelope.js';
77

88
/**
99
* DynamoDB Stream Envelope to extract data within NewImage/OldImage
1010
*
1111
* Note: Values are the parsed models. Images' values can also be None, and
1212
* length of the list is the record's amount in the original event.
1313
*/
14-
export class DynamoDBStreamEnvelope extends Envelope {
15-
public static parse<T extends ZodSchema>(
14+
export const DynamoDBStreamEnvelope = {
15+
parse<T extends ZodSchema>(
1616
data: unknown,
1717
schema: T
1818
): DynamoDBStreamEnvelopeResponse<z.infer<T>>[] {
1919
const parsedEnvelope = DynamoDBStreamSchema.parse(data);
2020

2121
return parsedEnvelope.Records.map((record) => {
2222
return {
23-
NewImage: super.parse(record.dynamodb.NewImage, schema),
24-
OldImage: super.parse(record.dynamodb.OldImage, schema),
23+
NewImage: Envelope.parse(record.dynamodb.NewImage, schema),
24+
OldImage: Envelope.parse(record.dynamodb.OldImage, schema),
2525
};
2626
});
27-
}
27+
},
2828

29-
public static safeParse<T extends ZodSchema>(
30-
data: unknown,
31-
schema: T
32-
): ParsedResult {
29+
safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
3330
const parsedEnvelope = DynamoDBStreamSchema.safeParse(data);
3431

3532
if (!parsedEnvelope.success) {
@@ -44,8 +41,14 @@ export class DynamoDBStreamEnvelope extends Envelope {
4441
const parsedLogEvents: DynamoDBStreamEnvelopeResponse<z.infer<T>>[] = [];
4542

4643
for (const record of parsedEnvelope.data.Records) {
47-
const parsedNewImage = super.safeParse(record.dynamodb.NewImage, schema);
48-
const parsedOldImage = super.safeParse(record.dynamodb.OldImage, schema);
44+
const parsedNewImage = Envelope.safeParse(
45+
record.dynamodb.NewImage,
46+
schema
47+
);
48+
const parsedOldImage = Envelope.safeParse(
49+
record.dynamodb.OldImage,
50+
schema
51+
);
4952
if (!parsedNewImage.success || !parsedOldImage.success) {
5053
return {
5154
success: false,
@@ -58,17 +61,16 @@ export class DynamoDBStreamEnvelope extends Envelope {
5861
}),
5962
originalEvent: data,
6063
};
61-
} else {
62-
parsedLogEvents.push({
63-
NewImage: parsedNewImage.data,
64-
OldImage: parsedOldImage.data,
65-
});
6664
}
65+
parsedLogEvents.push({
66+
NewImage: parsedNewImage.data,
67+
OldImage: parsedOldImage.data,
68+
});
6769
}
6870

6971
return {
7072
success: true,
7173
data: parsedLogEvents,
7274
};
73-
}
74-
}
75+
},
76+
};

packages/parser/src/envelopes/envelope.ts

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { z, type ZodSchema } from 'zod';
2-
import type { ParsedResult } from '../types/parser.js';
1+
import type { ZodSchema, z } from 'zod';
32
import { ParseError } from '../errors.js';
3+
import type { ParsedResult } from '../types/parser.js';
44

5-
export class Envelope {
5+
export const Envelope = {
66
/**
77
* Abstract function to parse the content of the envelope using provided schema.
88
* Both inputs are provided as unknown by the user.
@@ -11,10 +11,7 @@ export class Envelope {
1111
* @param data data to parse
1212
* @param schema schema
1313
*/
14-
public static readonly parse = <T extends ZodSchema>(
15-
data: unknown,
16-
schema: T
17-
): z.infer<T> => {
14+
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T> {
1815
if (typeof data !== 'object' && typeof data !== 'string') {
1916
throw new ParseError(
2017
`Invalid data type for envelope. Expected string or object, got ${typeof data}`
@@ -23,24 +20,25 @@ export class Envelope {
2320
try {
2421
if (typeof data === 'string') {
2522
return schema.parse(JSON.parse(data));
26-
} else if (typeof data === 'object') {
23+
}
24+
if (typeof data === 'object') {
2725
return schema.parse(data);
2826
}
2927
} catch (e) {
30-
throw new ParseError(`Failed to parse envelope`, { cause: e as Error });
28+
throw new ParseError('Failed to parse envelope', { cause: e as Error });
3129
}
32-
};
30+
},
3331

3432
/**
3533
* Abstract function to safely parse the content of the envelope using provided schema.
3634
* safeParse is used to avoid throwing errors, thus we catuch all errors and wrap them in the result.
3735
* @param input
3836
* @param schema
3937
*/
40-
public static readonly safeParse = <T extends ZodSchema>(
38+
safeParse<T extends ZodSchema>(
4139
input: unknown,
4240
schema: T
43-
): ParsedResult<unknown, z.infer<T>> => {
41+
): ParsedResult<unknown, z.infer<T>> {
4442
try {
4543
if (typeof input !== 'object' && typeof input !== 'string') {
4644
return {
@@ -63,19 +61,19 @@ export class Envelope {
6361
}
6462
: {
6563
success: false,
66-
error: new ParseError(`Failed to parse envelope`, {
64+
error: new ParseError('Failed to parse envelope', {
6765
cause: parsed.error,
6866
}),
6967
originalEvent: input,
7068
};
7169
} catch (e) {
7270
return {
7371
success: false,
74-
error: new ParseError(`Failed to parse envelope`, {
72+
error: new ParseError('Failed to parse envelope', {
7573
cause: e as Error,
7674
}),
7775
originalEvent: input,
7876
};
7977
}
80-
};
81-
}
78+
},
79+
};

0 commit comments

Comments
 (0)