-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgetting-started.js
More file actions
85 lines (77 loc) · 2.52 KB
/
Copy pathgetting-started.js
File metadata and controls
85 lines (77 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require("dotenv").config({ quiet: true });
const QuickStartLoyalty = require("./lib/quick-start-loyalty");
const QuickStartCoupons = require("./lib/quick-start-coupons");
const QuickStartEventTickets = require("./lib/quick-start-event-tickets");
const QuickStartFlights = require("./lib/quick-start-flights");
const createClient = require("./lib/client-factory");
const { loadConfig, validateConfig } = require("./config/config");
const examples = {
loyalty: QuickStartLoyalty,
coupons: QuickStartCoupons,
tickets: QuickStartEventTickets,
flights: QuickStartFlights,
};
function printResults(name, example, address) {
const region = address.includes("pub2") ? "pub2" : "pub1";
const passUrl = (id) => id && `https://${region}.pskt.io/${id}`;
const results = {
loyalty: {
enrollmentUrl:
example.shortCode && `https://${region}.pskt.io/c/${example.shortCode}`,
bronzePassUrl: passUrl(example.bronzeMemberId),
silverPassUrl: passUrl(example.silverMemberId),
},
coupons: {
baseCouponUrl: passUrl(example.baseCouponId),
vipCouponUrl: passUrl(example.vipCouponId),
},
tickets: { eventTicketUrl: passUrl(example.ticketId) },
flights: Object.fromEntries(
(example.boardingPasses || []).map((pass, index) => [
`boardingPass${index + 1}Url`,
pass.url || passUrl(pass.id),
]),
),
};
console.log("\nCreated resources:");
Object.entries(results[name]).forEach(([label, value]) => {
if (value) console.log(` ${label}: ${value}`);
});
}
async function run(name = process.argv[2] || "loyalty") {
const Example = examples[name];
if (!Example) {
throw new Error(
`Unknown example "${name}". Choose: ${Object.keys(examples).join(", ")}`,
);
}
const config = validateConfig(loadConfig(), {
requireAppleCertificate: name === "flights",
});
const connection = createClient(config);
const example = new Example(connection.client, config);
try {
await example.runQuickStart();
printResults(name, example, config.address);
} finally {
try {
if (config.keepAssets) {
console.log(
"PASSKIT_KEEP_ASSETS=true; generated resources were not deleted.",
);
} else {
console.log("Cleaning up generated resources...");
await example.cleanUp();
}
} finally {
connection.close();
}
}
}
if (require.main === module) {
run().catch((error) => {
console.error(error.message);
process.exitCode = 1;
});
}
module.exports = { examples, printResults, run };