Unified props passing to PKPass.from between template and PKPass

This commit is contained in:
Alexander Cerutti
2021-10-16 12:44:01 +02:00
parent 4a02c03c62
commit 2a7f5f818c
3 changed files with 17 additions and 27 deletions

View File

@@ -1052,7 +1052,8 @@ describe("PKPass", () => {
),
);
const newPass = await PKPass.from({
const newPass = await PKPass.from(
{
model: path.resolve(
__dirname,
"../examples/models/exampleBooking.pass",
@@ -1060,10 +1061,11 @@ describe("PKPass", () => {
certificates: {
...baseCerts,
},
props: {
},
{
voided: true,
},
});
);
expect(Object.keys(newPass[filesSymbol]).length).toBe(7);

View File

@@ -39,13 +39,10 @@ export default class PKPass extends Bundle {
static async from<S extends PKPass | Schemas.Template>(
source: S,
additionalProps?: S extends PKPass
? Schemas.OverridablePassProps
: never,
props?: Schemas.OverridablePassProps,
): Promise<PKPass> {
let certificates: Schemas.CertificatesSchema = undefined;
let buffers: Schemas.FileBuffers = undefined;
let props: Schemas.OverridablePassProps = {};
if (!source) {
throw new TypeError(
@@ -86,22 +83,15 @@ export default class PKPass extends Bundle {
buffers = await getModelFolderContents(source.model);
certificates = source.certificates;
props = Schemas.validate(
Schemas.OverridablePassProps,
source.props,
);
}
if (additionalProps && Object.keys(additionalProps).length) {
Object.assign(
props,
Schemas.validate(Schemas.OverridablePassProps, additionalProps),
return new PKPass(
buffers,
certificates,
Schemas.validate(Schemas.OverridablePassProps, props),
);
}
return new PKPass(buffers, certificates, props);
}
/**
* Creates a Bundle made of PKPass to be distributed
* as a `.pkpasses` zip file. Returns a Bundle instance

View File

@@ -154,13 +154,11 @@ export const PassProps = Joi.object<
export interface Template {
model: string;
certificates: CertificatesSchema;
props?: OverridablePassProps;
}
export const Template = Joi.object<Template>({
model: Joi.string().required(),
certificates: Joi.object().required(),
props: OverridablePassProps,
});
// --------- UTILITIES ---------- //