diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/api.ts | 9 | ||||
-rw-r--r-- | core/direct/client.ts | 12 | ||||
-rw-r--r-- | core/http/client.ts | 18 | ||||
-rw-r--r-- | core/http/server.ts | 14 | ||||
-rw-r--r-- | core/http/types.ts | 8 | ||||
-rw-r--r-- | core/raw/api.ts | 10 | ||||
-rw-r--r-- | core/readme.md | 12 |
7 files changed, 48 insertions, 35 deletions
diff --git a/core/api.ts b/core/api.ts index 017e131..51f976a 100644 --- a/core/api.ts +++ b/core/api.ts @@ -1,12 +1,13 @@ import { ParseResult } from "../language/types.ts"; /** - * @summary API interface + * @summary Core interface * - * This interface gets implemented by all API clients, so clients can be - * swapped easily. + * This interface gets implemented by all Core clients, so clients can be + * swapped easily. You should probably not directly use any Core, but use the + * abstracted API from ../api/ */ -export default abstract class API { +export default abstract class Core { /** @summary resolved when ready */ abstract ready: Promise<void>; diff --git a/core/direct/client.ts b/core/direct/client.ts index fda4b60..328cf1f 100644 --- a/core/direct/client.ts +++ b/core/direct/client.ts @@ -1,11 +1,11 @@ -import API from "../api.ts"; -import YomikunRAWAPI from "../raw/api.ts"; +import Core from "../api.ts"; +import RawCore from "../raw/api.ts"; /** - * @summary Yomikun direct API + * @summary Direct Core * - * Alias to YomikunRAWAPI. calls API methods directly, and thus only works - * server-side. Used to test the API locally without HTTP overhead. + * Alias to RawCore. Calls Core methods directly, and thus only works + * server-side. Used to test the Core locally without HTTP overhead. */ -export default class YomikunDirectAPIClient extends YomikunRAWAPI implements API { } +export default class DirectCoreClient extends RawCore implements Core { } diff --git a/core/http/client.ts b/core/http/client.ts index 365aa76..118e8f5 100644 --- a/core/http/client.ts +++ b/core/http/client.ts @@ -1,16 +1,16 @@ import "../../util/array.ts"; -import API from "../api.ts"; +import Core from "../api.ts"; import { ConnectionProps, ConnectionPropsDefault } from "./props.ts"; -import { APIRequest, APIRequestParseSentence, APIResponseParseSentence } from "./types.ts"; +import { CoreRequest, CoreRequestParseSentence, CoreResponseParseSentence } from "./types.ts"; /** - * @summary Yomikun HTTP API + * @summary HTTP Core client * - * Uses the Yomikun server to call API methods. Handles (de)serialization - * automatically. + * Connects to an instance of RemoteCoreServer to call Core methods. Handles + * (de)serialization automatically. */ -export default class YomikunRemoteAPIClient implements API { +export default class RemoteCoreClient implements Core { private props: ConnectionProps; ready: Promise<void> = Promise.resolve(); @@ -18,7 +18,7 @@ export default class YomikunRemoteAPIClient implements API { this.props = { ...ConnectionPropsDefault, ...options }; } - private async request(details: APIRequest) { + private async request(details: CoreRequest) { var response = await fetch(`http://${this.props.host}:${this.props.port}`, { method: "POST", headers: { @@ -31,13 +31,13 @@ export default class YomikunRemoteAPIClient implements API { } async parseSentence(input: string) { - var request: APIRequestParseSentence = { + var request: CoreRequestParseSentence = { command: "parseSentence", options: { input: input, }, }; - var { response } = await this.request(request) as APIResponseParseSentence; + var { response } = await this.request(request) as CoreResponseParseSentence; return response; } } diff --git a/core/http/server.ts b/core/http/server.ts index 1af8d25..0a9a082 100644 --- a/core/http/server.ts +++ b/core/http/server.ts @@ -2,22 +2,22 @@ import { serve } from "https://deno.land/std@0.192.0/http/server.ts"; import "../../util/string.ts"; -import YomikunRAWAPI from "../raw/api.ts"; +import RawCore from "../raw/api.ts"; import { ConnectionProps, ConnectionPropsDefault } from "./props.ts"; -import { APIRequest, APIRequestParseSentence, APIResponseParseSentence } from "./types.ts"; +import { CoreRequest, CoreRequestParseSentence, CoreResponseParseSentence } from "./types.ts"; -export default class YomikunRemoteAPIServer extends YomikunRAWAPI { +export default class RemoteCoreServer extends RawCore { private props: ConnectionProps; - private handlers: Record<string, (req: APIRequest) => Promise<Response>> = { + private handlers: Record<string, (req: CoreRequest) => Promise<Response>> = { parseSentence: async _req => { - var req = _req as APIRequestParseSentence; + var req = _req as CoreRequestParseSentence; var input = req.options?.input if (!input) return new Response("", { status: 404 }); return new Response(JSON.stringify({ command: "parseSentence", response: await this.parseSentence(input), - } as APIResponseParseSentence)); + } as CoreResponseParseSentence)); }, }; @@ -29,7 +29,7 @@ export default class YomikunRemoteAPIServer extends YomikunRAWAPI { async start() { serve(async (req) => { if (req.method != "POST") return new Response("", { status: 400 }); // wrong request (not post) - var request = (await req.text()).json({}) as APIRequest; + var request = (await req.text()).json({}) as CoreRequest; if (!request.command) return new Response("", { status: 400 }); // wrong request (no command) var handler = this.handlers[request.command]; if (!handler) return new Response("", { status: 404 }); // not found (unknown command) diff --git a/core/http/types.ts b/core/http/types.ts index bbba1b5..af2cfea 100644 --- a/core/http/types.ts +++ b/core/http/types.ts @@ -1,24 +1,24 @@ import { ParseResult } from "../../language/types.ts"; -export interface APIRequest { +export interface CoreRequest { command: string; options: any; }; -export interface APIRequestParseSentence extends APIRequest { +export interface CoreRequestParseSentence extends CoreRequest { command: "parseSentence"; options: { input: string; }; }; -export interface APIResponse { +export interface CoreResponse { command: string; response: any; // final: boolean; }; -export interface APIResponseParseSentence extends APIResponse { +export interface CoreResponseParseSentence extends CoreResponse { command: "parseSentence"; response: ParseResult; }; diff --git a/core/raw/api.ts b/core/raw/api.ts index 06db022..f47dead 100644 --- a/core/raw/api.ts +++ b/core/raw/api.ts @@ -1,15 +1,15 @@ -import API from "../api.ts"; +import Core from "../api.ts"; import Parser from "../../language/parser.ts"; import YomikunError from "../../util/error.ts"; -/** @summary internal Yomikun API client (DO NOT USE DIRECTLY) */ -export default class YomikunRAWAPI implements API { +/** @summary internal Core (DO NOT USE DIRECTLY) */ +export default class RawCore implements Core { private _parser: Parser; ready: Promise<void>; constructor() { - if (this.constructor === YomikunRAWAPI) { - throw new YomikunError("YomikunRAWAPI instantiated! please use YomikunDirectAPIClient instead"); + if (this.constructor === RawCore) { + throw new YomikunError("RawCore instantiated! Use DirectCoreClient instead!"); } this._parser = new Parser(); diff --git a/core/readme.md b/core/readme.md new file mode 100644 index 0000000..3c602a9 --- /dev/null +++ b/core/readme.md @@ -0,0 +1,12 @@ +# Core API + +This folder contains the Core API. A Core instance is used as a standardized +API for interfacing with internal classes/modules. Different Cores can be used +interchangeably with the [Yomikun API](../api/readme.md) for providing the API +over various types of connections. + +Currently implemented cores: + +- HTTP +- Direct + |