go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/static/_nextjs/src/api/graphs.ts (about)

     1  /**
     2   * Copyright (c) 2024 - Present. Will Charczuk. All rights reserved.
     3   * Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     4   */
     5  import { Viewport } from "reactflow";
     6  
     7  
     8  export interface Graph {
     9    id: string;
    10    label: string;
    11    stabilization_num: number;
    12    metadata: GraphMetadata;
    13  }
    14  
    15  export interface GraphMetadata {
    16    user_id: string;
    17    is_active: boolean;
    18    created_utc: Date;
    19    updated_utc?: Date;
    20    viewport_x: number;
    21    viewport_y: number;
    22    viewport_zoom: number;
    23  }
    24  
    25  export const emptyGraph: Graph = {
    26    id: '',
    27    label: '',
    28    stabilization_num: 1,
    29    metadata: {
    30      user_id: '',
    31      is_active: false,
    32      created_utc: new Date(),
    33      viewport_x: 0,
    34      viewport_y: 0,
    35      viewport_zoom: 1,
    36    }
    37  }
    38  
    39  export function viewportFromGraph(graph: Graph | null): Viewport {
    40    if (graph !== null) {
    41      const viewport = {
    42        x: graph.metadata.viewport_x,
    43        y: graph.metadata.viewport_y,
    44        zoom: graph.metadata.viewport_zoom,
    45      }
    46      return viewport
    47    }
    48    return { x: 0, y: 0, zoom: 1 }
    49  }
    50  
    51  export async function postGraphLoad(file: FileList): Promise<void> {
    52    const formData = new FormData();
    53    formData.append('file', file[0]);
    54  
    55    // Make a POST request to the server
    56    const res = await fetch('/api/v1/graph.load', {
    57      method: 'POST',
    58      body: formData,
    59    });
    60    if (!res?.ok) {
    61      const message = (await res.json()) as string;
    62      throw new Error(message)
    63    }
    64  }
    65  
    66  export async function getGraphs(): Promise<Graph[]> {
    67    const res = await fetch(`/api/v1/graphs`);
    68    if (!res?.ok) {
    69      const message = (await res.json()) as string;
    70      throw new Error(message)
    71    }
    72    const data = await res.json();
    73    return data as Graph[];
    74  };
    75  
    76  export async function getGraph(graphId: string): Promise<Graph> {
    77    const res = await fetch(`/api/v1/graph/${graphId}`);
    78    if (!res?.ok) {
    79      const message = (await res.json()) as string;
    80      throw new Error(message)
    81    }
    82    const data = await res.json();
    83    return data as Graph;
    84  };
    85  
    86  export async function getGraphActive(): Promise<Graph | null> {
    87    const res = await fetch(`/api/v1/graph.active`);
    88    if (res.status == 404) {
    89      return null
    90    }
    91    if (!res?.ok) {
    92      const message = (await res.json()) as string;
    93      throw new Error(message)
    94    }
    95    const data = await res.json();
    96    return data as Graph;
    97  };
    98  
    99  export async function putGraphActive(graphId: string): Promise<void> {
   100    const res = await fetch(`/api/v1/graph.active/${graphId}`, {
   101      method: 'PUT',
   102    });
   103    if (!res?.ok) {
   104      const message = (await res.json()) as string;
   105      throw new Error(message)
   106    }
   107  };
   108  
   109  export async function postGraph(graph: Graph): Promise<string> {
   110    const res = await fetch(`/api/v1/graph`, {
   111      method: 'POST',
   112      body: JSON.stringify(graph),
   113    });
   114    if (!res?.ok) {
   115      const message = (await res.json()) as string;
   116      throw new Error('server error creating graph: ' + message);
   117    }
   118    const data = await res.json();
   119    return data as string;
   120  };
   121  
   122  export async function patchGraph(graphId: string, patchData: { [key: string]: any }): Promise<void> {
   123    const res = await fetch(`/api/v1/graph/${graphId}`, {
   124      method: 'PATCH',
   125      body: JSON.stringify(patchData),
   126    });
   127    if (!res?.ok) {
   128      const message = (await res.json()) as string;
   129      throw new Error(message)
   130    }
   131  };
   132  
   133  export async function deleteGraph(graphId: string): Promise<void> {
   134    const res = await fetch(`/api/v1/graph/${graphId}`, {
   135      method: 'DELETE',
   136    });
   137    if (!res?.ok) {
   138      const message = (await res.json()) as string;
   139      throw new Error(message)
   140    }
   141  };
   142  
   143  export async function getGraphLogs(graphId: string): Promise<string> {
   144    const res = await fetch(`/api/v1/graph/${graphId}/logs`);
   145    if (!res?.ok) {
   146      const message = (await res.json()) as string;
   147      throw new Error(message)
   148    }
   149    const data = await res.json();
   150    return data as string;
   151  };