@harars/opencode-switch-openai-auth-plugin 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -437,6 +437,37 @@ function same(prev, next) {
437
437
  return false;
438
438
  return prev.refresh === next.refresh;
439
439
  }
440
+ function unwrap(input) {
441
+ if (!input || typeof input !== "object") {
442
+ return {
443
+ ok: true,
444
+ data: input
445
+ };
446
+ }
447
+ if ("error" in input && input.error !== undefined) {
448
+ return {
449
+ ok: false
450
+ };
451
+ }
452
+ if ("data" in input) {
453
+ return input.data === undefined ? {
454
+ ok: false
455
+ } : {
456
+ ok: true,
457
+ data: input.data
458
+ };
459
+ }
460
+ return {
461
+ ok: true,
462
+ data: input
463
+ };
464
+ }
465
+ async function authMethods(api) {
466
+ const res = unwrap(await api.client.provider.auth());
467
+ if (!res.ok)
468
+ return [];
469
+ return res.data.openai ?? [];
470
+ }
440
471
  function bind(api, authz) {
441
472
  useKeyboard((evt) => {
442
473
  if (evt.name !== "c" || evt.ctrl || evt.meta)
@@ -668,17 +699,15 @@ async function auto(api, index, method, authz) {
668
699
  }
669
700
  async function hasLogin(api) {
670
701
  try {
671
- const res = await api.client.provider.auth();
672
- const methods = res.data?.openai ?? [];
673
- return methods.some((item) => item.type === "oauth");
702
+ return (await authMethods(api)).some((item) => item.type === "oauth");
674
703
  } catch {
675
704
  return false;
676
705
  }
677
706
  }
678
707
  async function loginOpenAI(api) {
679
708
  try {
680
- const auth = await api.client.provider.auth();
681
- const methods = (auth.data?.openai ?? []).map((method2, index2) => ({
709
+ const available = await authMethods(api);
710
+ const methods = available.map((method2, index2) => ({
682
711
  method: method2,
683
712
  index: index2
684
713
  })).filter((item) => item.method.type === "oauth");
@@ -697,12 +726,12 @@ async function loginOpenAI(api) {
697
726
  const inputs = await prompts(api, method.label, method);
698
727
  if (method.prompts?.length && !inputs)
699
728
  return false;
700
- const authz = await api.client.provider.oauth.authorize({
729
+ const authz = unwrap(await api.client.provider.oauth.authorize({
701
730
  providerID: "openai",
702
731
  method: picked.index,
703
732
  inputs
704
- });
705
- if (authz.error || !authz.data) {
733
+ }));
734
+ if (!authz.ok) {
706
735
  api.ui.toast({
707
736
  variant: "error",
708
737
  message: "Login failed"
package/dist/tui.js CHANGED
@@ -437,6 +437,37 @@ function same(prev, next) {
437
437
  return false;
438
438
  return prev.refresh === next.refresh;
439
439
  }
440
+ function unwrap(input) {
441
+ if (!input || typeof input !== "object") {
442
+ return {
443
+ ok: true,
444
+ data: input
445
+ };
446
+ }
447
+ if ("error" in input && input.error !== undefined) {
448
+ return {
449
+ ok: false
450
+ };
451
+ }
452
+ if ("data" in input) {
453
+ return input.data === undefined ? {
454
+ ok: false
455
+ } : {
456
+ ok: true,
457
+ data: input.data
458
+ };
459
+ }
460
+ return {
461
+ ok: true,
462
+ data: input
463
+ };
464
+ }
465
+ async function authMethods(api) {
466
+ const res = unwrap(await api.client.provider.auth());
467
+ if (!res.ok)
468
+ return [];
469
+ return res.data.openai ?? [];
470
+ }
440
471
  function bind(api, authz) {
441
472
  useKeyboard((evt) => {
442
473
  if (evt.name !== "c" || evt.ctrl || evt.meta)
@@ -668,17 +699,15 @@ async function auto(api, index, method, authz) {
668
699
  }
669
700
  async function hasLogin(api) {
670
701
  try {
671
- const res = await api.client.provider.auth();
672
- const methods = res.data?.openai ?? [];
673
- return methods.some((item) => item.type === "oauth");
702
+ return (await authMethods(api)).some((item) => item.type === "oauth");
674
703
  } catch {
675
704
  return false;
676
705
  }
677
706
  }
678
707
  async function loginOpenAI(api) {
679
708
  try {
680
- const auth = await api.client.provider.auth();
681
- const methods = (auth.data?.openai ?? []).map((method2, index2) => ({
709
+ const available = await authMethods(api);
710
+ const methods = available.map((method2, index2) => ({
682
711
  method: method2,
683
712
  index: index2
684
713
  })).filter((item) => item.method.type === "oauth");
@@ -697,12 +726,12 @@ async function loginOpenAI(api) {
697
726
  const inputs = await prompts(api, method.label, method);
698
727
  if (method.prompts?.length && !inputs)
699
728
  return false;
700
- const authz = await api.client.provider.oauth.authorize({
729
+ const authz = unwrap(await api.client.provider.oauth.authorize({
701
730
  providerID: "openai",
702
731
  method: picked.index,
703
732
  inputs
704
- });
705
- if (authz.error || !authz.data) {
733
+ }));
734
+ if (!authz.ok) {
706
735
  api.ui.toast({
707
736
  variant: "error",
708
737
  message: "Login failed"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@harars/opencode-switch-openai-auth-plugin",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "OpenCode TUI plugin for switching saved OpenAI OAuth accounts",
5
5
  "repository": {
6
6
  "type": "git",