openalex-client
Version:
TypeScript SDK for OpenAlex academic database API
188 lines • 5.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConceptsService = void 0;
const base_1 = require("./base");
class ConceptsService extends base_1.BaseService {
constructor() {
super(...arguments);
this.endpoint = "concepts";
}
/**
* Get works related to a specific concept
*/
async getWorks(conceptId, params) {
const cleanId = this.cleanId(conceptId);
const response = await this.client.get(`/concepts/${cleanId}/works`, this.buildParams(params));
return response.data;
}
/**
* Get authors working in a specific concept area
*/
async getAuthors(conceptId, params) {
const cleanId = this.cleanId(conceptId);
const response = await this.client.get(`/concepts/${conceptId}/authors`, this.buildParams(params));
return response.data;
}
/**
* Search concepts by Wikidata ID
*/
async getByWikidata(wikidataId, params) {
const cleanId = wikidataId.replace(/^https?:\/\/www\.wikidata\.org\/wiki\//, "");
return this.get(`https://www.wikidata.org/wiki/${cleanId}`, params);
}
/**
* Get concepts by level (0-5, where 0 is most general)
*/
async getByLevel(level, params) {
if (level < 0 || level > 5) {
throw new Error("Concept level must be between 0 and 5");
}
const filterParams = {
...params,
filter: {
...params?.filter,
level: level,
},
};
return this.list(filterParams);
}
/**
* Get top-level concepts (level 0)
*/
async getTopLevel(params) {
return this.getByLevel(0, params);
}
/**
* Get concepts by minimum works count
*/
async getByMinWorksCount(minCount, params) {
const filterParams = {
...params,
filter: {
...params?.filter,
works_count: `>${minCount}`,
},
};
return this.list(filterParams);
}
/**
* Get concepts by citation count range
*/
async getByCitationRange(minCitations, maxCitations, params) {
const citationFilter = maxCitations
? `${minCitations}-${maxCitations}`
: `>${minCitations}`;
const filterParams = {
...params,
filter: {
...params?.filter,
cited_by_count: citationFilter,
},
};
return this.list(filterParams);
}
/**
* Get the most popular concepts
*/
async getMostPopular(params) {
return this.list({
...params,
sort: "works_count:desc",
});
}
/**
* Get the most cited concepts
*/
async getMostCited(params) {
return this.list({
...params,
sort: "cited_by_count:desc",
});
}
/**
* Search concepts by name/description
*/
async searchByName(name, params) {
return this.list({
...params,
search: name,
});
}
/**
* Get concepts with recent activity
*/
async getRecentlyActive(params) {
const currentYear = new Date().getFullYear();
const filterParams = {
...params,
filter: {
...params?.filter,
"counts_by_year.year": currentYear,
},
};
return this.list(filterParams);
}
/**
* Get ancestor concepts (broader concepts)
*/
async getAncestors(conceptId) {
const concept = await this.get(conceptId);
if (!concept.ancestors || concept.ancestors.length === 0) {
return [];
}
// Get full concept data for each ancestor
const ancestorPromises = concept.ancestors.map((ancestor) => this.get(ancestor.id));
return Promise.all(ancestorPromises);
}
/**
* Get related concepts
*/
async getRelated(conceptId) {
const concept = await this.get(conceptId);
if (!concept.related_concepts || concept.related_concepts.length === 0) {
return [];
}
// Get full concept data for each related concept
const relatedPromises = concept.related_concepts.map((related) => this.get(related.id));
return Promise.all(relatedPromises);
}
/**
* Get descendant concepts (narrower concepts)
*/
async getDescendants(conceptId, params) {
const cleanId = this.cleanId(conceptId);
const filterParams = {
...params,
filter: {
...params?.filter,
"ancestors.id": cleanId,
},
};
return this.list(filterParams);
}
/**
* Get concepts hierarchy tree
*/
async getHierarchy(conceptId, maxDepth = 3) {
const rootConcept = await this.get(conceptId);
return this.buildHierarchy(rootConcept, maxDepth, 0);
}
async buildHierarchy(concept, maxDepth, currentDepth) {
const hierarchy = {
concept,
children: [],
};
if (currentDepth < maxDepth) {
const descendants = await this.getDescendants(concept.id, {
per_page: 50,
});
for (const descendant of descendants.results) {
const childHierarchy = await this.buildHierarchy(descendant, maxDepth, currentDepth + 1);
hierarchy.children.push(childHierarchy);
}
}
return hierarchy;
}
}
exports.ConceptsService = ConceptsService;
//# sourceMappingURL=concepts.js.map