@central-ticket/queue-sdk 0.0.2 → 0.0.3

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.
@@ -0,0 +1,63 @@
1
+ import {
2
+ CENTRALQ_DEFAULT_API_URL
3
+ } from "./chunk-P73Q2ZIO.mjs";
4
+
5
+ // src/server.ts
6
+ function normalizeApiUrl(apiUrl) {
7
+ return apiUrl.replace(/\/+$/, "");
8
+ }
9
+ async function postQueueServer(apiUrl, path, secretKey, body, fallback) {
10
+ const response = await fetch(`${normalizeApiUrl(apiUrl)}${path}`, {
11
+ method: "POST",
12
+ headers: {
13
+ "Content-Type": "application/json",
14
+ "x-api-key": secretKey
15
+ },
16
+ body: JSON.stringify(body)
17
+ });
18
+ const data = await response.json().catch(() => fallback);
19
+ return { status: response.status, data };
20
+ }
21
+ async function validateQueueTokenServer(options) {
22
+ const apiUrl = options.apiUrl ?? CENTRALQ_DEFAULT_API_URL;
23
+ return postQueueServer(
24
+ apiUrl,
25
+ `/api/queue/verify/${encodeURIComponent(options.eventId)}`,
26
+ options.secretKey,
27
+ { token: options.token },
28
+ { valid: false, reason: "Respuesta inv\xE1lida" }
29
+ );
30
+ }
31
+ async function verifyQueueAccessServer(options) {
32
+ const apiUrl = options.apiUrl ?? CENTRALQ_DEFAULT_API_URL;
33
+ return postQueueServer(
34
+ apiUrl,
35
+ `/api/queue/verify/${encodeURIComponent(options.eventId)}/access`,
36
+ options.secretKey,
37
+ { token: options.token ?? null },
38
+ {
39
+ allowed: false,
40
+ reason: "invalid_response",
41
+ message: "Respuesta inv\xE1lida"
42
+ }
43
+ );
44
+ }
45
+ async function completeQueueAccessServer(options) {
46
+ const apiUrl = options.apiUrl ?? CENTRALQ_DEFAULT_API_URL;
47
+ return postQueueServer(
48
+ apiUrl,
49
+ `/api/queue/verify/${encodeURIComponent(options.eventId)}/complete`,
50
+ options.secretKey,
51
+ {
52
+ token: options.token,
53
+ completionId: options.completionId
54
+ },
55
+ { completed: false, reason: "invalid_response" }
56
+ );
57
+ }
58
+
59
+ export {
60
+ validateQueueTokenServer,
61
+ verifyQueueAccessServer,
62
+ completeQueueAccessServer
63
+ };
@@ -0,0 +1,115 @@
1
+ import {
2
+ CENTRALQ_DEFAULT_API_URL
3
+ } from "./chunk-P73Q2ZIO.mjs";
4
+
5
+ // src/server.ts
6
+ var DEFAULT_TIMEOUT_MS = 5e3;
7
+ var CentralQServerRequestError = class extends Error {
8
+ constructor(code, message, options) {
9
+ super(message, options);
10
+ this.name = "CentralQServerRequestError";
11
+ this.code = code;
12
+ }
13
+ };
14
+ function normalizeApiUrl(apiUrl) {
15
+ const trimmed = apiUrl.trim().replace(/\/+$/, "");
16
+ return trimmed.endsWith("/api") ? trimmed.slice(0, -4) : trimmed;
17
+ }
18
+ function validateOptions(options) {
19
+ if (!options.secretKey?.trim()) {
20
+ throw new TypeError("CentralQ secretKey es requerida");
21
+ }
22
+ if (options.timeoutMs !== void 0 && options.timeoutMs <= 0) {
23
+ throw new TypeError("CentralQ timeoutMs debe ser mayor a cero");
24
+ }
25
+ }
26
+ var CentralQServerClient = class {
27
+ constructor(options) {
28
+ validateOptions(options);
29
+ this.apiUrl = normalizeApiUrl(options.apiUrl ?? CENTRALQ_DEFAULT_API_URL);
30
+ this.secretKey = options.secretKey.trim();
31
+ this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
32
+ this.fetchFn = options.fetch ?? globalThis.fetch;
33
+ }
34
+ validateToken(input) {
35
+ return this.post(
36
+ `/api/queue/verify/${encodeURIComponent(input.eventId)}`,
37
+ { token: input.token },
38
+ { valid: false, reason: "Respuesta inv\xE1lida" }
39
+ );
40
+ }
41
+ verifyAccess(input) {
42
+ return this.post(
43
+ `/api/queue/verify/${encodeURIComponent(input.eventId)}/access`,
44
+ { token: input.token ?? null },
45
+ {
46
+ allowed: false,
47
+ reason: "invalid_response",
48
+ message: "Respuesta inv\xE1lida"
49
+ }
50
+ );
51
+ }
52
+ completeAccess(input) {
53
+ return this.post(
54
+ `/api/queue/verify/${encodeURIComponent(input.eventId)}/complete`,
55
+ {
56
+ token: input.token,
57
+ completionId: input.completionId
58
+ },
59
+ { completed: false, reason: "invalid_response" }
60
+ );
61
+ }
62
+ async post(path, body, fallback) {
63
+ const controller = new AbortController();
64
+ const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
65
+ try {
66
+ const response = await this.fetchFn(`${this.apiUrl}${path}`, {
67
+ method: "POST",
68
+ headers: {
69
+ "Content-Type": "application/json",
70
+ "x-api-key": this.secretKey
71
+ },
72
+ body: JSON.stringify(body),
73
+ signal: controller.signal
74
+ });
75
+ const data = await response.json().catch(() => fallback);
76
+ return { status: response.status, data };
77
+ } catch (error) {
78
+ if (controller.signal.aborted) {
79
+ throw new CentralQServerRequestError(
80
+ "timeout",
81
+ `CentralQ no respondi\xF3 en ${this.timeoutMs}ms`,
82
+ { cause: error }
83
+ );
84
+ }
85
+ throw new CentralQServerRequestError(
86
+ "network_error",
87
+ "No se pudo conectar con CentralQ",
88
+ { cause: error }
89
+ );
90
+ } finally {
91
+ clearTimeout(timeout);
92
+ }
93
+ }
94
+ };
95
+ function createCentralQServerClient(options) {
96
+ return new CentralQServerClient(options);
97
+ }
98
+ function validateQueueTokenServer(options) {
99
+ return createCentralQServerClient(options).validateToken(options);
100
+ }
101
+ function verifyQueueAccessServer(options) {
102
+ return createCentralQServerClient(options).verifyAccess(options);
103
+ }
104
+ function completeQueueAccessServer(options) {
105
+ return createCentralQServerClient(options).completeAccess(options);
106
+ }
107
+
108
+ export {
109
+ CentralQServerRequestError,
110
+ CentralQServerClient,
111
+ createCentralQServerClient,
112
+ validateQueueTokenServer,
113
+ verifyQueueAccessServer,
114
+ completeQueueAccessServer
115
+ };
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { C as CentralQ, a as CentralQClient, b as CentralQClientOptions, c as CentralQCreateOptions, d as CentralQOptions, E as ErrorDetail, e as ExpiredDetail, P as PassedDetail, f as PositionDetail, Q as QueueStatus, g as createCentralQClient } from './centralq-C3ukDo0x.mjs';
2
- export { QueueTokenValidationResult, ValidateQueueTokenServerOptions, validateQueueTokenServer } from './server.mjs';
2
+ export { CentralQServerClient, CentralQServerClientOptions, CentralQServerRequestError, CentralQServerRequestErrorCode, CompleteQueueAccessServerOptions, QueueAccessCompletionResult, QueueAccessValidationResult, QueueTokenValidationResult, ValidateQueueTokenServerOptions, VerifyQueueAccessServerOptions, completeQueueAccessServer, createCentralQServerClient, validateQueueTokenServer, verifyQueueAccessServer } from './server.mjs';
3
3
  import * as lit from 'lit';
4
4
  import { LitElement } from 'lit';
5
5
 
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { C as CentralQ, a as CentralQClient, b as CentralQClientOptions, c as CentralQCreateOptions, d as CentralQOptions, E as ErrorDetail, e as ExpiredDetail, P as PassedDetail, f as PositionDetail, Q as QueueStatus, g as createCentralQClient } from './centralq-C3ukDo0x.js';
2
- export { QueueTokenValidationResult, ValidateQueueTokenServerOptions, validateQueueTokenServer } from './server.js';
2
+ export { CentralQServerClient, CentralQServerClientOptions, CentralQServerRequestError, CentralQServerRequestErrorCode, CompleteQueueAccessServerOptions, QueueAccessCompletionResult, QueueAccessValidationResult, QueueTokenValidationResult, ValidateQueueTokenServerOptions, VerifyQueueAccessServerOptions, completeQueueAccessServer, createCentralQServerClient, validateQueueTokenServer, verifyQueueAccessServer } from './server.js';
3
3
  import * as lit from 'lit';
4
4
  import { LitElement } from 'lit';
5
5
 
package/dist/index.js CHANGED
@@ -149,9 +149,14 @@ __export(index_exports, {
149
149
  CENTRALQ_DEFAULT_API_URL: () => CENTRALQ_DEFAULT_API_URL,
150
150
  CentralQ: () => CentralQ,
151
151
  CentralQClient: () => CentralQClient,
152
+ CentralQServerClient: () => CentralQServerClient,
153
+ CentralQServerRequestError: () => CentralQServerRequestError,
152
154
  QueueHttpClient: () => QueueHttpClient,
155
+ completeQueueAccessServer: () => completeQueueAccessServer,
153
156
  createCentralQClient: () => createCentralQClient,
154
- validateQueueTokenServer: () => validateQueueTokenServer
157
+ createCentralQServerClient: () => createCentralQServerClient,
158
+ validateQueueTokenServer: () => validateQueueTokenServer,
159
+ verifyQueueAccessServer: () => verifyQueueAccessServer
155
160
  });
156
161
  module.exports = __toCommonJS(index_exports);
157
162
 
@@ -638,34 +643,118 @@ function createCentralQClient(options) {
638
643
  }
639
644
 
640
645
  // src/server.ts
646
+ var DEFAULT_TIMEOUT_MS = 5e3;
647
+ var CentralQServerRequestError = class extends Error {
648
+ constructor(code, message, options) {
649
+ super(message, options);
650
+ this.name = "CentralQServerRequestError";
651
+ this.code = code;
652
+ }
653
+ };
641
654
  function normalizeApiUrl2(apiUrl) {
642
- return apiUrl.replace(/\/+$/, "");
655
+ const trimmed = apiUrl.trim().replace(/\/+$/, "");
656
+ return trimmed.endsWith("/api") ? trimmed.slice(0, -4) : trimmed;
643
657
  }
644
- async function validateQueueTokenServer(options) {
645
- const apiUrl = options.apiUrl ?? CENTRALQ_DEFAULT_API_URL;
646
- const response = await fetch(
647
- `${normalizeApiUrl2(apiUrl)}/api/queue/verify/${encodeURIComponent(options.eventId)}`,
648
- {
649
- method: "POST",
650
- headers: {
651
- "Content-Type": "application/json",
652
- "x-api-key": options.secretKey
658
+ function validateOptions(options) {
659
+ if (!options.secretKey?.trim()) {
660
+ throw new TypeError("CentralQ secretKey es requerida");
661
+ }
662
+ if (options.timeoutMs !== void 0 && options.timeoutMs <= 0) {
663
+ throw new TypeError("CentralQ timeoutMs debe ser mayor a cero");
664
+ }
665
+ }
666
+ var CentralQServerClient = class {
667
+ constructor(options) {
668
+ validateOptions(options);
669
+ this.apiUrl = normalizeApiUrl2(options.apiUrl ?? CENTRALQ_DEFAULT_API_URL);
670
+ this.secretKey = options.secretKey.trim();
671
+ this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
672
+ this.fetchFn = options.fetch ?? globalThis.fetch;
673
+ }
674
+ validateToken(input) {
675
+ return this.post(
676
+ `/api/queue/verify/${encodeURIComponent(input.eventId)}`,
677
+ { token: input.token },
678
+ { valid: false, reason: "Respuesta inv\xE1lida" }
679
+ );
680
+ }
681
+ verifyAccess(input) {
682
+ return this.post(
683
+ `/api/queue/verify/${encodeURIComponent(input.eventId)}/access`,
684
+ { token: input.token ?? null },
685
+ {
686
+ allowed: false,
687
+ reason: "invalid_response",
688
+ message: "Respuesta inv\xE1lida"
689
+ }
690
+ );
691
+ }
692
+ completeAccess(input) {
693
+ return this.post(
694
+ `/api/queue/verify/${encodeURIComponent(input.eventId)}/complete`,
695
+ {
696
+ token: input.token,
697
+ completionId: input.completionId
653
698
  },
654
- body: JSON.stringify({ token: options.token })
699
+ { completed: false, reason: "invalid_response" }
700
+ );
701
+ }
702
+ async post(path, body, fallback) {
703
+ const controller = new AbortController();
704
+ const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
705
+ try {
706
+ const response = await this.fetchFn(`${this.apiUrl}${path}`, {
707
+ method: "POST",
708
+ headers: {
709
+ "Content-Type": "application/json",
710
+ "x-api-key": this.secretKey
711
+ },
712
+ body: JSON.stringify(body),
713
+ signal: controller.signal
714
+ });
715
+ const data = await response.json().catch(() => fallback);
716
+ return { status: response.status, data };
717
+ } catch (error) {
718
+ if (controller.signal.aborted) {
719
+ throw new CentralQServerRequestError(
720
+ "timeout",
721
+ `CentralQ no respondi\xF3 en ${this.timeoutMs}ms`,
722
+ { cause: error }
723
+ );
724
+ }
725
+ throw new CentralQServerRequestError(
726
+ "network_error",
727
+ "No se pudo conectar con CentralQ",
728
+ { cause: error }
729
+ );
730
+ } finally {
731
+ clearTimeout(timeout);
655
732
  }
656
- );
657
- const data = await response.json().catch(() => ({
658
- valid: false,
659
- reason: "Respuesta inv\xE1lida"
660
- }));
661
- return { status: response.status, data };
733
+ }
734
+ };
735
+ function createCentralQServerClient(options) {
736
+ return new CentralQServerClient(options);
737
+ }
738
+ function validateQueueTokenServer(options) {
739
+ return createCentralQServerClient(options).validateToken(options);
740
+ }
741
+ function verifyQueueAccessServer(options) {
742
+ return createCentralQServerClient(options).verifyAccess(options);
743
+ }
744
+ function completeQueueAccessServer(options) {
745
+ return createCentralQServerClient(options).completeAccess(options);
662
746
  }
663
747
  // Annotate the CommonJS export names for ESM import in node:
664
748
  0 && (module.exports = {
665
749
  CENTRALQ_DEFAULT_API_URL,
666
750
  CentralQ,
667
751
  CentralQClient,
752
+ CentralQServerClient,
753
+ CentralQServerRequestError,
668
754
  QueueHttpClient,
755
+ completeQueueAccessServer,
669
756
  createCentralQClient,
670
- validateQueueTokenServer
757
+ createCentralQServerClient,
758
+ validateQueueTokenServer,
759
+ verifyQueueAccessServer
671
760
  });
package/dist/index.mjs CHANGED
@@ -1,6 +1,11 @@
1
1
  import {
2
- validateQueueTokenServer
3
- } from "./chunk-JJ33EJ65.mjs";
2
+ CentralQServerClient,
3
+ CentralQServerRequestError,
4
+ completeQueueAccessServer,
5
+ createCentralQServerClient,
6
+ validateQueueTokenServer,
7
+ verifyQueueAccessServer
8
+ } from "./chunk-FAJWYFUK.mjs";
4
9
  import {
5
10
  CentralQ,
6
11
  CentralQClient,
@@ -15,7 +20,12 @@ export {
15
20
  CENTRALQ_DEFAULT_API_URL,
16
21
  CentralQ,
17
22
  CentralQClient,
23
+ CentralQServerClient,
24
+ CentralQServerRequestError,
18
25
  QueueHttpClient,
26
+ completeQueueAccessServer,
19
27
  createCentralQClient,
20
- validateQueueTokenServer
28
+ createCentralQServerClient,
29
+ validateQueueTokenServer,
30
+ verifyQueueAccessServer
21
31
  };
package/dist/server.d.mts CHANGED
@@ -1,6 +1,10 @@
1
- interface ValidateQueueTokenServerOptions {
1
+ interface CentralQServerClientOptions {
2
2
  apiUrl?: string;
3
3
  secretKey: string;
4
+ timeoutMs?: number;
5
+ fetch?: typeof fetch;
6
+ }
7
+ interface ValidateQueueTokenServerOptions extends CentralQServerClientOptions {
4
8
  eventId: string;
5
9
  token: string;
6
10
  }
@@ -13,9 +17,85 @@ type QueueTokenValidationResult = {
13
17
  valid: false;
14
18
  reason: string;
15
19
  };
20
+ interface VerifyQueueAccessServerOptions extends CentralQServerClientOptions {
21
+ eventId: string;
22
+ token?: string;
23
+ }
24
+ type QueueAccessValidationResult = {
25
+ allowed: true;
26
+ eventId: string;
27
+ protected: boolean;
28
+ reason?: string;
29
+ userId?: string;
30
+ expiresAt?: string;
31
+ } | {
32
+ allowed: false;
33
+ eventId?: string;
34
+ protected?: boolean;
35
+ reason?: string;
36
+ message?: string;
37
+ };
38
+ interface CompleteQueueAccessServerOptions extends CentralQServerClientOptions {
39
+ eventId: string;
40
+ token: string;
41
+ completionId: string;
42
+ }
43
+ type QueueAccessCompletionResult = {
44
+ completed: true;
45
+ alreadyCompleted: boolean;
46
+ } | {
47
+ completed: false;
48
+ reason: string;
49
+ };
50
+ type CentralQServerRequestErrorCode = "network_error" | "timeout";
51
+ declare class CentralQServerRequestError extends Error {
52
+ readonly code: CentralQServerRequestErrorCode;
53
+ constructor(code: CentralQServerRequestErrorCode, message: string, options?: {
54
+ cause?: unknown;
55
+ });
56
+ }
57
+ declare class CentralQServerClient {
58
+ private readonly apiUrl;
59
+ private readonly secretKey;
60
+ private readonly timeoutMs;
61
+ private readonly fetchFn;
62
+ constructor(options: CentralQServerClientOptions);
63
+ validateToken(input: {
64
+ eventId: string;
65
+ token: string;
66
+ }): Promise<{
67
+ status: number;
68
+ data: QueueTokenValidationResult;
69
+ }>;
70
+ verifyAccess(input: {
71
+ eventId: string;
72
+ token?: string;
73
+ }): Promise<{
74
+ status: number;
75
+ data: QueueAccessValidationResult;
76
+ }>;
77
+ completeAccess(input: {
78
+ eventId: string;
79
+ token: string;
80
+ completionId: string;
81
+ }): Promise<{
82
+ status: number;
83
+ data: QueueAccessCompletionResult;
84
+ }>;
85
+ private post;
86
+ }
87
+ declare function createCentralQServerClient(options: CentralQServerClientOptions): CentralQServerClient;
16
88
  declare function validateQueueTokenServer(options: ValidateQueueTokenServerOptions): Promise<{
17
89
  status: number;
18
90
  data: QueueTokenValidationResult;
19
91
  }>;
92
+ declare function verifyQueueAccessServer(options: VerifyQueueAccessServerOptions): Promise<{
93
+ status: number;
94
+ data: QueueAccessValidationResult;
95
+ }>;
96
+ declare function completeQueueAccessServer(options: CompleteQueueAccessServerOptions): Promise<{
97
+ status: number;
98
+ data: QueueAccessCompletionResult;
99
+ }>;
20
100
 
21
- export { type QueueTokenValidationResult, type ValidateQueueTokenServerOptions, validateQueueTokenServer };
101
+ export { CentralQServerClient, type CentralQServerClientOptions, CentralQServerRequestError, type CentralQServerRequestErrorCode, type CompleteQueueAccessServerOptions, type QueueAccessCompletionResult, type QueueAccessValidationResult, type QueueTokenValidationResult, type ValidateQueueTokenServerOptions, type VerifyQueueAccessServerOptions, completeQueueAccessServer, createCentralQServerClient, validateQueueTokenServer, verifyQueueAccessServer };
package/dist/server.d.ts CHANGED
@@ -1,6 +1,10 @@
1
- interface ValidateQueueTokenServerOptions {
1
+ interface CentralQServerClientOptions {
2
2
  apiUrl?: string;
3
3
  secretKey: string;
4
+ timeoutMs?: number;
5
+ fetch?: typeof fetch;
6
+ }
7
+ interface ValidateQueueTokenServerOptions extends CentralQServerClientOptions {
4
8
  eventId: string;
5
9
  token: string;
6
10
  }
@@ -13,9 +17,85 @@ type QueueTokenValidationResult = {
13
17
  valid: false;
14
18
  reason: string;
15
19
  };
20
+ interface VerifyQueueAccessServerOptions extends CentralQServerClientOptions {
21
+ eventId: string;
22
+ token?: string;
23
+ }
24
+ type QueueAccessValidationResult = {
25
+ allowed: true;
26
+ eventId: string;
27
+ protected: boolean;
28
+ reason?: string;
29
+ userId?: string;
30
+ expiresAt?: string;
31
+ } | {
32
+ allowed: false;
33
+ eventId?: string;
34
+ protected?: boolean;
35
+ reason?: string;
36
+ message?: string;
37
+ };
38
+ interface CompleteQueueAccessServerOptions extends CentralQServerClientOptions {
39
+ eventId: string;
40
+ token: string;
41
+ completionId: string;
42
+ }
43
+ type QueueAccessCompletionResult = {
44
+ completed: true;
45
+ alreadyCompleted: boolean;
46
+ } | {
47
+ completed: false;
48
+ reason: string;
49
+ };
50
+ type CentralQServerRequestErrorCode = "network_error" | "timeout";
51
+ declare class CentralQServerRequestError extends Error {
52
+ readonly code: CentralQServerRequestErrorCode;
53
+ constructor(code: CentralQServerRequestErrorCode, message: string, options?: {
54
+ cause?: unknown;
55
+ });
56
+ }
57
+ declare class CentralQServerClient {
58
+ private readonly apiUrl;
59
+ private readonly secretKey;
60
+ private readonly timeoutMs;
61
+ private readonly fetchFn;
62
+ constructor(options: CentralQServerClientOptions);
63
+ validateToken(input: {
64
+ eventId: string;
65
+ token: string;
66
+ }): Promise<{
67
+ status: number;
68
+ data: QueueTokenValidationResult;
69
+ }>;
70
+ verifyAccess(input: {
71
+ eventId: string;
72
+ token?: string;
73
+ }): Promise<{
74
+ status: number;
75
+ data: QueueAccessValidationResult;
76
+ }>;
77
+ completeAccess(input: {
78
+ eventId: string;
79
+ token: string;
80
+ completionId: string;
81
+ }): Promise<{
82
+ status: number;
83
+ data: QueueAccessCompletionResult;
84
+ }>;
85
+ private post;
86
+ }
87
+ declare function createCentralQServerClient(options: CentralQServerClientOptions): CentralQServerClient;
16
88
  declare function validateQueueTokenServer(options: ValidateQueueTokenServerOptions): Promise<{
17
89
  status: number;
18
90
  data: QueueTokenValidationResult;
19
91
  }>;
92
+ declare function verifyQueueAccessServer(options: VerifyQueueAccessServerOptions): Promise<{
93
+ status: number;
94
+ data: QueueAccessValidationResult;
95
+ }>;
96
+ declare function completeQueueAccessServer(options: CompleteQueueAccessServerOptions): Promise<{
97
+ status: number;
98
+ data: QueueAccessCompletionResult;
99
+ }>;
20
100
 
21
- export { type QueueTokenValidationResult, type ValidateQueueTokenServerOptions, validateQueueTokenServer };
101
+ export { CentralQServerClient, type CentralQServerClientOptions, CentralQServerRequestError, type CentralQServerRequestErrorCode, type CompleteQueueAccessServerOptions, type QueueAccessCompletionResult, type QueueAccessValidationResult, type QueueTokenValidationResult, type ValidateQueueTokenServerOptions, type VerifyQueueAccessServerOptions, completeQueueAccessServer, createCentralQServerClient, validateQueueTokenServer, verifyQueueAccessServer };
package/dist/server.js CHANGED
@@ -20,7 +20,12 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/server.ts
21
21
  var server_exports = {};
22
22
  __export(server_exports, {
23
- validateQueueTokenServer: () => validateQueueTokenServer
23
+ CentralQServerClient: () => CentralQServerClient,
24
+ CentralQServerRequestError: () => CentralQServerRequestError,
25
+ completeQueueAccessServer: () => completeQueueAccessServer,
26
+ createCentralQServerClient: () => createCentralQServerClient,
27
+ validateQueueTokenServer: () => validateQueueTokenServer,
28
+ verifyQueueAccessServer: () => verifyQueueAccessServer
24
29
  });
25
30
  module.exports = __toCommonJS(server_exports);
26
31
 
@@ -28,29 +33,113 @@ module.exports = __toCommonJS(server_exports);
28
33
  var CENTRALQ_DEFAULT_API_URL = "http://localhost:3001";
29
34
 
30
35
  // src/server.ts
36
+ var DEFAULT_TIMEOUT_MS = 5e3;
37
+ var CentralQServerRequestError = class extends Error {
38
+ constructor(code, message, options) {
39
+ super(message, options);
40
+ this.name = "CentralQServerRequestError";
41
+ this.code = code;
42
+ }
43
+ };
31
44
  function normalizeApiUrl(apiUrl) {
32
- return apiUrl.replace(/\/+$/, "");
45
+ const trimmed = apiUrl.trim().replace(/\/+$/, "");
46
+ return trimmed.endsWith("/api") ? trimmed.slice(0, -4) : trimmed;
33
47
  }
34
- async function validateQueueTokenServer(options) {
35
- const apiUrl = options.apiUrl ?? CENTRALQ_DEFAULT_API_URL;
36
- const response = await fetch(
37
- `${normalizeApiUrl(apiUrl)}/api/queue/verify/${encodeURIComponent(options.eventId)}`,
38
- {
39
- method: "POST",
40
- headers: {
41
- "Content-Type": "application/json",
42
- "x-api-key": options.secretKey
48
+ function validateOptions(options) {
49
+ if (!options.secretKey?.trim()) {
50
+ throw new TypeError("CentralQ secretKey es requerida");
51
+ }
52
+ if (options.timeoutMs !== void 0 && options.timeoutMs <= 0) {
53
+ throw new TypeError("CentralQ timeoutMs debe ser mayor a cero");
54
+ }
55
+ }
56
+ var CentralQServerClient = class {
57
+ constructor(options) {
58
+ validateOptions(options);
59
+ this.apiUrl = normalizeApiUrl(options.apiUrl ?? CENTRALQ_DEFAULT_API_URL);
60
+ this.secretKey = options.secretKey.trim();
61
+ this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
62
+ this.fetchFn = options.fetch ?? globalThis.fetch;
63
+ }
64
+ validateToken(input) {
65
+ return this.post(
66
+ `/api/queue/verify/${encodeURIComponent(input.eventId)}`,
67
+ { token: input.token },
68
+ { valid: false, reason: "Respuesta inv\xE1lida" }
69
+ );
70
+ }
71
+ verifyAccess(input) {
72
+ return this.post(
73
+ `/api/queue/verify/${encodeURIComponent(input.eventId)}/access`,
74
+ { token: input.token ?? null },
75
+ {
76
+ allowed: false,
77
+ reason: "invalid_response",
78
+ message: "Respuesta inv\xE1lida"
79
+ }
80
+ );
81
+ }
82
+ completeAccess(input) {
83
+ return this.post(
84
+ `/api/queue/verify/${encodeURIComponent(input.eventId)}/complete`,
85
+ {
86
+ token: input.token,
87
+ completionId: input.completionId
43
88
  },
44
- body: JSON.stringify({ token: options.token })
89
+ { completed: false, reason: "invalid_response" }
90
+ );
91
+ }
92
+ async post(path, body, fallback) {
93
+ const controller = new AbortController();
94
+ const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
95
+ try {
96
+ const response = await this.fetchFn(`${this.apiUrl}${path}`, {
97
+ method: "POST",
98
+ headers: {
99
+ "Content-Type": "application/json",
100
+ "x-api-key": this.secretKey
101
+ },
102
+ body: JSON.stringify(body),
103
+ signal: controller.signal
104
+ });
105
+ const data = await response.json().catch(() => fallback);
106
+ return { status: response.status, data };
107
+ } catch (error) {
108
+ if (controller.signal.aborted) {
109
+ throw new CentralQServerRequestError(
110
+ "timeout",
111
+ `CentralQ no respondi\xF3 en ${this.timeoutMs}ms`,
112
+ { cause: error }
113
+ );
114
+ }
115
+ throw new CentralQServerRequestError(
116
+ "network_error",
117
+ "No se pudo conectar con CentralQ",
118
+ { cause: error }
119
+ );
120
+ } finally {
121
+ clearTimeout(timeout);
45
122
  }
46
- );
47
- const data = await response.json().catch(() => ({
48
- valid: false,
49
- reason: "Respuesta inv\xE1lida"
50
- }));
51
- return { status: response.status, data };
123
+ }
124
+ };
125
+ function createCentralQServerClient(options) {
126
+ return new CentralQServerClient(options);
127
+ }
128
+ function validateQueueTokenServer(options) {
129
+ return createCentralQServerClient(options).validateToken(options);
130
+ }
131
+ function verifyQueueAccessServer(options) {
132
+ return createCentralQServerClient(options).verifyAccess(options);
133
+ }
134
+ function completeQueueAccessServer(options) {
135
+ return createCentralQServerClient(options).completeAccess(options);
52
136
  }
53
137
  // Annotate the CommonJS export names for ESM import in node:
54
138
  0 && (module.exports = {
55
- validateQueueTokenServer
139
+ CentralQServerClient,
140
+ CentralQServerRequestError,
141
+ completeQueueAccessServer,
142
+ createCentralQServerClient,
143
+ validateQueueTokenServer,
144
+ verifyQueueAccessServer
56
145
  });
package/dist/server.mjs CHANGED
@@ -1,8 +1,18 @@
1
1
  import {
2
- validateQueueTokenServer
3
- } from "./chunk-JJ33EJ65.mjs";
2
+ CentralQServerClient,
3
+ CentralQServerRequestError,
4
+ completeQueueAccessServer,
5
+ createCentralQServerClient,
6
+ validateQueueTokenServer,
7
+ verifyQueueAccessServer
8
+ } from "./chunk-FAJWYFUK.mjs";
4
9
  import "./chunk-P73Q2ZIO.mjs";
5
10
  import "./chunk-XRJFNASX.mjs";
6
11
  export {
7
- validateQueueTokenServer
12
+ CentralQServerClient,
13
+ CentralQServerRequestError,
14
+ completeQueueAccessServer,
15
+ createCentralQServerClient,
16
+ validateQueueTokenServer,
17
+ verifyQueueAccessServer
8
18
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@central-ticket/queue-sdk",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "keywords": [
5
5
  "virtual-queue",
6
6
  "waiting-room",