@pomeloanalytics/api 0.0.0-stage → 0.1.0-beta.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/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/README.md +154 -2
- package/dist/client.d.ts +52 -0
- package/dist/client.js +47 -0
- package/dist/errors.d.ts +20 -0
- package/dist/errors.js +38 -0
- package/dist/generated/openapi.d.ts +1887 -0
- package/dist/generated/openapi.js +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +2 -0
- package/dist/transport.d.ts +9 -0
- package/dist/transport.js +134 -0
- package/dist/types.d.ts +37 -0
- package/dist/types.js +1 -0
- package/package.json +27 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { createPomeloClient, PomeloClient } from './client.js';
|
|
2
|
+
export type { ApiPath, ApiData, ApiParams, TenantsParams, TimeseriesParams, BreakdownParams, OverviewParams, ContentParams, AcquisitionParams, ActionsParams, GoalsParams, GoalConversionsParams, UsageHistoryParams } from './client.js';
|
|
3
|
+
export type { components, paths } from './generated/openapi.js';
|
|
4
|
+
export { PomeloApiError, PomeloTransportError } from './errors.js';
|
|
5
|
+
export type { ApiResponse, Fetch, PomeloClientOptions, RequestOptions, ResponseMetadata, TransportErrorCode } from './types.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ApiResponse, PomeloClientOptions, RequestOptions } from './types.js';
|
|
2
|
+
type Query = Readonly<Record<string, string | number | boolean | undefined>>;
|
|
3
|
+
/** Internal transport. Public operations are derived from the reviewed API contract. */
|
|
4
|
+
export declare class Transport {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(options: PomeloClientOptions);
|
|
7
|
+
get<T>(path: string, query: Query, options?: RequestOptions): Promise<ApiResponse<T>>;
|
|
8
|
+
}
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { PomeloApiError, PomeloTransportError } from './errors.js';
|
|
2
|
+
const identifier = /^[A-Za-z0-9_.:-]{1,128}$/;
|
|
3
|
+
const errorCode = /^[A-Za-z][A-Za-z0-9_.:-]{0,95}$/;
|
|
4
|
+
function assertServer() {
|
|
5
|
+
if (typeof window !== 'undefined' || typeof process === 'undefined' || !process.versions?.node) {
|
|
6
|
+
throw new PomeloTransportError('UNSUPPORTED_RUNTIME');
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function timeout(value) {
|
|
10
|
+
const result = value ?? 30_000;
|
|
11
|
+
if (!Number.isSafeInteger(result) || result < 1 || result > 2_147_483_647) {
|
|
12
|
+
throw new PomeloTransportError('INVALID_OPTIONS');
|
|
13
|
+
}
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
function record(value) {
|
|
17
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
18
|
+
}
|
|
19
|
+
function responseMetadata(headers, fallbackId) {
|
|
20
|
+
const rawId = headers.get('x-request-id');
|
|
21
|
+
const requestId = rawId && identifier.test(rawId) ? rawId : fallbackId;
|
|
22
|
+
const rawRetry = headers.get('retry-after');
|
|
23
|
+
const retryAfter = rawRetry && (/^\d+$/.test(rawRetry) || !Number.isNaN(Date.parse(rawRetry))) ? rawRetry : undefined;
|
|
24
|
+
const numeric = (name) => {
|
|
25
|
+
const raw = headers.get(name);
|
|
26
|
+
const value = raw && /^\d+$/.test(raw) ? Number(raw) : undefined;
|
|
27
|
+
return value !== undefined && Number.isSafeInteger(value) ? value : undefined;
|
|
28
|
+
};
|
|
29
|
+
const limit = numeric('x-ratelimit-limit');
|
|
30
|
+
const remaining = numeric('x-ratelimit-remaining');
|
|
31
|
+
const reset = numeric('x-ratelimit-reset');
|
|
32
|
+
return {
|
|
33
|
+
...(requestId ? { requestId } : {}),
|
|
34
|
+
...(retryAfter ? { retryAfter } : {}),
|
|
35
|
+
rateLimit: { ...(limit !== undefined ? { limit } : {}), ...(remaining !== undefined ? { remaining } : {}), ...(reset !== undefined ? { reset } : {}) },
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** Internal transport. Public operations are derived from the reviewed API contract. */
|
|
39
|
+
export class Transport {
|
|
40
|
+
#token;
|
|
41
|
+
#baseUrl;
|
|
42
|
+
#timeout;
|
|
43
|
+
#fetch;
|
|
44
|
+
constructor(options) {
|
|
45
|
+
try {
|
|
46
|
+
const url = new URL(options.baseUrl ?? 'https://api.pomeloanalytics.com');
|
|
47
|
+
if (url.protocol !== 'https:' || url.username || url.password || url.search || url.hash || url.pathname !== '/') {
|
|
48
|
+
throw new PomeloTransportError('INVALID_OPTIONS');
|
|
49
|
+
}
|
|
50
|
+
if (typeof options.token !== 'string' || !/^[\x21-\x7e]+$/.test(options.token)) {
|
|
51
|
+
throw new PomeloTransportError('INVALID_OPTIONS');
|
|
52
|
+
}
|
|
53
|
+
this.#baseUrl = url.origin;
|
|
54
|
+
this.#token = options.token;
|
|
55
|
+
this.#timeout = timeout(options.timeoutMs);
|
|
56
|
+
this.#fetch = options.fetch ?? globalThis.fetch;
|
|
57
|
+
if (typeof this.#fetch !== 'function')
|
|
58
|
+
throw new PomeloTransportError('INVALID_OPTIONS');
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
throw new PomeloTransportError('INVALID_OPTIONS');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async get(path, query, options = {}) {
|
|
65
|
+
assertServer();
|
|
66
|
+
if (!/^\/v1\/[a-z/]+$/.test(path) || (options.requestId !== undefined && !identifier.test(options.requestId))) {
|
|
67
|
+
throw new PomeloTransportError('INVALID_OPTIONS');
|
|
68
|
+
}
|
|
69
|
+
const requestTimeout = timeout(options.timeoutMs ?? this.#timeout);
|
|
70
|
+
if (options.signal?.aborted)
|
|
71
|
+
throw new PomeloTransportError('ABORTED');
|
|
72
|
+
const url = new URL(path, this.#baseUrl);
|
|
73
|
+
for (const [name, value] of Object.entries(query)) {
|
|
74
|
+
if (value !== undefined)
|
|
75
|
+
url.searchParams.set(name, String(value));
|
|
76
|
+
}
|
|
77
|
+
const requestId = options.requestId ?? globalThis.crypto.randomUUID();
|
|
78
|
+
const headers = new Headers({ Accept: 'application/json', Authorization: `Bearer ${this.#token}` });
|
|
79
|
+
headers.set('X-Request-Id', requestId);
|
|
80
|
+
const controller = new AbortController();
|
|
81
|
+
let timedOut = false;
|
|
82
|
+
const abort = () => controller.abort();
|
|
83
|
+
options.signal?.addEventListener('abort', abort, { once: true });
|
|
84
|
+
const timer = setTimeout(() => { timedOut = true; controller.abort(); }, requestTimeout);
|
|
85
|
+
let metadata;
|
|
86
|
+
let abortRequest;
|
|
87
|
+
const aborted = new Promise((_resolve, reject) => {
|
|
88
|
+
abortRequest = () => reject(new PomeloTransportError(timedOut ? 'TIMEOUT' : 'ABORTED', metadata));
|
|
89
|
+
controller.signal.addEventListener('abort', abortRequest, { once: true });
|
|
90
|
+
});
|
|
91
|
+
try {
|
|
92
|
+
const response = await Promise.race([this.#fetch(url, { method: 'GET', headers, signal: controller.signal, redirect: 'error', credentials: 'omit', cache: 'no-store' }), aborted]);
|
|
93
|
+
metadata = responseMetadata(response.headers, requestId);
|
|
94
|
+
let payload;
|
|
95
|
+
try {
|
|
96
|
+
payload = await Promise.race([response.json(), aborted]);
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
if (controller.signal.aborted)
|
|
100
|
+
throw new PomeloTransportError(timedOut ? 'TIMEOUT' : 'ABORTED', metadata);
|
|
101
|
+
if (response.ok)
|
|
102
|
+
throw new PomeloTransportError('INVALID_RESPONSE', metadata);
|
|
103
|
+
}
|
|
104
|
+
if (!response.ok) {
|
|
105
|
+
const envelope = record(payload) && record(payload.error) ? payload.error : undefined;
|
|
106
|
+
const code = typeof envelope?.code === 'string' && errorCode.test(envelope.code) ? envelope.code : 'HTTP_ERROR';
|
|
107
|
+
const bodyId = typeof envelope?.request_id === 'string' && identifier.test(envelope.request_id) ? envelope.request_id : undefined;
|
|
108
|
+
const headerId = response.headers.get('x-request-id');
|
|
109
|
+
if ((!headerId || !identifier.test(headerId)) && bodyId)
|
|
110
|
+
metadata = { ...metadata, requestId: bodyId };
|
|
111
|
+
throw new PomeloApiError(response.status, code, metadata, envelope?.details, payload);
|
|
112
|
+
}
|
|
113
|
+
if (!record(payload))
|
|
114
|
+
throw new PomeloTransportError('INVALID_RESPONSE', metadata);
|
|
115
|
+
if (record(payload.meta)) {
|
|
116
|
+
metadata = { ...metadata, metering: payload.meta };
|
|
117
|
+
}
|
|
118
|
+
return { data: payload, metadata };
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
if (error instanceof PomeloApiError || error instanceof PomeloTransportError)
|
|
122
|
+
throw error;
|
|
123
|
+
if (controller.signal.aborted)
|
|
124
|
+
throw new PomeloTransportError(timedOut ? 'TIMEOUT' : 'ABORTED', metadata);
|
|
125
|
+
throw new PomeloTransportError('NETWORK_ERROR', metadata);
|
|
126
|
+
}
|
|
127
|
+
finally {
|
|
128
|
+
clearTimeout(timer);
|
|
129
|
+
if (abortRequest)
|
|
130
|
+
controller.signal.removeEventListener('abort', abortRequest);
|
|
131
|
+
options.signal?.removeEventListener('abort', abort);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { components } from './generated/openapi.js';
|
|
2
|
+
/** Fetch implementation, injectable for tests and server-side integrations. */
|
|
3
|
+
export type Fetch = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
4
|
+
export interface PomeloClientOptions {
|
|
5
|
+
/** API token. Keep this value in server-side configuration only. */
|
|
6
|
+
token: string;
|
|
7
|
+
/** Defaults to https://api.pomeloanalytics.com. Custom origins must use HTTPS. */
|
|
8
|
+
baseUrl?: string;
|
|
9
|
+
/** Defaults to 30 seconds. Includes reading and decoding the response. */
|
|
10
|
+
timeoutMs?: number;
|
|
11
|
+
fetch?: Fetch;
|
|
12
|
+
}
|
|
13
|
+
export interface RequestOptions {
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
/** Override the client's timeout for this request. */
|
|
16
|
+
timeoutMs?: number;
|
|
17
|
+
/** Correlation identifier sent as X-Request-Id. Defaults to a new UUID. */
|
|
18
|
+
requestId?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface ResponseMetadata {
|
|
21
|
+
requestId?: string;
|
|
22
|
+
/** Value from Retry-After, when present. No retry is performed by the SDK. */
|
|
23
|
+
retryAfter?: string;
|
|
24
|
+
/** Original API meta object, when present. Field names and additional values are preserved. */
|
|
25
|
+
metering?: components['schemas']['MeteredMeta'];
|
|
26
|
+
rateLimit: {
|
|
27
|
+
limit?: number;
|
|
28
|
+
remaining?: number;
|
|
29
|
+
/** Unix timestamp in seconds. */
|
|
30
|
+
reset?: number;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export interface ApiResponse<T> {
|
|
34
|
+
data: T;
|
|
35
|
+
metadata: ResponseMetadata;
|
|
36
|
+
}
|
|
37
|
+
export type TransportErrorCode = 'INVALID_OPTIONS' | 'UNSUPPORTED_RUNTIME' | 'NETWORK_ERROR' | 'TIMEOUT' | 'ABORTED' | 'INVALID_RESPONSE';
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pomeloanalytics/api",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "Server-side TypeScript client for the Pomelo Analytics API.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/pomeloanalytics/pomelo-js.git",
|
|
10
|
+
"directory": "packages/api"
|
|
11
|
+
},
|
|
12
|
+
"bugs": { "url": "https://github.com/pomeloanalytics/pomelo-js/issues" },
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": { "types": "./dist/index.d.ts", "import": "./dist/index.js", "default": "./dist/index.js" }
|
|
17
|
+
},
|
|
18
|
+
"files": ["dist", "README.md", "CHANGELOG.md", "LICENSE"],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"registry": "https://registry.npmjs.org/",
|
|
21
|
+
"access": "public",
|
|
22
|
+
"tag": "beta"
|
|
23
|
+
},
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"engines": { "node": "^22.13.0 || ^24.0.0" },
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc -p tsconfig.json"
|
|
28
|
+
}
|
|
29
|
+
}
|