@baseplate-dev/sync
Version:
Library for syncing Baseplate descriptions
74 lines • 2.68 kB
JavaScript
import { toMerged } from 'es-toolkit';
import { KEBAB_CASE_REGEX } from '#src/utils/validation.js';
/**
* Creates a provider type
*
* @param name The name of the provider type
* @param options The options for the provider type
* @returns The provider type
*/
export function createProviderType(name, options) {
if (!KEBAB_CASE_REGEX.test(name)) {
throw new Error(`Provider type name must be in kebab case (lowercase with dashes): ${name}`);
}
return {
type: 'type',
name,
isReadOnly: options?.isReadOnly ?? false,
dependency() {
return {
...this,
type: 'dependency',
isReadOnly: options?.isReadOnly ?? false,
options: {},
optional() {
return toMerged(this, { options: { optional: true } });
},
reference(exportName) {
if (this.options.exportName !== undefined) {
throw new Error('Cannot overwrite export name on provider type');
}
return toMerged(this, {
options: { exportName },
});
},
optionalReference(exportName) {
if (this.options.exportName !== undefined) {
throw new Error('Cannot overwrite export name on provider type');
}
return toMerged(this, {
// empty string is equivalent to resolving to undefined
options: { exportName: exportName ?? '', optional: true },
});
},
parentScopeOnly() {
return toMerged(this, { options: { useParentScope: true } });
},
};
},
export(scope, exportName) {
return {
...this,
type: 'export',
isReadOnly: options?.isReadOnly ?? false,
exports: [{ scope, exportName }],
andExport(scope, exportName) {
return toMerged(this, {
exports: [...this.exports, { scope, exportName }],
});
},
};
},
};
}
/**
* Creates a read-only provider type
*
* @param name The name of the provider type
* @param options The options for the provider type
* @returns The provider type
*/
export function createReadOnlyProviderType(name, options) {
return createProviderType(name, { ...options, isReadOnly: true });
}
//# sourceMappingURL=providers.js.map