@thedev132/yellowpages
Version:
HCB Yellow Pages for JS/TS
170 lines (166 loc) • 5.12 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
Category: () => Category,
Merchant: () => Merchant
});
module.exports = __toCommonJS(index_exports);
// src/category.ts
var _Category = class _Category {
constructor({ code, key }) {
if (!code && !key) {
throw new Error("Either code or key must be provided");
}
this.code = code;
this.key = key;
}
static async initialize() {
if (this.categoriesByCode === null) {
const response = await fetch(
"https://raw.githubusercontent.com/hackclub/yellow_pages/main/lib/yellow_pages/categories.yaml"
);
const text = await response.text();
const yaml = await import("js-yaml");
const data = yaml.load(text);
this.categoriesByCode = Object.entries(data).reduce(
(acc, [code, obj]) => {
acc[code.toString()] = {
code: code.toString(),
...obj,
key: obj.key
};
return acc;
},
{}
);
this.categoriesByKey = Object.values(this.categoriesByCode).reduce(
(acc, category) => {
acc[category.key] = category;
return acc;
},
{}
);
}
}
static lookup(params) {
return new _Category(params);
}
get category() {
if (!_Category.categoriesByCode || !_Category.categoriesByKey) {
throw new Error(
"Categories not initialized. Call Category.initialize() first"
);
}
if (this.code) {
return _Category.categoriesByCode[this.code];
} else if (this.key) {
return _Category.categoriesByKey[this.key];
}
return void 0;
}
getCode() {
return this.category?.code;
}
getName() {
return this.category?.name;
}
getKey() {
return this.category?.key;
}
inDataset() {
return this.category !== void 0;
}
};
_Category.categoriesByCode = null;
_Category.categoriesByKey = null;
var Category = _Category;
// src/merchant.ts
var _Merchant = class _Merchant {
constructor({ networkId }) {
this.networkId = networkId;
}
static async initialize() {
if (this.merchants === null) {
const response = await fetch(
"https://raw.githubusercontent.com/hackclub/yellow_pages/main/lib/yellow_pages/merchants.yaml"
);
const text = await response.text();
const yaml = await import("js-yaml");
const data = yaml.load(text);
this.merchants = data.reduce((acc, merchant) => {
if (!merchant.name) return acc;
const filename = merchant.name.replace(/[ '-]/g, "").toLowerCase();
const iconUrl = `https://raw.githubusercontent.com/hackclub/yellow_pages/main/lib/assets/icons/${filename}.svg`;
merchant.network_ids.forEach((networkId) => {
acc[networkId] = {
name: merchant.name,
iconUrl
};
});
return acc;
}, {});
}
}
static lookup(params) {
return new _Merchant(params);
}
get merchant() {
if (!_Merchant.merchants) {
throw new Error(
"Merchants not initialized. Call Merchant.initialize() first"
);
}
return _Merchant.merchants[this.networkId];
}
getName() {
return this.merchant?.name;
}
async getIcon() {
if (!this.merchant?.iconUrl) return void 0;
try {
const response = await fetch(this.merchant.iconUrl);
if (!response.ok) return void 0;
return await response.text();
} catch {
return void 0;
}
}
inDataset() {
return this.merchant !== void 0;
}
};
_Merchant.merchants = null;
var Merchant = _Merchant;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Category,
Merchant
});