oxford-dictionary-api-client
Version:
A Node.js and browser wrapper for Oxford Dictionary API
109 lines (108 loc) • 3.87 kB
JavaScript
const l = (s) => Array.isArray(s) ? s.join(",") : String(s), o = (s = {}) => {
const t = Object.entries(s).filter(([, r]) => r !== void 0).map(([r, e]) => [r, l(e)]);
return new URLSearchParams(t).toString();
}, c = (s, t) => t ? `${s}?${t}` : s, u = (s) => {
const t = s.findIndex((e) => !e);
return (t === -1 ? s : s.slice(0, t)).join("/");
};
class m {
#t;
#r;
constructor(t, r) {
this.#t = r, this.#r = t;
}
async request(t) {
try {
const r = { "Content-Type": "application/json" }, e = { ...this.#t, ...r }, a = `${this.#r}${t}`, n = await fetch(a, { headers: e });
if (!n.ok)
throw await n.json();
return n.json();
} catch (r) {
throw console.error(r), r;
}
}
async entries(t, r, e = {}) {
const a = o(e), n = `/entries/${t}/${r}`, i = c(n, a);
return this.request(i);
}
async translations(t, r, e, a = {}) {
const n = o(a), i = `/translations/${t}/${r}/${e}`, h = c(i, n);
return this.request(h);
}
async words(t, r, e = {}) {
const a = o({ ...e, q: r }), n = `/words/${t}`, i = c(n, a);
return this.request(i);
}
async lemmas(t, r, e = {}) {
const a = o(e), n = `/lemmas/${t}/${r}`, i = c(n, a);
return this.request(i);
}
async inflections(t, r, e = {}) {
const a = o(e), n = `/inflections/${t}/${r}`, i = c(n, a);
return this.request(i);
}
async thesaurus(t, r, e = {}) {
const a = o(e), n = `/thesaurus/${t}/${r}`, i = c(n, a);
return this.request(i);
}
async sentences(t, r, e = {}) {
const a = o(e), n = `/sentences/${t}/${r}`, i = c(n, a);
return this.request(i);
}
utility = {
dictionaryNames: ({
targetLang: t,
sourceLang: r
}) => {
const e = o({ targetLang: t, sourceLang: r }), n = c("/languages", e);
return this.request(n);
},
filters: (t) => {
const r = u(["/filters", t]);
return this.request(r);
},
fields: (t) => {
const r = u(["/fields", t]);
return this.request(r);
},
lexicalCategories: (t, r) => {
const e = u(["/lexicalCategories", t, r]);
return this.request(e);
},
registers: (t, r) => {
const e = u(["/registers", t, r]);
return this.request(e);
},
domains: (t, r) => {
const e = u(["/domains", t, r]);
return this.request(e);
},
grammaticalFeatures: (t, r) => {
const e = u(["/grammaticalFeatures", t, r]);
return this.request(e);
}
};
search = {
entries: (t, r, e = {}) => {
const a = o({ ...e, q: r }), n = `/search/${t}`, i = c(n, a);
return this.request(i);
},
thesaurus: (t, r, e = {}) => {
const a = o({ ...e, q: r }), n = `/search/thesaurus/${t}`, i = c(n, a);
return this.request(i);
},
translations: (t, r, e, a = {}) => {
const n = o({ ...a, q: e }), i = `/search/translations/${t}/${r}`, h = c(i, n);
return this.request(h);
}
};
}
var d = /* @__PURE__ */ ((s) => (s.Headword = "headword", s.Inflection = "inflection", s.Phrase = "phrase", s))(d || {}), q = /* @__PURE__ */ ((s) => (s.Ipa = "ipa", s.DictionarySpecific = "dictionarySpecific", s.OupToneCode = "oupToneCode", s))(q || {}), g = /* @__PURE__ */ ((s) => (s.Pre = "pre", s.Post = "post", s))(g || {}), $ = /* @__PURE__ */ ((s) => (s.CloseMatch = "close match", s.Related = "related", s.SeeAlso = "see also", s.VariantSpelling = "variant spelling", s.Abbreviation = "abbreviation", s))($ || {}), p = /* @__PURE__ */ ((s) => (s.GrammaticalNote = "grammaticalNote", s.UsageNote = "usageNote", s.EditorialNote = "editorialNote", s.EncyclopedicNote = "encyclopedicNote", s.EtymologyNote = "etymologyNote", s.TechnicalNote = "technicalNote", s))(p || {});
export {
g as CollocationType,
$ as CrossReferenceType,
d as EntryType,
p as NoteType,
m as OxfordDictionary,
q as ToneType
};