From cd8e865dad326f85ff2357ad90bbd6aa65dea68e Mon Sep 17 00:00:00 2001 From: vnugent Date: Wed, 6 Sep 2023 13:51:13 -0400 Subject: initial commit --- .../src/entries/background/server-api/endpoints.ts | 72 +++++++++++++++++++ .../src/entries/background/server-api/index.ts | 80 ++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 extension/src/entries/background/server-api/endpoints.ts create mode 100644 extension/src/entries/background/server-api/index.ts (limited to 'extension/src/entries/background/server-api') 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 . + + +import { useAxios } from "@vnuge/vnlib.browser"; +import { Method } from "axios"; + +export interface EndpointDefinition { + readonly method: Method + path(request?: any): string + onRequest: (request?: any) => Promise + onResponse: (response: any, request?: any) => Promise +} + +export interface EndpointDefinitionReg extends EndpointDefinition { + readonly id: T +} + +export const initEndponts = () => { + + const endpoints = new Map(); + + const registerEndpoint = (def: EndpointDefinitionReg) => { + //Store the handler by its id + endpoints.set(def.id, def); + return def; + } + + const getEndpoint = (id: T): EndpointDefinition | undefined => { + return endpoints.get(id); + } + + const execRequest = async (id: string, request?: any): Promise => { + 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 . + + +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) => { + 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) => { + 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 -- cgit