Skip to content
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

feat(parser): Add appsync resolver event Zod schemas #3301

Merged
merged 4 commits into from
Nov 22, 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
17 changes: 10 additions & 7 deletions packages/parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
"require": "./lib/cjs/schemas/apigwv2.js",
"import": "./lib/esm/schemas/apigwv2.js"
},
"./schemas/appsync": {
"require": "./lib/cjs/schemas/appsync.js",
"import": "./lib/esm/schemas/appsync.js"
},
"./schemas/cloudformation-custom-resources": {
"require": "./lib/cjs/schemas/cloudformation-custom-resources.js",
"import": "./lib/esm/schemas/cloudformation-custom-resources.js"
Expand Down Expand Up @@ -183,10 +187,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 @@ -207,6 +208,10 @@
"./lib/cjs/schemas/apigwv2.d.ts",
"./lib/esm/schemas/apigwv2.d.ts"
],
"schemas/appsync": [
"./lib/cjs/schemas/appsync.d.ts",
"./lib/esm/schemas/appsync.d.ts"
],
"schemas/cloudformation-custom-resources": [
"./lib/cjs/schemas/cloudformation-custom-resources.d.ts",
"./lib/esm/schemas/cloudformation-custom-resources.d.ts"
Expand Down Expand Up @@ -327,9 +332,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
254 changes: 254 additions & 0 deletions packages/parser/src/schemas/appsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
import { z } from 'zod';

const AppSyncIamIdentity = z.object({
accountId: z.string(),
cognitoIdentityPoolId: z.string().nullable(),
cognitoIdentityId: z.string().nullable(),
sourceIp: z.array(z.string()),
username: z.string(),
userArn: z.string(),
cognitoIdentityAuthType: z.string().nullable(),
cognitoIdentityAuthProvider: z.string().nullable(),
svozza marked this conversation as resolved.
Show resolved Hide resolved
});

const AppSyncCognitoIdentity = z.object({
sub: z.string(),
issuer: z.string(),
username: z.string(),
claims: z.any(),
sourceIp: z.array(z.string()),
defaultAuthStrategy: z.string(),
groups: z.array(z.string()).nullable(),
});

const AppSyncOidcIdentity = z.object({
claims: z.any(),
issuer: z.string(),
sub: z.string(),
});

const AppSyncLambdaIdentity = z.object({
resolverContext: z.any(),
});

const AppSyncIdentity = z.union([
AppSyncCognitoIdentity,
AppSyncIamIdentity,
AppSyncOidcIdentity,
AppSyncLambdaIdentity,
]);

/**
* A zod schema for an AppSync resolver event
*
* @example
* ```json
* {
* "arguments": {
* "id": "1973493"
* },
* "source": null,
* "identity": {
* "accountId": "012345678901",
* "cognitoIdentityAuthProvider": null,
* "cognitoIdentityAuthType": null,
* "cognitoIdentityId": null,
* "cognitoIdentityPoolId": null,
* "sourceIp": ["10.10.10.10"],
* "userArn": "arn:aws:sts::012345678901:assumed-role/role",
* "username": "AROAXYKJUOW6FHGUSK5FA:username"
* },
* "request": {
* "headers": {
* "x-forwarded-for": "1.1.1.1, 2.2.2.2",
* "cloudfront-viewer-country": "US",
* "cloudfront-is-tablet-viewer": "false",
* "via": "2.0 xxxxxxxxxxxxxxxx.cloudfront.net (CloudFront)",
* "cloudfront-forwarded-proto": "https",
* "origin": "https://us-west-1.console.aws.amazon.com",
* "content-length": "217",
* "accept-language": "en-US,en;q=0.9",
* "host": "xxxxxxxxxxxxxxxx.appsync-api.us-west-1.amazonaws.com",
* "x-forwarded-proto": "https",
* "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36",
* "accept": "*!/!*",
* "cloudfront-is-mobile-viewer": "false",
* "cloudfront-is-smarttv-viewer": "false",
* "accept-encoding": "gzip, deflate, br",
* "referer": "https://us-west-1.console.aws.amazon.com/appsync/home?region=us-west-1",
* "content-type": "application/json",
* "sec-fetch-mode": "cors",
* "x-amz-cf-id": "3aykhqlUwQeANU-HGY7E_guV5EkNeMMtwyOgiA==",
* "x-amzn-trace-id": "Root=1-5f512f51-fac632066c5e848ae714",
* "authorization": "eyJraWQiOiJScWFCSlJqYVJlM0hrSnBTUFpIcVRXazNOW...",
* "sec-fetch-dest": "empty",
* "x-amz-user-agent": "AWS-Console-AppSync/",
* "cloudfront-is-desktop-viewer": "true",
* "sec-fetch-site": "cross-site",
* "x-forwarded-port": "443"
* }
* },
* "prev": {
* "result": {}
* },
* "info": {
* "selectionSetList": ["id", "field1", "field2"],
* "selectionSetGraphQL": "{\n id\n field1\n field2\n}",
* "parentTypeName": "Mutation",
* "fieldName": "createSomething",
* "variables": {}
* },
* "stash": {}
* }
* ```
*
* @see {@link https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference-js.html}
*/

const AppSyncResolverSchema = z.object({
arguments: z.record(z.any()),
identity: z.optional(AppSyncIdentity),
source: z.record(z.any()).nullable(),
request: z.object({
domainName: z.string().nullable(),
headers: z.record(z.string()),
}),
info: z.object({
selectionSetList: z.array(z.string()),
selectionSetGraphQL: z.string(),
parentTypeName: z.string(),
fieldName: z.string(),
variables: z.record(z.any()),
}),
prev: z
.object({
result: z.record(z.any()),
})
.nullable(),
stash: z.record(z.any()),
});

/**
* A zod schema for a batch AppSync resolver event
*
* @example
* ```json
* [{
* "arguments": {
* "id": "1973493"
* },
* "source": null,
* "identity": {
* "accountId": "012345678901",
* "cognitoIdentityAuthProvider": "cognitoIdentityAuthProvider",
* "cognitoIdentityAuthType": "cognitoIdentityAuthType",
* "cognitoIdentityId": "cognitoIdentityId",
* "cognitoIdentityPoolId": "cognitoIdentityPoolId",
* "sourceIp": ["10.10.10.10"],
* "userArn": "arn:aws:sts::012345678901:assumed-role/role",
* "username": "AROAXYKJUOW6FHGUSK5FA:username"
* },
* "request": {
* "headers": {
* "x-forwarded-for": "1.1.1.1, 2.2.2.2",
* "cloudfront-viewer-country": "US",
* "cloudfront-is-tablet-viewer": "false",
* "via": "2.0 xxxxxxxxxxxxxxxx.cloudfront.net (CloudFront)",
* "cloudfront-forwarded-proto": "https",
* "origin": "https://us-west-1.console.aws.amazon.com",
* "content-length": "217",
* "accept-language": "en-US,en;q=0.9",
* "host": "xxxxxxxxxxxxxxxx.appsync-api.us-west-1.amazonaws.com",
* "x-forwarded-proto": "https",
* "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36",
* "accept": "*!/!*",
* "cloudfront-is-mobile-viewer": "false",
* "cloudfront-is-smarttv-viewer": "false",
* "accept-encoding": "gzip, deflate, br",
* "referer": "https://us-west-1.console.aws.amazon.com/appsync/home?region=us-west-1",
* "content-type": "application/json",
* "sec-fetch-mode": "cors",
* "x-amz-cf-id": "3aykhqlUwQeANU-HGY7E_guV5EkNeMMtwyOgiA==",
* "x-amzn-trace-id": "Root=1-5f512f51-fac632066c5e848ae714",
* "authorization": "eyJraWQiOiJScWFCSlJqYVJlM0hrSnBTUFpIcVRXazNOW...",
* "sec-fetch-dest": "empty",
* "x-amz-user-agent": "AWS-Console-AppSync/",
* "cloudfront-is-desktop-viewer": "true",
* "sec-fetch-site": "cross-site",
* "x-forwarded-port": "443"
* }
* },
* "prev": {
* "result": {}
* },
* "info": {
* "selectionSetList": ["id", "field1", "field2"],
* "selectionSetGraphQL": "{\n id\n field1\n field2\n}",
* "parentTypeName": "Mutation",
* "fieldName": "createSomething",
* "variables": {}
* },
* "stash": {}
* },
* {
* "arguments": {
* "id": "1987311"
* },
* "source": null,
* "identity": {
* "claims": {
* "sub": "sub"
* },
* "issuer": "issuer",
* "sub": "sub
* },
* "request": {
* "headers": {
* "x-forwarded-for": "1.1.1.1, 2.2.2.2",
* "cloudfront-viewer-country": "US",
* "cloudfront-is-tablet-viewer": "false",
* "via": "2.0 xxxxxxxxxxxxxxxx.cloudfront.net (CloudFront)",
* "cloudfront-forwarded-proto": "https",
* "origin": "https://us-west-1.console.aws.amazon.com",
* "content-length": "217",
* "accept-language": "en-US,en;q=0.9",
* "host": "xxxxxxxxxxxxxxxx.appsync-api.us-west-1.amazonaws.com",
* "x-forwarded-proto": "https",
* "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36",
* "accept": "*!/!*",
* "cloudfront-is-mobile-viewer": "false",
* "cloudfront-is-smarttv-viewer": "false",
* "accept-encoding": "gzip, deflate, br",
* "referer": "https://us-west-1.console.aws.amazon.com/appsync/home?region=us-west-1",
* "content-type": "application/json",
* "sec-fetch-mode": "cors",
* "x-amz-cf-id": "3aykhqlUwQeANU-HGY7E_guV5EkNeMMtwyOgiA==",
* "x-amzn-trace-id": "Root=1-5f512f51-fac632066c5e848ae714",
* "authorization": "eyJraWQiOiJScWFCSlJqYVJlM0hrSnBTUFpIcVRXazNOW...",
* "sec-fetch-dest": "empty",
* "x-amz-user-agent": "AWS-Console-AppSync/",
* "cloudfront-is-desktop-viewer": "true",
* "sec-fetch-site": "cross-site",
* "x-forwarded-port": "443"
* }
* },
* "prev": {
* "result": {}
* },
* "info": {
* "selectionSetList": ["id", "field1", "field2"],
* "selectionSetGraphQL": "{\n id\n field1\n field2\n}",
* "parentTypeName": "Mutation",
* "fieldName": "createSomething",
* "variables": {}
* },
* "stash": {}
* }]
* ```
*
* @see {@link https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-lambda-resolvers.html#advanced-use-case-batching}
*/

const AppSyncBatchResolverSchema = z.array(AppSyncResolverSchema);

export { AppSyncResolverSchema, AppSyncBatchResolverSchema };
4 changes: 4 additions & 0 deletions packages/parser/src/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export {
APIGatewayRequestAuthorizerEventSchema,
APIGatewayTokenAuthorizerEventSchema,
} from './apigw.js';
export {
AppSyncResolverSchema,
AppSyncBatchResolverSchema,
} from './appsync.js';
export {
APIGatewayProxyEventV2Schema,
APIGatewayRequestAuthorizerEventV2Schema,
Expand Down
2 changes: 2 additions & 0 deletions packages/parser/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export type {
APIGatewayProxyEventV2,
APIGatewayRequestContextV2,
APIGatewayRequestAuthorizerV2,
AppSyncResolverEvent,
AppSyncBatchResolverEvent,
S3Event,
S3EventNotificationEventBridge,
S3SqsEventNotification,
Expand Down
8 changes: 8 additions & 0 deletions packages/parser/src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type {
APIGatewayRequestContextV2Schema,
AlbMultiValueHeadersSchema,
AlbSchema,
AppSyncBatchResolverSchema,
AppSyncResolverSchema,
CloudFormationCustomResourceCreateSchema,
CloudFormationCustomResourceDeleteSchema,
CloudFormationCustomResourceUpdateSchema,
Expand Down Expand Up @@ -55,6 +57,10 @@ type APIGatewayRequestContextV2 = z.infer<
typeof APIGatewayRequestContextV2Schema
>;

type AppSyncResolverEvent = z.infer<typeof AppSyncResolverSchema>;

type AppSyncBatchResolverEvent = z.infer<typeof AppSyncBatchResolverSchema>;

type CloudFormationCustomResourceCreateEvent = z.infer<
typeof CloudFormationCustomResourceCreateSchema
>;
Expand Down Expand Up @@ -134,6 +140,8 @@ export type {
APIGatewayProxyEventV2,
APIGatewayRequestAuthorizerV2,
APIGatewayRequestContextV2,
AppSyncResolverEvent,
AppSyncBatchResolverEvent,
CloudFormationCustomResourceCreateEvent,
CloudFormationCustomResourceDeleteEvent,
CloudFormationCustomResourceUpdateEvent,
Expand Down
Loading