j-queue-sdk-web
Advanced tools
Comparing version
@@ -38,5 +38,22 @@ import { Socket } from 'socket.io-client'; | ||
private static setupSocket; | ||
/** | ||
* Adds a listener for queue status updates. | ||
* @param listener Callback function to receive status updates. | ||
*/ | ||
static addStatusListener(listener: (status: NonNullable<ConnectionState['queueStatus']>) => void): void; | ||
/** | ||
* Removes a queue status listener. | ||
* @param listener Callback function to remove. | ||
*/ | ||
static removeStatusListener(listener: (status: NonNullable<ConnectionState['queueStatus']>) => void): void; | ||
/** | ||
* Gets the current queue status. | ||
* @returns Current queue status or null if not initialized. | ||
*/ | ||
static getQueueStatus(): ConnectionState['queueStatus']; | ||
/** | ||
* Initializes the J-Queue SDK with the provided configuration. | ||
* @param config Configuration for the SDK. | ||
* @returns Promise resolving to an object with a disconnect method. | ||
*/ | ||
static init({ wsUrl, apiUrl, socketConfig, popupConfig, customEvents, option, }: InitConfig): Promise<{ | ||
@@ -47,2 +64,15 @@ disconnect: () => void; | ||
private static disconnect; | ||
/** | ||
* Initializes the SDK using attributes from the script tag. | ||
* Automatically called when the script is loaded with data-* attributes. | ||
* Supported attributes: | ||
* - data-ws-url: WebSocket URL (required). | ||
* - data-api-url: API URL for queue operations. | ||
* - data-storage-token-key: sessionStorage key for queue token. | ||
* - data-storage-connect-key: sessionStorage key for connect key. | ||
* - data-connect-key: Connect key for socket query. | ||
* - data-show-loading: Show loading popup on connect ('true'/'false'). | ||
* - data-language: Language for popup ('en'/'ko'). | ||
* - data-text-color: Text color for popup. | ||
*/ | ||
static initFromScriptAttributes(): void; | ||
@@ -52,3 +82,3 @@ } | ||
interface Window { | ||
ConnectionJQueueSdkWeb: typeof ConnectionJQueueSdkWeb; | ||
ConnectionJQueueSdkWeb?: typeof ConnectionJQueueSdkWeb; | ||
} | ||
@@ -55,0 +85,0 @@ } |
@@ -16,3 +16,3 @@ "use strict"; | ||
static log(message, type = 'info', error) { | ||
const prefix = '[J-Queue]'; | ||
const prefix = `[J-Queue]`; | ||
const logMethod = { error: console.error, warn: console.warn, info: console.log }[type]; | ||
@@ -194,3 +194,3 @@ logMethod(`${prefix} ${message}`, error !== null && error !== void 0 ? error : ''); | ||
this.log('Socket.IO connected'); | ||
this.removePopup(); // Remove loading popup if present | ||
this.removePopup(); | ||
this.startStatusEmission(currentTtlInterval.value); | ||
@@ -211,3 +211,3 @@ }); | ||
this.log('Socket.IO connection error', 'error', error); | ||
this.removePopup(); // Remove loading popup on connection error | ||
this.removePopup(); | ||
}); | ||
@@ -222,11 +222,28 @@ socket.on('disconnect', (reason) => { | ||
} | ||
/** | ||
* Adds a listener for queue status updates. | ||
* @param listener Callback function to receive status updates. | ||
*/ | ||
static addStatusListener(listener) { | ||
this.statusListeners.push(listener); | ||
} | ||
/** | ||
* Removes a queue status listener. | ||
* @param listener Callback function to remove. | ||
*/ | ||
static removeStatusListener(listener) { | ||
this.statusListeners = this.statusListeners.filter((l) => l !== listener); | ||
} | ||
/** | ||
* Gets the current queue status. | ||
* @returns Current queue status or null if not initialized. | ||
*/ | ||
static getQueueStatus() { | ||
return this.state.queueStatus; | ||
} | ||
/** | ||
* Initializes the J-Queue SDK with the provided configuration. | ||
* @param config Configuration for the SDK. | ||
* @returns Promise resolving to an object with a disconnect method. | ||
*/ | ||
static init(_a) { | ||
@@ -256,3 +273,3 @@ return __awaiter(this, arguments, void 0, function* ({ wsUrl, apiUrl = '', socketConfig = {}, popupConfig = { | ||
this.removePopup(); | ||
return { disconnect: () => this.disconnect() }; | ||
throw error; | ||
} | ||
@@ -292,7 +309,18 @@ }); | ||
} | ||
// Changed from private to public static to allow global access | ||
/** | ||
* Initializes the SDK using attributes from the script tag. | ||
* Automatically called when the script is loaded with data-* attributes. | ||
* Supported attributes: | ||
* - data-ws-url: WebSocket URL (required). | ||
* - data-api-url: API URL for queue operations. | ||
* - data-storage-token-key: sessionStorage key for queue token. | ||
* - data-storage-connect-key: sessionStorage key for connect key. | ||
* - data-connect-key: Connect key for socket query. | ||
* - data-show-loading: Show loading popup on connect ('true'/'false'). | ||
* - data-language: Language for popup ('en'/'ko'). | ||
* - data-text-color: Text color for popup. | ||
*/ | ||
static initFromScriptAttributes() { | ||
if (typeof document === 'undefined') | ||
return; | ||
// Find the script tag that loaded the SDK | ||
const scripts = document.getElementsByTagName('script'); | ||
@@ -311,14 +339,17 @@ let sdkScript = null; | ||
// Extract attributes | ||
const wsUrl = sdkScript.getAttribute('wsUrl'); | ||
const storageTokenKey = sdkScript.getAttribute('storageTokenKey') || this.CONFIG.STORAGE_TOKEN_KEY; | ||
const storageConnectKey = sdkScript.getAttribute('storageConnectKey') || this.CONFIG.STORAGE_CONNECT_KEY; | ||
const connectKey = sdkScript.getAttribute('connectKey'); | ||
const isShowLoadingOnConnect = sdkScript.getAttribute('isShowLoadingOnConnect') === 'true'; | ||
const wsUrl = sdkScript.getAttribute('data-ws-url'); | ||
const apiUrl = sdkScript.getAttribute('data-api-url') || ''; | ||
const storageTokenKey = sdkScript.getAttribute('data-storage-token-key') || this.CONFIG.STORAGE_TOKEN_KEY; | ||
const storageConnectKey = sdkScript.getAttribute('data-storage-connect-key') || this.CONFIG.STORAGE_CONNECT_KEY; | ||
const connectKey = sdkScript.getAttribute('data-connect-key'); | ||
const isShowLoadingOnConnect = sdkScript.getAttribute('data-show-loading') === 'true'; | ||
const language = sdkScript.getAttribute('data-language'); | ||
const textColor = sdkScript.getAttribute('data-text-color'); | ||
if (!wsUrl) { | ||
this.log('wsUrl attribute is required in script tag', 'error'); | ||
this.log('data-ws-url attribute is required in script tag', 'error'); | ||
return; | ||
} | ||
// Prepare configuration | ||
const config = { | ||
wsUrl, | ||
apiUrl, | ||
option: { | ||
@@ -331,7 +362,4 @@ storageTokenKey, | ||
}, | ||
popupConfig: { | ||
isShowLoadingOnConnect, | ||
}, | ||
popupConfig: Object.assign(Object.assign({ isShowLoadingOnConnect }, (language && { language })), (textColor && { textColor })), | ||
}; | ||
// Initialize the SDK | ||
this.init(config).catch((error) => { | ||
@@ -422,7 +450,15 @@ this.log('Auto-initialization from script attributes failed', 'error', error); | ||
ConnectionJQueueSdkWeb.statusListeners = []; | ||
if (typeof window !== 'undefined') { | ||
window.ConnectionJQueueSdkWeb = ConnectionJQueueSdkWeb; | ||
// Automatically initialize if script tag attributes are present | ||
ConnectionJQueueSdkWeb.initFromScriptAttributes(); | ||
} | ||
// Initialize in browser environment | ||
(function () { | ||
if (typeof window !== 'undefined') { | ||
// Ensure window.ConnectionJQueueSdkWeb is set | ||
window.ConnectionJQueueSdkWeb = ConnectionJQueueSdkWeb; | ||
try { | ||
ConnectionJQueueSdkWeb.initFromScriptAttributes(); | ||
} | ||
catch (error) { | ||
console.error(`[J-Queue] Failed to initialize from script attributes`, error); | ||
} | ||
} | ||
})(); | ||
exports.default = ConnectionJQueueSdkWeb; |
@@ -11,2 +11,3 @@ /** | ||
* Language for popup messages ('en' for English, 'ko' for Korean). | ||
* Can be set via script tag attribute `data-jqueue-language`. | ||
*/ | ||
@@ -16,2 +17,3 @@ language?: 'en' | 'ko'; | ||
* Text color for popup content. | ||
* Can be set via script tag attribute `data-jqueue-text-color`. | ||
*/ | ||
@@ -33,3 +35,3 @@ textColor?: string; | ||
* Whether to show a loading popup during connection initialization. | ||
* Can be set via script tag attribute `isShowLoadingOnConnect` (e.g., `isShowLoadingOnConnect="true"`). | ||
* Can be set via script tag attribute `data-jqueue-show-loading` ('true'/'false'). | ||
*/ | ||
@@ -65,3 +67,3 @@ isShowLoadingOnConnect?: boolean; | ||
* Configuration for initializing the J-Queue SDK. | ||
* Can be passed programmatically or via script tag attributes. | ||
* Can be passed programmatically or via script tag attributes with `data-jqueue-` prefix. | ||
*/ | ||
@@ -71,7 +73,8 @@ export interface InitConfig { | ||
* WebSocket URL for the queue service. | ||
* Required; can be set via script tag attribute `wsUrl`. | ||
* Required; set via script tag attribute `data-jqueue-ws-url`. | ||
*/ | ||
wsUrl: string; | ||
/** | ||
* Optional API URL for additional queue operations (e.g., leave request). | ||
* API URL for additional queue operations (e.g., leave request). | ||
* Set via script tag attribute `data-jqueue-api-url`. | ||
*/ | ||
@@ -85,3 +88,3 @@ apiUrl?: string; | ||
* Query parameters for the Socket.IO connection. | ||
* Supports `connect_key` for authentication, which can be set via script tag attribute `connectKey`. | ||
* Supports `connect_key` for authentication, set via script tag attribute `data-jqueue-connect-key`. | ||
*/ | ||
@@ -121,3 +124,3 @@ query?: Record<string, string | number | undefined>; | ||
* Key for storing the queue token in sessionStorage. | ||
* Can be set via script tag attribute `storageTokenKey`. | ||
* Set via script tag attribute `data-jqueue-storage-token-key`. | ||
*/ | ||
@@ -127,3 +130,3 @@ storageTokenKey?: string; | ||
* Key for storing the connect key in sessionStorage. | ||
* Can be set via script tag attribute `storageConnectKey`. | ||
* Set via script tag attribute `data-jqueue-storage-connect-key`. | ||
*/ | ||
@@ -130,0 +133,0 @@ storageConnectKey?: string; |
{ | ||
"name": "j-queue-sdk-web", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "A TypeScript package to check WebSocket connection status and control web access with a popup", | ||
@@ -5,0 +5,0 @@ "main": "dist/j-queue-sdk-web.js", |
@@ -106,3 +106,3 @@ import { io, Socket } from 'socket.io-client'; | ||
private static log(message: string, type: 'info' | 'warn' | 'error' = 'info', error?: unknown): void { | ||
const prefix = '[J-Queue]'; | ||
const prefix = `[J-Queue]`; | ||
const logMethod = { error: console.error, warn: console.warn, info: console.log }[type]; | ||
@@ -297,3 +297,3 @@ logMethod(`${prefix} ${message}`, error ?? ''); | ||
this.log('Socket.IO connected'); | ||
this.removePopup(); // Remove loading popup if present | ||
this.removePopup(); | ||
this.startStatusEmission(currentTtlInterval.value); | ||
@@ -319,3 +319,3 @@ }); | ||
this.log('Socket.IO connection error', 'error', error); | ||
this.removePopup(); // Remove loading popup on connection error | ||
this.removePopup(); | ||
}); | ||
@@ -333,2 +333,6 @@ | ||
/** | ||
* Adds a listener for queue status updates. | ||
* @param listener Callback function to receive status updates. | ||
*/ | ||
public static addStatusListener(listener: (status: NonNullable<ConnectionState['queueStatus']>) => void): void { | ||
@@ -338,2 +342,6 @@ this.statusListeners.push(listener); | ||
/** | ||
* Removes a queue status listener. | ||
* @param listener Callback function to remove. | ||
*/ | ||
public static removeStatusListener(listener: (status: NonNullable<ConnectionState['queueStatus']>) => void): void { | ||
@@ -343,2 +351,6 @@ this.statusListeners = this.statusListeners.filter((l) => l !== listener); | ||
/** | ||
* Gets the current queue status. | ||
* @returns Current queue status or null if not initialized. | ||
*/ | ||
public static getQueueStatus(): ConnectionState['queueStatus'] { | ||
@@ -348,2 +360,7 @@ return this.state.queueStatus; | ||
/** | ||
* Initializes the J-Queue SDK with the provided configuration. | ||
* @param config Configuration for the SDK. | ||
* @returns Promise resolving to an object with a disconnect method. | ||
*/ | ||
public static async init({ | ||
@@ -383,3 +400,3 @@ wsUrl, | ||
this.removePopup(); | ||
return { disconnect: () => this.disconnect() }; | ||
throw error; | ||
} | ||
@@ -420,7 +437,18 @@ } | ||
// Changed from private to public static to allow global access | ||
/** | ||
* Initializes the SDK using attributes from the script tag. | ||
* Automatically called when the script is loaded with data-* attributes. | ||
* Supported attributes: | ||
* - data-ws-url: WebSocket URL (required). | ||
* - data-api-url: API URL for queue operations. | ||
* - data-storage-token-key: sessionStorage key for queue token. | ||
* - data-storage-connect-key: sessionStorage key for connect key. | ||
* - data-connect-key: Connect key for socket query. | ||
* - data-show-loading: Show loading popup on connect ('true'/'false'). | ||
* - data-language: Language for popup ('en'/'ko'). | ||
* - data-text-color: Text color for popup. | ||
*/ | ||
public static initFromScriptAttributes(): void { | ||
if (typeof document === 'undefined') return; | ||
// Find the script tag that loaded the SDK | ||
const scripts = document.getElementsByTagName('script'); | ||
@@ -442,16 +470,19 @@ let sdkScript: HTMLScriptElement | null = null; | ||
// Extract attributes | ||
const wsUrl = sdkScript.getAttribute('wsUrl'); | ||
const storageTokenKey = sdkScript.getAttribute('storageTokenKey') || this.CONFIG.STORAGE_TOKEN_KEY; | ||
const storageConnectKey = sdkScript.getAttribute('storageConnectKey') || this.CONFIG.STORAGE_CONNECT_KEY; | ||
const connectKey = sdkScript.getAttribute('connectKey'); | ||
const isShowLoadingOnConnect = sdkScript.getAttribute('isShowLoadingOnConnect') === 'true'; | ||
const wsUrl = sdkScript.getAttribute('data-ws-url'); | ||
const apiUrl = sdkScript.getAttribute('data-api-url') || ''; | ||
const storageTokenKey = sdkScript.getAttribute('data-storage-token-key') || this.CONFIG.STORAGE_TOKEN_KEY; | ||
const storageConnectKey = sdkScript.getAttribute('data-storage-connect-key') || this.CONFIG.STORAGE_CONNECT_KEY; | ||
const connectKey = sdkScript.getAttribute('data-connect-key'); | ||
const isShowLoadingOnConnect = sdkScript.getAttribute('data-show-loading') === 'true'; | ||
const language = sdkScript.getAttribute('data-language') as 'en' | 'ko' | undefined; | ||
const textColor = sdkScript.getAttribute('data-text-color'); | ||
if (!wsUrl) { | ||
this.log('wsUrl attribute is required in script tag', 'error'); | ||
this.log('data-ws-url attribute is required in script tag', 'error'); | ||
return; | ||
} | ||
// Prepare configuration | ||
const config: InitConfig = { | ||
wsUrl, | ||
apiUrl, | ||
option: { | ||
@@ -466,6 +497,7 @@ storageTokenKey, | ||
isShowLoadingOnConnect, | ||
...(language && { language }), | ||
...(textColor && { textColor }), | ||
}, | ||
}; | ||
// Initialize the SDK | ||
this.init(config).catch((error) => { | ||
@@ -477,15 +509,23 @@ this.log('Auto-initialization from script attributes failed', 'error', error); | ||
// Properly extend the Window interface | ||
declare global { | ||
interface Window { | ||
ConnectionJQueueSdkWeb: typeof ConnectionJQueueSdkWeb; | ||
ConnectionJQueueSdkWeb?: typeof ConnectionJQueueSdkWeb; | ||
} | ||
} | ||
if (typeof window !== 'undefined') { | ||
window.ConnectionJQueueSdkWeb = ConnectionJQueueSdkWeb; | ||
// Automatically initialize if script tag attributes are present | ||
ConnectionJQueueSdkWeb.initFromScriptAttributes(); | ||
} | ||
// Initialize in browser environment | ||
(function () { | ||
if (typeof window !== 'undefined') { | ||
// Ensure window.ConnectionJQueueSdkWeb is set | ||
window.ConnectionJQueueSdkWeb = ConnectionJQueueSdkWeb; | ||
try { | ||
ConnectionJQueueSdkWeb.initFromScriptAttributes(); | ||
} catch (error) { | ||
console.error(`[J-Queue] Failed to initialize from script attributes`, error); | ||
} | ||
} | ||
})(); | ||
export default ConnectionJQueueSdkWeb; | ||
export type { InitConfig, OnlineQueueStatus, PopupConfig } from './types'; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
479163
1.34%1738
6.76%