|
| 1 | +import type { Exchange } from "@urql/core"; |
| 2 | +import type { GadgetSubscriptionClientOptions } from "./AnyConnection.js"; |
| 3 | + |
| 4 | +/** All the options for a Gadget client */ |
| 5 | +export interface ClientOptions { |
| 6 | + /** |
| 7 | + * The HTTP GraphQL endpoint this connection should connect to |
| 8 | + **/ |
| 9 | + endpoint?: string; |
| 10 | + /** |
| 11 | + * The authentication strategy for connecting to the upstream API |
| 12 | + **/ |
| 13 | + authenticationMode?: AuthenticationModeOptions; |
| 14 | + /** |
| 15 | + * The Websockets GraphQL endpoint this connection should connect to for transactional processing |
| 16 | + **/ |
| 17 | + websocketsEndpoint?: string; |
| 18 | + /** |
| 19 | + * Custom options to pass along to the WS clients when creating them |
| 20 | + **/ |
| 21 | + subscriptionClientOptions?: GadgetSubscriptionClientOptions; |
| 22 | + /** |
| 23 | + * The `WebSocket` constructor to use for building websockets. Defaults to `globalThis.WebSocket`. |
| 24 | + **/ |
| 25 | + websocketImplementation?: any; |
| 26 | + /** |
| 27 | + * The `fetch` function to use for making HTTP requests. Defaults to `globalThis.fetch`. |
| 28 | + **/ |
| 29 | + fetchImplementation?: typeof fetch; |
| 30 | + /** |
| 31 | + * Which of the Gadget application's environments this connection should connect to |
| 32 | + **/ |
| 33 | + environment?: string; |
| 34 | + /** |
| 35 | + * The ID of the application. Not required -- only used for emitting telemetry |
| 36 | + **/ |
| 37 | + applicationId?: string; |
| 38 | + /** |
| 39 | + * The root URL of the app's public HTTP surface. Used for building fully-qualified URLs when `api.fetch` is called with relative paths. |
| 40 | + * |
| 41 | + * This only needs to be passed if you are overriding the `endpoint` parameter to something that can't be used for building fully-qualified URLs from relative imports. |
| 42 | + **/ |
| 43 | + baseRouteURL?: string; |
| 44 | + /** |
| 45 | + * A list of exchanges to merge into the default exchanges used by the client. |
| 46 | + */ |
| 47 | + exchanges?: Exchanges; |
| 48 | +} |
| 49 | + |
| 50 | +/** Options to configure a specific browser-based authentication mode */ |
| 51 | +export interface BrowserSessionAuthenticationModeOptions { |
| 52 | + /** |
| 53 | + * The initial token to set for browser authentication. |
| 54 | + * This is useful when your session is initialized by some external authentication system, like OAuth. |
| 55 | + */ |
| 56 | + initialToken?: string; |
| 57 | + |
| 58 | + /** |
| 59 | + * Configures how the authentication token is persisted. See `BrowserSessionStorageType`. |
| 60 | + */ |
| 61 | + storageType?: BrowserSessionStorageType; |
| 62 | + /** |
| 63 | + * The shop ID to set shop tenant. Useful for fetching shop-specific data. |
| 64 | + */ |
| 65 | + shopId?: string; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * If using the `browserSession` authentication mode, sets how long the stored authentication information will last for for each user. |
| 70 | + */ |
| 71 | +export enum BrowserSessionStorageType { |
| 72 | + /** |
| 73 | + * `Durable` authentications ask the browser to keep the user's authentication information around for as long as it can, like the "Remember Me" button on a lot of webpages. Uses `window.localStorage` to store authentication tokens. |
| 74 | + */ |
| 75 | + Durable = "Durable", |
| 76 | + /** |
| 77 | + * `Session` authentications ask the browser to keep the user's authentication information around for a given browser tab, and then remove it when the tab is closed. Useful for high security scenarios where authenticated sessions are sensitive and should be forgotten quickly, or where the user's identity is temporary and only needs to last a short while. Uses `window.sessionStorage` to store authentication tokens. |
| 78 | + */ |
| 79 | + Session = "session", |
| 80 | + /** |
| 81 | + * `Temporary` authentications don't ask the browser to keep the user's authentication information around at all, such that refreshing the page will result in the user having no saved authentication state and likely being logged out. Useful for high security scenarios where authenticated sessions are sensitive and should be forgotten quickly. |
| 82 | + */ |
| 83 | + Temporary = "temporary", |
| 84 | +} |
| 85 | + |
| 86 | +/** Describes how to authenticate an instance of the client with the Gadget platform */ |
| 87 | +export interface AuthenticationModeOptions { |
| 88 | + // Use an API key to authenticate with Gadget. |
| 89 | + // Not strictly required, but without this the client might be useless depending on the app's permissions. |
| 90 | + apiKey?: string; |
| 91 | + |
| 92 | + // Use a web browser's `localStorage` or `sessionStorage` to persist authentication information. |
| 93 | + // This allows the browser to have a persistent identity as the user navigates around and logs in and out. |
| 94 | + browserSession?: boolean | BrowserSessionAuthenticationModeOptions; |
| 95 | + |
| 96 | + // Use no authentication at all, and get access only to the data that the Unauthenticated backend role has access to. |
| 97 | + anonymous?: true; |
| 98 | + |
| 99 | + // @deprecated Use internal instead |
| 100 | + internalAuthToken?: string; |
| 101 | + |
| 102 | + // @private Use an internal platform auth token for authentication |
| 103 | + // This is used to communicate within Gadget itself and shouldn't be used to connect to Gadget from other systems |
| 104 | + internal?: { |
| 105 | + authToken: string; |
| 106 | + actAsSession?: boolean; |
| 107 | + getSessionId?: () => Promise<string | undefined>; |
| 108 | + }; |
| 109 | + |
| 110 | + // @private Use a passed custom function for managing authentication. For some fancy integrations that the API client supports, like embedded Shopify apps, we use platform native features to authenticate with the Gadget backend. |
| 111 | + custom?: { |
| 112 | + processFetch(input: RequestInfo | URL, init: RequestInit): Promise<void>; |
| 113 | + processTransactionConnectionParams(params: Record<string, any>): Promise<void>; |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +export interface Exchanges { |
| 118 | + /** |
| 119 | + * Exchanges to add before all other exchanges. |
| 120 | + */ |
| 121 | + beforeAll?: Exchange[]; |
| 122 | + /** |
| 123 | + * Exchanges to add before any async exchanges. |
| 124 | + */ |
| 125 | + beforeAsync?: Exchange[]; |
| 126 | + /** |
| 127 | + * Exchanges to add after all other exchanges. |
| 128 | + */ |
| 129 | + afterAll?: Exchange[]; |
| 130 | +} |
0 commit comments