Преглед изворни кода

Add Type info and add `init` utils to global obj

JC Brand пре 4 месеци
родитељ
комит
82c65367c8

+ 3 - 3
src/headless/plugins/muc/muc.js

@@ -27,7 +27,7 @@ import { TimeoutError, ItemNotFoundError, StanzaError } from '../../shared/error
 import { computeAffiliationsDelta, setAffiliations, getAffiliationList } from './affiliations/utils.js';
 import { initStorage, createStore } from '../../utils/storage.js';
 import { isArchived, parseErrorStanza } from '../../shared/parsers.js';
-import { getUniqueId, isErrorObject, safeSave } from '../../utils/index.js';
+import { getUniqueId, safeSave } from '../../utils/index.js';
 import { isUniView } from '../../utils/session.js';
 import { parseMUCMessage, parseMUCPresence } from './parsers.js';
 import { sendMarker } from '../../shared/actions.js';
@@ -1699,7 +1699,7 @@ class MUC extends ModelWithVCard(ModelWithMessages(ColorAwareModel(ChatBoxBase))
             'node': 'x-roomuser-item',
         });
         const result = await api.sendIQ(stanza, null, false);
-        if (isErrorObject(result)) {
+        if (u.isErrorObject(result)) {
             throw result;
         }
         // Result might be undefined due to a timeout
@@ -2359,7 +2359,7 @@ class MUC extends ModelWithVCard(ModelWithMessages(ColorAwareModel(ChatBoxBase))
      * @param {MUCMessageAttributes|StanzaParseError} attrs_or_error - A promise which resolves to the message attributes.
      */
     async onMessage (attrs_or_error) {
-        if (isErrorObject(attrs_or_error)) {
+        if (u.isErrorObject(attrs_or_error)) {
             return log.error(/** @type {Error} */ (attrs_or_error).message);
         }
 

+ 12 - 2
src/headless/types/utils/index.d.ts

@@ -8,7 +8,6 @@ export function isEmptyMessage(attrs: any): boolean;
  * inserted before the mentioned nicknames.
  */
 export function prefixMentions(message: any): any;
-export function isErrorObject(o: any): o is Error;
 export function safeSave(model: any, attributes: any, options: any): void;
 export function getRandomInt(max: any): number;
 /**
@@ -20,7 +19,6 @@ declare const _default: {
     getRandomInt: typeof getRandomInt;
     getUniqueId: typeof getUniqueId;
     isEmptyMessage: typeof isEmptyMessage;
-    isErrorObject: typeof isErrorObject;
     onMultipleEvents: typeof onMultipleEvents;
     prefixMentions: typeof prefixMentions;
     safeSave: typeof safeSave;
@@ -64,11 +62,21 @@ declare const _default: {
     isError(obj: unknown): boolean;
     isFunction(val: unknown): boolean;
     isUndefined(x: unknown): boolean;
+    isErrorObject(o: unknown): boolean;
     isValidJID(jid?: string | null): boolean;
     isValidMUCJID(jid: string): boolean;
     isSameBareJID(jid1: string, jid2: string): boolean;
     isSameDomain(jid1: string, jid2: string): boolean;
     getJIDFromURI(jid: string): string;
+    initPlugins(_converse: ConversePrivateGlobal): void;
+    initClientConfig(_converse: ConversePrivateGlobal): Promise<void>;
+    initSessionStorage(_converse: ConversePrivateGlobal): Promise<void>;
+    setUserJID(jid: string): Promise<string>;
+    initSession(_converse: ConversePrivateGlobal, jid: string): Promise<void>;
+    registerGlobalEventHandlers(_converse: ConversePrivateGlobal): void;
+    cleanup(_converse: ConversePrivateGlobal): Promise<void>;
+    attemptNonPreboundSession(credentials?: import("./types.js").Credentials, automatic?: boolean): Promise<void>;
+    savedLoginInfo(jid: string): Promise<Model>;
     isElement(el: unknown): boolean;
     isTagEqual(stanza: Element | typeof import("strophe.js").Builder, name: string): boolean;
     stringToElement(s: string): Element;
@@ -116,4 +124,6 @@ declare function shouldCreateMessage(attrs: any): any;
 declare function triggerEvent(el: Element, name: string, type?: string, bubbles?: boolean, cancelable?: boolean): void;
 import * as url from './url.js';
 import * as session from './session.js';
+import * as init from './init.js';
+import { Model } from '@converse/skeletor';
 //# sourceMappingURL=index.d.ts.map

+ 9 - 4
src/headless/types/utils/init.d.ts

@@ -41,17 +41,22 @@ export function registerGlobalEventHandlers(_converse: ConversePrivateGlobal): v
  * @param {ConversePrivateGlobal} _converse
  */
 export function cleanup(_converse: ConversePrivateGlobal): Promise<void>;
-export function attemptNonPreboundSession(credentials: any, automatic: any): Promise<void>;
+/**
+ * @param {import('./types').Credentials} [credentials]
+ * @param {boolean} [automatic]
+ */
+export function attemptNonPreboundSession(credentials?: import("./types").Credentials, automatic?: boolean): Promise<void>;
 /**
  * Fetch the stored SCRAM keys for the given JID, if available.
  *
  * The user's plaintext password is not stored, nor any material from which
  * the user's plaintext password could be recovered.
  *
- * @param { String } jid - The XMPP address for which to fetch the SCRAM keys
- * @returns { Promise } A promise which resolves once we've fetched the previously
+ * @param {String} jid - The XMPP address for which to fetch the SCRAM keys
+ * @returns {Promise<Model>} A promise which resolves once we've fetched the previously
  *  used login keys.
  */
-export function savedLoginInfo(jid: string): Promise<any>;
+export function savedLoginInfo(jid: string): Promise<Model>;
 export type ConversePrivateGlobal = any;
+import { Model } from '@converse/skeletor';
 //# sourceMappingURL=init.d.ts.map

+ 5 - 0
src/headless/types/utils/object.d.ts

@@ -19,4 +19,9 @@ export function isFunction(val: unknown): boolean;
  * @returns {boolean} True if the value is undefined, false otherwise.
  */
 export function isUndefined(x: unknown): boolean;
+/**
+ * @param {unknown} o - The value to check.
+ * @returns {boolean} True if the value is an Error
+ */
+export function isErrorObject(o: unknown): boolean;
 //# sourceMappingURL=object.d.ts.map

+ 5 - 0
src/headless/types/utils/types.d.ts

@@ -0,0 +1,5 @@
+export type Credentials = {
+    jid: string;
+    password: string;
+};
+//# sourceMappingURL=types.d.ts.map

+ 3 - 5
src/headless/utils/index.js

@@ -10,6 +10,7 @@ import * as arraybuffer from './arraybuffer.js';
 import * as color from './color.js';
 import * as form from './form.js';
 import * as html from './html.js';
+import * as init from './init.js';
 import * as jid from './jid';
 import * as object from './object.js';
 import * as promise from './promise.js';
@@ -19,6 +20,7 @@ import * as storage from './storage.js';
 import * as text from './text.js';
 import * as url from './url.js';
 
+
 /**
  * @typedef {Record<string, Function>} CommonUtils
  * @typedef {Record<'muc'|'mam', CommonUtils>} PluginUtils
@@ -77,10 +79,6 @@ function shouldCreateMessage (attrs) {
         !isEmptyMessage(attrs);
 }
 
-export function isErrorObject (o) {
-    return o instanceof Error;
-}
-
 /**
  * Call the callback once all the events have been triggered
  * @param { Array } events: An array of objects, with keys `object` and
@@ -154,6 +152,7 @@ export default Object.assign({
     ...color,
     ...form,
     ...html,
+    ...init,
     ...jid,
     ...object,
     ...promise,
@@ -165,7 +164,6 @@ export default Object.assign({
     getRandomInt,
     getUniqueId,
     isEmptyMessage,
-    isErrorObject,
     onMultipleEvents,
     prefixMentions,
     safeSave,

+ 16 - 9
src/headless/utils/init.js

@@ -284,7 +284,7 @@ export async function cleanup (_converse) {
  * Fetches login credentials from the server.
  * @param {number} [wait=0]
  *  The time to wait and debounce subsequent calls to this function before making the request.
- * @returns {Promise<{jid: string, password: string}>}
+ * @returns {Promise<import('./types').Credentials>}
  *  A promise that resolves with the provided login credentials (JID and password).
  * @throws {Error} If the request fails or returns an error status.
  */
@@ -319,6 +319,9 @@ function fetchLoginCredentials (wait=0) {
 }
 
 
+/**
+ * @returns {Promise<import('./types').Credentials>}
+ */
 async function getLoginCredentialsFromURL () {
     let credentials;
     let wait = 0;
@@ -365,10 +368,14 @@ async function getLoginCredentialsFromSCRAMKeys () {
 
     const login_info = await savedLoginInfo(jid);
     const scram_keys = login_info.get('scram_keys');
-    return scram_keys ? { jid , 'password': scram_keys } : null;
+    return scram_keys ? { jid , password: scram_keys } : null;
 }
 
 
+/**
+ * @param {import('./types').Credentials} [credentials]
+ * @param {boolean} [automatic]
+ */
 export async function attemptNonPreboundSession (credentials, automatic) {
     const { api } = _converse;
 
@@ -417,8 +424,8 @@ export async function attemptNonPreboundSession (credentials, automatic) {
  * The user's plaintext password is not stored, nor any material from which
  * the user's plaintext password could be recovered.
  *
- * @param { String } jid - The XMPP address for which to fetch the SCRAM keys
- * @returns { Promise } A promise which resolves once we've fetched the previously
+ * @param {String} jid - The XMPP address for which to fetch the SCRAM keys
+ * @returns {Promise<Model>} A promise which resolves once we've fetched the previously
  *  used login keys.
  */
 export async function savedLoginInfo (jid) {
@@ -431,11 +438,11 @@ export async function savedLoginInfo (jid) {
 
 
 /**
- * @param { Object } [credentials]
- * @param { string } credentials.password
- * @param { Object } credentials.password
- * @param { string } credentials.password.ck
- * @returns { Promise<void> }
+ * @param {Object} [credentials]
+ * @param {string} credentials.password
+ * @param {Object} credentials.password
+ * @param {string} credentials.password.ck
+ * @returns {Promise<void>}
  */
 async function connect (credentials) {
     const { api } = _converse;

+ 8 - 0
src/headless/utils/object.js

@@ -39,3 +39,11 @@ export function isFunction(val) {
 export function isUndefined(x) {
     return typeof x === "undefined";
 }
+
+/**
+ * @param {unknown} o - The value to check.
+ * @returns {boolean} True if the value is an Error
+ */
+export function isErrorObject (o) {
+    return o instanceof Error;
+}

+ 4 - 0
src/headless/utils/types.ts

@@ -0,0 +1,4 @@
+export type Credentials = {
+    jid: string;
+    password: string;
+}