mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 18:25:24 +00:00
Set serverless examples to return directly the pass instead of uploading them on S3
This commit is contained in:
@@ -50,10 +50,10 @@ All the examples, except fields ones, require a `modelName` to be passed in quer
|
|||||||
Pass in queryString all the pass props you want to apply them to the final result.
|
Pass in queryString all the pass props you want to apply them to the final result.
|
||||||
|
|
||||||
| Example name | Endpoint name | Additional notes |
|
| Example name | Endpoint name | Additional notes |
|
||||||
| -------------- | ----------------- | ----------------------------------------------------------------------------------------------------- |
|
| -------------- | ----------------- | --------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| localize | `/localize` | - |
|
| localize | `/localize` | - |
|
||||||
| fields | `/fields` | - |
|
| fields | `/fields` | - |
|
||||||
| expirationDate | `/expirationDate` | - |
|
| expirationDate | `/expirationDate` | - |
|
||||||
| scratch | `/scratch` | - |
|
| scratch | `/scratch` | - |
|
||||||
| barcodes | `/barcodes` | Using `?alt=true` query parameter, will lead to barcode string message usage instead of selected ones |
|
| barcodes | `/barcodes` | Using `?alt=true` query parameter, will lead to barcode string message usage instead of selected ones |
|
||||||
| pkpasses | `/pkpasses` | - |
|
| pkpasses | `/pkpasses` | This example shows how to upload the pkpasses file on S3, even if it is discouraged. It has been done just to share the knowledge |
|
||||||
|
|||||||
@@ -120,6 +120,11 @@ export async function pkpasses(event: ALBEvent) {
|
|||||||
|
|
||||||
const pkpasses = PKPass.pack(...passes);
|
const pkpasses = PKPass.pack(...passes);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Although the other passes are served as files, in this example
|
||||||
|
* we are uploading on s3 (local) just see how it works.
|
||||||
|
*/
|
||||||
|
|
||||||
const buffer = pkpasses.getAsBuffer();
|
const buffer = pkpasses.getAsBuffer();
|
||||||
const passName = `GeneratedPass-${Math.random()}.pkpasses`;
|
const passName = `GeneratedPass-${Math.random()}.pkpasses`;
|
||||||
|
|
||||||
@@ -136,7 +141,7 @@ export async function pkpasses(event: ALBEvent) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Please note that redirection to `Location` does not work
|
* Please note that redirection to `Location` does not work
|
||||||
* if you open this code in another device if this is run
|
* if you open this code in another device if this is is running
|
||||||
* offline. This because `Location` is on localhost. Didn't
|
* offline. This because `Location` is on localhost. Didn't
|
||||||
* find yet a way to solve this.
|
* find yet a way to solve this.
|
||||||
*/
|
*/
|
||||||
@@ -145,7 +150,7 @@ export async function pkpasses(event: ALBEvent) {
|
|||||||
statusCode: 302,
|
statusCode: 302,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/vnd.apple.pkpass",
|
"Content-Type": "application/vnd.apple.pkpass",
|
||||||
Location: Location,
|
Location,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -148,7 +148,9 @@ export async function* createPassGenerator(
|
|||||||
passOptions?: Object,
|
passOptions?: Object,
|
||||||
): AsyncGenerator<PKPass, ALBResult, PKPass> {
|
): AsyncGenerator<PKPass, ALBResult, PKPass> {
|
||||||
const [template, certificates, s3] = await Promise.all([
|
const [template, certificates, s3] = await Promise.all([
|
||||||
modelName ? getModel(modelName) : Promise.resolve({}),
|
modelName
|
||||||
|
? getModel(modelName)
|
||||||
|
: Promise.resolve({} as ReturnType<typeof getModel>),
|
||||||
getCertificates(),
|
getCertificates(),
|
||||||
getS3Instance(),
|
getS3Instance(),
|
||||||
]);
|
]);
|
||||||
@@ -175,18 +177,6 @@ export async function* createPassGenerator(
|
|||||||
pass = yield pass;
|
pass = yield pass;
|
||||||
|
|
||||||
const buffer = pass.getAsBuffer();
|
const buffer = pass.getAsBuffer();
|
||||||
const passName = `GeneratedPass-${Math.random()}.pkpass`;
|
|
||||||
|
|
||||||
const { Location } = await s3
|
|
||||||
.upload({
|
|
||||||
Bucket: config.PASSES_S3_TEMP_BUCKET,
|
|
||||||
Key: passName,
|
|
||||||
ContentType: pass.mimeType,
|
|
||||||
/** Marking it as expiring in 5 minutes, because passes should not be stored */
|
|
||||||
Expires: new Date(Date.now() + 5 * 60 * 1000),
|
|
||||||
Body: buffer,
|
|
||||||
})
|
|
||||||
.promise();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Please note that redirection to `Location` does not work
|
* Please note that redirection to `Location` does not work
|
||||||
@@ -196,10 +186,18 @@ export async function* createPassGenerator(
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: 302,
|
statusCode: 200,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/vnd.apple.pkpass",
|
"Content-Type": pass.mimeType,
|
||||||
Location: Location,
|
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* It is required for the file to be served
|
||||||
|
* as base64, so it won't be altered in AWS.
|
||||||
|
*
|
||||||
|
* @see https://aws.amazon.com/it/blogs/compute/handling-binary-data-using-amazon-api-gateway-http-apis/
|
||||||
|
* "For the response path, API Gateway inspects the isBase64Encoding flag returned from Lambda."
|
||||||
|
*/
|
||||||
|
body: buffer.toString("base64"),
|
||||||
|
isBase64Encoded: true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user