Fixed Serverless examples: they were not returning 400 if model name was not provided

This commit is contained in:
Alexander Cerutti
2022-01-15 23:56:40 +01:00
parent 267ca6baff
commit 0a18c5200d
5 changed files with 75 additions and 20 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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(),

View File

@@ -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() {