-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(parser): add docs for parser utility (#1835)
* WIP: parser * fix test imports * remove unnecessary exports * add custom validation * remove unnecessary export * add warning * remove duplicate imports * add types and error handlig * remove comment from annotations * minor changes * revert merge changes * merged package-lock * Update docs/utilities/parser.md Co-authored-by: Andrea Amorosi <[email protected]> * Update docs/utilities/parser.md Co-authored-by: Andrea Amorosi <[email protected]> * adjust imports to new implementation * add safeParse * fixed line highlight * typo * revert index.md, add private scope to snippets packagef * Update docs/utilities/parser.md Co-authored-by: Andrea Amorosi <[email protected]> * add parser to main, fixed zod install command * fix callout indent * fix tooltip --------- Co-authored-by: Andrea Amorosi <[email protected]>
- Loading branch information
1 parent
badfef5
commit e0de20b
Showing
20 changed files
with
718 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { Context } from 'aws-lambda'; | ||
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; | ||
import { parser } from '@aws-lambda-powertools/parser'; | ||
import { z } from 'zod'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
const logger = new Logger(); | ||
|
||
const orderSchema = z.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array( | ||
z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}) | ||
), | ||
optionalField: z.string().optional(), | ||
}); | ||
|
||
type Order = z.infer<typeof orderSchema>; | ||
|
||
class Lambda implements LambdaInterface { | ||
@parser({ schema: orderSchema }) | ||
public async handler(event: Order, _context: Context): Promise<void> { | ||
// event is now typed as Order | ||
for (const item of event.items) { | ||
logger.info('Processing item', { item }); | ||
} | ||
} | ||
} | ||
|
||
const myFunction = new Lambda(); | ||
export const handler = myFunction.handler.bind(myFunction); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { Context } from 'aws-lambda'; | ||
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; | ||
import { parser } from '@aws-lambda-powertools/parser'; | ||
import { z } from 'zod'; | ||
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
const logger = new Logger(); | ||
|
||
const orderSchema = z.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array( | ||
z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}) | ||
), | ||
optionalField: z.string().optional(), | ||
}); | ||
|
||
type Order = z.infer<typeof orderSchema>; | ||
|
||
class Lambda implements LambdaInterface { | ||
@parser({ schema: orderSchema, envelope: EventBridgeEnvelope }) // (1)! | ||
public async handler(event: Order, _context: Context): Promise<void> { | ||
// event is now typed as Order | ||
for (const item of event.items) { | ||
logger.info('Processing item', item); // (2)! | ||
} | ||
} | ||
} | ||
|
||
const myFunction = new Lambda(); | ||
export const handler = myFunction.handler.bind(myFunction); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { Context } from 'aws-lambda'; | ||
import { parser } from '@aws-lambda-powertools/parser/middleware'; | ||
import { z } from 'zod'; | ||
import middy from '@middy/core'; | ||
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
const logger = new Logger(); | ||
|
||
const orderSchema = z.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array( | ||
z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}) | ||
), | ||
optionalField: z.string().optional(), | ||
}); | ||
|
||
type Order = z.infer<typeof orderSchema>; | ||
|
||
const lambdaHandler = async ( | ||
event: Order, | ||
_context: Context | ||
): Promise<void> => { | ||
for (const item of event.items) { | ||
// item is parsed as OrderItem | ||
logger.info('Processing item', { item }); | ||
} | ||
}; | ||
|
||
export const handler = middy(lambdaHandler).use( | ||
parser({ schema: orderSchema, envelope: EventBridgeEnvelope }) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"version": "0", | ||
"id": "6a7e8feb-b491-4cf7-a9f1-bf3703467718", | ||
"detail-type": "OrderPurchased", | ||
"source": "OrderService", | ||
"account": "111122223333", | ||
"time": "2020-10-22T18:43:48Z", | ||
"region": "us-west-1", | ||
"resources": ["some_additional"], | ||
"detail": { | ||
"id": 10876546789, | ||
"description": "My order", | ||
"items": [ | ||
{ | ||
"id": 1015938732, | ||
"quantity": 1, | ||
"description": "item xpto" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { Context } from 'aws-lambda'; | ||
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; | ||
import { parser } from '@aws-lambda-powertools/parser'; | ||
import { z } from 'zod'; | ||
import { EventBridgeSchema } from '@aws-lambda-powertools/parser/schemas'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
const logger = new Logger(); | ||
|
||
const orderSchema = z.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array( | ||
z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}) | ||
), | ||
optionalField: z.string().optional(), | ||
}); | ||
|
||
const orderEventSchema = EventBridgeSchema.extend({ | ||
detail: orderSchema, // (1)! | ||
}); | ||
|
||
type OrderEvent = z.infer<typeof orderEventSchema>; | ||
|
||
class Lambda implements LambdaInterface { | ||
@parser({ schema: orderEventSchema }) // (2)! | ||
public async handler(event: OrderEvent, _context: Context): Promise<void> { | ||
for (const item of event.detail.items) { | ||
// process OrderItem | ||
logger.info('Processing item', { item }); // (3)! | ||
} | ||
} | ||
} | ||
|
||
const myFunction = new Lambda(); | ||
export const handler = myFunction.handler.bind(myFunction); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { Context } from 'aws-lambda'; | ||
import { z } from 'zod'; | ||
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes'; | ||
import { EventBridgeSchema } from '@aws-lambda-powertools/parser/schemas'; | ||
import type { EventBridgeEvent } from '@aws-lambda-powertools/parser/types'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
const logger = new Logger(); | ||
|
||
const orderSchema = z.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array( | ||
z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}) | ||
), | ||
optionalField: z.string().optional(), | ||
}); | ||
type Order = z.infer<typeof orderSchema>; | ||
|
||
export const handler = async ( | ||
event: EventBridgeEvent, | ||
_context: Context | ||
): Promise<void> => { | ||
const parsedEvent = EventBridgeSchema.parse(event); // (1)! | ||
logger.info('Parsed event', parsedEvent); | ||
|
||
const orders: Order = EventBridgeEnvelope.parse(event, orderSchema); // (2)! | ||
logger.info('Parsed orders', orders); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { Context } from 'aws-lambda'; | ||
import { z } from 'zod'; | ||
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes'; | ||
import { EventBridgeSchema } from '@aws-lambda-powertools/parser/schemas'; | ||
import type { EventBridgeEvent } from '@aws-lambda-powertools/parser/types'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
const logger = new Logger(); | ||
|
||
const orderSchema = z.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array( | ||
z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}) | ||
), | ||
optionalField: z.string().optional(), | ||
}); | ||
|
||
export const handler = async ( | ||
event: EventBridgeEvent, | ||
_context: Context | ||
): Promise<void> => { | ||
const parsedEvent = EventBridgeSchema.safeParse(event); // (1)! | ||
parsedEvent.success | ||
? logger.info('Event parsed successfully', parsedEvent.data) | ||
: logger.error('Event parsing failed', parsedEvent.error); | ||
const parsedEvenlope = EventBridgeEnvelope.safeParse(event, orderSchema); // (2)! | ||
parsedEvenlope.success | ||
? logger.info('Event envelope parsed successfully', parsedEvenlope.data) | ||
: logger.error('Event envelope parsing failed', parsedEvenlope.error); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { Context } from 'aws-lambda'; | ||
import { parser } from '@aws-lambda-powertools/parser/middleware'; | ||
import { z } from 'zod'; | ||
import middy from '@middy/core'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
const logger = new Logger(); | ||
|
||
const orderSchema = z.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array( | ||
z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}) | ||
), | ||
optionalField: z.string().optional(), | ||
}); | ||
|
||
type Order = z.infer<typeof orderSchema>; | ||
|
||
const lambdaHandler = async ( | ||
event: Order, | ||
_context: Context | ||
): Promise<void> => { | ||
for (const item of event.items) { | ||
// item is parsed as OrderItem | ||
logger.info('Processing item', { item }); | ||
} | ||
}; | ||
|
||
export const handler = middy(lambdaHandler).use( | ||
parser({ schema: orderSchema }) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { z } from 'zod'; | ||
|
||
const orderItemSchema = z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}); | ||
|
||
export const orderSchema = z | ||
.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array(orderItemSchema).refine((items) => items.length > 0, { | ||
message: 'Order must have at least one item', // (1)! | ||
}), | ||
optionalField: z.string().optional(), | ||
}) | ||
.refine((order) => order.id > 100 && order.items.length > 100, { | ||
message: | ||
'All orders with more than 100 items must have an id greater than 100', // (2)! | ||
}); |
Oops, something went wrong.