@mitumba/sdk 5.2.0 → 5.4.0

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.mjs CHANGED
@@ -677,6 +677,9 @@ var AuthModule = class {
677
677
  };
678
678
 
679
679
  // src/modules/oauth.ts
680
+ function isConsentRequired(result) {
681
+ return result.consent_required === true;
682
+ }
680
683
  var OAuthModule = class {
681
684
  constructor(client) {
682
685
  this.client = client;
@@ -788,6 +791,90 @@ var OAuthModule = class {
788
791
  async userinfo(options) {
789
792
  return this.client.get("/auth/oauth/userinfo", void 0, options);
790
793
  }
794
+ /**
795
+ * Record the current user's consent to a client's scopes.
796
+ *
797
+ * The accounts consent screen calls this (after the user approves) before retrying
798
+ * {@link OAuthModule.authorize}, which then finds covering consent and mints a code. Requires an
799
+ * authenticated session. Re-granting with a wider scope set supersedes the prior consent.
800
+ *
801
+ * @param input - The client and the space-delimited scopes being granted
802
+ * @param options - Optional per-request options
803
+ * @returns `{ ok: true }` on success
804
+ * @throws {APIError} On `400 invalid_client` / `invalid_scope`, or `401` without a session
805
+ */
806
+ async grantConsent(input, options) {
807
+ return this.client.post("/auth/oauth/consent", input, options);
808
+ }
809
+ /**
810
+ * Withdraw the current user's consent for a client (account-console self-service).
811
+ *
812
+ * @param clientId - The client to revoke consent for
813
+ * @param options - Optional per-request options
814
+ * @returns `{ ok: true, revoked }` — `revoked` is false when there was nothing live to revoke
815
+ * @throws {APIError} On `401` without a session
816
+ */
817
+ async revokeConsent(clientId, options) {
818
+ return this.client.delete(
819
+ `/auth/oauth/consent/${encodeURIComponent(clientId)}`,
820
+ void 0,
821
+ options
822
+ );
823
+ }
824
+ /**
825
+ * List the current user's live consents, for a "connected apps" account-management view.
826
+ *
827
+ * @param options - Optional per-request options
828
+ * @returns The live consents (client id + name, granted scopes, grant time)
829
+ * @throws {APIError} On `401` without a session
830
+ */
831
+ async listConsents(options) {
832
+ return this.client.get("/auth/oauth/consents", void 0, options);
833
+ }
834
+ /**
835
+ * Attempt a silent (`prompt=none`) authorization — the "sign in once, every app knows you" path.
836
+ *
837
+ * Sends the authorize request with `prompt=none`, meaning: mint a code only if it can be done with
838
+ * no interaction, otherwise tell me why so I can fall back. Unlike {@link OAuthModule.authorize},
839
+ * the expected non-success outcomes are RETURNED (discriminated on `ok`), not thrown, so a caller
840
+ * can branch cleanly without a try/catch:
841
+ *
842
+ * - `{ ok: true, code, state }` — redirect to `redirect_uri?code=..&state=..` as usual.
843
+ * - `{ ok: false, reason: 'login_required' }` — no usable session, or this client is not
844
+ * authorized for silent auth. Fall back to a visible sign-in (or stay logged out).
845
+ * - `{ ok: false, reason: 'interaction_required' }` — consent needed; fall back to a visible flow.
846
+ * - `{ ok: false, reason: 'unauthenticated' }` — no session reached the server (HTTP 401).
847
+ *
848
+ * Only genuinely unexpected errors (network failure, 5xx, an unrecognised error code) throw.
849
+ * `code_challenge_method` is fixed to `'S256'` and `prompt` to `'none'` by the SDK. This is a
850
+ * top-level/first-party call carrying the session the SDK holds; the server never mints for a
851
+ * client that lacks the silent-auth privilege (it returns `login_required`).
852
+ *
853
+ * @param input - The authorize request (client, redirect URI, state, PKCE challenge, scope)
854
+ * @param options - Optional per-request options (abort signal, extra headers)
855
+ * @returns A discriminated success-or-fallback result
856
+ * @throws {APIError} Only on unexpected failures (network, 5xx, unknown error code)
857
+ */
858
+ async silentAuthorize(input, options) {
859
+ try {
860
+ const result = await this.client.post(
861
+ "/auth/oauth/authorize",
862
+ { ...input, code_challenge_method: "S256", prompt: "none" },
863
+ options
864
+ );
865
+ if (isConsentRequired(result)) return { ok: false, reason: "interaction_required" };
866
+ return { ok: true, code: result.code, state: result.state };
867
+ } catch (err) {
868
+ if (err instanceof APIError) {
869
+ if (err.status === 401) return { ok: false, reason: "unauthenticated" };
870
+ if (err.code === "login_required") return { ok: false, reason: "login_required" };
871
+ if (err.code === "interaction_required" || err.code === "consent_required") {
872
+ return { ok: false, reason: "interaction_required" };
873
+ }
874
+ }
875
+ throw err;
876
+ }
877
+ }
791
878
  };
792
879
 
793
880
  // src/modules/listings.ts
@@ -2668,6 +2755,6 @@ var MitumbaClient = class {
2668
2755
  }
2669
2756
  };
2670
2757
 
2671
- export { APIClient, APIError, AdminModule, AuthModule, BALE_ORDER_STATUSES, BusinessesModule, CONDITIONS, CartModule, DEFAULT_SHARE_ORIGIN, DESIRED_RESOLUTIONS, DISPUTE_REASONS, DISPUTE_STATUSES, DisputesModule, IDBTokenStore, LISTING_STATUSES, ListingsModule, MailerModule, MemoryTokenStore, MessagesModule, MitumbaClient, NotificationsModule, OAuthModule, ORDER_STATUSES, OrdersModule, PAYMENT_STATUSES, PayModule, RealtimeProtocolError, RealtimeUnavailableError, ReportsModule, ReviewsModule, SearchModule, SettingsModule, StoresModule, TWO_FACTOR_METHOD_TYPES, UploadsModule, VaziModule, WholesaleModule, WishlistsModule, baleShareUrl, createTokenStore, isMessagingAPIError, listingShareUrl, storeShareUrl };
2758
+ export { APIClient, APIError, AdminModule, AuthModule, BALE_ORDER_STATUSES, BusinessesModule, CONDITIONS, CartModule, DEFAULT_SHARE_ORIGIN, DESIRED_RESOLUTIONS, DISPUTE_REASONS, DISPUTE_STATUSES, DisputesModule, IDBTokenStore, LISTING_STATUSES, ListingsModule, MailerModule, MemoryTokenStore, MessagesModule, MitumbaClient, NotificationsModule, OAuthModule, ORDER_STATUSES, OrdersModule, PAYMENT_STATUSES, PayModule, RealtimeProtocolError, RealtimeUnavailableError, ReportsModule, ReviewsModule, SearchModule, SettingsModule, StoresModule, TWO_FACTOR_METHOD_TYPES, UploadsModule, VaziModule, WholesaleModule, WishlistsModule, baleShareUrl, createTokenStore, isConsentRequired, isMessagingAPIError, listingShareUrl, storeShareUrl };
2672
2759
  //# sourceMappingURL=index.mjs.map
2673
2760
  //# sourceMappingURL=index.mjs.map