Skip to content

chore(maintenance): migrate parser utility to biome #2822

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

Merged
merged 4 commits into from
Jul 24, 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
13 changes: 4 additions & 9 deletions packages/parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,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/parser#readme",
Expand Down Expand Up @@ -62,10 +62,7 @@
},
"typesVersions": {
"*": {
"types": [
"./lib/cjs/types/index.d.ts",
"./lib/esm/types/index.d.ts"
],
"types": ["./lib/cjs/types/index.d.ts", "./lib/esm/types/index.d.ts"],
"middleware": [
"./lib/cjs/middleware/parser.d.ts",
"./lib/esm/middleware/parser.d.ts"
Expand All @@ -90,9 +87,7 @@
},
"main": "./lib/cjs/index.js",
"types": "./lib/cjs/index.d.ts",
"files": [
"lib"
],
"files": ["lib"],
"repository": {
"type": "git",
"url": "git+https://github.com/aws-powertools/powertools-lambda-typescript.git"
Expand Down
25 changes: 11 additions & 14 deletions packages/parser/src/envelopes/apigw.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { Envelope } from './envelope.js';
import { z, type ZodSchema } from 'zod';
import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import { APIGatewayProxyEventSchema } from '../schemas/apigw.js';
import type { ParsedResult } from '../types/parser.js';
import { ParseError } from '../errors.js';
import { Envelope } from './envelope';

/**
* API Gateway envelope to extract data within body key
*/
export class ApiGatewayEnvelope extends Envelope {
public static parse<T extends ZodSchema>(
data: unknown,
schema: T
): z.infer<T> {
return super.parse(APIGatewayProxyEventSchema.parse(data).body, schema);
}
export const ApiGatewayEnvelope = {
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T> {
return Envelope.parse(APIGatewayProxyEventSchema.parse(data).body, schema);
},

public static safeParse<T extends ZodSchema>(
safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult<unknown, z.infer<T>> {
Expand All @@ -30,7 +27,7 @@ export class ApiGatewayEnvelope extends Envelope {
};
}

const parsedBody = super.safeParse(parsedEnvelope.data.body, schema);
const parsedBody = Envelope.safeParse(parsedEnvelope.data.body, schema);

if (!parsedBody.success) {
return {
Expand All @@ -43,5 +40,5 @@ export class ApiGatewayEnvelope extends Envelope {
}

return parsedBody;
}
}
},
};
31 changes: 14 additions & 17 deletions packages/parser/src/envelopes/apigwv2.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { z, type ZodSchema } from 'zod';
import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import { APIGatewayProxyEventV2Schema } from '../schemas/apigwv2.js';
import { Envelope } from './envelope.js';
import type { ParsedResult } from '../types/index.js';
import { ParseError } from '../errors.js';
import { Envelope } from './envelope.js';

/**
* API Gateway V2 envelope to extract data within body key
*/
export class ApiGatewayV2Envelope extends Envelope {
public static parse<T extends ZodSchema>(
data: unknown,
schema: T
): z.infer<T> {
return super.parse(APIGatewayProxyEventV2Schema.parse(data).body, schema);
}
export const ApiGatewayV2Envelope = {
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T> {
return Envelope.parse(
APIGatewayProxyEventV2Schema.parse(data).body,
schema
);
},

public static safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult {
safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
const parsedEnvelope = APIGatewayProxyEventV2Schema.safeParse(data);
if (!parsedEnvelope.success) {
return {
Expand All @@ -30,7 +27,7 @@ export class ApiGatewayV2Envelope extends Envelope {
};
}

const parsedBody = super.safeParse(parsedEnvelope.data.body, schema);
const parsedBody = Envelope.safeParse(parsedEnvelope.data.body, schema);

if (!parsedBody.success) {
return {
Expand All @@ -43,5 +40,5 @@ export class ApiGatewayV2Envelope extends Envelope {
}

return parsedBody;
}
}
},
};
31 changes: 12 additions & 19 deletions packages/parser/src/envelopes/cloudwatch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { z, type ZodSchema } from 'zod';
import { Envelope } from './envelope.js';
import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import { CloudWatchLogsSchema } from '../schemas/index.js';
import type { ParsedResult } from '../types/index.js';
import { ParseError } from '../errors.js';
import { Envelope } from './envelope.js';

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

return parsedEnvelope.awslogs.data.logEvents.map((record) => {
return super.parse(record.message, schema);
return Envelope.parse(record.message, schema);
});
}
},

public static safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult {
safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
const parsedEnvelope = CloudWatchLogsSchema.safeParse(data);

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

for (const record of parsedEnvelope.data.awslogs.data.logEvents) {
const parsedMessage = super.safeParse(record.message, schema);
const parsedMessage = Envelope.safeParse(record.message, schema);
if (!parsedMessage.success) {
return {
success: false,
Expand All @@ -52,14 +46,13 @@ export class CloudWatchEnvelope extends Envelope {
}),
originalEvent: data,
};
} else {
parsedLogEvents.push(parsedMessage.data);
}
parsedLogEvents.push(parsedMessage.data);
}

return {
success: true,
data: parsedLogEvents,
};
}
}
},
};
44 changes: 23 additions & 21 deletions packages/parser/src/envelopes/dynamodb.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
import { z, type ZodSchema } from 'zod';
import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import { DynamoDBStreamSchema } from '../schemas/index.js';
import type { DynamoDBStreamEnvelopeResponse } from '../types/envelope.js';
import type { ParsedResult, ParsedResultError } from '../types/index.js';
import { Envelope } from './envelope.js';
import { ParseError } from '../errors.js';
import type { DynamoDBStreamEnvelopeResponse } from '../types/envelope.js';

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

return parsedEnvelope.Records.map((record) => {
return {
NewImage: super.parse(record.dynamodb.NewImage, schema),
OldImage: super.parse(record.dynamodb.OldImage, schema),
NewImage: Envelope.parse(record.dynamodb.NewImage, schema),
OldImage: Envelope.parse(record.dynamodb.OldImage, schema),
};
});
}
},

public static safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult {
safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
const parsedEnvelope = DynamoDBStreamSchema.safeParse(data);

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

for (const record of parsedEnvelope.data.Records) {
const parsedNewImage = super.safeParse(record.dynamodb.NewImage, schema);
const parsedOldImage = super.safeParse(record.dynamodb.OldImage, schema);
const parsedNewImage = Envelope.safeParse(
record.dynamodb.NewImage,
schema
);
const parsedOldImage = Envelope.safeParse(
record.dynamodb.OldImage,
schema
);
if (!parsedNewImage.success || !parsedOldImage.success) {
return {
success: false,
Expand All @@ -58,17 +61,16 @@ export class DynamoDBStreamEnvelope extends Envelope {
}),
originalEvent: data,
};
} else {
parsedLogEvents.push({
NewImage: parsedNewImage.data,
OldImage: parsedOldImage.data,
});
}
parsedLogEvents.push({
NewImage: parsedNewImage.data,
OldImage: parsedOldImage.data,
});
}

return {
success: true,
data: parsedLogEvents,
};
}
}
},
};
30 changes: 14 additions & 16 deletions packages/parser/src/envelopes/envelope.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { z, type ZodSchema } from 'zod';
import type { ParsedResult } from '../types/parser.js';
import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import type { ParsedResult } from '../types/parser.js';

export class Envelope {
export const Envelope = {
/**
* Abstract function to parse the content of the envelope using provided schema.
* Both inputs are provided as unknown by the user.
Expand All @@ -11,10 +11,7 @@ export class Envelope {
* @param data data to parse
* @param schema schema
*/
public static readonly parse = <T extends ZodSchema>(
data: unknown,
schema: T
): z.infer<T> => {
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T> {
if (typeof data !== 'object' && typeof data !== 'string') {
throw new ParseError(
`Invalid data type for envelope. Expected string or object, got ${typeof data}`
Expand All @@ -23,24 +20,25 @@ export class Envelope {
try {
if (typeof data === 'string') {
return schema.parse(JSON.parse(data));
} else if (typeof data === 'object') {
}
if (typeof data === 'object') {
return schema.parse(data);
}
} catch (e) {
throw new ParseError(`Failed to parse envelope`, { cause: e as Error });
throw new ParseError('Failed to parse envelope', { cause: e as Error });
}
};
},

/**
* Abstract function to safely parse the content of the envelope using provided schema.
* safeParse is used to avoid throwing errors, thus we catuch all errors and wrap them in the result.
* @param input
* @param schema
*/
public static readonly safeParse = <T extends ZodSchema>(
safeParse<T extends ZodSchema>(
input: unknown,
schema: T
): ParsedResult<unknown, z.infer<T>> => {
): ParsedResult<unknown, z.infer<T>> {
try {
if (typeof input !== 'object' && typeof input !== 'string') {
return {
Expand All @@ -63,19 +61,19 @@ export class Envelope {
}
: {
success: false,
error: new ParseError(`Failed to parse envelope`, {
error: new ParseError('Failed to parse envelope', {
cause: parsed.error,
}),
originalEvent: input,
};
} catch (e) {
return {
success: false,
error: new ParseError(`Failed to parse envelope`, {
error: new ParseError('Failed to parse envelope', {
cause: e as Error,
}),
originalEvent: input,
};
}
};
}
},
};
Loading
Loading