github.com/pluralsh/plural-cli@v0.9.5/pkg/ui/web/src/services/wails.ts (about)

     1  import { WizardStepConfig } from '@pluralsh/design-system'
     2  
     3  import { ui } from '../../wailsjs/go/models'
     4  import {
     5    Context,
     6    Install,
     7    Project,
     8    Provider,
     9    Token,
    10  } from '../../wailsjs/go/ui/Client'
    11  import { SetClipboard } from '../../wailsjs/go/ui/Window'
    12  import { Provider as APIProvider } from '../graphql/generated/graphql'
    13  import {
    14    Client,
    15    ClientBinding,
    16    PluralContext,
    17    PluralProject,
    18  } from '../types/client'
    19  
    20  import Application = ui.Application;
    21  
    22  /**
    23   * List of supported client methods based on API Go client.
    24   * @see pkg/api/client.go
    25   */
    26  enum Binding {
    27    Token = 'Token',
    28    Project = 'Project',
    29    Provider = 'Provider',
    30    Context = 'Context',
    31    Install = 'Install',
    32    SetClipboard = 'SetClipboard',
    33  }
    34  
    35  /**
    36   * Client mapping from defined bindings to exposed Go backend methods.
    37   * Abstracts the backend calls and wraps them with proper return types
    38   * to simplify usage in the UI.
    39   * @see Binding
    40   */
    41  const Plural: Client = {
    42    [Binding.Token]: (): Promise<string> => Token(),
    43    [Binding.Project]: (): Promise<PluralProject> => Project() as Promise<PluralProject>,
    44    [Binding.Provider]: (): Promise<APIProvider> => Provider() as Promise<APIProvider>,
    45    [Binding.Context]: (): Promise<PluralContext> => Context() as Promise<PluralContext>,
    46    [Binding.Install]: (apps: Array<WizardStepConfig>, domains: Array<string>, buckets: Array<string>): Promise<void> => Install(apps as Array<Application>, domains, buckets) as Promise<void>,
    47    [Binding.SetClipboard]: (text: string): Promise<void> => SetClipboard(text),
    48  }
    49  
    50  /**
    51   * Factory that simplifies getting wrapped client binding methods.
    52   * @param binding
    53   * @constructor
    54   */
    55  function ClientBindingFactory<TResult>(binding: Binding): ClientBinding<TResult> {
    56    const bindingFn: ClientBinding<TResult> = Plural[binding] as ClientBinding<TResult>
    57  
    58    if (!bindingFn) throw new Error(`Unsupported client endpoint: ${binding}`)
    59  
    60    return bindingFn
    61  }
    62  
    63  export { ClientBindingFactory, Binding }