import "../../util/array.ts"; import Core, { CoreExport, CoreImport, CoreSearch, CoreUser } from "../api.ts"; import { ConnectionProps, ConnectionPropsDefault } from "./props.ts"; import { CoreRequest, CoreRequestSearchSentence, CoreRequestSearchTerms, CoreRequestUserTermPriority, CoreResponseSearchSentence, CoreResponseSearchTerms, CoreResponseUserTermPriority } from "./types.ts"; /** * @summary HTTP Core client * * Connects to an instance of RemoteCoreServer to call Core methods. Handles * (de)serialization automatically. */ export default class RemoteCoreClient implements Core { public ready: Promise = Promise.resolve(); private props: ConnectionProps; constructor(options?: ConnectionProps) { this.props = { ...ConnectionPropsDefault, ...options }; } private async request(details: CoreRequest) { var response = await fetch(`http://${this.props.host}:${this.props.port}`, { method: "POST", headers: { "Accept": "application/json", "Content-Type": "application/json", }, body: JSON.stringify(details), }); return response.json(); } public search: CoreSearch = { terms: async term => { var request: CoreRequestSearchTerms = { command: "search.terms", options: { term, }, }; var { response } = await this.request(request) as CoreResponseSearchTerms; return response; }, sentence: async (sentence, optional?) => { var request: CoreRequestSearchSentence = { command: "search.sentence", options: { sentence, optional, }, }; var { response } = await this.request(request) as CoreResponseSearchSentence; return response; }, }; public user: CoreUser = { termPriority: async (userID, expression, reading, priority) => { var request: CoreRequestUserTermPriority = { command: "user.termPriority", options: { userID, expression, reading, priority, }, }; var { response } = await this.request(request) as CoreResponseUserTermPriority; return response; }, }; public import: CoreImport = {}; public export: CoreExport = {}; }