web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

lib.ts (3030B)


      1 import {
      2     JobFeedbackStatus,
      3     KIND_JOB_FEEDBACK,
      4     RadrootsJobFeedback,
      5     RadrootsJobRequest,
      6     RadrootsJobResult,
      7 } from "@radroots/events-bindings";
      8 import type { NostrEventFigure, NostrSignedEvent } from "../../types/nostr.js";
      9 import { nostr_event_create } from "../lib.js";
     10 import { tags_job_feedback, tags_job_request, tags_job_result } from "./tags.js";
     11 
     12 export const nostr_event_job_request = async (
     13     opts: NostrEventFigure<{ data: RadrootsJobRequest }>,
     14 ): Promise<NostrSignedEvent | undefined> => {
     15     const { data } = opts;
     16     const tags = await tags_job_request(data);
     17     return nostr_event_create({
     18         ...opts,
     19         basis: {
     20             kind: data.kind,
     21             content: "",
     22             tags,
     23         },
     24     });
     25 };
     26 
     27 export const nostr_event_job_result = async (
     28     opts: NostrEventFigure<{ data: RadrootsJobResult }>,
     29 ): Promise<NostrSignedEvent | undefined> => {
     30     const { data } = opts;
     31     const tags = await tags_job_result(data);
     32     return nostr_event_create({
     33         ...opts,
     34         basis: {
     35             kind: data.kind,
     36             content: data.content || "",
     37             tags,
     38         },
     39     });
     40 };
     41 
     42 export const nostr_event_job_feedback = async (
     43     opts: NostrEventFigure<{ data: RadrootsJobFeedback }>,
     44 ): Promise<NostrSignedEvent | undefined> => {
     45     const { data } = opts;
     46     const tags = await tags_job_feedback(data);
     47     return nostr_event_create({
     48         ...opts,
     49         basis: {
     50             kind: data.kind,
     51             content: data.content || "",
     52             tags,
     53         },
     54     });
     55 };
     56 
     57 export const nostr_event_job_feedback_todo = async (
     58     opts: NostrEventFigure<{
     59         request_event_id: string;
     60         status:
     61             | JobFeedbackStatus
     62             | "payment-required"
     63             | "processing"
     64             | "error"
     65             | "success"
     66             | "partial";
     67         content?: string;
     68         options?: {
     69             request_relay_hint?: string;
     70             extra_info?: string;
     71             customer_pubkey?: string;
     72             amount_sat?: number;
     73             bolt11?: string;
     74             encrypted?: boolean;
     75         };
     76     }>,
     77 ): Promise<NostrSignedEvent | undefined> => {
     78     const { request_event_id, status, content, options } = opts;
     79 
     80     const fb: RadrootsJobFeedback = {
     81         kind: KIND_JOB_FEEDBACK,
     82         status: status as JobFeedbackStatus,
     83         extra_info: options?.extra_info,
     84         request_event: {
     85             id: request_event_id,
     86             ...(options?.request_relay_hint ? { relays: options.request_relay_hint } : {}),
     87         },
     88         customer_pubkey: options?.customer_pubkey,
     89         payment:
     90             options?.amount_sat !== undefined
     91                 ? { amount_sat: options.amount_sat, bolt11: options?.bolt11 }
     92                 : undefined,
     93         content,
     94         encrypted: !!options?.encrypted,
     95     };
     96 
     97     const tags = await tags_job_feedback(fb);
     98 
     99     return nostr_event_create({
    100         ...opts,
    101         basis: { kind: KIND_JOB_FEEDBACK, content: content ?? "", tags },
    102     });
    103 };