@ethersphere/bee-js 13.1.1-upcoming.gebd4706 → 13.2.0-upcoming.g65e5d19
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/README.md +1 -1
- package/dist/cjs/api/stake.js +1 -1
- package/dist/cjs/manifest/manifest.js +1 -3
- package/dist/cjs/modules/chequebook.js +2 -2
- package/dist/cjs/modules/stake.js +8 -2
- package/dist/cjs/types/schema/stake.js +4 -0
- package/dist/cjs/utils/error.js +3 -0
- package/dist/cjs/utils/http.js +18 -6
- package/dist/codemod/transforms/v13.js +25 -16
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/api/stake.js +1 -1
- package/dist/mjs/manifest/manifest.js +1 -3
- package/dist/mjs/modules/chequebook.js +2 -2
- package/dist/mjs/modules/stake.js +12 -2
- package/dist/mjs/types/schema/stake.js +4 -1
- package/dist/mjs/utils/error.js +3 -0
- package/dist/mjs/utils/http.js +17 -8
- package/dist/types/api/chunk.d.ts +2 -2
- package/dist/types/api/soc.d.ts +2 -2
- package/dist/types/api/stake.d.ts +4 -1
- package/dist/types/chunk/soc.d.ts +4 -4
- package/dist/types/modules/chunk.d.ts +2 -2
- package/dist/types/modules/messaging.d.ts +2 -2
- package/dist/types/modules/stake.d.ts +3 -1
- package/dist/types/types/index.d.ts +7 -1
- package/dist/types/types/schema/stake.d.ts +1 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -69,7 +69,7 @@ types. Where `NumberString` is present, `number` is disallowed in order to avoid
|
|
|
69
69
|
### Byte primitives
|
|
70
70
|
|
|
71
71
|
All the classes below extend `Bytes`, therefore the following methods are available on all of them: `toUint8Array`,
|
|
72
|
-
`toHex`, `toBase64`, `toBase32`, `toUtf8`, `
|
|
72
|
+
`toHex`, `toBase64`, `toBase32`, `toUtf8`, `parsePayloadAsJson`, `static keccak256`, `static fromUtf8`.
|
|
73
73
|
|
|
74
74
|
The `toString` method uses `toHex`.
|
|
75
75
|
|
package/dist/cjs/api/stake.js
CHANGED
|
@@ -18,7 +18,7 @@ async function getStake(requestOptions) {
|
|
|
18
18
|
responseType: 'json',
|
|
19
19
|
url: STAKE_ENDPOINT,
|
|
20
20
|
});
|
|
21
|
-
return stake_1.GetStakeResponse.parse(response.data)
|
|
21
|
+
return stake_1.GetStakeResponse.parse(response.data);
|
|
22
22
|
}
|
|
23
23
|
async function getWithdrawableStake(requestOptions) {
|
|
24
24
|
const response = await (0, http_1.http)(requestOptions, {
|
|
@@ -43,9 +43,7 @@ async function loadRecursively(node, bee, options, requestOptions) {
|
|
|
43
43
|
};
|
|
44
44
|
}
|
|
45
45
|
const data = (await bee.data.download(fork.node.selfAddress, downloadOptions, requestOptions)).toUint8Array();
|
|
46
|
-
|
|
47
|
-
fork.node.targetAddress = loaded.targetAddress;
|
|
48
|
-
fork.node.forks = loaded.forks;
|
|
46
|
+
fork.node.hydrate(data);
|
|
49
47
|
fork.node.path = fork.prefix;
|
|
50
48
|
fork.node.parent = node;
|
|
51
49
|
await loadRecursively(fork.node, bee, options, requestOptions);
|
|
@@ -71,7 +71,7 @@ class Chequebook {
|
|
|
71
71
|
*/
|
|
72
72
|
async deposit(amount, gasPrice, requestOptions) {
|
|
73
73
|
const amountString = amount instanceof tokens_1.BZZ ? amount.toPLURString() : (0, type_1.asNumberString)(amount, { min: 1n, name: 'amount' });
|
|
74
|
-
const gasPriceString = gasPrice ? (0, type_1.asNumberString)(
|
|
74
|
+
const gasPriceString = gasPrice ? (0, type_1.asNumberString)(gasPrice, { min: 0n, name: 'gasPrice' }) : undefined;
|
|
75
75
|
return api.depositTokens(this.context.getRequestOptionsForCall(requestOptions), amountString, gasPriceString);
|
|
76
76
|
}
|
|
77
77
|
/**
|
|
@@ -83,7 +83,7 @@ class Chequebook {
|
|
|
83
83
|
*/
|
|
84
84
|
async withdraw(amount, gasPrice, requestOptions) {
|
|
85
85
|
const amountString = amount instanceof tokens_1.BZZ ? amount.toPLURString() : (0, type_1.asNumberString)(amount, { min: 1n, name: 'amount' });
|
|
86
|
-
const gasPriceString = gasPrice ? (0, type_1.asNumberString)(
|
|
86
|
+
const gasPriceString = gasPrice ? (0, type_1.asNumberString)(gasPrice, { min: 0n, name: 'gasPrice' }) : undefined;
|
|
87
87
|
return api.withdrawTokens(this.context.getRequestOptionsForCall(requestOptions), amountString, gasPriceString);
|
|
88
88
|
}
|
|
89
89
|
}
|
|
@@ -54,7 +54,13 @@ class Stake {
|
|
|
54
54
|
* @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
|
|
55
55
|
*/
|
|
56
56
|
async get(requestOptions) {
|
|
57
|
-
|
|
57
|
+
const { stakedAmount } = await stakeApi.getStake(this.context.getRequestOptionsForCall(requestOptions));
|
|
58
|
+
return stakedAmount;
|
|
59
|
+
}
|
|
60
|
+
/** Gets the minimum next deposit, or `null` when Bee cannot determine it (e.g. the price oracle is unavailable). */
|
|
61
|
+
async getMinimumDeposit(requestOptions) {
|
|
62
|
+
const { minimumDeposit } = await stakeApi.getStake(this.context.getRequestOptionsForCall(requestOptions));
|
|
63
|
+
return minimumDeposit;
|
|
58
64
|
}
|
|
59
65
|
/**
|
|
60
66
|
* Gets the amount of withdrawable staked BZZ.
|
|
@@ -65,7 +71,7 @@ class Stake {
|
|
|
65
71
|
return stakeApi.getWithdrawableStake(this.context.getRequestOptionsForCall(requestOptions));
|
|
66
72
|
}
|
|
67
73
|
/**
|
|
68
|
-
* Stakes the given amount of BZZ.
|
|
74
|
+
* Stakes the given amount of BZZ. Must be at least {@link getMinimumDeposit}, otherwise Bee rejects it with 400.
|
|
69
75
|
*
|
|
70
76
|
* Be aware that staked BZZ tokens can **not** be withdrawn.
|
|
71
77
|
*
|
|
@@ -7,6 +7,10 @@ const tokens_1 = require("../../utils/tokens");
|
|
|
7
7
|
const type_1 = require("../../utils/type");
|
|
8
8
|
exports.GetStakeResponse = zod_1.z.object({
|
|
9
9
|
stakedAmount: zod_1.z.string().transform(s => tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(s, { name: 'stakedAmount' }))),
|
|
10
|
+
minimumDeposit: zod_1.z
|
|
11
|
+
.string()
|
|
12
|
+
.nullish()
|
|
13
|
+
.transform(s => (s ? tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(s, { name: 'minimumDeposit' })) : null)),
|
|
10
14
|
});
|
|
11
15
|
exports.GetWithdrawableStakeResponse = zod_1.z.object({
|
|
12
16
|
withdrawableAmount: zod_1.z.string().transform(s => tokens_1.BZZ.fromPLUR((0, type_1.asNumberString)(s, { name: 'withdrawableAmount' }))),
|
package/dist/cjs/utils/error.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.BeeResponseError = exports.BeeArgumentError = exports.BeeError = void 0;
|
|
|
4
4
|
class BeeError extends Error {
|
|
5
5
|
constructor(message) {
|
|
6
6
|
super(message);
|
|
7
|
+
this.name = 'BeeError';
|
|
7
8
|
}
|
|
8
9
|
}
|
|
9
10
|
exports.BeeError = BeeError;
|
|
@@ -11,6 +12,7 @@ class BeeArgumentError extends BeeError {
|
|
|
11
12
|
constructor(message, value) {
|
|
12
13
|
super(message);
|
|
13
14
|
this.value = value;
|
|
15
|
+
this.name = 'BeeArgumentError';
|
|
14
16
|
}
|
|
15
17
|
}
|
|
16
18
|
exports.BeeArgumentError = BeeArgumentError;
|
|
@@ -22,6 +24,7 @@ class BeeResponseError extends BeeError {
|
|
|
22
24
|
this.responseBody = responseBody;
|
|
23
25
|
this.status = status;
|
|
24
26
|
this.statusText = statusText;
|
|
27
|
+
this.name = 'BeeResponseError';
|
|
25
28
|
this.response = { status, data: responseBody, statusText };
|
|
26
29
|
}
|
|
27
30
|
}
|
package/dist/cjs/utils/http.js
CHANGED
|
@@ -53,7 +53,7 @@ async function http(options, config) {
|
|
|
53
53
|
debug(`${method} ${url}`, { headers: requestConfig.headers, params: requestConfig.params });
|
|
54
54
|
const res = await fetch(url, requestConfig);
|
|
55
55
|
if (!res.ok)
|
|
56
|
-
await throwHttpError(method, url, res
|
|
56
|
+
await throwHttpError(method, url, res);
|
|
57
57
|
return toBeeResponse(res, requestConfig.responseType);
|
|
58
58
|
}
|
|
59
59
|
catch (e) {
|
|
@@ -87,11 +87,23 @@ function attachBody(config) {
|
|
|
87
87
|
config.headers = { 'content-type': ct, ...config.headers };
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
|
-
async function throwHttpError(method, url, res
|
|
91
|
-
const
|
|
92
|
-
const bodyMsg = typeof
|
|
93
|
-
const message = bodyMsg
|
|
94
|
-
throw new index_1.BeeResponseError(method, url, message,
|
|
90
|
+
async function throwHttpError(method, url, res) {
|
|
91
|
+
const data = await readErrorBody(res);
|
|
92
|
+
const bodyMsg = typeof data === 'string' ? data : JSON.stringify(data);
|
|
93
|
+
const message = bodyMsg ? `${res.statusText}: ${bodyMsg}` : res.statusText;
|
|
94
|
+
throw new index_1.BeeResponseError(method, url, message, data, res.status, res.statusText);
|
|
95
|
+
}
|
|
96
|
+
async function readErrorBody(res) {
|
|
97
|
+
const raw = await res.text().catch(() => '');
|
|
98
|
+
if (!raw) {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
try {
|
|
102
|
+
return JSON.parse(raw);
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return raw;
|
|
106
|
+
}
|
|
95
107
|
}
|
|
96
108
|
function toBeeError(err, method, url) {
|
|
97
109
|
if (err.name === 'AbortError') {
|
|
@@ -171,14 +171,19 @@ const METHOD_MAP = {
|
|
|
171
171
|
function transform(sourceFile, checker) {
|
|
172
172
|
const source = sourceFile.text;
|
|
173
173
|
const isNewBee = (node) => !!node && ts.isNewExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === 'Bee';
|
|
174
|
-
|
|
174
|
+
const typeNameText = (name) => (ts.isIdentifier(name) ? name.text : name.right.text);
|
|
175
|
+
// Does the type annotation mention `Bee`, plain or wrapped (`Bee | null`, `Mocked<Bee>`,
|
|
176
|
+
// `jest.Mocked<Bee> & Extra`, …)? Arrays and tuples are left out — they don't expose Bee's members.
|
|
175
177
|
const typeMentionsBee = (type) => {
|
|
176
178
|
if (!type)
|
|
177
179
|
return false;
|
|
178
|
-
if (ts.
|
|
179
|
-
return
|
|
180
|
-
if (ts.isUnionTypeNode(type))
|
|
180
|
+
if (ts.isParenthesizedTypeNode(type))
|
|
181
|
+
return typeMentionsBee(type.type);
|
|
182
|
+
if (ts.isUnionTypeNode(type) || ts.isIntersectionTypeNode(type))
|
|
181
183
|
return type.types.some(typeMentionsBee);
|
|
184
|
+
if (ts.isTypeReferenceNode(type)) {
|
|
185
|
+
return typeNameText(type.typeName) === 'Bee' || (type.typeArguments ?? []).some(typeMentionsBee);
|
|
186
|
+
}
|
|
182
187
|
return false;
|
|
183
188
|
};
|
|
184
189
|
// Syntactic pass — identify Bee bindings from annotations and `new Bee(...)` alone, so
|
|
@@ -214,10 +219,17 @@ function transform(sourceFile, checker) {
|
|
|
214
219
|
ts.forEachChild(node, collect);
|
|
215
220
|
};
|
|
216
221
|
collect(sourceFile);
|
|
222
|
+
const isBeeJsFile = (file) => /[/\\]bee-js[/\\]/.test(file) || /[/\\]bee\.(d\.)?ts$/.test(file);
|
|
223
|
+
// A wrapper type (`Mocked<Bee>`, `Partial<Bee>`, …) has no Bee symbol of its own, but the
|
|
224
|
+
// members it maps over still point back at the Bee class they came from.
|
|
225
|
+
const hasBeeMember = (type, memberName) => (type.getProperty(memberName)?.getDeclarations() ?? []).some(decl => {
|
|
226
|
+
const parent = decl.parent;
|
|
227
|
+
return (ts.isClassDeclaration(parent) && parent.name?.text === 'Bee' && isBeeJsFile(parent.getSourceFile().fileName));
|
|
228
|
+
});
|
|
217
229
|
// A receiver is a Bee if identified syntactically (above) OR resolved by the type checker
|
|
218
230
|
// to the `Bee` class declared by bee-js — the latter additionally catches imported/shared
|
|
219
|
-
// instances
|
|
220
|
-
const isBeeReceiver = (expr) => {
|
|
231
|
+
// instances, factory calls (`getBee()`) and mock wrappers when the project's types resolve.
|
|
232
|
+
const isBeeReceiver = (expr, memberName) => {
|
|
221
233
|
if (ts.isIdentifier(expr) && beeNames.has(expr.text))
|
|
222
234
|
return true;
|
|
223
235
|
if (ts.isPropertyAccessExpression(expr) &&
|
|
@@ -225,17 +237,14 @@ function transform(sourceFile, checker) {
|
|
|
225
237
|
beeFields.has(expr.name.text)) {
|
|
226
238
|
return true;
|
|
227
239
|
}
|
|
228
|
-
const
|
|
240
|
+
const type = checker.getTypeAtLocation(expr);
|
|
241
|
+
if (memberName && hasBeeMember(type, memberName))
|
|
242
|
+
return true;
|
|
243
|
+
const symbol = type.getSymbol();
|
|
229
244
|
if (!symbol || symbol.getName() !== 'Bee') {
|
|
230
245
|
return false;
|
|
231
246
|
}
|
|
232
|
-
return (symbol.getDeclarations() ?? []).some(decl =>
|
|
233
|
-
if (!ts.isClassDeclaration(decl)) {
|
|
234
|
-
return false;
|
|
235
|
-
}
|
|
236
|
-
const file = decl.getSourceFile().fileName;
|
|
237
|
-
return /[/\\]bee-js[/\\]/.test(file) || /[/\\]bee\.(d\.)?ts$/.test(file);
|
|
238
|
-
});
|
|
247
|
+
return (symbol.getDeclarations() ?? []).some(decl => ts.isClassDeclaration(decl) && isBeeJsFile(decl.getSourceFile().fileName));
|
|
239
248
|
};
|
|
240
249
|
// ChunkBuilder.hash() now returns a Reference, not a raw Uint8Array.
|
|
241
250
|
const isChunkBuilderReceiver = (expr) => {
|
|
@@ -434,7 +443,7 @@ function transform(sourceFile, checker) {
|
|
|
434
443
|
// Any `<bee>.<method>` access — called or not — so bare method references migrate too.
|
|
435
444
|
if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.name)) {
|
|
436
445
|
const mapping = METHOD_MAP[node.name.text];
|
|
437
|
-
if (mapping && isBeeReceiver(node.expression)) {
|
|
446
|
+
if (mapping && isBeeReceiver(node.expression, node.name.text)) {
|
|
438
447
|
// Preserve optional chaining on the receiver: `bee?.uploadData` → `bee?.data.upload`.
|
|
439
448
|
const separator = node.questionDotToken ? '?.' : '.';
|
|
440
449
|
replacements.push({
|
|
@@ -468,7 +477,7 @@ function transform(sourceFile, checker) {
|
|
|
468
477
|
text: `${quote}${mapping.newName}${quote}`,
|
|
469
478
|
});
|
|
470
479
|
}
|
|
471
|
-
else if (mapping && isBeeReceiver(receiverArg)) {
|
|
480
|
+
else if (mapping && isBeeReceiver(receiverArg, methodArg.text)) {
|
|
472
481
|
replacements.push({
|
|
473
482
|
start: receiverArg.getStart(sourceFile),
|
|
474
483
|
end: receiverArg.getEnd(),
|