withAccessToken
Higher-order function fetching an authorization token to then access it. This makes it easier to handle OAuth in your different commands whether they're view commands, no-view commands, or menu-bar commands.
Signature
function withAccessToken<T = any>(
options: WithAccessTokenParameters,
): <U extends WithAccessTokenComponentOrFn<T>>(
fnOrComponent: U,
) => U extends (props: T) => Promise<void> | void ? Promise<void> : React.FunctionComponent<T>;Arguments
options is an object containing:
options.authorize: a function that initiates the OAuth token retrieval process. It returns a promise that resolves to an access token.options.personalAccessToken: an optional string that represents an already obtained personal access token. Whenoptions.personalAccessTokenis provided, it uses that token. Otherwise, it callsoptions.authorizeto fetch an OAuth token asynchronously.options.client: an optional instance of a PKCE Client that you can create using Raycast API. This client is used to return theidTokenas part of theonAuthorizecallback below.options.onAuthorize: an optional callback function that is called once the user has been properly logged in through OAuth. This function is called with thetoken, its type (oauthif it comes from an OAuth flow orpersonalif it's a personal access token), andidTokenif it's returned fromoptions.client's initial token set.
Return
Returns the wrapped component if used in a view command or the wrapped function if used in a no-view command.
Example
import { List } from "@raycast/api";
import { withAccessToken } from "@raycast/utils";
import { authorize } from "./oauth";
function AuthorizedComponent(props) {
return; // ...
}
export default withAccessToken({ authorize })(AuthorizedComponent);import { showHUD } from "@raycast/api";
import { withAccessToken } from "@raycast/utils";
import { authorize } from "./oauth";
async function AuthorizedCommand() {
await showHUD("Authorized");
}
export default withAccessToken({ authorize })(AuthorizedCommand);Types
WithAccessTokenParameters
type OAuthType = "oauth" | "personal";
type OnAuthorizeParams = {
token: string;
type: OAuthType;
idToken: string | null; // only present if `options.client` has been provided
};
type WithAccessTokenParameters = {
client?: OAuth.PKCEClient;
authorize: () => Promise<string>;
personalAccessToken?: string;
onAuthorize?: (params: OnAuthorizeParams) => void;
};WithAccessTokenComponentOrFn
type WithAccessTokenComponentOrFn<T = any> = ((params: T) => Promise<void> | void) | React.ComponentType<T>;Last updated

