mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 19:25:23 +00:00
Added implementation of abstract models
This commit is contained in:
63
src/abstract.ts
Normal file
63
src/abstract.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { Certificates, FinalCertificates, PartitionedBundle, OverridesSupportedOptions, FactoryOptions } from "./schema";
|
||||
import { getModelContents, readCertificatesFromOptions } from "./parser";
|
||||
import formatMessage from "./messages";
|
||||
|
||||
const abmCertificates = Symbol("certificates");
|
||||
const abmModel = Symbol("model");
|
||||
const abmOverrides = Symbol("overrides");
|
||||
|
||||
export interface AbstractFactoryOptions extends Omit<FactoryOptions, "certificates"> {
|
||||
certificates?: Certificates;
|
||||
}
|
||||
|
||||
interface AbstractModelOptions {
|
||||
bundle: PartitionedBundle;
|
||||
certificates: FinalCertificates;
|
||||
overrides?: OverridesSupportedOptions;
|
||||
}
|
||||
|
||||
export async function createAbstractModel(options: AbstractFactoryOptions) {
|
||||
if (!(options && Object.keys(options).length)) {
|
||||
throw new Error(formatMessage("CP_NO_OPTS"));
|
||||
}
|
||||
|
||||
try {
|
||||
const [bundle, certificates] = await Promise.all([
|
||||
getModelContents(options.model),
|
||||
readCertificatesFromOptions(options.certificates)
|
||||
]);
|
||||
|
||||
return new AbstractModel({
|
||||
bundle,
|
||||
certificates,
|
||||
overrides: options.overrides
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
throw new Error(formatMessage("CP_INIT_ERROR", "abstract model", err));
|
||||
}
|
||||
}
|
||||
|
||||
export class AbstractModel {
|
||||
private [abmCertificates]: FinalCertificates;
|
||||
private [abmModel]: PartitionedBundle;
|
||||
private [abmOverrides]: OverridesSupportedOptions;
|
||||
|
||||
constructor(options: AbstractModelOptions) {
|
||||
this[abmModel] = options.bundle;
|
||||
this[abmCertificates] = options.certificates,
|
||||
this[abmOverrides] = options.overrides
|
||||
}
|
||||
|
||||
get certificates(): FinalCertificates {
|
||||
return this[abmCertificates];
|
||||
}
|
||||
|
||||
get bundle(): PartitionedBundle {
|
||||
return this[abmModel];
|
||||
}
|
||||
|
||||
get overrides(): OverridesSupportedOptions {
|
||||
return this[abmOverrides];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user