Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting name ttl and validity #12

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions src/nameManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class NameManager {
/**
* @typedef {Object} nameOptions
* @property {boolean} [enabled] Whether the name is enabled or not.
* @property {number} [ttl] Seconds for the Browser and IPFS Nodes to Cache the IPNS Record
* @property {number} [validity] Seconds for the DHT record to be valid.
*/

/**
Expand All @@ -68,13 +70,20 @@ class NameManager {
},
) {
try {
let createData = {
label,
cid,
enabled: options?.enabled !== false,
}
if (options?.validity) {
createData.validity = options.validity;
}
if (options?.ttl) {
createData.ttl = options.ttl;
}
const createResponse = await this.#client.request({
method: "POST",
data: {
label,
cid,
enabled: options?.enabled !== false,
},
data: createData,
});
return createResponse.data;
} catch (err) {
Expand Down Expand Up @@ -107,14 +116,21 @@ class NameManager {
},
) {
try {
let importData = {
label,
cid,
network_private_key: privateKey,
enabled: options?.enabled !== false,
}
if (options?.validity) {
importData.validity = options.validity;
}
if (options?.ttl) {
importData.ttl = options.ttl;
}
const importResponse = await this.#client.request({
method: "POST",
data: {
label,
cid,
network_private_key: privateKey,
enabled: options?.enabled !== false,
},
data: importData,
});
return importResponse.data;
} catch (err) {
Expand All @@ -138,6 +154,12 @@ class NameManager {
const updateOptions = {
cid,
};
if (options?.validity) {
updateOptions.validity = options.validity;
}
if (options?.ttl) {
updateOptions.ttl = options.ttl;
}
if (options?.enabled) {
updateOptions.enabled = Boolean(options.enabled);
}
Expand Down
7 changes: 6 additions & 1 deletion test/nameManager.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ test("create name", async () => {
process.env.TEST_NAME_KEY || process.env.TEST_KEY,
process.env.TEST_NAME_SECRET || process.env.TEST_SECRET,
),
createdName = await nameManager.create(testNameLabel, TEST_CID);
createdName = await nameManager.create(testNameLabel, TEST_CID, {
ttl: 9000,
validity: 86400
});
await nameManager.delete(testNameLabel);
assert.strictEqual(createdName.label, testNameLabel);
assert.strictEqual(createdName.ttl, 9000);
assert.strictEqual(createdName.validity, 86400);
assert.strictEqual(createdName.cid, TEST_CID);
});

Expand Down
5 changes: 4 additions & 1 deletion test/nameManager.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ test("create name", async () => {
process.env.TEST_NAME_KEY || process.env.TEST_KEY,
process.env.TEST_NAME_SECRET || process.env.TEST_SECRET,
),
createdName = await nameManager.create(testNameLabel, TEST_CID);
createdName = await nameManager.create(testNameLabel, TEST_CID, {
ttl: 9000,
validity: 86400
});
await nameManager.delete(testNameLabel);
assert.strictEqual(createdName.label, testNameLabel);
assert.strictEqual(createdName.cid, TEST_CID);
Expand Down
Loading