Skip to content
12 changes: 10 additions & 2 deletions packages/parser/src/envelopes/apigw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ 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 { Envelope } from './envelope.js';
import { Envelope, envelopeDiscriminator } from './envelope.js';

/**
* API Gateway envelope to extract data within body key
*/
export const ApiGatewayEnvelope = {
/**
* This is a discriminator to differentiate whether an envelope returns an array or an object
* @hidden
*/
[envelopeDiscriminator]: 'object' as const,
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T> {
return Envelope.parse(APIGatewayProxyEventSchema.parse(data).body, schema);
},

safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult<unknown, z.infer<T>> {
const parsedEnvelope = APIGatewayProxyEventSchema.safeParse(data);
if (!parsedEnvelope.success) {
return {
Expand Down
12 changes: 10 additions & 2 deletions packages/parser/src/envelopes/apigwv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import { APIGatewayProxyEventV2Schema } from '../schemas/apigwv2.js';
import type { ParsedResult } from '../types/index.js';
import { Envelope } from './envelope.js';
import { Envelope, envelopeDiscriminator } from './envelope.js';

/**
* API Gateway V2 envelope to extract data within body key
*/
export const ApiGatewayV2Envelope = {
/**
* This is a discriminator to differentiate whether an envelope returns an array or an object
* @hidden
*/
[envelopeDiscriminator]: 'object' as const,
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T> {
return Envelope.parse(
APIGatewayProxyEventV2Schema.parse(data).body,
schema
);
},

safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult<unknown, z.infer<T>> {
const parsedEnvelope = APIGatewayProxyEventV2Schema.safeParse(data);
if (!parsedEnvelope.success) {
return {
Expand Down
12 changes: 10 additions & 2 deletions packages/parser/src/envelopes/cloudwatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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 { Envelope } from './envelope.js';
import { Envelope, envelopeDiscriminator } from './envelope.js';

/**
* CloudWatch Envelope to extract a List of log records.
Expand All @@ -14,6 +14,11 @@ import { Envelope } from './envelope.js';
* Note: The record will be parsed the same way so if model is str
*/
export const CloudWatchEnvelope = {
/**
* This is a discriminator to differentiate whether an envelope returns an array or an object
* @hidden
*/
[envelopeDiscriminator]: 'array' as const,
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T>[] {
const parsedEnvelope = CloudWatchLogsSchema.parse(data);

Expand All @@ -22,7 +27,10 @@ export const CloudWatchEnvelope = {
});
},

safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult<unknown, z.infer<T>[]> {
const parsedEnvelope = CloudWatchLogsSchema.safeParse(data);

if (!parsedEnvelope.success) {
Expand Down
12 changes: 10 additions & 2 deletions packages/parser/src/envelopes/dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 { Envelope, envelopeDiscriminator } from './envelope.js';

/**
* DynamoDB Stream Envelope to extract data within NewImage/OldImage
Expand All @@ -12,6 +12,11 @@ import { Envelope } from './envelope.js';
* length of the list is the record's amount in the original event.
*/
export const DynamoDBStreamEnvelope = {
/**
* This is a discriminator to differentiate whether an envelope returns an array or an object
* @hidden
*/
[envelopeDiscriminator]: 'array' as const,
parse<T extends ZodSchema>(
data: unknown,
schema: T
Expand All @@ -26,7 +31,10 @@ export const DynamoDBStreamEnvelope = {
});
},

safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult<unknown, DynamoDBStreamEnvelopeResponse<z.infer<T>>[]> {
const parsedEnvelope = DynamoDBStreamSchema.safeParse(data);

if (!parsedEnvelope.success) {
Expand Down
10 changes: 9 additions & 1 deletion packages/parser/src/envelopes/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import type { ParsedResult } from '../types/parser.js';

export const Envelope = {
const Envelope = {
/**
* Abstract function to parse the content of the envelope using provided schema.
* Both inputs are provided as unknown by the user.
Expand Down Expand Up @@ -67,3 +67,11 @@ export const Envelope = {
}
},
};

/**
* This is a discriminator to differentiate whether an envelope returns an array or an object
* @hidden
*/
const envelopeDiscriminator = Symbol.for('returnType');

export { Envelope, envelopeDiscriminator };
7 changes: 6 additions & 1 deletion packages/parser/src/envelopes/event-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import { EventBridgeSchema } from '../schemas/index.js';
import type { ParsedResult } from '../types/index.js';
import { Envelope } from './envelope.js';
import { Envelope, envelopeDiscriminator } from './envelope.js';

/**
* Envelope for EventBridge schema that extracts and parses data from the `detail` key.
*/
export const EventBridgeEnvelope = {
/**
* This is a discriminator to differentiate whether an envelope returns an array or an object
* @hidden
*/
[envelopeDiscriminator]: 'object' as const,
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T> {
return Envelope.parse(EventBridgeSchema.parse(data).detail, schema);
},
Expand Down
12 changes: 10 additions & 2 deletions packages/parser/src/envelopes/kafka.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
KafkaSelfManagedEventSchema,
} from '../schemas/kafka.js';
import type { KafkaMskEvent, ParsedResult } from '../types/index.js';
import { Envelope } from './envelope.js';
import { Envelope, envelopeDiscriminator } from './envelope.js';

/**
* Kafka event envelope to extract data within body key
Expand All @@ -17,6 +17,11 @@ import { Envelope } from './envelope.js';
*/

export const KafkaEnvelope = {
/**
* This is a discriminator to differentiate whether an envelope returns an array or an object
* @hidden
*/
[envelopeDiscriminator]: 'array' as const,
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T>[] {
// manually fetch event source to decide between Msk or SelfManaged
const eventSource = (data as KafkaMskEvent).eventSource;
Expand All @@ -35,7 +40,10 @@ export const KafkaEnvelope = {
});
},

safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult<unknown, z.infer<T>[]> {
// manually fetch event source to deside between Msk or SelfManaged
const eventSource = (data as KafkaMskEvent).eventSource;

Expand Down
12 changes: 10 additions & 2 deletions packages/parser/src/envelopes/kinesis-firehose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import { KinesisFirehoseSchema } from '../schemas/index.js';
import type { ParsedResult } from '../types/index.js';
import { Envelope } from './envelope.js';
import { Envelope, envelopeDiscriminator } from './envelope.js';

/**
* Kinesis Firehose Envelope to extract array of Records
Expand All @@ -17,6 +17,11 @@ import { Envelope } from './envelope.js';
* https://docs.aws.amazon.com/lambda/latest/dg/services-kinesisfirehose.html
*/
export const KinesisFirehoseEnvelope = {
/**
* This is a discriminator to differentiate whether an envelope returns an array or an object
* @hidden
*/
[envelopeDiscriminator]: 'array' as const,
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T>[] {
const parsedEnvelope = KinesisFirehoseSchema.parse(data);

Expand All @@ -25,7 +30,10 @@ export const KinesisFirehoseEnvelope = {
});
},

safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult<unknown, z.infer<T>[]> {
const parsedEnvelope = KinesisFirehoseSchema.safeParse(data);

if (!parsedEnvelope.success) {
Expand Down
12 changes: 10 additions & 2 deletions packages/parser/src/envelopes/kinesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import { KinesisDataStreamSchema } from '../schemas/kinesis.js';
import type { ParsedResult } from '../types/index.js';
import { Envelope } from './envelope.js';
import { Envelope, envelopeDiscriminator } from './envelope.js';

/**
* Kinesis Data Stream Envelope to extract array of Records
Expand All @@ -15,6 +15,11 @@ import { Envelope } from './envelope.js';
* all items in the list will be parsed as str and not as JSON (and vice versa)
*/
export const KinesisEnvelope = {
/**
* This is a discriminator to differentiate whether an envelope returns an array or an object
* @hidden
*/
[envelopeDiscriminator]: 'array' as const,
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T>[] {
const parsedEnvelope = KinesisDataStreamSchema.parse(data);

Expand All @@ -23,7 +28,10 @@ export const KinesisEnvelope = {
});
},

safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult<unknown, z.infer<T>[]> {
const parsedEnvelope = KinesisDataStreamSchema.safeParse(data);
if (!parsedEnvelope.success) {
return {
Expand Down
7 changes: 6 additions & 1 deletion packages/parser/src/envelopes/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import { LambdaFunctionUrlSchema } from '../schemas/index.js';
import type { ParsedResult } from '../types/index.js';
import { Envelope } from './envelope.js';
import { Envelope, envelopeDiscriminator } from './envelope.js';

/**
* Lambda function URL envelope to extract data within body key
*/
export const LambdaFunctionUrlEnvelope = {
/**
* This is a discriminator to differentiate whether an envelope returns an array or an object
* @hidden
*/
[envelopeDiscriminator]: 'object' as const,
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T> {
const parsedEnvelope = LambdaFunctionUrlSchema.parse(data);

Expand Down
24 changes: 20 additions & 4 deletions packages/parser/src/envelopes/sns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ParseError } from '../errors.js';
import { SnsSchema, SnsSqsNotificationSchema } from '../schemas/sns.js';
import { SqsSchema } from '../schemas/sqs.js';
import type { ParsedResult } from '../types/index.js';
import { Envelope } from './envelope.js';
import { Envelope, envelopeDiscriminator } from './envelope.js';

/**
* SNS Envelope to extract array of Records
Expand All @@ -15,6 +15,11 @@ import { Envelope } from './envelope.js';
* all items in the list will be parsed as str and npt as JSON (and vice versa)
*/
export const SnsEnvelope = {
/**
* This is a discriminator to differentiate whether an envelope returns an array or an object
* @hidden
*/
[envelopeDiscriminator]: 'array' as const,
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T>[] {
const parsedEnvelope = SnsSchema.parse(data);

Expand All @@ -23,7 +28,10 @@ export const SnsEnvelope = {
});
},

safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult<unknown, z.infer<T>[]> {
const parsedEnvelope = SnsSchema.safeParse(data);

if (!parsedEnvelope.success) {
Expand Down Expand Up @@ -70,7 +78,12 @@ export const SnsEnvelope = {
*
*/
export const SnsSqsEnvelope = {
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T> {
/**
* This is a discriminator to differentiate whether an envelope returns an array or an object
* @hidden
*/
[envelopeDiscriminator]: 'array' as const,
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T>[] {
const parsedEnvelope = SqsSchema.parse(data);

return parsedEnvelope.Records.map((record) => {
Expand All @@ -82,7 +95,10 @@ export const SnsSqsEnvelope = {
});
},

safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult<unknown, z.infer<T>[]> {
const parsedEnvelope = SqsSchema.safeParse(data);
if (!parsedEnvelope.success) {
return {
Expand Down
12 changes: 10 additions & 2 deletions packages/parser/src/envelopes/sqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import { SqsSchema } from '../schemas/sqs.js';
import type { ParsedResult } from '../types/index.js';
import { Envelope } from './envelope.js';
import { Envelope, envelopeDiscriminator } from './envelope.js';

/**
* SQS Envelope to extract array of Records
Expand All @@ -14,6 +14,11 @@ import { Envelope } from './envelope.js';
* all items in the list will be parsed as str and npt as JSON (and vice versa)
*/
export const SqsEnvelope = {
/**
* This is a discriminator to differentiate whether an envelope returns an array or an object
* @hidden
*/
[envelopeDiscriminator]: 'array' as const,
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T>[] {
const parsedEnvelope = SqsSchema.parse(data);

Expand All @@ -22,7 +27,10 @@ export const SqsEnvelope = {
});
},

safeParse<T extends ZodSchema>(data: unknown, schema: T): ParsedResult {
safeParse<T extends ZodSchema>(
data: unknown,
schema: T
): ParsedResult<unknown, z.infer<T>[]> {
const parsedEnvelope = SqsSchema.safeParse(data);
if (!parsedEnvelope.success) {
return {
Expand Down
7 changes: 6 additions & 1 deletion packages/parser/src/envelopes/vpc-lattice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import type { ZodSchema, z } from 'zod';
import { ParseError } from '../errors.js';
import { VpcLatticeSchema } from '../schemas/index.js';
import type { ParsedResult } from '../types/index.js';
import { Envelope } from './envelope.js';
import { Envelope, envelopeDiscriminator } from './envelope.js';

/**
* Amazon VPC Lattice envelope to extract data within body key
*/

export const VpcLatticeEnvelope = {
/**
* This is a discriminator to differentiate whether an envelope returns an array or an object
* @hidden
*/
[envelopeDiscriminator]: 'object' as const,
parse<T extends ZodSchema>(data: unknown, schema: T): z.infer<T> {
const parsedEnvelope = VpcLatticeSchema.parse(data);

Expand Down
Loading
Loading