Skip to content

feat(parser): implement safeParse option #2244

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 18 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
merged
  • Loading branch information
am29d committed Mar 21, 2024
commit 96bd1b1c335648fb53930134434a99d64b7542cd
23 changes: 13 additions & 10 deletions packages/parser/src/parserDecorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types';
import { Context, Handler } from 'aws-lambda';
import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types';
import type { Context, Handler } from 'aws-lambda';
import { ZodSchema, z } from 'zod';
import { parse } from './parser.js';
import type { ParserOptions, ParsedResult } from './types/index.js';
Expand All @@ -10,14 +10,15 @@ import type { ParserOptions, ParsedResult } from './types/index.js';
* @example
* ```typescript
*
* import type { SqSEvent } from '@aws-lambda-powertools/parser/types;
* import { parser } from '@aws-lambda-powertools/parser';
* import { SqsEnvelope } from '@aws-lambda-powertools/parser/envelopes/';
* import types { SqSEvent } from '@aws-lambda-powertools/parser/types;
* import { z } from 'zod';
* import { SqsEnvelope } from '@aws-lambda-powertools/parser/envelopes';
*
*
* const Order = z.object({
* orderId: z.string(),
* description: z.string(),
* orderId: z.string(),
* description: z.string(),
* }
*
* class Lambda extends LambdaInterface {
Expand All @@ -39,14 +40,16 @@ import type { ParserOptions, ParsedResult } from './types/index.js';
* @example
* ```typescript
*
* import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
* import type { SqSEvent, ParsedResult } from '@aws-lambda-powertools/parser/types;
* import { z } from 'zod';
* import { parser } from '@aws-lambda-powertools/parser';
* import { SqsEnvelope } from '@aws-lambda-powertools/parser/envelopes/';
* import types { SqSEvent, ParsedResult } from '@aws-lambda-powertools/parser/types;
* import { SqsEnvelope } from '@aws-lambda-powertools/parser/envelopes';
*
*
* const Order = z.object({
* orderId: z.string(),
* description: z.string(),
* orderId: z.string(),
* description: z.string(),
* }
*
* class Lambda extends LambdaInterface {
Expand Down
30 changes: 15 additions & 15 deletions packages/parser/src/types/envelope.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
ApiGatewayEnvelope,
KinesisFirehoseEnvelope,
KinesisEnvelope,
Expand All @@ -16,17 +16,17 @@ import {
} from '../envelopes/index.js';

export type Envelope =
| typeof ApiGatewayEnvelope
| typeof ApiGatewayV2Envelope
| typeof CloudWatchEnvelope
| typeof DynamoDBStreamEnvelope
| typeof EventBridgeEnvelope
| typeof KafkaEnvelope
| typeof KinesisEnvelope
| typeof KinesisFirehoseEnvelope
| typeof LambdaFunctionUrlEnvelope
| typeof SnsEnvelope
| typeof SnsSqsEnvelope
| typeof SqsEnvelope
| typeof VpcLatticeEnvelope
| typeof VpcLatticeV2Envelope;
| ApiGatewayEnvelope
| ApiGatewayV2Envelope
| CloudWatchEnvelope
| DynamoDBStreamEnvelope
| EventBridgeEnvelope
| KafkaEnvelope
| KinesisEnvelope
| KinesisFirehoseEnvelope
| LambdaFunctionUrlEnvelope
| SnsEnvelope
| SnsSqsEnvelope
| SqsEnvelope
| VpcLatticeEnvelope
| VpcLatticeV2Envelope;
19 changes: 13 additions & 6 deletions packages/parser/src/types/parser.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import { ZodSchema, ZodError } from 'zod';
import { Envelope } from './envelope.js';
import type { ZodSchema, ZodError } from 'zod';
import type { Envelope } from './envelope.js';

export type ParserOptions<S extends ZodSchema> = {
type ParserOptions<S extends ZodSchema> = {
schema: S;
envelope?: Envelope;
safeParse?: boolean;
};

export type ParsedResultSuccess<Output> = {
type ParsedResultSuccess<Output> = {
success: true;
data: Output;
};

export type ParsedResultError<Input> = {
type ParsedResultError<Input> = {
success: false;
error: ZodError | Error;
originalEvent: Input;
};

export type ParsedResult<Input = unknown, Output = unknown> =
type ParsedResult<Input = unknown, Output = unknown> =
| ParsedResultSuccess<Output>
| ParsedResultError<Input>;

export type {
ParserOptions,
ParsedResult,
ParsedResultError,
ParsedResultSuccess,
};