From 0a18c5200dcf149f32f824d5e6aeb90467325325 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sat, 15 Jan 2022 23:56:40 +0100 Subject: [PATCH] Fixed Serverless examples: they were not returning 400 if model name was not provided --- examples/serverless/src/functions/barcodes.ts | 11 ++++- .../src/functions/expirationDate.ts | 14 +++++-- examples/serverless/src/functions/localize.ts | 11 ++++- examples/serverless/src/functions/pkpasses.ts | 41 ++++++++++++++++++- examples/serverless/src/shared.ts | 18 ++++---- 5 files changed, 75 insertions(+), 20 deletions(-) diff --git a/examples/serverless/src/functions/barcodes.ts b/examples/serverless/src/functions/barcodes.ts index 3fd089d..8a5d423 100644 --- a/examples/serverless/src/functions/barcodes.ts +++ b/examples/serverless/src/functions/barcodes.ts @@ -1,13 +1,20 @@ import { ALBEvent, ALBResult } from "aws-lambda"; import { PKPass } from "../../../../lib"; -import { finish400WithoutModelName, createPassGenerator } from "../shared"; +import { + throwClientErrorWithoutModelName, + createPassGenerator, +} from "../shared"; /** * Lambda for barcodes example */ export async function barcodes(event: ALBEvent) { - finish400WithoutModelName(event); + try { + throwClientErrorWithoutModelName(event); + } catch (err) { + return err; + } const { modelName, alt, ...passOptions } = event.queryStringParameters; diff --git a/examples/serverless/src/functions/expirationDate.ts b/examples/serverless/src/functions/expirationDate.ts index 706973e..2a62c2d 100644 --- a/examples/serverless/src/functions/expirationDate.ts +++ b/examples/serverless/src/functions/expirationDate.ts @@ -1,14 +1,20 @@ -import { ALBEvent, ALBResult } from "aws-lambda"; -import { Context } from "vm"; +import { ALBEvent, ALBResult, Context } from "aws-lambda"; import { PKPass } from "passkit-generator"; -import { finish400WithoutModelName, createPassGenerator } from "../shared"; +import { + throwClientErrorWithoutModelName, + createPassGenerator, +} from "../shared"; /** * Lambda for expirationDate example */ export async function expirationDate(event: ALBEvent, context: Context) { - finish400WithoutModelName(event); + try { + throwClientErrorWithoutModelName(event); + } catch (err) { + return err; + } const { modelName, ...passOptions } = event.queryStringParameters; diff --git a/examples/serverless/src/functions/localize.ts b/examples/serverless/src/functions/localize.ts index 59c0783..a8ba895 100644 --- a/examples/serverless/src/functions/localize.ts +++ b/examples/serverless/src/functions/localize.ts @@ -1,4 +1,7 @@ -import { finish400WithoutModelName, createPassGenerator } from "../shared"; +import { + throwClientErrorWithoutModelName, + createPassGenerator, +} from "../shared"; import type { ALBEvent, ALBResult } from "aws-lambda"; import type { PKPass } from "passkit-generator"; @@ -7,7 +10,11 @@ import type { PKPass } from "passkit-generator"; */ export async function localize(event: ALBEvent) { - finish400WithoutModelName(event); + try { + throwClientErrorWithoutModelName(event); + } catch (err) { + return err; + } const { modelName, ...passOptions } = event.queryStringParameters; diff --git a/examples/serverless/src/functions/pkpasses.ts b/examples/serverless/src/functions/pkpasses.ts index 9714f24..11d4fc6 100644 --- a/examples/serverless/src/functions/pkpasses.ts +++ b/examples/serverless/src/functions/pkpasses.ts @@ -1,3 +1,34 @@ +/** + * PKPasses generation through PKPass.pack static method + * example. + * Here it is showed manual model reading and + * creating through another PKPass because in the other + * examples, creation through templates is already shown + * + * PLEASE NOTE THAT, AT TIME OF WRITING, THIS EXAMPLE WORKS + * ONLY IF PASSES ARE DOWNLOADED FROM SAFARI, due to the + * support of PKPasses archives. To test this, you might + * need to open a tunnel through NGROK if you cannot access + * to your local machine (in my personal case, developing + * under WSL is a pretty big limitation sometimes). + * + * PLEASE ALSO NOTE that, AT TIME OF WRITING (iOS 15.0 - 15.2) + * Pass Viewer suffers of a really curious bug: issuing several + * passes within the same pkpasses archive, all with the same + * serialNumber, will lead to have a broken view and to add + * just one pass. You can see the screenshots below: + * + * https://imgur.com/bDTbcDg.jpg + * https://imgur.com/Y4GpuHT.jpg + * https://i.imgur.com/qbJMy1d.jpg + * + * - "Alberto, come to look at APPLE." + * **Alberto looks** + * - "MAMMA MIA!"" + * + * A feedback to Apple have been sent for this. + */ + import { ALBEvent } from "aws-lambda"; import { PKPass } from "passkit-generator"; import { @@ -5,7 +36,7 @@ import { getSpecificFileInModel, getS3Instance, getRandomColorPart, - finish400WithoutModelName, + throwClientErrorWithoutModelName, } from "../shared"; import config from "../../config.json"; @@ -14,7 +45,13 @@ import config from "../../config.json"; */ export async function pkpasses(event: ALBEvent) { - finish400WithoutModelName(event); + try { + throwClientErrorWithoutModelName(event); + } catch (err) { + return err; + } + + console.log(event.queryStringParameters); const [certificates, iconFromModel, s3] = await Promise.all([ getCertificates(), diff --git a/examples/serverless/src/shared.ts b/examples/serverless/src/shared.ts index 3d83015..347166d 100644 --- a/examples/serverless/src/shared.ts +++ b/examples/serverless/src/shared.ts @@ -7,17 +7,15 @@ import { PKPass } from "passkit-generator"; const S3: { instance: AWS.S3 } = { instance: undefined }; -export function finish400WithoutModelName(event: ALBEvent) { - if (event.queryStringParameters?.modelName) { - return; +export function throwClientErrorWithoutModelName(event: ALBEvent) { + if (!event.queryStringParameters?.modelName) { + throw { + statusCode: 400, + body: JSON.stringify({ + message: "modelName is missing in query params", + }), + }; } - - return { - statusCode: 400, - body: JSON.stringify({ - message: "modelName is missing in query params", - }), - }; } export function getRandomColorPart() {