@magpie-community/opencode-zcode-auth 0.1.36 → 0.1.37

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.
Files changed (3) hide show
  1. package/README.md +2 -1
  2. package/index.mjs +18 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -216,7 +216,8 @@ reads the app's preview list and posts each plan it lists to
216
216
  account's ZCode session, as the app does. The answer is judged by the
217
217
  body's code, not the HTTP status:
218
218
 
219
- - nothing listed: *inactive*;
219
+ - nothing listed: *inactive* (ZCode answers an account with nothing to
220
+ claim `{"code":0,"msg":""}`, with no list at all);
220
221
  - code 0: *claimed* (the next request uses the plan);
221
222
  - code 1003 "target already exists" (HTTP 200): *done*, claimed before;
222
223
  - code 3007 "captcha verify failed": *captcha*, the plan is claimed in
package/index.mjs CHANGED
@@ -663,7 +663,7 @@ async function claimHint(s) {
663
663
  } catch {
664
664
  return null
665
665
  }
666
- const plans = (Array.isArray(d?.plans) ? d.plans : []).filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim())
666
+ const plans = (previewPlans(d) ?? []).filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim())
667
667
  if (!plans.length) return null
668
668
  const n = plans.length
669
669
  const plan = first(...plans.map((p) => (typeof p?.name === "string" ? p.name : "")), "ZCode gift plan")
@@ -718,6 +718,20 @@ async function claimOne(s, planID) {
718
718
  return { outcome: "failed", why: `${msg || "error"} (HTTP ${res.status}, code ${code})` }
719
719
  }
720
720
 
721
+ // previewPlans is /billing/preview's plan list. An account with nothing to
722
+ // claim is answered {"code":0,"msg":"","logid":…}: no data at all, not
723
+ // "plans": [] (the owner's account read 10-30 for hykql's report on
724
+ // Discord; the list is left out when it is empty). call has already judged the code, so a reply
725
+ // without data, or data without plans, is a list of none. Only data that
726
+ // isn't an object, or plans that are there and aren't a list, don't read
727
+ // (null, unknown).
728
+ function previewPlans(d) {
729
+ if (d == null) return []
730
+ if (typeof d !== "object" || Array.isArray(d)) return null
731
+ if (d.plans == null) return []
732
+ return Array.isArray(d.plans) ? d.plans : null
733
+ }
734
+
721
735
  // claimDaily claims every gift plan ZCode's preview lists for the account
722
736
  // (#503), as auth.checkin's answer: inactive when it lists none, claimed
723
737
  // when one was claimed now, done when each was claimed before, captcha
@@ -734,8 +748,9 @@ async function claimDaily(s) {
734
748
  } catch (e) {
735
749
  return { outcome: "failed", message: `gift plans to claim: ${e?.message ?? String(e)}` }
736
750
  }
737
- if (!Array.isArray(d?.plans)) return { outcome: "failed", message: "gift plans to claim: no plan list in the reply" }
738
- const plans = d.plans.filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim())
751
+ const listed = previewPlans(d)
752
+ if (!listed) return { outcome: "failed", message: "gift plans to claim: the plan list doesn't read" }
753
+ const plans = listed.filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim())
739
754
  if (!plans.length) return { outcome: "inactive", message: "no ZCode gift plan to claim" }
740
755
  const got = []
741
756
  for (const p of plans) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-zcode-auth",
3
- "version": "0.1.36",
3
+ "version": "0.1.37",
4
4
  "description": "OpenCode provider plugin: signs in to Z.ai's or BigModel's (智谱) GLM Coding Plan as ZCode does, including team seats and ZCode's Start Plan, and claims its gift plans daily",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",