idxs 0.0.1 → 0.0.2
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/dist/QueryBuilder.d.ts +233 -0
- package/dist/QueryBuilder.d.ts.map +1 -0
- package/dist/QueryBuilder.js +169 -0
- package/dist/QueryBuilder.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { type Abi, type AbiParameter, type AbiParameterToPrimitiveType, type ParseAbi } from 'abitype';
|
|
2
|
+
import { Kysely } from 'kysely';
|
|
3
|
+
import type * as IS from './IndexSupply.js';
|
|
4
|
+
declare module 'kysely' {
|
|
5
|
+
interface SelectQueryBuilder<O> {
|
|
6
|
+
execute(): Promise<O[] & {
|
|
7
|
+
cursor: string;
|
|
8
|
+
}>;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Standard Index Supply EVM tables.
|
|
13
|
+
*/
|
|
14
|
+
export declare namespace Tables {
|
|
15
|
+
/** Blocks table containing blockchain block data. */
|
|
16
|
+
type Blocks = {
|
|
17
|
+
/** Chain ID. */
|
|
18
|
+
chain: number;
|
|
19
|
+
/** Block number. */
|
|
20
|
+
num: number;
|
|
21
|
+
/** Block timestamp. */
|
|
22
|
+
timestamp: string;
|
|
23
|
+
/** Block size in bytes. */
|
|
24
|
+
size: number;
|
|
25
|
+
/** Gas limit for the block. */
|
|
26
|
+
gas_limit: string;
|
|
27
|
+
/** Gas used in the block. */
|
|
28
|
+
gas_used: string;
|
|
29
|
+
/** Block nonce. */
|
|
30
|
+
nonce: string;
|
|
31
|
+
/** Block hash. */
|
|
32
|
+
hash: string;
|
|
33
|
+
/** Receipts root hash. */
|
|
34
|
+
receipts_root: string;
|
|
35
|
+
/** State root hash. */
|
|
36
|
+
state_root: string;
|
|
37
|
+
/** Extra data. */
|
|
38
|
+
extra_data: string;
|
|
39
|
+
/** Miner address. */
|
|
40
|
+
miner: string;
|
|
41
|
+
};
|
|
42
|
+
/** Transactions table containing transaction data. */
|
|
43
|
+
type Txs = {
|
|
44
|
+
/** Chain ID. */
|
|
45
|
+
chain: number;
|
|
46
|
+
/** Block number. */
|
|
47
|
+
block_num: number;
|
|
48
|
+
/** Block timestamp. */
|
|
49
|
+
block_timestamp: string;
|
|
50
|
+
/** Transaction index in block. */
|
|
51
|
+
idx: number;
|
|
52
|
+
/** Transaction type. */
|
|
53
|
+
type: number;
|
|
54
|
+
/** Gas limit. */
|
|
55
|
+
gas: string;
|
|
56
|
+
/** Gas price. */
|
|
57
|
+
gas_price: string;
|
|
58
|
+
/** Transaction nonce. */
|
|
59
|
+
nonce: string;
|
|
60
|
+
/** Transaction hash. */
|
|
61
|
+
hash: string;
|
|
62
|
+
/** Sender address. */
|
|
63
|
+
from: string;
|
|
64
|
+
/** Recipient address. */
|
|
65
|
+
to: string;
|
|
66
|
+
/** Transaction input data. */
|
|
67
|
+
input: string;
|
|
68
|
+
/** Transaction value. */
|
|
69
|
+
value: string;
|
|
70
|
+
};
|
|
71
|
+
/** Logs table containing event log data. */
|
|
72
|
+
type Logs = {
|
|
73
|
+
/** Chain ID. */
|
|
74
|
+
chain: number;
|
|
75
|
+
/** Block number. */
|
|
76
|
+
block_num: number;
|
|
77
|
+
/** Block timestamp. */
|
|
78
|
+
block_timestamp: string;
|
|
79
|
+
/** Log index in block. */
|
|
80
|
+
log_idx: number;
|
|
81
|
+
/** Transaction hash. */
|
|
82
|
+
tx_hash: string;
|
|
83
|
+
/** Contract address that emitted the log. */
|
|
84
|
+
address: string;
|
|
85
|
+
/** Event topics (indexed parameters). */
|
|
86
|
+
topics: string[];
|
|
87
|
+
/** Event data (non-indexed parameters). */
|
|
88
|
+
data: string;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Database schema type containing all standard Index Supply tables.
|
|
93
|
+
*/
|
|
94
|
+
export type Database = {
|
|
95
|
+
blocks: Tables.Blocks;
|
|
96
|
+
txs: Tables.Txs;
|
|
97
|
+
logs: Tables.Logs;
|
|
98
|
+
};
|
|
99
|
+
export type QueryBuilder<rootAbi extends Abi | undefined = undefined> = Kysely<Database & (rootAbi extends Abi ? AbiToDatabase<rootAbi> : {})> & {
|
|
100
|
+
/**
|
|
101
|
+
* Sets the cursor position for pagination.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* const qb = QueryBuilder.from(is).atCursor('1-12345')
|
|
106
|
+
*
|
|
107
|
+
* // Or with object notation
|
|
108
|
+
* const qb = QueryBuilder.from(is).atCursor({ chainId: 1, blockNumber: 12345 })
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @param cursor - The cursor to start from.
|
|
112
|
+
* @returns A new QueryBuilder instance with the cursor set.
|
|
113
|
+
*/
|
|
114
|
+
atCursor: (cursor: QueryBuilder.Cursor) => Omit<QueryBuilder<rootAbi>, 'cursor'>;
|
|
115
|
+
/**
|
|
116
|
+
* Adds ABI definitions to enable querying custom event/function tables.
|
|
117
|
+
*
|
|
118
|
+
* @param abi - The ABI array to add.
|
|
119
|
+
* @returns A new QueryBuilder instance with the ABI added.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* const abi = [{ type: 'event', name: 'Transfer', inputs: [...] }] as const
|
|
124
|
+
* const qb = QueryBuilder.from(is).withAbi(abi)
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
withAbi: <const abi extends Abi>(abi: abi) => QueryBuilder<rootAbi extends Abi ? [...rootAbi, ...abi] : abi>;
|
|
128
|
+
/**
|
|
129
|
+
* Adds human-readable signatures to enable querying custom event/function tables.
|
|
130
|
+
*
|
|
131
|
+
* @param signatures - Array of human-readable event/function signatures.
|
|
132
|
+
* @returns A new QueryBuilder instance with the signatures added.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* const qb = QueryBuilder.from(is).withSignatures([
|
|
137
|
+
* 'event Transfer(address indexed from, address indexed to, uint256 value)',
|
|
138
|
+
* 'event Approval(address indexed owner, address indexed spender, uint256 value)',
|
|
139
|
+
* ])
|
|
140
|
+
*
|
|
141
|
+
* const transfers = await qb
|
|
142
|
+
* .selectFrom('transfer')
|
|
143
|
+
* .select(['from', 'to', 'value'])
|
|
144
|
+
* .execute()
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
withSignatures: <const signatures extends readonly string[]>(signatures: signatures) => QueryBuilder<rootAbi extends Abi ? [...rootAbi, ...ParseAbi<signatures>] : ParseAbi<signatures>>;
|
|
148
|
+
};
|
|
149
|
+
export declare namespace QueryBuilder {
|
|
150
|
+
/** Cursor type for pagination. Can be a string or an object with `chainId` and `blockNumber`. */
|
|
151
|
+
type Cursor = IS.Cursor;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Creates a [Kysely-based](https://kysely.dev) QueryBuilder instance from an Index Supply client.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```ts
|
|
158
|
+
* import { IndexSupply, QueryBuilder } from 'idxs'
|
|
159
|
+
*
|
|
160
|
+
* const is = IndexSupply.create({ apiKey: 'your-api-key' })
|
|
161
|
+
* const qb = QueryBuilder.from(is)
|
|
162
|
+
*
|
|
163
|
+
* // Query transactions
|
|
164
|
+
* const txs = await qb
|
|
165
|
+
* .selectFrom('txs')
|
|
166
|
+
* .select(['hash', 'from', 'to', 'value'])
|
|
167
|
+
* .where('chain', '=', 1)
|
|
168
|
+
* .limit(10)
|
|
169
|
+
* .execute()
|
|
170
|
+
*
|
|
171
|
+
* console.log(txs) // [{ hash: '0x...', from: '0x...', to: '0x...', value: '...' }, ...]
|
|
172
|
+
* console.log(txs.cursor) // '1-12345678'
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* // Query with event signatures
|
|
178
|
+
* const transfers = await qb
|
|
179
|
+
* .withSignatures(['event Transfer(address indexed from, address indexed to, uint256 value)'])
|
|
180
|
+
* .selectFrom('transfer')
|
|
181
|
+
* .select(['from', 'to', 'value'])
|
|
182
|
+
* .where('chain', '=', 1)
|
|
183
|
+
* .limit(100)
|
|
184
|
+
* .execute()
|
|
185
|
+
* ```
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```ts
|
|
189
|
+
* // Pagination with cursor
|
|
190
|
+
*
|
|
191
|
+
* let qb = QueryBuilder.from(is)
|
|
192
|
+
*
|
|
193
|
+
* let cursor: string | undefined
|
|
194
|
+
* while (true) {
|
|
195
|
+
* if (cursor) qb = qb.atCursor(cursor)
|
|
196
|
+
* const txs = await qb.selectFrom('txs').select(['hash']).limit(100).execute()
|
|
197
|
+
* if (txs.length === 0) break
|
|
198
|
+
* cursor = txs.cursor
|
|
199
|
+
* // Process txs...
|
|
200
|
+
* }
|
|
201
|
+
* ```
|
|
202
|
+
*
|
|
203
|
+
* @param options - The Index Supply client instance (must have `fetch` and `live` methods).
|
|
204
|
+
* @returns A new Kysely-based QueryBuilder instance.
|
|
205
|
+
*/
|
|
206
|
+
export declare function from(options: from.Options): QueryBuilder;
|
|
207
|
+
export declare namespace from {
|
|
208
|
+
/** Options for creating a QueryBuilder. */
|
|
209
|
+
type Options = Pick<IS.IS, 'fetch' | 'live'>;
|
|
210
|
+
/** Return type of the `from` function. */
|
|
211
|
+
type ReturnValue = QueryBuilder;
|
|
212
|
+
}
|
|
213
|
+
/** @internal */
|
|
214
|
+
type RemoveDuplicates<abi extends Abi, seen extends string = never> = abi extends readonly [
|
|
215
|
+
infer first,
|
|
216
|
+
...infer rest extends Abi
|
|
217
|
+
] ? first extends {
|
|
218
|
+
name: infer name extends string;
|
|
219
|
+
} ? Lowercase<name> extends seen ? RemoveDuplicates<rest, seen> : readonly [first, ...RemoveDuplicates<rest, seen | Lowercase<name>>] : RemoveDuplicates<rest, seen> : readonly [];
|
|
220
|
+
/** @internal */
|
|
221
|
+
type AbiToDatabase<abiOrSignatures extends Abi | readonly string[], abi extends Abi = RemoveDuplicates<abiOrSignatures extends readonly string[] ? ParseAbi<abiOrSignatures> : abiOrSignatures>> = {
|
|
222
|
+
[key in abi[number] as key extends {
|
|
223
|
+
name: infer name extends string;
|
|
224
|
+
} ? Lowercase<name> : never]: key extends {
|
|
225
|
+
inputs: infer inputs extends readonly AbiParameter[];
|
|
226
|
+
type: 'function' | 'event';
|
|
227
|
+
} ? AbiParameterToPrimitiveType<{
|
|
228
|
+
components: inputs;
|
|
229
|
+
type: 'tuple';
|
|
230
|
+
}> & Tables.Logs : never;
|
|
231
|
+
};
|
|
232
|
+
export {};
|
|
233
|
+
//# sourceMappingURL=QueryBuilder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueryBuilder.d.ts","sourceRoot":"","sources":["../src/QueryBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,GAAG,EACR,KAAK,YAAY,EACjB,KAAK,2BAA2B,EAEhC,KAAK,QAAQ,EAEd,MAAM,SAAS,CAAA;AAOhB,OAAO,EAAE,MAAM,EAAgE,MAAM,QAAQ,CAAA;AAC7F,OAAO,KAAK,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAE3C,OAAO,QAAQ,QAAQ,CAAC;IAEtB,UAAU,kBAAkB,CAAC,CAAC;QAC5B,OAAO,IAAI,OAAO,CAAC,CAAC,EAAE,GAAG;YAAE,MAAM,EAAE,MAAM,CAAA;SAAE,CAAC,CAAA;KAC7C;CACF;AAED;;GAEG;AACH,yBAAiB,MAAM,CAAC;IACtB,qDAAqD;IACrD,KAAY,MAAM,GAAG;QACnB,gBAAgB;QAChB,KAAK,EAAE,MAAM,CAAA;QACb,oBAAoB;QACpB,GAAG,EAAE,MAAM,CAAA;QACX,uBAA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AAC,MAAM,CAAC,IAAI,GAAG,SAAS;QACjC,IAAI,EAAE,MAAM,IAAI,SAAS,MAAM,CAAA;KAChC,GACG,SAAS,CAAC,IAAI,CAAC,GACf,KAAK,GAAG,GAAG,SAAS;QACtB,MAAM,EAAE,MAAM,MAAM,SAAS,SAAS,YAAY,EAAE,CAAA;QACpD,IAAI,EAAE,UAAU,GAAG,OAAO,CAAA;KAC3B,GACG,2BAA2B,CAAC;QAC1B,UAAU,EAAE,MAAM,CAAA;QAClB,IAAI,EAAE,OAAO,CAAA;KACd,CAAC,GACA,MAAM,CAAC,IAAI,GACb,KAAK;CACV,CAAA"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { formatAbi, parseAbiItem, } from 'abitype';
|
|
2
|
+
import { Kysely, PostgresAdapter, PostgresIntrospector, PostgresQueryCompiler } from 'kysely';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a [Kysely-based](https://kysely.dev) QueryBuilder instance from an Index Supply client.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { IndexSupply, QueryBuilder } from 'idxs'
|
|
9
|
+
*
|
|
10
|
+
* const is = IndexSupply.create({ apiKey: 'your-api-key' })
|
|
11
|
+
* const qb = QueryBuilder.from(is)
|
|
12
|
+
*
|
|
13
|
+
* // Query transactions
|
|
14
|
+
* const txs = await qb
|
|
15
|
+
* .selectFrom('txs')
|
|
16
|
+
* .select(['hash', 'from', 'to', 'value'])
|
|
17
|
+
* .where('chain', '=', 1)
|
|
18
|
+
* .limit(10)
|
|
19
|
+
* .execute()
|
|
20
|
+
*
|
|
21
|
+
* console.log(txs) // [{ hash: '0x...', from: '0x...', to: '0x...', value: '...' }, ...]
|
|
22
|
+
* console.log(txs.cursor) // '1-12345678'
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* // Query with event signatures
|
|
28
|
+
* const transfers = await qb
|
|
29
|
+
* .withSignatures(['event Transfer(address indexed from, address indexed to, uint256 value)'])
|
|
30
|
+
* .selectFrom('transfer')
|
|
31
|
+
* .select(['from', 'to', 'value'])
|
|
32
|
+
* .where('chain', '=', 1)
|
|
33
|
+
* .limit(100)
|
|
34
|
+
* .execute()
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* // Pagination with cursor
|
|
40
|
+
*
|
|
41
|
+
* let qb = QueryBuilder.from(is)
|
|
42
|
+
*
|
|
43
|
+
* let cursor: string | undefined
|
|
44
|
+
* while (true) {
|
|
45
|
+
* if (cursor) qb = qb.atCursor(cursor)
|
|
46
|
+
* const txs = await qb.selectFrom('txs').select(['hash']).limit(100).execute()
|
|
47
|
+
* if (txs.length === 0) break
|
|
48
|
+
* cursor = txs.cursor
|
|
49
|
+
* // Process txs...
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @param options - The Index Supply client instance (must have `fetch` and `live` methods).
|
|
54
|
+
* @returns A new Kysely-based QueryBuilder instance.
|
|
55
|
+
*/
|
|
56
|
+
export function from(options) {
|
|
57
|
+
let cursor;
|
|
58
|
+
const signatures = [];
|
|
59
|
+
function inner(o = {}) {
|
|
60
|
+
cursor ??= o.cursor;
|
|
61
|
+
signatures.push(...(o.signatures ?? []));
|
|
62
|
+
const kysely = new Kysely({
|
|
63
|
+
dialect: {
|
|
64
|
+
createAdapter: () => new PostgresAdapter(),
|
|
65
|
+
createDriver: () => new Driver({ ...options, cursor, signatures }),
|
|
66
|
+
createIntrospector: (db) => new PostgresIntrospector(db),
|
|
67
|
+
createQueryCompiler: () => new PostgresQueryCompiler(),
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
return {
|
|
71
|
+
atCursor(cursor) {
|
|
72
|
+
return inner({ cursor });
|
|
73
|
+
},
|
|
74
|
+
selectFrom: kysely.selectFrom.bind(kysely),
|
|
75
|
+
withAbi(abi) {
|
|
76
|
+
return inner({ signatures: formatAbi(abi) });
|
|
77
|
+
},
|
|
78
|
+
withSignatures(signatures) {
|
|
79
|
+
return inner({ signatures });
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
return inner();
|
|
84
|
+
}
|
|
85
|
+
/** @internal */
|
|
86
|
+
class Driver {
|
|
87
|
+
options;
|
|
88
|
+
constructor(options) {
|
|
89
|
+
this.options = options;
|
|
90
|
+
}
|
|
91
|
+
async init() {
|
|
92
|
+
// Noop
|
|
93
|
+
}
|
|
94
|
+
async acquireConnection() {
|
|
95
|
+
return new Connection(this.options);
|
|
96
|
+
}
|
|
97
|
+
async beginTransaction() {
|
|
98
|
+
throw new Error('Transactions are not supported');
|
|
99
|
+
}
|
|
100
|
+
async commitTransaction() {
|
|
101
|
+
throw new Error('Transactions are not supported');
|
|
102
|
+
}
|
|
103
|
+
async rollbackTransaction() {
|
|
104
|
+
throw new Error('Transactions are not supported');
|
|
105
|
+
}
|
|
106
|
+
async releaseConnection() {
|
|
107
|
+
// Noop
|
|
108
|
+
}
|
|
109
|
+
async destroy() {
|
|
110
|
+
// Noop
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/** @internal */
|
|
114
|
+
class Connection {
|
|
115
|
+
options;
|
|
116
|
+
constructor(options) {
|
|
117
|
+
this.options = options;
|
|
118
|
+
}
|
|
119
|
+
async executeQuery(compiledQuery) {
|
|
120
|
+
const { query, signatures } = this.prepareQuery(compiledQuery);
|
|
121
|
+
const result = await this.options.fetch({
|
|
122
|
+
cursor: this.options.cursor,
|
|
123
|
+
query,
|
|
124
|
+
signatures: signatures,
|
|
125
|
+
});
|
|
126
|
+
return this.parseResult(result);
|
|
127
|
+
}
|
|
128
|
+
async *streamQuery(compiledQuery) {
|
|
129
|
+
const { query, signatures } = this.prepareQuery(compiledQuery);
|
|
130
|
+
// Use the live streaming endpoint
|
|
131
|
+
for await (const result of this.options.live({
|
|
132
|
+
cursor: this.options.cursor,
|
|
133
|
+
query,
|
|
134
|
+
signatures: signatures,
|
|
135
|
+
}))
|
|
136
|
+
yield this.parseResult(result);
|
|
137
|
+
}
|
|
138
|
+
parseResult(result) {
|
|
139
|
+
const rows = result.rows;
|
|
140
|
+
Object.defineProperty(rows, 'cursor', {
|
|
141
|
+
value: result.cursor,
|
|
142
|
+
enumerable: false,
|
|
143
|
+
writable: false,
|
|
144
|
+
});
|
|
145
|
+
return { rows };
|
|
146
|
+
}
|
|
147
|
+
prepareQuery(compiledQuery) {
|
|
148
|
+
let query = compiledQuery.sql;
|
|
149
|
+
compiledQuery.parameters.forEach((param, i) => {
|
|
150
|
+
const placeholder = `$${i + 1}`;
|
|
151
|
+
query = query.replaceAll(placeholder, String(param));
|
|
152
|
+
});
|
|
153
|
+
const signatures = [];
|
|
154
|
+
for (const signature of this.options.signatures ?? []) {
|
|
155
|
+
const abiItem = parseAbiItem(signature);
|
|
156
|
+
if (!('name' in abiItem))
|
|
157
|
+
continue;
|
|
158
|
+
const signatureName = abiItem.name.toLowerCase();
|
|
159
|
+
const regex = new RegExp(`(from|join) "(${signatureName})"`, 'gi');
|
|
160
|
+
const matches = regex.test(query);
|
|
161
|
+
if (!matches)
|
|
162
|
+
continue;
|
|
163
|
+
query = query.replace(regex, `$1 $2`);
|
|
164
|
+
signatures.push(signature);
|
|
165
|
+
}
|
|
166
|
+
return { query, signatures };
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=QueryBuilder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueryBuilder.js","sourceRoot":"","sources":["../src/QueryBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,SAAS,EAET,YAAY,GACb,MAAM,SAAS,CAAA;AAOhB,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,QAAQ,CAAA;AAuK7F;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;YACrC,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;QAC5B,CAAC;QAED,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,CAAA;IAC9B,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAC/C,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAC/C,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,KAAK,YAAY,MAAM,mBAAmB,CAAA;AACjD,OAAO,KAAK,EAAE,MAAM,mBAAmB,CAAA"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAC/C,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAC/C,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,KAAK,YAAY,MAAM,mBAAmB,CAAA;AACjD,OAAO,KAAK,EAAE,MAAM,mBAAmB,CAAA"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED