Skip to content

Commit

Permalink
OAuth utils: Fix no view props
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaslombart committed Jan 31, 2024
1 parent 4e31f8f commit 6f29afe
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@raycast/utils",
"version": "1.11.1",
"version": "1.11.2",
"description": "Set of utilities to streamline building Raycast extensions",
"author": "Raycast Technologies Ltd.",
"homepage": "https://developers.raycast.com/utils-reference",
Expand Down
33 changes: 20 additions & 13 deletions src/oauth/withAccessToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type WithAccessTokenParameters = {
onAuthorize?: (params: OnAuthorizeParams) => void;
};

export type WithAccessTokenComponentOrFn<T = any> = ((params: T) => Promise<void> | void) | React.ComponentType<T>;

Check warning on line 43 in src/oauth/withAccessToken.tsx

View workflow job for this annotation

GitHub Actions / tests

Unexpected any. Specify a different type

/**
* Higher-order component to wrap a given component or function and set an access token in a shared global variable.
*
Expand Down Expand Up @@ -67,24 +69,29 @@ type WithAccessTokenParameters = {
* @param {string} options.personalAccessToken - An optional personal access token.
* @returns {React.ComponentType<T>} The wrapped component.
*/
export function withAccessToken<T>(
export function withAccessToken<T = any>(

Check warning on line 72 in src/oauth/withAccessToken.tsx

View workflow job for this annotation

GitHub Actions / tests

Unexpected any. Specify a different type
options: WithAccessTokenParameters,
): <U extends (() => Promise<void> | void) | React.ComponentType<T>>(
): <U extends WithAccessTokenComponentOrFn<T>>(
fnOrComponent: U,
) => U extends () => Promise<void> | void ? Promise<void> : React.FunctionComponent<T>;
) => U extends (props: T) => Promise<void> | void ? Promise<void> : React.FunctionComponent<T>;
export function withAccessToken<T>(options: WithAccessTokenParameters) {
if (environment.commandMode === "no-view") {
return async (fn: () => Promise<void> | (() => void)) => {
if (!token) {
token = options.personalAccessToken ?? (await options.authorize());
type = options.personalAccessToken ? "personal" : "oauth";
const idToken = (await options.client?.getTokens())?.idToken;

if (options.onAuthorize) {
await Promise.resolve(options.onAuthorize({ token, type, idToken }));
return (fn: (props: T) => Promise<void> | (() => void)) => {
const noViewFn = async (props: T) => {
if (!token) {
token = options.personalAccessToken ?? (await options.authorize());
type = options.personalAccessToken ? "personal" : "oauth";
const idToken = (await options.client?.getTokens())?.idToken;

if (options.onAuthorize) {
await Promise.resolve(options.onAuthorize({ token, type, idToken }));
}
}
}
return fn();

return fn(props);
};

return noViewFn;
};
}

Expand Down
2 changes: 1 addition & 1 deletion tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 39 additions & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,50 @@
"description": "Utils Smoke Tests",
"mode": "view"
},
{
"name": "oauth-with-props",
"title": "OAuth with Props",
"subtitle": "Utils Smoke Tests",
"description": "Utils Smoke Tests",
"mode": "view",
"arguments": [
{
"name": "text",
"placeholder": "Text",
"type": "text",
"required": true
}
]
},
{
"name": "oauth-no-view",
"title": "OAuth No View",
"subtitle": "Utils Smoke Tests",
"description": "Utils Smoke Tests",
"mode": "no-view"
"mode": "no-view",
"arguments": [
{
"name": "comment",
"placeholder": "Comment",
"type": "text",
"required": true
}
]
},
{
"name": "oauth-no-view-with-props",
"title": "OAuth No View with Props",
"subtitle": "Utils Smoke Tests",
"description": "Utils Smoke Tests",
"mode": "no-view",
"arguments": [
{
"name": "text",
"placeholder": "Text",
"type": "text",
"required": true
}
]
}
],
"dependencies": {
Expand Down
18 changes: 18 additions & 0 deletions tests/src/oauth-no-view-with-props.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { LaunchProps, showHUD } from "@raycast/api";
import { getAccessToken, withAccessToken, OAuthService } from "@raycast/utils";

const linear = OAuthService.linear({
scope: "read write",
onAuthorize(params) {
console.log(params);
},
});

type CommandProps = LaunchProps<{ arguments: Arguments.OauthNoViewWithProps }>;

async function Command(props: CommandProps) {
const { token } = getAccessToken();
await showHUD(`${props.arguments.text} ${token}`);
}

export default withAccessToken<CommandProps>(linear)(Command);
15 changes: 15 additions & 0 deletions tests/src/oauth-with-props.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Detail, LaunchProps } from "@raycast/api";
import { getAccessToken, withAccessToken, OAuthService } from "@raycast/utils";

const github = OAuthService.github({
scope: "notifications repo read:org read:user read:project",
});

type AuthorizedComponentProps = LaunchProps<{ arguments: Arguments.OauthWithProps }>;

function AuthorizedComponent(props: AuthorizedComponentProps) {
console.log(props);
return <Detail markdown={`## Access token\n\n${getAccessToken().token}\n\n## Argument\n\n${props.arguments.text}`} />;
}

export default withAccessToken(github)(AuthorizedComponent);

0 comments on commit 6f29afe

Please sign in to comment.