dwnpm
Version:
Decentralized Registry Package Manager (DRPM) helps developers publish, install, find and manage Decentralized Packages (DPKs) published to Decentralized Web Nodes (DWNs). DRPM does this by looking up a Decentralized Identifier (DID) to find its DID docum
91 lines • 3.49 kB
JavaScript
import { DEFAULT_DWN_URL, DRPM_PROTOCOL_B64URL } from '../../config.js';
import { Logger } from '../logger.js';
import { DrlUtils } from './drl-utils.js';
import dwn from './protocol.js';
export class DrlBuilder {
baseDrl;
path = '';
query = [];
constructor({ endpoint, did }) {
if (!did)
throw new Error('DID required to build DRL');
if (!endpoint)
Logger.warn('No DWN Endpoint Found! Using the default endpoint is not recommended.');
this.baseDrl = `${endpoint ?? DEFAULT_DWN_URL}/${did}`;
}
// Start building with base DRL
static create({ did, endpoint }) {
return new DrlBuilder({ did, endpoint });
}
// Add a generic path (e.g., query, read)
addPath({ pathSegment }) {
this.path = `${this.path}/${pathSegment}`;
return this;
}
addProtocolEncoded() {
this.path = `${this.path}/read/protocols/${DRPM_PROTOCOL_B64URL}`;
return this;
}
// Add encoded protocol to the path
addProtocol({ protocol }) {
const encodedProtocol = DrlUtils.base64urlEncode(protocol ?? dwn.protocol);
this.path = `${this.path}/read/protocols/${encodedProtocol}`;
return this;
}
// Add encoded protocol to the path
addProtocolPath({ protocolPath }) {
this.path = `${this.path}/${protocolPath}`;
return this;
}
// Add filter query parameters dynamically
addFilter({ key, value, subKey }) {
const filter = subKey
? `filter.${key}.${subKey}=${value}`
: `filter.${key}=${value}`;
this.query.push(filter);
return this;
}
// Add multiple filters dynamically, supporting arrays for filters like tags
addFilters({ filters }) {
Object.keys(filters).forEach(key => {
const filter = filters[key];
// Case 1: Simple key-value pair, e.g., { 'protocolPath': 'package' }
if (typeof filter === 'string') {
this.addFilter({ key, value: filter });
}
// Case 2: List of objects with value and subKey,
// e.g. { tags: [{ value: 'tool5', subKey: 'name' }, { value: '6.1.0', subKey: 'version' }] }
else if (Array.isArray(filter)) {
filter.forEach(({ value, subKey }) => {
this.addFilter({ key, value, subKey });
});
}
// Case 3: Single object with value and subKey, e.g., { tags: { value: 'tool5', subKey: 'name' } }
else if (DrlUtils.isJsonObject(filter)) {
const { value, subKey } = filter ?? {};
this.addFilter({ key, value, subKey });
}
});
return this;
}
// Build and return the final DRL
build() {
const queryString = this.query.length ? `?${this.query.join('&')}` : '';
console.log('this', this);
return `${this.baseDrl}${this.path}${queryString}`;
}
// Handle building a query-based DRL
buildDrlQuery({ filters }) {
this.addPath({ pathSegment: 'query' });
this.addFilters({ filters });
return this.build();
}
// Handle building a protocol read DRL with optional protocolPath and filters
buildDrlRead({ protocolPath, filters }) {
this.addProtocol({});
this.addProtocolPath({ protocolPath });
this.addFilters({ filters });
return this.build();
}
}
//# sourceMappingURL=drl-builder.js.map