j-queue-sdk-web
Advanced tools
Comparing version
@@ -46,2 +46,3 @@ import { Socket } from 'socket.io-client'; | ||
private static disconnect; | ||
static initFromScriptAttributes(): void; | ||
} | ||
@@ -48,0 +49,0 @@ declare global { |
@@ -99,3 +99,3 @@ "use strict"; | ||
<div style="padding: 20px; text-align: center;"> | ||
<p style="font-size: 16px; line-height: 1.5; margin: 0 0 20px 0; color: transparent;"> | ||
<p style="font-size: 16px; line-height: 1.5; margin: 0 0 20px 0; color: transparent;"> | ||
${messages.MESS_1}<br>${messages.MESS_2} | ||
@@ -114,4 +114,4 @@ </p> | ||
static getAdjustedPollInterval(position) { | ||
const pollTime = (position >= 100) ? this.CONFIG.TTL_INTERVAL + (position / 100) * 1000 : this.CONFIG.TTL_INTERVAL; | ||
return (pollTime > this.CONFIG.MAX_TTL_INTERVAL ? this.CONFIG.MAX_TTL_INTERVAL : pollTime); | ||
const pollTime = position >= 100 ? this.CONFIG.TTL_INTERVAL + (position / 100) * 1000 : this.CONFIG.TTL_INTERVAL; | ||
return pollTime > this.CONFIG.MAX_TTL_INTERVAL ? this.CONFIG.MAX_TTL_INTERVAL : pollTime; | ||
} | ||
@@ -232,7 +232,7 @@ static clearInterval() { | ||
return __awaiter(this, arguments, void 0, function* ({ wsUrl, apiUrl = '', socketConfig = {}, popupConfig = { | ||
isShowLoadingOnConnect: false | ||
isShowLoadingOnConnect: false, | ||
}, customEvents = {}, option = { storageTokenKey: this.CONFIG.STORAGE_TOKEN_KEY, storageConnectKey: this.CONFIG.STORAGE_CONNECT_KEY }, }) { | ||
var _b, _c, _d; | ||
if (!wsUrl) | ||
throw new Error('Both wsUrl are required'); | ||
throw new Error('wsUrl is required'); | ||
if (typeof window === 'undefined') | ||
@@ -245,3 +245,2 @@ throw new Error('Socket.IO is not supported in this environment'); | ||
try { | ||
// Show loading popup if isShowLoadingOnConnect is true | ||
if (popupConfig === null || popupConfig === void 0 ? void 0 : popupConfig.isShowLoadingOnConnect) { | ||
@@ -256,3 +255,3 @@ const content = this.getLoadingPopupContent((_d = popupConfig.language) !== null && _d !== void 0 ? _d : 'ko', popupConfig); | ||
this.log('Initialization failed', 'error', error); | ||
this.removePopup(); // Remove loading popup on init failure | ||
this.removePopup(); | ||
return { disconnect: () => this.disconnect() }; | ||
@@ -293,2 +292,48 @@ } | ||
} | ||
// Changed from private to public static to allow global access | ||
static initFromScriptAttributes() { | ||
if (typeof document === 'undefined') | ||
return; | ||
// Find the script tag that loaded the SDK | ||
const scripts = document.getElementsByTagName('script'); | ||
let sdkScript = null; | ||
for (const script of scripts) { | ||
if (script.src.includes('j-queue-sdk-web')) { | ||
sdkScript = script; | ||
break; | ||
} | ||
} | ||
if (!sdkScript) { | ||
this.log('Could not find J-Queue SDK script tag', 'error'); | ||
return; | ||
} | ||
// 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'; | ||
if (!wsUrl) { | ||
this.log('wsUrl attribute is required in script tag', 'error'); | ||
return; | ||
} | ||
// Prepare configuration | ||
const config = { | ||
wsUrl, | ||
option: { | ||
storageTokenKey, | ||
storageConnectKey, | ||
}, | ||
socketConfig: { | ||
query: connectKey ? { connect_key: connectKey } : {}, | ||
}, | ||
popupConfig: { | ||
isShowLoadingOnConnect, | ||
}, | ||
}; | ||
// Initialize the SDK | ||
this.init(config).catch((error) => { | ||
this.log('Auto-initialization from script attributes failed', 'error', error); | ||
}); | ||
} | ||
} | ||
@@ -377,3 +422,5 @@ ConnectionJQueueSdkWeb.CONFIG = { | ||
window.ConnectionJQueueSdkWeb = ConnectionJQueueSdkWeb; | ||
// Automatically initialize if script tag attributes are present | ||
ConnectionJQueueSdkWeb.initFromScriptAttributes(); | ||
} | ||
exports.default = ConnectionJQueueSdkWeb; |
@@ -0,10 +1,38 @@ | ||
/** | ||
* Configuration for the popup displayed during queue operations. | ||
*/ | ||
export interface PopupConfig { | ||
/** | ||
* Custom HTML content for the popup or a function that returns content based on queue position. | ||
*/ | ||
content?: string | ((position: number) => string); | ||
/** | ||
* Language for popup messages ('en' for English, 'ko' for Korean). | ||
*/ | ||
language?: 'en' | 'ko'; | ||
/** | ||
* Text color for popup content. | ||
*/ | ||
textColor?: string; | ||
/** | ||
* Starting color for the loader gradient. | ||
*/ | ||
loaderGradientStart?: string; | ||
/** | ||
* Ending color for the loader gradient. | ||
*/ | ||
loaderGradientEnd?: string; | ||
/** | ||
* Custom CSS styles for the popup. | ||
*/ | ||
style?: string; | ||
/** | ||
* Whether to show a loading popup during connection initialization. | ||
* Can be set via script tag attribute `isShowLoadingOnConnect` (e.g., `isShowLoadingOnConnect="true"`). | ||
*/ | ||
isShowLoadingOnConnect?: boolean; | ||
} | ||
/** | ||
* Enum representing possible queue statuses. | ||
*/ | ||
export declare enum OnlineQueueStatus { | ||
@@ -15,17 +43,62 @@ WAITING = 1, | ||
} | ||
/** | ||
* Response structure for queue status updates. | ||
*/ | ||
export interface StatusResponse { | ||
/** | ||
* Unique identifier for the queue entry. | ||
*/ | ||
uuid: string; | ||
/** | ||
* Current position in the queue. | ||
*/ | ||
position: number; | ||
/** | ||
* Current status of the queue entry. | ||
*/ | ||
status: OnlineQueueStatus; | ||
} | ||
/** | ||
* Configuration for initializing the J-Queue SDK. | ||
* Can be passed programmatically or via script tag attributes. | ||
*/ | ||
export interface InitConfig { | ||
/** | ||
* WebSocket URL for the queue service. | ||
* Required; can be set via script tag attribute `wsUrl`. | ||
*/ | ||
wsUrl: string; | ||
/** | ||
* Optional API URL for additional queue operations (e.g., leave request). | ||
*/ | ||
apiUrl?: string; | ||
/** | ||
* Configuration for the Socket.IO connection. | ||
*/ | ||
socketConfig?: { | ||
/** | ||
* Query parameters for the Socket.IO connection. | ||
* Supports `connect_key` for authentication, which can be set via script tag attribute `connectKey`. | ||
*/ | ||
query?: Record<string, string | number | undefined>; | ||
/** | ||
* Transport methods for Socket.IO (e.g., ['websocket']). | ||
*/ | ||
transports?: string[]; | ||
/** | ||
* Number of reconnection attempts. | ||
*/ | ||
reconnectionAttempts?: number; | ||
/** | ||
* Delay between reconnection attempts (in milliseconds). | ||
*/ | ||
reconnectionDelay?: number; | ||
}; | ||
/** | ||
* Configuration for the popup UI. | ||
*/ | ||
popupConfig?: PopupConfig; | ||
/** | ||
* Custom event handlers for Socket.IO events. | ||
*/ | ||
customEvents?: Record<string, (data: any, utils: { | ||
@@ -37,6 +110,17 @@ createPopup: (html: string, style?: string) => void; | ||
}) => void>; | ||
/** | ||
* Optional settings for storage keys. | ||
*/ | ||
option?: { | ||
/** | ||
* Key for storing the queue token in sessionStorage. | ||
* Can be set via script tag attribute `storageTokenKey`. | ||
*/ | ||
storageTokenKey?: string; | ||
/** | ||
* Key for storing the connect key in sessionStorage. | ||
* Can be set via script tag attribute `storageConnectKey`. | ||
*/ | ||
storageConnectKey?: string; | ||
}; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.OnlineQueueStatus = void 0; | ||
/** | ||
* Enum representing possible queue statuses. | ||
*/ | ||
var OnlineQueueStatus; | ||
@@ -5,0 +8,0 @@ (function (OnlineQueueStatus) { |
{ | ||
"name": "j-queue-sdk-web", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"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", |
@@ -84,3 +84,3 @@ # j-queue-sdk-web | ||
- `socketConfig` (object, optional): | ||
- `query` (object): Additional query parameters sent to the Socket.IO server (e.g., `{ app_id: 'XXXXX', service_name: 'NEWS', connect_key: 'CONNECT_KEY' }`). | ||
- `query` (object): Additional query parameters sent to the Socket.IO server (e.g., `{ connect_key: 'CONNECT_KEY' }`). | ||
- `transports` (string[]): Transport methods (e.g., `['websocket']`). Defaults to `['websocket']`. | ||
@@ -87,0 +87,0 @@ - `reconnectionAttempts` (number): Number of reconnection attempts (default: `3`). |
@@ -185,3 +185,3 @@ import { io, Socket } from 'socket.io-client'; | ||
<div style="padding: 20px; text-align: center;"> | ||
<p style="font-size: 16px; line-height: 1.5; margin: 0 0 20px 0; color: transparent;"> | ||
<p style="font-size: 16px; line-height: 1.5; margin: 0 0 20px 0; color: transparent;"> | ||
${messages.MESS_1}<br>${messages.MESS_2} | ||
@@ -201,4 +201,4 @@ </p> | ||
private static getAdjustedPollInterval(position: number): number { | ||
const pollTime = (position >= 100) ? this.CONFIG.TTL_INTERVAL + (position / 100) * 1000 : this.CONFIG.TTL_INTERVAL; | ||
return (pollTime > this.CONFIG.MAX_TTL_INTERVAL ? this.CONFIG.MAX_TTL_INTERVAL : pollTime); | ||
const pollTime = position >= 100 ? this.CONFIG.TTL_INTERVAL + (position / 100) * 1000 : this.CONFIG.TTL_INTERVAL; | ||
return pollTime > this.CONFIG.MAX_TTL_INTERVAL ? this.CONFIG.MAX_TTL_INTERVAL : pollTime; | ||
} | ||
@@ -349,3 +349,3 @@ | ||
popupConfig = { | ||
isShowLoadingOnConnect: false | ||
isShowLoadingOnConnect: false, | ||
}, | ||
@@ -355,3 +355,3 @@ customEvents = {}, | ||
}: InitConfig): Promise<{ disconnect: () => void }> { | ||
if (!wsUrl) throw new Error('Both wsUrl are required'); | ||
if (!wsUrl) throw new Error('wsUrl is required'); | ||
if (typeof window === 'undefined') throw new Error('Socket.IO is not supported in this environment'); | ||
@@ -370,3 +370,2 @@ | ||
try { | ||
// Show loading popup if isShowLoadingOnConnect is true | ||
if (popupConfig?.isShowLoadingOnConnect) { | ||
@@ -381,3 +380,3 @@ const content = this.getLoadingPopupContent(popupConfig.language ?? 'ko', popupConfig); | ||
this.log('Initialization failed', 'error', error); | ||
this.removePopup(); // Remove loading popup on init failure | ||
this.removePopup(); | ||
return { disconnect: () => this.disconnect() }; | ||
@@ -418,2 +417,55 @@ } | ||
} | ||
// Changed from private to public static to allow global access | ||
public static initFromScriptAttributes(): void { | ||
if (typeof document === 'undefined') return; | ||
// Find the script tag that loaded the SDK | ||
const scripts = document.getElementsByTagName('script'); | ||
let sdkScript: HTMLScriptElement | null = null; | ||
for (const script of scripts) { | ||
if (script.src.includes('j-queue-sdk-web')) { | ||
sdkScript = script; | ||
break; | ||
} | ||
} | ||
if (!sdkScript) { | ||
this.log('Could not find J-Queue SDK script tag', 'error'); | ||
return; | ||
} | ||
// 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'; | ||
if (!wsUrl) { | ||
this.log('wsUrl attribute is required in script tag', 'error'); | ||
return; | ||
} | ||
// Prepare configuration | ||
const config: InitConfig = { | ||
wsUrl, | ||
option: { | ||
storageTokenKey, | ||
storageConnectKey, | ||
}, | ||
socketConfig: { | ||
query: connectKey ? { connect_key: connectKey } : {}, | ||
}, | ||
popupConfig: { | ||
isShowLoadingOnConnect, | ||
}, | ||
}; | ||
// Initialize the SDK | ||
this.init(config).catch((error) => { | ||
this.log('Auto-initialization from script attributes failed', 'error', error); | ||
}); | ||
} | ||
} | ||
@@ -429,2 +481,4 @@ | ||
window.ConnectionJQueueSdkWeb = ConnectionJQueueSdkWeb; | ||
// Automatically initialize if script tag attributes are present | ||
ConnectionJQueueSdkWeb.initFromScriptAttributes(); | ||
} | ||
@@ -431,0 +485,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
472823
2.05%1628
12.82%0
-100%