aboutsummaryrefslogtreecommitdiff
path: root/extension/src/entries/background/server-api
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2023-09-06 13:51:13 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2023-09-06 13:51:13 -0400
commitcd8e865dad326f85ff2357ad90bbd6aa65dea68e (patch)
tree0d4a0bb8bafc4f807407e99c5e6bf4e1cb34217a /extension/src/entries/background/server-api
initial commit
Diffstat (limited to 'extension/src/entries/background/server-api')
-rw-r--r--extension/src/entries/background/server-api/endpoints.ts72
-rw-r--r--extension/src/entries/background/server-api/index.ts80
2 files changed, 152 insertions, 0 deletions
diff --git a/extension/src/entries/background/server-api/endpoints.ts b/extension/src/entries/background/server-api/endpoints.ts
new file mode 100644
index 0000000..a7f1488
--- /dev/null
+++ b/extension/src/entries/background/server-api/endpoints.ts
@@ -0,0 +1,72 @@
+// Copyright (C) 2023 Vaughn Nugent
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+
+import { useAxios } from "@vnuge/vnlib.browser";
+import { Method } from "axios";
+
+export interface EndpointDefinition {
+ readonly method: Method
+ path(request?: any): string
+ onRequest: (request?: any) => Promise<any>
+ onResponse: (response: any, request?: any) => Promise<any>
+}
+
+export interface EndpointDefinitionReg<T extends string> extends EndpointDefinition {
+ readonly id: T
+}
+
+export const initEndponts = () => {
+
+ const endpoints = new Map<string, EndpointDefinition>();
+
+ const registerEndpoint = <T extends string>(def: EndpointDefinitionReg<T>) => {
+ //Store the handler by its id
+ endpoints.set(def.id, def);
+ return def;
+ }
+
+ const getEndpoint = <T extends string>(id: T): EndpointDefinition | undefined => {
+ return endpoints.get(id);
+ }
+
+ const execRequest = async <T>(id: string, request?: any): Promise<T> => {
+ const endpoint = getEndpoint(id);
+ if (!endpoint) {
+ throw new Error(`Endpoint ${id} not found`);
+ }
+
+ //Compute the path from the request
+ const path = endpoint.path(request);
+
+ //Execute the request handler
+ const req = await endpoint.onRequest(request);
+
+ //Get axios
+ const axios = useAxios(null);
+
+ //Exec the request
+ const { data } = await axios({ method: endpoint.method, url: path, data: req });
+
+ //exec the response handler and return its result
+ return await endpoint.onResponse(data, request);
+ }
+
+ return {
+ registerEndpoint,
+ getEndpoint,
+ execRequest
+ }
+}
diff --git a/extension/src/entries/background/server-api/index.ts b/extension/src/entries/background/server-api/index.ts
new file mode 100644
index 0000000..3e1ada0
--- /dev/null
+++ b/extension/src/entries/background/server-api/index.ts
@@ -0,0 +1,80 @@
+// Copyright (C) 2023 Vaughn Nugent
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+
+import { Ref } from "vue"
+import { WebMessage } from "@vnuge/vnlib.browser"
+import { initEndponts } from "./endpoints"
+import { NostrEvent, NostrPubKey, NostrRelay } from "../types"
+
+export enum Endpoints {
+ GetKeys = 'getKeys',
+ DeleteKey = 'deleteKey',
+ SignEvent = 'signEvent',
+ GetRelays = 'getRelays',
+ SetRelay = 'setRelay',
+ Encrypt = 'encrypt',
+ Decrypt = 'decrypt',
+}
+
+export const initApi = (nostrUrl: Ref<string>) => {
+ const { registerEndpoint, execRequest } = initEndponts()
+
+ registerEndpoint({
+ id: Endpoints.GetKeys,
+ method: 'GET',
+ path: () => `${nostrUrl.value}?type=getKeys`,
+ onRequest: () => Promise.resolve(),
+ onResponse: (response) => Promise.resolve(response)
+ })
+
+ registerEndpoint({
+ id: Endpoints.DeleteKey,
+ method: 'DELETE',
+ path: (key: NostrPubKey) => `${nostrUrl.value}?type=identity&key_id=${key.Id}`,
+ onRequest: () => Promise.resolve(),
+ onResponse: (response: WebMessage) => response.getResultOrThrow()
+ })
+
+ registerEndpoint({
+ id: Endpoints.SignEvent,
+ method: 'POST',
+ path: () => `${nostrUrl.value}?type=signEvent`,
+ onRequest: (event: NostrEvent) => Promise.resolve(event),
+ onResponse: async (response: WebMessage<NostrEvent>) => {
+ return response.getResultOrThrow()
+ }
+ })
+
+ registerEndpoint({
+ id: Endpoints.GetRelays,
+ method: 'GET',
+ path: () => `${nostrUrl.value}?type=getRelays`,
+ onRequest: () => Promise.resolve(),
+ onResponse: (response) => Promise.resolve(response)
+ })
+
+ registerEndpoint({
+ id: Endpoints.SetRelay,
+ method: 'POST',
+ path: () => `${nostrUrl.value}?type=relay`,
+ onRequest: (relay: NostrRelay) => Promise.resolve(relay),
+ onResponse: (response) => Promise.resolve(response)
+ })
+
+ return {
+ execRequest
+ }
+} \ No newline at end of file