github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/webapp/javascript/services/apiKeys.ts (about)

     1  import { Result } from '@webapp/util/fp';
     2  import type { ZodError } from 'zod';
     3  import {
     4    APIKeys,
     5    apikeyModel,
     6    apiKeysSchema,
     7    APIKey,
     8  } from '@webapp/models/apikeys';
     9  import { request, parseResponse } from './base';
    10  import type { RequestError } from './base';
    11  
    12  export interface FetchAPIKeysError {
    13    message?: string;
    14  }
    15  
    16  export async function fetchAPIKeys(): Promise<
    17    Result<APIKeys, RequestError | ZodError>
    18  > {
    19    const response = await request('/api/keys');
    20    return parseResponse<APIKeys>(response, apiKeysSchema);
    21  }
    22  
    23  export async function createAPIKey(data: {
    24    name: string;
    25    role: string;
    26    ttlSeconds: number;
    27  }): Promise<Result<APIKey, RequestError | ZodError>> {
    28    const response = await request('/api/keys', {
    29      method: 'POST',
    30      body: JSON.stringify(data),
    31    });
    32  
    33    return parseResponse(response, apikeyModel);
    34  }
    35  
    36  export async function deleteAPIKey(data: {
    37    id: number;
    38  }): Promise<Result<unknown, RequestError | ZodError>> {
    39    const response = await request(`/api/keys/${data.id}`, {
    40      method: 'DELETE',
    41    });
    42  
    43    if (response.isOk) {
    44      return Result.ok();
    45    }
    46  
    47    return Result.err<APIKeys, RequestError>(response.error);
    48  }