diff --git a/src/nameManager.js b/src/nameManager.js index 54a02df..7cd8329 100644 --- a/src/nameManager.js +++ b/src/nameManager.js @@ -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. */ /** @@ -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) { @@ -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) { @@ -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); } diff --git a/test/nameManager.spec.cjs b/test/nameManager.spec.cjs index 283f7fd..7d7d40b 100644 --- a/test/nameManager.spec.cjs +++ b/test/nameManager.spec.cjs @@ -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); }); diff --git a/test/nameManager.spec.mjs b/test/nameManager.spec.mjs index c48fe2c..403a5ce 100644 --- a/test/nameManager.spec.mjs +++ b/test/nameManager.spec.mjs @@ -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);