diff --git a/examples/6-hashlock-transfer.http b/examples/6-hashlock-transfer.http index 159dfdc..9cd1d47 100644 --- a/examples/6-hashlock-transfer.http +++ b/examples/6-hashlock-transfer.http @@ -14,7 +14,7 @@ Content-Type: application/json ### -# Create status of corresponding hashlock transfer +# Get status of corresponding hashlock transfer GET http://localhost:5040/hashlock-status/0x7c0023168317e7010af1e8f5d583d2990bd0f7141f2068a9269ca92d9128c95a/0x0000000000000000000000000000000000000000/indra53QJ6exujj3pQQ8BPuTDfQqBXvpyzkz8uS4fwbNMtMZKQZWuUS ### diff --git a/src/helpers/utilities.ts b/src/helpers/utilities.ts index 8c13d52..13770a5 100644 --- a/src/helpers/utilities.ts +++ b/src/helpers/utilities.ts @@ -39,7 +39,7 @@ export function verifyType(value: any, type: string): boolean { } } -export async function requireParam(obj: any, param: string, type = "string"): Promise { +export function requireParam(obj: any, param: string, type = "string"): void { if (typeof obj[param] === "undefined" || !verifyType(obj[param], type)) { throw new Error(`Invalid or missing ${param}`); } diff --git a/src/index.ts b/src/index.ts index 42625e4..71fa75a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -125,7 +125,7 @@ app.after(() => { Routes.get.fundingBalance.opts, async (req, res) => { try { - await requireParam(req.params, "assetId"); + requireParam(req.params, "assetId"); res .status(200) @@ -145,9 +145,9 @@ app.after(() => { app.get(Routes.get.balance.url, Routes.get.balance.opts, async (req, res) => { try { - await requireParam(req.params, "assetId"); + requireParam(req.params, "assetId"); if (!config.legacyMode) { - await requireParam(req.params, "publicIdentifier"); + requireParam(req.params, "publicIdentifier"); } let client: Client | undefined; try { @@ -191,7 +191,7 @@ app.after(() => { app.get(Routes.get.config.url, Routes.get.config.opts, async (req, res) => { try { if (!config.legacyMode) { - await requireParam(req.params, "publicIdentifier"); + requireParam(req.params, "publicIdentifier"); } const client = multiClient.getClient(req.params.publicIdentifier); res.status(200).send(client.getConfig()); @@ -210,11 +210,11 @@ app.after(() => { Routes.get.hashLockStatus.opts, async (req, res) => { try { - await requireParam(req.params, "lockHash"); - await requireParam(req.params, "assetId"); + requireParam(req.params, "lockHash"); + requireParam(req.params, "assetId"); const { lockHash, assetId } = req.params; if (!config.legacyMode) { - await requireParam(req.params, "publicIdentifier"); + requireParam(req.params, "publicIdentifier"); } const client = multiClient.getClient(req.params.publicIdentifier); res @@ -224,7 +224,11 @@ app.after(() => { ); } catch (error) { app.log.error(error); - res.status(500).send({ message: error.message }); + let statusCode = 500; + if ((error.message as string).includes(`No HashLock Transfer found for lockHash`)) { + statusCode = 404; + } + res.status(statusCode).send({ message: error.message }); } }, ); @@ -238,10 +242,10 @@ app.after(() => { Routes.get.linkedStatus.opts, async (req, res) => { try { - await requireParam(req.params, "paymentId"); + requireParam(req.params, "paymentId"); const { paymentId } = req.params; if (!config.legacyMode) { - await requireParam(req.params, "publicIdentifier"); + requireParam(req.params, "publicIdentifier"); } const client = multiClient.getClient(req.params.publicIdentifier); res @@ -263,9 +267,9 @@ app.after(() => { Routes.get.appinstanceDetails.opts, async (req, res) => { try { - await requireParam(req.params, "appIdentityHash"); + requireParam(req.params, "appIdentityHash"); if (!config.legacyMode) { - await requireParam(req.params, "publicIdentifier"); + requireParam(req.params, "publicIdentifier"); } const client = multiClient.getClient(req.params.publicIdentifier); res @@ -290,7 +294,7 @@ app.after(() => { async (req, res) => { try { if (!config.legacyMode) { - await requireParam(req.params, "publicIdentifier"); + requireParam(req.params, "publicIdentifier"); } const client = multiClient.getClient(req.params.publicIdentifier); res @@ -311,7 +315,7 @@ app.after(() => { app.post(Routes.post.create.url, Routes.post.create.opts, async (req, res) => { try { - await requireParam(req.body, "index", "number"); + requireParam(req.body, "index", "number"); res .status(200) .send( @@ -332,7 +336,7 @@ app.after(() => { Routes.post.mnemonic.opts, async (req, res) => { try { - await requireParam(req.body, "mnemonic"); + requireParam(req.body, "mnemonic"); if (multiClient.keyring.mnemonic !== req.body.mnemonic) { await multiClient.reset(); } @@ -354,11 +358,11 @@ app.after(() => { Routes.post.onchainTransfer.opts, async (req, res) => { try { - await requireParam(req.body, "amount"); - await requireParam(req.body, "assetId"); - await requireParam(req.body, "recipient"); + requireParam(req.body, "amount"); + requireParam(req.body, "assetId"); + requireParam(req.body, "recipient"); if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } res .status(200) @@ -380,7 +384,7 @@ app.after(() => { async (req, res) => { try { if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } const client = await multiClient.connectClient(req.body); res.status(200).send(await client.getConfig()); @@ -401,7 +405,7 @@ app.after(() => { async (req, res) => { try { if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } await multiClient.disconnectClient(req.body?.publicIdentifier); res.status(200).send({ success: true }); @@ -421,13 +425,13 @@ app.after(() => { Routes.post.hashLockTransfer.opts, async (req, res) => { try { - await requireParam(req.body, "amount"); - await requireParam(req.body, "assetId"); - await requireParam(req.body, "lockHash"); - await requireParam(req.body, "timelock"); - await requireParam(req.body, "recipient"); + requireParam(req.body, "amount"); + requireParam(req.body, "assetId"); + requireParam(req.body, "lockHash"); + requireParam(req.body, "timelock"); + requireParam(req.body, "recipient"); if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } const client = multiClient.getClient(req.body.publicIdentifier); res @@ -449,10 +453,10 @@ app.after(() => { Routes.post.hashLockResolve.opts, async (req, res) => { try { - await requireParam(req.body, "preImage"); - await requireParam(req.body, "assetId"); + requireParam(req.body, "preImage"); + requireParam(req.body, "assetId"); if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } const client = multiClient.getClient(req.body.publicIdentifier); res @@ -474,10 +478,10 @@ app.after(() => { Routes.post.linkedTransfer.opts, async (req, res) => { try { - await requireParam(req.body, "assetId"); - await requireParam(req.body, "amount"); + requireParam(req.body, "assetId"); + requireParam(req.body, "amount"); if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } const client = multiClient.getClient(req.body.publicIdentifier); res @@ -499,10 +503,10 @@ app.after(() => { Routes.post.linkedResolve.opts, async (req, res) => { try { - await requireParam(req.body, "preImage"); - await requireParam(req.body, "paymentId"); + requireParam(req.body, "preImage"); + requireParam(req.body, "paymentId"); if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } const client = multiClient.getClient(req.body.publicIdentifier); res @@ -520,10 +524,10 @@ app.after(() => { app.post(Routes.post.fund.url, Routes.post.fund.opts, async (req, res) => { try { - await requireParam(req.body, "amount"); - await requireParam(req.body, "assetId"); + requireParam(req.body, "amount"); + requireParam(req.body, "assetId"); if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } const client = multiClient.getClient(req.body.publicIdentifier); res @@ -546,10 +550,10 @@ app.after(() => { Routes.post.deposit.opts, async (req, res) => { try { - await requireParam(req.body, "amount"); - await requireParam(req.body, "assetId"); + requireParam(req.body, "amount"); + requireParam(req.body, "assetId"); if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } const client = multiClient.getClient(req.body.publicIdentifier); res.status(200).send(await client.deposit(req.body)); @@ -569,9 +573,9 @@ app.after(() => { { ...Routes.post.requestCollateral.opts, preHandler: app.auth([app.verifyApiKey]) }, async (req, res) => { try { - await requireParam(req.body, "assetId"); + requireParam(req.body, "assetId"); if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } const client = multiClient.getClient(req.body.publicIdentifier); await client.requestCollateral(req.body); @@ -589,12 +593,12 @@ app.after(() => { app.post(Routes.post.swap.url, Routes.post.swap.opts, async (req, res) => { try { - await requireParam(req.body, "amount"); - await requireParam(req.body, "fromAssetId"); - await requireParam(req.body, "swapRate"); - await requireParam(req.body, "toAssetId"); + requireParam(req.body, "amount"); + requireParam(req.body, "fromAssetId"); + requireParam(req.body, "swapRate"); + requireParam(req.body, "toAssetId"); if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } const client = multiClient.getClient(req.body.publicIdentifier); res.status(200).send(await client.swap(req.body)); @@ -613,10 +617,10 @@ app.after(() => { Routes.post.withdraw.opts, async (req, res) => { try { - await requireParam(req.body, "amount"); - await requireParam(req.body, "assetId"); + requireParam(req.body, "amount"); + requireParam(req.body, "assetId"); if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } const client = multiClient.getClient(req.body.publicIdentifier); res.status(200).send(await client.withdraw(req.body)); @@ -662,10 +666,10 @@ app.after(() => { Routes.post.subscribe.opts, async (req, res) => { try { - await requireParam(req.body, "event"); - await requireParam(req.body, "webhook"); + requireParam(req.body, "event"); + requireParam(req.body, "webhook"); if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } const client = multiClient.getClient(req.body.publicIdentifier); res.status(200).send(await client.subscribe(req.body)); @@ -688,9 +692,9 @@ app.after(() => { Routes.post.batchSubscribe.opts, async (req, res) => { try { - await requireParam(req.body, "params", "array"); + requireParam(req.body, "params", "array"); if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } const client = multiClient.getClient(req.body.publicIdentifier); res @@ -717,9 +721,9 @@ app.after(() => { Routes.delete.subscribe.opts, async (req, res) => { try { - await requireParam(req.body, "id"); + requireParam(req.body, "id"); if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } const client = multiClient.getClient(req.body.publicIdentifier); res.status(200).send(await client.unsubscribe(req.body.id)); @@ -742,9 +746,9 @@ app.after(() => { Routes.delete.batchSubscribe.opts, async (req, res) => { try { - await requireParam(req.body, "ids", "array"); + requireParam(req.body, "ids", "array"); if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } const client = multiClient.getClient(req.body.publicIdentifier); res.status(200).send(await client.unsubscribeBatch(req.body.ids)); @@ -767,7 +771,7 @@ app.after(() => { async (req, res) => { try { if (!config.legacyMode) { - await requireParam(req.body, "publicIdentifier"); + requireParam(req.body, "publicIdentifier"); } const client = multiClient.getClient(req.body.publicIdentifier); res.status(200).send(await client.unsubscribeAll());