Skip to content

Commit

Permalink
Add support for setting name ttl and validity
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsmedley committed Sep 18, 2024
1 parent 1b6fa88 commit df19fef
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
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

0 comments on commit df19fef

Please sign in to comment.