@chatlychat/sdk-node 0.1.0 → 0.2.1

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,178 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { ChatlyApiError, ChatlyRateLimitError, ChatlyServerError, ChatlyTransportError, errorForResponse, parseRetryAfter, retryAfterHeader, } from './errors.js';
3
+ /** Production. Never a dev or example host — see the note in {@link Transport}. */
4
+ export const DEFAULT_API_URL = 'https://api.chatlychat.com';
5
+ /** The package version, stamped into `User-Agent` so our logs can see SDK age. */
6
+ export const SDK_VERSION = '0.2.1';
7
+ export const DEFAULT_RETRY = { attempts: 3, baseDelayMs: 250, maxDelayMs: 4_000 };
8
+ /**
9
+ * One HTTP concern, in one place: auth, idempotency, retry, error mapping.
10
+ *
11
+ * Resource namespaces hold no HTTP logic at all — they build a path and a
12
+ * body and hand both here. That is what makes the retry policy and the error
13
+ * model uniform across 55 routes instead of per-method.
14
+ */
15
+ export class Transport {
16
+ base;
17
+ headers;
18
+ timeoutMs;
19
+ fetchFn;
20
+ retry;
21
+ sleepFn;
22
+ /**
23
+ * When set, every write uses this exact `Idempotency-Key` instead of a fresh
24
+ * UUID. Set by `ChatlyClient.withIdempotencyKey`, which returns a client
25
+ * sharing this transport's configuration with a pinned key.
26
+ */
27
+ pinnedIdempotencyKey;
28
+ constructor(opts, pinnedIdempotencyKey = null) {
29
+ if (typeof opts.apiKey !== 'string' || opts.apiKey.length === 0) {
30
+ throw new TypeError('ChatlyClient: apiKey is required and must be a non-empty string');
31
+ }
32
+ /*
33
+ * Production, not a dev hostname.
34
+ *
35
+ * This was `https://api.livechat.local`, which resolves nowhere — so a
36
+ * developer who followed the published docs and omitted `apiUrl` got an
37
+ * SDK whose every call failed, and the docs page carried a warning
38
+ * telling each reader to override it. Reported by a customer who
39
+ * integrated from the docs alone, not caught here, because every test in
40
+ * this package passes an explicit `apiUrl`. The same defect shipped in
41
+ * the Kotlin and Swift SDKs as `api.chatly.example`.
42
+ */
43
+ this.base = (opts.apiUrl ?? DEFAULT_API_URL).replace(/\/+$/, '');
44
+ this.headers = new Headers({
45
+ authorization: `Bearer ${opts.apiKey}`,
46
+ 'content-type': 'application/json',
47
+ accept: 'application/json',
48
+ 'user-agent': `chatly-sdk-node/${SDK_VERSION}`,
49
+ });
50
+ this.timeoutMs = opts.timeoutMs ?? 15_000;
51
+ this.fetchFn = opts.fetchFn ?? globalThis.fetch;
52
+ this.retry = { ...DEFAULT_RETRY, ...opts.retry };
53
+ this.sleepFn = opts.sleepFn ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
54
+ this.pinnedIdempotencyKey = pinnedIdempotencyKey;
55
+ }
56
+ /** A copy of this transport with a pinned idempotency key. */
57
+ withPinnedKey(key) {
58
+ const clone = Object.create(Transport.prototype);
59
+ Object.assign(clone, this, { pinnedIdempotencyKey: key });
60
+ return clone;
61
+ }
62
+ async get(path, query = {}) {
63
+ return this.send('GET', path, query, undefined);
64
+ }
65
+ async post(path, body, query = {}) {
66
+ return this.send('POST', path, query, body);
67
+ }
68
+ async patch(path, body, query = {}) {
69
+ return this.send('PATCH', path, query, body);
70
+ }
71
+ async put(path, body, query = {}) {
72
+ return this.send('PUT', path, query, body);
73
+ }
74
+ async del(path, query = {}) {
75
+ return this.send('DELETE', path, query, undefined);
76
+ }
77
+ /**
78
+ * The escape hatch, and the one method every other method goes through.
79
+ *
80
+ * 85 of the 140 key-accessible routes have no typed method here. Without a
81
+ * public way in, reaching one means abandoning the SDK and hand-rolling
82
+ * HTTP with a bearer token — losing the retry policy, the error model and
83
+ * the idempotency contract in the process.
84
+ */
85
+ async send(method, path, query = {}, body) {
86
+ const url = new URL(`${this.base}${path}`);
87
+ for (const [key, value] of Object.entries(query)) {
88
+ if (value === undefined) {
89
+ continue;
90
+ }
91
+ if (Array.isArray(value)) {
92
+ // Repeated keys stay repeated: `kinds=kb&kinds=ticket`. Joining them
93
+ // with a comma produces one parameter whose value is not a member of
94
+ // the enum — which is what the MCP server's `search` tool did.
95
+ for (const item of value) {
96
+ url.searchParams.append(key, item);
97
+ }
98
+ }
99
+ else {
100
+ url.searchParams.set(key, String(value));
101
+ }
102
+ }
103
+ const isWrite = method !== 'GET';
104
+ /*
105
+ * One key for the whole call, retries included.
106
+ *
107
+ * Minting a fresh key per ATTEMPT would defeat the header's only purpose:
108
+ * attempt 2 of a POST that actually succeeded upstream would be accepted
109
+ * as a new operation and double-write. Minting it per CALL means a retry
110
+ * is recognised as the same logical operation and replayed.
111
+ */
112
+ const idempotencyKey = isWrite ? (this.pinnedIdempotencyKey ?? randomUUID()) : null;
113
+ let lastError;
114
+ for (let attempt = 1; attempt <= this.retry.attempts; attempt++) {
115
+ try {
116
+ return await this.attempt(url, method, body, idempotencyKey);
117
+ }
118
+ catch (err) {
119
+ lastError = err;
120
+ const retryable = err instanceof ChatlyTransportError ||
121
+ err instanceof ChatlyRateLimitError ||
122
+ err instanceof ChatlyServerError;
123
+ if (!retryable || attempt === this.retry.attempts) {
124
+ throw err;
125
+ }
126
+ const advised = err instanceof ChatlyApiError && err.retryAfter !== null ? err.retryAfter * 1000 : null;
127
+ const backoff = Math.min(this.retry.baseDelayMs * 2 ** (attempt - 1), this.retry.maxDelayMs);
128
+ // Jitter: a fleet of workers retrying in lockstep is a second outage.
129
+ const jittered = backoff * (0.5 + Math.random() / 2);
130
+ await this.sleepFn(advised ?? jittered);
131
+ }
132
+ }
133
+ throw lastError;
134
+ }
135
+ async attempt(url, method, body, idempotencyKey) {
136
+ const ctrl = new AbortController();
137
+ const timer = setTimeout(() => ctrl.abort(), this.timeoutMs);
138
+ try {
139
+ const headers = new Headers(this.headers);
140
+ if (idempotencyKey !== null) {
141
+ headers.set('idempotency-key', idempotencyKey);
142
+ }
143
+ let res;
144
+ try {
145
+ res = await this.fetchFn(url.toString(), {
146
+ method,
147
+ headers,
148
+ signal: ctrl.signal,
149
+ ...(body === undefined ? {} : { body: JSON.stringify(body) }),
150
+ });
151
+ }
152
+ catch (cause) {
153
+ const why = ctrl.signal.aborted ? `timed out after ${String(this.timeoutMs)}ms` : 'failed';
154
+ throw new ChatlyTransportError(`Chatly request ${why}: ${method} ${url.pathname}`, cause);
155
+ }
156
+ if (!res.ok) {
157
+ throw errorForResponse({
158
+ status: res.status,
159
+ body: await res.text(),
160
+ requestId: res.headers.get('x-request-id'),
161
+ retryAfter: parseRetryAfter(retryAfterHeader(res.headers), Date.now()),
162
+ });
163
+ }
164
+ if (res.status === 204) {
165
+ return undefined;
166
+ }
167
+ const text = await res.text();
168
+ if (text.length === 0) {
169
+ return undefined;
170
+ }
171
+ return JSON.parse(text);
172
+ }
173
+ finally {
174
+ clearTimeout(timer);
175
+ }
176
+ }
177
+ }
178
+ //# sourceMappingURL=transport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transport.js","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EACL,cAAc,EACd,oBAAoB,EACpB,iBAAiB,EACjB,oBAAoB,EACpB,gBAAgB,EAChB,eAAe,EACf,gBAAgB,GACjB,MAAM,aAAa,CAAC;AAErB,mFAAmF;AACnF,MAAM,CAAC,MAAM,eAAe,GAAG,4BAA4B,CAAC;AAE5D,kFAAkF;AAClF,MAAM,CAAC,MAAM,WAAW,GAAG,OAAO,CAAC;AAWnC,MAAM,CAAC,MAAM,aAAa,GAAgB,EAAE,QAAQ,EAAE,CAAC,EAAE,WAAW,EAAE,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC;AAc/F;;;;;;GAMG;AACH,MAAM,OAAO,SAAS;IACH,IAAI,CAAS;IACb,OAAO,CAAU;IACjB,SAAS,CAAS;IAClB,OAAO,CAAe;IACtB,KAAK,CAAc;IACnB,OAAO,CAAgC;IACxD;;;;OAIG;IACa,oBAAoB,CAAgB;IAEpD,YAAmB,IAAsB,EAAE,uBAAsC,IAAI;Q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iBAC9D,CAAC,CAAC;YACL,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,mBAAmB,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC;gBAC3F,MAAM,IAAI,oBAAoB,CAAC,kBAAkB,GAAG,KAAK,MAAM,IAAI,GAAG,CAAC,QAAQ,EAAE,EAAE,KAAK,CAAC,CAAC;YAC5F,CAAC;YAED,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;gBACZ,MAAM,gBAAgB,CAAC;oBACrB,MAAM,EAAE,GAAG,CAAC,MAAM;oBAClB,IAAI,EAAE,MAAM,GAAG,CAAC,IAAI,EAAE;oBACtB,SAAS,EAAE,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;oBAC1C,UAAU,EAAE,eAAe,CAAC,gBAAgB,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC;iBACvE,CAAC,CAAC;YACL,CAAC;YACD,IAAI,GAAG,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;gBACvB,OAAO,SAAc,CAAC;YACxB,CAAC;YACD,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;YAC9B,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBACtB,OAAO,SAAc,CAAC;YACxB,CAAC;YACD,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAM,CAAC;QAC/B,CAAC;gBAAS,CAAC;YACT,YAAY,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,35 @@
1
+ /** Freshness window for `t`, in seconds. Matches what the docs recommend. */
2
+ export declare const WEBHOOK_SIGNATURE_TOLERANCE_SECONDS = 300;
3
+ export interface VerifyWebhookSignatureInput {
4
+ /**
5
+ * The EXACT bytes we POSTed. Pass the raw body — re-serialising parsed JSON
6
+ * reorders keys and changes whitespace, and the digest will not match.
7
+ */
8
+ body: string | Uint8Array;
9
+ /** The `x-livechat-signature` header value: `t=<unix seconds>,v1=<hex>`. */
10
+ signatureHeader: string;
11
+ /** The endpoint's `whsec_…` signing secret. */
12
+ secret: string;
13
+ /**
14
+ * How far `t` may be from now before the delivery is treated as a replay.
15
+ * Defaults to {@link WEBHOOK_SIGNATURE_TOLERANCE_SECONDS}; pass `Infinity`
16
+ * only when re-verifying an archived delivery.
17
+ */
18
+ toleranceSeconds?: number;
19
+ /** Escape hatch for tests. Unix seconds. */
20
+ nowSeconds?: number;
21
+ }
22
+ /**
23
+ * Verify the signature header on a webhook delivery from the platform.
24
+ *
25
+ * `v1` is HMAC-SHA256 over `` `${t}.${body}` `` — the timestamp is bound INTO
26
+ * the digest, which is what makes the replay window enforceable at all.
27
+ *
28
+ * This used to HMAC the body ALONE, so it rejected every genuine delivery the
29
+ * platform sent: the producer has always signed `${t}.${body}` and our own
30
+ * published documentation has always described that construction. Anyone
31
+ * following either the SDK or the docs got a hard "invalid signature" on 100%
32
+ * of real traffic. The wire format is unchanged — only this verifier was wrong.
33
+ */
34
+ export declare function verifyWebhookSignature(input: VerifyWebhookSignatureInput): boolean;
35
+ //# sourceMappingURL=webhooks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhooks.d.ts","sourceRoot":"","sources":["../src/webhooks.ts"],"names":[],"mappings":"AAEA,6EAA6E;AAC7E,eAAO,MAAM,mCAAmC,MAAM,CAAC;AAEvD,MAAM,WAAW,2BAA2B;IAC1C;;;OAGG;IACH,IAAI,EAAE,MAAM,GAAG,UAAU,CAAC;IAC1B,4EAA4E;IAC5E,eAAe,EAAE,MAAM,CAAC;IACxB,gDAAgD;IAChD,MAAM,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,4CAA4C;IAC5C,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,2BAA2B,GAAG,OAAO,CAiClF"}
@@ -0,0 +1,47 @@
1
+ import { createHmac, timingSafeEqual } from 'node:crypto';
2
+ /** Freshness window for `t`, in seconds. Matches what the docs recommend. */
3
+ export const WEBHOOK_SIGNATURE_TOLERANCE_SECONDS = 300;
4
+ /**
5
+ * Verify the signature header on a webhook delivery from the platform.
6
+ *
7
+ * `v1` is HMAC-SHA256 over `` `${t}.${body}` `` — the timestamp is bound INTO
8
+ * the digest, which is what makes the replay window enforceable at all.
9
+ *
10
+ * This used to HMAC the body ALONE, so it rejected every genuine delivery the
11
+ * platform sent: the producer has always signed `${t}.${body}` and our own
12
+ * published documentation has always described that construction. Anyone
13
+ * following either the SDK or the docs got a hard "invalid signature" on 100%
14
+ * of real traffic. The wire format is unchanged — only this verifier was wrong.
15
+ */
16
+ export function verifyWebhookSignature(input) {
17
+ const parts = new Map();
18
+ for (const part of input.signatureHeader.split(',')) {
19
+ const eq = part.indexOf('=');
20
+ if (eq > 0) {
21
+ parts.set(part.slice(0, eq).trim(), part.slice(eq + 1).trim());
22
+ }
23
+ }
24
+ const rawTs = parts.get('t');
25
+ const tag = parts.get('v1');
26
+ if (rawTs === undefined || tag === undefined) {
27
+ return false;
28
+ }
29
+ const issuedAt = Number.parseInt(rawTs, 10);
30
+ if (!Number.isFinite(issuedAt)) {
31
+ return false;
32
+ }
33
+ const tolerance = input.toleranceSeconds ?? WEBHOOK_SIGNATURE_TOLERANCE_SECONDS;
34
+ const now = input.nowSeconds ?? Math.floor(Date.now() / 1000);
35
+ if (Math.abs(now - issuedAt) > tolerance) {
36
+ return false;
37
+ }
38
+ const hmac = createHmac('sha256', input.secret);
39
+ hmac.update(`${rawTs}.`);
40
+ hmac.update(typeof input.body === 'string' ? Buffer.from(input.body, 'utf8') : input.body);
41
+ const a = Buffer.from(tag, 'hex');
42
+ const b = Buffer.from(hmac.digest('hex'), 'hex');
43
+ // Length check first: timingSafeEqual throws on a length mismatch, and a
44
+ // thrown verifier is an accepted webhook in a handler that forgot try/catch.
45
+ return a.length === b.length && timingSafeEqual(a, b);
46
+ }
47
+ //# sourceMappingURL=webhooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhooks.js","sourceRoot":"","sources":["../src/webhooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE1D,6EAA6E;AAC7E,MAAM,CAAC,MAAM,mCAAmC,GAAG,GAAG,CAAC;AAsBvD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAAkC;IACvE,MAAM,KAAK,GAAG,IAAI,GAAG,EAAkB,CAAC;IACxC,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,eAAe,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QACpD,MAAM,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAC7B,IAAI,EAAE,GAAG,CAAC,EAAE,CAAC;YACX,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;QACjE,CAAC;IACH,CAAC;IAED,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC7B,MAAM,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC5B,IAAI,KAAK,KAAK,SAAS,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QAC7C,OAAO,KAAK,CAAC;IACf,CAAC;IACD,MAAM,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAC5C,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC/B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,SAAS,GAAG,KAAK,CAAC,gBAAgB,IAAI,mCAAmC,CAAC;IAChF,MAAM,GAAG,GAAG,KAAK,CAAC,UAAU,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC;IAC9D,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,QAAQ,CAAC,GAAG,SAAS,EAAE,CAAC;QACzC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAChD,IAAI,CAAC,MAAM,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC;IACzB,IAAI,CAAC,MAAM,CAAC,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC3F,MAAM,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAClC,MAAM,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,CAAC;IACjD,yEAAyE;IACzE,6EAA6E;IAC7E,OAAO,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,IAAI,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACxD,CAAC"}
package/package.json CHANGED
@@ -1,17 +1,18 @@
1
1
  {
2
2
  "name": "@chatlychat/sdk-node",
3
- "version": "0.1.0",
4
- "description": "Server-side SDK for the LiveChat platform — REST client + webhook signature verification.",
3
+ "version": "0.2.1",
4
+ "description": "Official Node.js SDK for the Chatly API — typed REST client, webhook signature verification, and the widget identity hash.",
5
5
  "license": "MIT",
6
- "homepage": "https://github.com/livechat/platform/tree/main/packages/sdk-node",
6
+ "homepage": "https://github.com/chatlychat/chatly-sdk-node#readme",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "git+https://github.com/livechat/platform.git",
10
- "directory": "packages/sdk-node"
9
+ "url": "git+https://github.com/chatlychat/chatly-sdk-node.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/chatlychat/chatly-sdk-node/issues"
11
13
  },
12
- "bugs": "https://github.com/livechat/platform/issues",
13
14
  "keywords": [
14
- "livechat",
15
+ "chatly",
15
16
  "chat",
16
17
  "messaging",
17
18
  "support",
@@ -19,7 +20,7 @@
19
20
  "sdk",
20
21
  "server"
21
22
  ],
22
- "author": "LiveChat Platform Engineering",
23
+ "author": "Chatly",
23
24
  "type": "module",
24
25
  "main": "./dist/index.js",
25
26
  "module": "./dist/index.js",
@@ -38,13 +39,6 @@
38
39
  "CHANGELOG.md"
39
40
  ],
40
41
  "sideEffects": false,
41
- "scripts": {
42
- "build": "tsc -p tsconfig.json",
43
- "prepublishOnly": "pnpm build",
44
- "typecheck": "tsc -p tsconfig.json --noEmit",
45
- "lint": "eslint src --max-warnings=0",
46
- "test": "vitest run"
47
- },
48
42
  "publishConfig": {
49
43
  "access": "public",
50
44
  "registry": "https://registry.npmjs.org/",
@@ -54,11 +48,17 @@
54
48
  "node": ">=18"
55
49
  },
56
50
  "devDependencies": {
57
- "@livechat/config": "workspace:*",
58
- "@livechat/shared": "workspace:*",
59
- "@types/node": "catalog:",
60
- "typescript": "catalog:",
61
- "vitest": "catalog:"
51
+ "@types/node": "20.14.10",
52
+ "typescript": "5.6.2",
53
+ "vitest": "2.1.2",
54
+ "@livechat/config": "0.1.0",
55
+ "@livechat/shared": "0.1.0"
62
56
  },
63
- "dependencies": {}
64
- }
57
+ "dependencies": {},
58
+ "scripts": {
59
+ "build": "tsc -p tsconfig.build.json",
60
+ "typecheck": "tsc -p tsconfig.json --noEmit",
61
+ "lint": "eslint src --max-warnings=0",
62
+ "test": "vitest run"
63
+ }
64
+ }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=index.spec.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.spec.d.ts","sourceRoot":"","sources":["../src/index.spec.ts"],"names":[],"mappings":""}